Commit d6b15477 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Merge branch '777-automatic-notification-after-uploading-a-new-photo-album' into 'master'

Resolve "Automatic notification after uploading a new photo album"

Closes #777

See merge request !1211
parents 54be22a3 cc752c9c
# 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 ...@@ -6,12 +6,15 @@ import logging
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils import timezone
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from PIL import Image from PIL import Image
from members.models import Member
from photos.services import photo_determine_rotation from photos.services import photo_determine_rotation
from utils.translation import ModelTranslateMeta, MultilingualField from utils.translation import ModelTranslateMeta, MultilingualField
from pushnotifications.models import ScheduledMessage, Category
COVER_FILENAME = 'cover.jpg' COVER_FILENAME = 'cover.jpg'
...@@ -126,6 +129,11 @@ class Album(models.Model, metaclass=ModelTranslateMeta): ...@@ -126,6 +129,11 @@ class Album(models.Model, metaclass=ModelTranslateMeta):
default=False default=False
) )
new_album_notification = models.ForeignKey(
ScheduledMessage, on_delete=models.deletion.SET_NULL,
blank=True, null=True
)
_cover = models.OneToOneField( _cover = models.OneToOneField(
Photo, Photo,
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
...@@ -163,6 +171,37 @@ class Album(models.Model, metaclass=ModelTranslateMeta): ...@@ -163,6 +171,37 @@ class Album(models.Model, metaclass=ModelTranslateMeta):
# dirname is only set for new objects, to avoid ever changing it # dirname is only set for new objects, to avoid ever changing it
if self.pk is None: if self.pk is None:
self.dirname = self.slug 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) super().save(*args, **kwargs)
@property @property
......
...@@ -23,7 +23,10 @@ class IsAlbumAccesibleTest(TestCase): ...@@ -23,7 +23,10 @@ class IsAlbumAccesibleTest(TestCase):
def test_is_album_accessible(self): def test_is_album_accessible(self):
request = self.rf.get('/') request = self.rf.get('/')
request.member = None 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): with self.subTest(membership=None):
self.assertFalse(is_album_accessible(request, album)) self.assertFalse(is_album_accessible(request, album))
...@@ -66,7 +69,10 @@ class GetAnnotatedAccessibleAlbumsTest(TestCase): ...@@ -66,7 +69,10 @@ class GetAnnotatedAccessibleAlbumsTest(TestCase):
def test_get_annotated_accessible_albums(self): def test_get_annotated_accessible_albums(self):
request = self.rf.get('/') request = self.rf.get('/')
request.member = None 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() album.save()
self.assertEqual(Album.objects.count(), 1) self.assertEqual(Album.objects.count(), 1)
......
Supports Markdown
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