# Path: deck/permissions.py
# def has_manage_schedule_permission(user, event):
#     return user.is_superuser or event.user_in_jury(user)
# 
# Path: deck/models.py
# class Event(DeckBaseModel):
#     allow_public_voting = models.BooleanField(_('Allow Public Voting'),
#                                               default=True)
#     closing_date = models.DateTimeField(null=False, blank=False)
#     slots = models.SmallIntegerField(_('Slots'), default=10)
# 
#     # relations
#     jury = models.OneToOneField(to='jury.Jury', related_name='event',
#                                 null=True, blank=True)
#     anonymous_voting = models.BooleanField(
#         _('Anonymous Voting?'), default=False)
# 
#     class Meta:
#         ordering = ['-closing_date', '-created_at']
#         verbose_name = _('Event')
#         verbose_name_plural = _('Events')
# 
#     @property
#     def closing_date_is_passed(self):
#         return timezone.now() > self.closing_date
# 
#     @property
#     def closing_date_is_close(self):
#         if self.closing_date_is_passed:
#             return False
#         return timezone.now() > self.closing_date - timezone.timedelta(days=7)
# 
#     def get_absolute_url(self):
#         return reverse('view_event', kwargs={'slug': self.slug})
# 
#     def user_can_see_proposals(self, user):
#         can_see_proposals = False
#         if user.is_superuser or self.author == user:
#             can_see_proposals = True
#         elif self.allow_public_voting:
#             can_see_proposals = True
#         elif (not user.is_anonymous() and
#               self.jury.users.filter(pk=user.pk).exists()):
#             can_see_proposals = True
#         return can_see_proposals
# 
#     def get_proposers_count(self):
#         return self.proposals.values_list(
#             'author', flat=True).distinct().count()
# 
#     def get_votes_count(self):
#         return self.proposals.values_list('votes', flat=True).count()
# 
#     def get_votes_to_export(self):
#         return self.proposals.values(
#             'id', 'title', 'author__username', 'author__email'
#         ).annotate(
#             Sum('votes__rate')
#         ).annotate(Count('votes'))
# 
#     def get_schedule(self):
#         schedule = Activity.objects.filter(track__event=self)\
#             .cached_authors()\
#             .annotate(Sum('proposal__votes__rate'))\
#             .extra(select=dict(track_isnull='track_id IS NULL'))\
#             .order_by('track_isnull', 'track_order',
#                       '-proposal__votes__rate__sum')
#         return schedule
# 
#     def get_not_approved_schedule(self):
#         return self.proposals\
#             .cached_authors()\
#             .filter(
#                 models.Q(is_approved=False) |
#                 models.Q(track__isnull=True))
# 
#     def get_main_track(self):
#         return self.tracks.first()
# 
#     def filter_not_scheduled_by_slots(self):
#         return self.get_not_approved_schedule()[:self.slots]
# 
#     def user_in_jury(self, user):
#         return self.jury.users.filter(pk=user.pk).exists()


from django.test import TestCase
from django.conf import settings
from mock import patch, Mock
from model_mommy import mommy
from deck.permissions import has_manage_schedule_permission
from deck.models import Event




class PermissionsTestCase(TestCase):

    def setUp(self):
        self.user = mommy.make(settings.AUTH_USER_MODEL)
        self.event = mommy.make(Event)

    def test_super_user_is_always_allowed(self):
        self.user.is_superuser = True


# Path: deck/permissions.py
# def has_manage_schedule_permission(user, event):
#     return user.is_superuser or event.user_in_jury(user)
# 
# Path: deck/models.py
# class Event(DeckBaseModel):
#     allow_public_voting = models.BooleanField(_('Allow Public Voting'),
#                                               default=True)
#     closing_date = models.DateTimeField(null=False, blank=False)
#     slots = models.SmallIntegerField(_('Slots'), default=10)
# 
#     # relations
#     jury = models.OneToOneField(to='jury.Jury', related_name='event',
#                                 null=True, blank=True)
#     anonymous_voting = models.BooleanField(
#         _('Anonymous Voting?'), default=False)
# 
#     class Meta:
#         ordering = ['-closing_date', '-created_at']
#         verbose_name = _('Event')
#         verbose_name_plural = _('Events')
# 
#     @property
#     def closing_date_is_passed(self):
#         return timezone.now() > self.closing_date
# 
#     @property
#     def closing_date_is_close(self):
#         if self.closing_date_is_passed:
#             return False
#         return timezone.now() > self.closing_date - timezone.timedelta(days=7)
# 
#     def get_absolute_url(self):
#         return reverse('view_event', kwargs={'slug': self.slug})
# 
#     def user_can_see_proposals(self, user):
#         can_see_proposals = False
#         if user.is_superuser or self.author == user:
#             can_see_proposals = True
#         elif self.allow_public_voting:
#             can_see_proposals = True
#         elif (not user.is_anonymous() and
#               self.jury.users.filter(pk=user.pk).exists()):
#             can_see_proposals = True
#         return can_see_proposals
# 
#     def get_proposers_count(self):
#         return self.proposals.values_list(
#             'author', flat=True).distinct().count()
# 
#     def get_votes_count(self):
#         return self.proposals.values_list('votes', flat=True).count()
# 
#     def get_votes_to_export(self):
#         return self.proposals.values(
#             'id', 'title', 'author__username', 'author__email'
#         ).annotate(
#             Sum('votes__rate')
#         ).annotate(Count('votes'))
# 
#     def get_schedule(self):
#         schedule = Activity.objects.filter(track__event=self)\
#             .cached_authors()\
#             .annotate(Sum('proposal__votes__rate'))\
#             .extra(select=dict(track_isnull='track_id IS NULL'))\
#             .order_by('track_isnull', 'track_order',
#                       '-proposal__votes__rate__sum')
#         return schedule
# 
#     def get_not_approved_schedule(self):
#         return self.proposals\
#             .cached_authors()\
#             .filter(
#                 models.Q(is_approved=False) |
#                 models.Q(track__isnull=True))
# 
#     def get_main_track(self):
#         return self.tracks.first()
# 
#     def filter_not_scheduled_by_slots(self):
#         return self.get_not_approved_schedule()[:self.slots]
# 
#     def user_in_jury(self, user):
#         return self.jury.users.filter(pk=user.pk).exists()


# Path: deck/tests/test_permissions.py
from django.test import TestCase
from django.conf import settings
from mock import patch, Mock
from model_mommy import mommy
from deck.permissions import has_manage_schedule_permission
from deck.models import Event





class PermissionsTestCase(TestCase):

    def setUp(self):
        self.user = mommy.make(settings.AUTH_USER_MODEL)
        self.event = mommy.make(Event)

    def test_super_user_is_always_allowed(self):
        self.user.is_superuser = True


        has_permission = has_manage_schedule_permission(self.user, self.event)