Commit cc752c9c authored by Tobias van der Werff's avatar Tobias van der Werff Committed by Sébastiaan Versteeg

Resolve "Automatic notification after uploading a new photo album"

parent 54be22a3
# Generated by Django 2.2 on 2019-05-08 19:06
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('pushnotifications', '0013_auto_20181111_1319'),
('photos', '0010_merge_20170510_2047'),
]
operations = [
migrations.AddField(
model_name='album',
name='new_album_notification',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='pushnotifications.ScheduledMessage'),
),
]
......@@ -6,12 +6,15 @@ import logging
from django.conf import settings
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _
from PIL import Image
from members.models import Member
from photos.services import photo_determine_rotation
from utils.translation import ModelTranslateMeta, MultilingualField
from pushnotifications.models import ScheduledMessage, Category
COVER_FILENAME = 'cover.jpg'
......@@ -126,6 +129,11 @@ class Album(models.Model, metaclass=ModelTranslateMeta):
default=False
)
new_album_notification = models.ForeignKey(
ScheduledMessage, on_delete=models.deletion.SET_NULL,
blank=True, null=True
)
_cover = models.OneToOneField(
Photo,
on_delete=models.SET_NULL,
......@@ -163,6 +171,37 @@ class Album(models.Model, metaclass=ModelTranslateMeta):
# dirname is only set for new objects, to avoid ever changing it
if self.pk is None:
self.dirname = self.slug
if (not self.hidden and (self.new_album_notification is None
or not self.new_album_notification.sent)):
new_album_notification_time = (timezone.now() +
timezone.timedelta(hours=1))
new_album_notification = ScheduledMessage()
if (self.new_album_notification is not None
and not self.new_album_notification.sent):
new_album_notification = self.new_album_notification
new_album_notification.title_en = 'New album uploaded'
new_album_notification.title_nl = 'Nieuw album geüpload'
new_album_notification.body_en = ('A new photo album \'{}\' has '
'just been uploaded'
.format(self.title_en))
new_album_notification.body_nl = ('Een nieuw fotoalbum \'{}\' is '
'zojuist geüpload'
.format(self.title_nl))
new_album_notification.category = Category.objects.get(key='photo')
new_album_notification.url = self.get_absolute_url()
new_album_notification.time = new_album_notification_time
new_album_notification.save()
self.new_album_notification = new_album_notification
self.new_album_notification.users.set(Member.current_members.all())
elif (self.hidden and self.new_album_notification is not None
and not self.new_album_notification.sent):
existing_notification = self.new_album_notification
self.new_album_notification = None
existing_notification.delete()
super().save(*args, **kwargs)
@property
......
......@@ -23,7 +23,10 @@ class IsAlbumAccesibleTest(TestCase):
def test_is_album_accessible(self):
request = self.rf.get('/')
request.member = None
album = Album(date=datetime(year=2017, month=1, day=1))
album = Album(
date=datetime(year=2017, month=1, day=1),
slug='test'
)
with self.subTest(membership=None):
self.assertFalse(is_album_accessible(request, album))
......@@ -66,7 +69,10 @@ class GetAnnotatedAccessibleAlbumsTest(TestCase):
def test_get_annotated_accessible_albums(self):
request = self.rf.get('/')
request.member = None
album = Album(date=datetime(year=2017, month=1, day=1))
album = Album(
date=datetime(year=2017, month=1, day=1),
slug='test'
)
album.save()
self.assertEqual(Album.objects.count(), 1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment