def test_can_see(self): # Everyone can see submissions exhibition = Exhibition(title='New Exhibition', description='description goes here', released_at=timezone.now()) student_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.user) staff_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.staff_user) submission = Submission(exhibition=exhibition, artwork=student_artwork) self.assertTrue(submission.can_see()) self.assertTrue(submission.can_see(self.user)) self.assertTrue(submission.can_see(self.staff_user)) self.assertTrue(submission.can_see(self.super_user))
def get_context_data(self, **kwargs): context = super(CreateSubmissionView, self).get_context_data(**kwargs) # Restrict list of artwork the current user can submit context['form'].fields['artwork'].queryset = Artwork.can_save_queryset( context['form'].fields['artwork'].queryset, self.request.user) # Restrict to specific artwork if given artwork_id = self.kwargs.get('artwork') exclude_exhibitions = [] if artwork_id: context['form'].fields['artwork'].queryset =\ context['form'].fields['artwork'].queryset.filter(id=artwork_id) # Fetch the exhibitions this artwork has already been submitted to exclude_exhibitions = Submission.objects.filter( artwork__exact=artwork_id).order_by('-created_at').values_list( 'exhibition', flat=True) # Restrict list of exhibitions the current user can see, # and exclude exhibitions this artwork has already been submitted to context['form'].fields[ 'exhibition'].queryset = Exhibition.can_see_queryset( context['form'].fields['exhibition'].queryset, self.request.user).exclude(id__in=exclude_exhibitions) return context
def get_context_data(self, **kwargs): context = super(CreateSubmissionView, self).get_context_data(**kwargs) # Restrict list of artwork the current user can submit context['form'].fields['artwork'].queryset = Artwork.can_save_queryset( context['form'].fields['artwork'].queryset, self.request.user) # Restrict to specific artwork if given artwork_id = self.kwargs.get('artwork') exclude_exhibitions = [] if artwork_id: context['form'].fields['artwork'].queryset =\ context['form'].fields['artwork'].queryset.filter(id=artwork_id) # Fetch the exhibitions this artwork has already been submitted to exclude_exhibitions = Submission.objects.filter( artwork__exact=artwork_id).order_by('-created_at').values_list('exhibition', flat=True) # Restrict list of exhibitions the current user can see, # and exclude exhibitions this artwork has already been submitted to context['form'].fields['exhibition'].queryset = Exhibition.can_see_queryset( context['form'].fields['exhibition'].queryset, self.request.user).exclude(id__in=exclude_exhibitions) return context
def test_str(self): artwork = Artwork(title='Empty code', code='// code goes here') self.assertEquals( str(artwork), 'Empty code' )
def test_can_save_released_exhibition(self): exhibition = Exhibition(title='New Exhibition', description='description goes here', released_at=timezone.now()) self.assertTrue(exhibition.released_yet) student_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.user) staff_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.staff_user) # only authors can submit artwork submission = Submission(exhibition=exhibition, artwork=student_artwork) self.assertTrue(submission.can_save(self.user)) self.assertFalse(submission.can_save(self.staff_user)) self.assertFalse(submission.can_save(self.super_user)) submission = Submission(exhibition=exhibition, artwork=staff_artwork) self.assertFalse(submission.can_save(self.user)) self.assertTrue(submission.can_save(self.staff_user)) self.assertFalse(submission.can_save(self.super_user))
def test_save_unique(self): exhibition = Exhibition( title='New Exhibition', description='description goes here', released_at=timezone.now(), author=self.user) exhibition.save() artwork = Artwork(title='New Artwork', code='// code goes here', author=self.user) artwork.save() submission1 = Submission(exhibition=exhibition, artwork=artwork, submitted_by=self.user) submission2 = Submission(exhibition=exhibition, artwork=artwork, submitted_by=self.user) # submissions must be unique self.assertEqual(submission1.save(), None) self.assertRaisesRegexp( IntegrityError, 'columns exhibition_id, artwork_id are not unique', submission2.save)
def test_str(self): exhibition = Exhibition(title='New Exhibition', description='description goes here') artwork = Artwork(title='New Artwork', code='// code goes here') submission = Submission(exhibition=exhibition, artwork=artwork) self.assertEquals( str(submission), 'New Exhibition :: New Artwork' )
def nouv_art(request): sauvegarde = False form = NouvAlb(request.POST or None, request.FILES) if form.is_valid(): artwork = Artwork() artwork.title = form.cleaned_data["title"] artwork.photo = form.cleaned_data["photo"] artwork.reference = form.cleaned_data["reference"] artwork.created_at = form.cleaned_data["created_at"] artwork.available = form.cleaned_data["available"] artwork.save() sauvegarde = True return render(request, 'art_form.html', { 'form': form, 'sauvegarde': sauvegarde })
def test_can_save_unreleased_exhibition(self): exhibition = Exhibition( title='New Exhibition', description='description goes here', released_at=timezone.now() + timedelta(hours=24)) self.assertFalse(exhibition.released_yet) student_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.user) staff_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.staff_user) # students cannot submit to unreleased exhibitions submission = Submission(exhibition=exhibition, artwork=student_artwork) self.assertFalse(submission.can_save(self.user)) self.assertFalse(submission.can_save(self.staff_user)) self.assertFalse(submission.can_save(self.super_user)) # but staff can submission = Submission(exhibition=exhibition, artwork=staff_artwork) self.assertFalse(submission.can_save(self.user)) self.assertTrue(submission.can_save(self.staff_user)) self.assertFalse(submission.can_save(self.super_user))
def get_queryset(self): '''Show artwork authored by the given, or current, user''' qs = Artwork.can_see_queryset(user=self.request.user) # Show a single author's work? author_id = self._get_author_id() if author_id: qs = qs.filter(author__exact=author_id) # Show shared work only? if self._get_shared(): qs = qs.filter(shared__gt=0) # Show most recently modified first return qs.order_by('-modified_at')
def test_cant_save_shared(self): student = Artwork( author=self.user, title='Empty code', shared=1, code='// code goes here') self.assertFalse(student.can_save(self.user)) self.assertFalse(student.can_save(self.staff_user)) self.assertFalse(student.can_save(self.super_user)) staff = Artwork( author=self.staff_user, title='Empty code', shared=1, code='// code goes here') self.assertFalse(staff.can_save(self.user)) self.assertFalse(staff.can_save(self.staff_user)) self.assertFalse(staff.can_save(self.super_user)) superuser = Artwork( author=self.super_user, title='Empty code', shared=1, code='// code goes here') self.assertFalse(superuser.can_save(self.user)) self.assertFalse(superuser.can_save(self.staff_user)) self.assertFalse(superuser.can_save(self.super_user))
def test_can_save_shared_queryset(self): student = Artwork( author=self.user, title='Empty code', shared=1, code='// code goes here') student.save() staff = Artwork( author=self.staff_user, title='Empty code', shared=1, code='// code goes here') staff.save() superuser = Artwork( author=self.super_user, title='Empty code', shared=1, code='// code goes here') superuser.save() public_qs = Artwork.can_save_queryset() self.assertEqual(len(public_qs.all()), 0) student_qs = Artwork.can_save_queryset(user=self.user) self.assertEqual(len(student_qs.all()), 0) staff_qs = Artwork.can_save_queryset(user=self.staff_user) self.assertEqual(len(staff_qs.all()), 0) super_qs = Artwork.can_save_queryset(user=self.super_user) self.assertEqual(len(super_qs.all()), 0)
def test_can_see_private(self): student = Artwork( author=self.user, title='Empty code', code='// code goes here') self.assertTrue(student.can_see(self.user)) self.assertFalse(student.can_see(self.staff_user)) self.assertFalse(student.can_see(self.super_user)) staff = Artwork( author=self.staff_user, title='Empty code', code='// code goes here') self.assertFalse(staff.can_see(self.user)) self.assertTrue(staff.can_see(self.staff_user)) self.assertFalse(staff.can_see(self.super_user)) superuser = Artwork( author=self.super_user, title='Empty code', code='// code goes here') self.assertFalse(superuser.can_see(self.user)) self.assertFalse(superuser.can_see(self.staff_user)) self.assertTrue(superuser.can_see(self.super_user))
def test_can_see_private_queryset(self): student = Artwork( author=self.user, title='Empty code', code='// code goes here') student.save() staff = Artwork( author=self.staff_user, title='Empty code', code='// code goes here') staff.save() superuser = Artwork( author=self.super_user, title='Empty code', code='// code goes here') superuser.save() public_qs = Artwork.can_see_queryset() self.assertEqual(len(public_qs.all()), 0) student_qs = Artwork.can_see_queryset(user=self.user) self.assertEqual(len(student_qs.all()), 1) self.assertEqual(student_qs.all()[0].id, student.id) staff_qs = Artwork.can_see_queryset(user=self.staff_user) self.assertEqual(len(staff_qs.all()), 1) self.assertEqual(staff_qs.all()[0].id, staff.id) super_qs = Artwork.can_see_queryset(user=self.super_user) self.assertEqual(len(super_qs.all()), 1) self.assertEqual(super_qs.all()[0].id, superuser.id)