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)
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')