Improve photos services code

parent 9e6a8dfe
......@@ -3,17 +3,13 @@ from django.db.models import When, Value, BooleanField, ExpressionWrapper, Q, \
def is_album_accessible(request, album):
if request.member and request.member.current_membership is None:
albums_filter = Q(pk__in=[])
for membership in request.member.membership_set.all():
if membership.until is not None:
albums_filter |= (Q(date__gte=membership.since) & Q(
date__lte=membership.until))
else:
albums_filter |= (Q(date__gte=membership.since))
return album.filter(albums_filter).exists()
elif request.member and request.member.current_membership is not None:
if request.member and request.member.current_membership is not None:
return True
elif request.member and request.member.current_membership is None:
# This user is currently not a member, so need to check if he/she
# can view this album by checking the membership
filter = Q(since__lte=album.date) & Q(until__gte=album.date)
return request.member.membership_set.filter(filter).count() > 0
return False
......@@ -40,13 +36,3 @@ def get_annotated_accessible_albums(request, albums):
Value(True), output_field=BooleanField()))
return albums
def can_view_album(request, album):
if request.member.current_membership is None:
# This user is currently not a member, so need to check if he/she
# can view this album by checking the membership
filter = Q(since__lte=album.date) & (Q(until__gte=album.date) |
Q(until=None))
return request.member.membership_set.filter(filter).count() > 0
return True
......@@ -2,10 +2,14 @@ from io import BytesIO
from zipfile import ZipFile
from PIL import Image
from django.test import Client, TestCase
from django.test import Client, TestCase, RequestFactory
from django.utils.datetime_safe import datetime
from freezegun import freeze_time
from members.models import Member
from .models import Photo, Album, determine_rotation
from members.models import Member, Membership
from photos import services
from .models import Photo, Album
from .models import determine_rotation
def create_zip(photos):
......@@ -136,3 +140,47 @@ class AlbumUploadTest(TestCase):
follow=True)
self.assertEqual(Photo.objects.first().rotation, 90)
class ServicesTest(TestCase):
fixtures = ['members.json']
@classmethod
def setUpTestData(cls):
cls.member = Member.objects.filter(username="testuser").first()
def setUp(self):
self.rf = RequestFactory()
@freeze_time('2017-01-01')
def test_is_album_accessible(self):
request = self.rf.get('/')
request.member = None
album = Album(date=datetime(year=2017, month=1, day=1))
with self.subTest(membership=None):
self.assertFalse(services.is_album_accessible(request, album))
request.member = self.member
with self.subTest(membership=None):
self.assertFalse(services.is_album_accessible(request, album))
membership = Membership.objects.create(
user=self.member, type=Membership.MEMBER,
since=datetime(year=2016, month=1, day=1))
with self.subTest(membership_since=membership.since,
membership_until=membership.until):
self.assertTrue(services.is_album_accessible(request, album))
membership.until = datetime(year=2016, month=1, day=1)
membership.save()
with self.subTest(membership_since=membership.since,
membership_until=membership.until):
self.assertFalse(services.is_album_accessible(request, album))
membership.until = datetime(year=2017, month=1, day=1)
membership.save()
with self.subTest(membership_since=membership.since,
membership_until=membership.until):
self.assertTrue(services.is_album_accessible(request, album))
......@@ -64,7 +64,7 @@ def _render_album_page(request, album):
@login_required
def album(request, slug):
album = get_object_or_404(Album, slug=slug)
if services.can_view_album(request, album):
if services.is_album_accessible(request, album):
return _render_album_page(request, album)
raise Http404("Sorry, you're not allowed to view this album")
......
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