# Path: woodstock/models/participant.py
# class Participant(Person):
#     event_parts = models.ManyToManyField('woodstock.EventPart', related_name="participants",
#         blank=True, through="Attendance")
#     invitee = models.ForeignKey('woodstock.Invitee', blank=True, null=True,
#         default=None, related_name="participants")
# 
#     class Meta:
#         ordering = ('surname',)
#         verbose_name = 'Participant'
#         verbose_name_plural = 'Participants'
#         app_label = 'woodstock'
# 
#     def _attend_events(self, event_parts):
#         """
#         Attend events without callback and other functianlity.
#         """
#         attendances = []
#         success = True
#         for part in event_parts:
#             success &= not part.fully_booked()
#             attendances.append(Attendance.objects.create(participant=self,
#                 event_part=part, confirmed=True))
#         if success:
#             return attendances
#         # remove confirmation if something didn't went well
#         for attendance in attendances:
#             attendance.confirmed = False
#             attendance.save()
#         return False
# 
#     @callback('attend_events')
#     def attend_events(self, event_parts):
#         """
#         tries to attend all parts in event_parts
#         """
#         from woodstock.models import Event
#         if settings.SUBSCRIPTION_CONSUMES_INVITATION and self.attendances.count():
#             raise exceptions.PermissionDenied(_("You can only attend one event."))
#         if not settings.SUBSCRIPTION_ALLOW_MULTIPLE_EVENTS or not settings.SUBSCRIPTION_ALLOW_MULTIPLE_EVENTPARTS:
#             events = Event.objects.active().filter(
#                 models.Q(parts__attendances__confirmed=True, parts__attendances__participant=self)
#                 | models.Q(parts__in=event_parts)
#                 )
#             if not settings.SUBSCRIPTION_ALLOW_MULTIPLE_EVENTS and events.distinct().count() > 1:
#                 raise exceptions.PermissionDenied(_("You can only attend one event."))
#             if not settings.SUBSCRIPTION_ALLOW_MULTIPLE_EVENTPARTS and events.distinct().count() != events.count():
#                 raise exceptions.PermissionDenied(_("You can only attend one eventpart per event."))
#         attendances = self._attend_events(event_parts)
#         if not attendances:
#             return False
#         # send emails
#         for event in Event.objects.for_attendances(attendances):
#             event.send_subscribe_mail(self)
#         return attendances
# 
#     def _cancel_events(self, event_parts, delete=True):
#         """
#         Cancels event attendances
#         """
#         if event_parts:
#             queryset = self.attendances.filter(event_part__in=event_parts)
#         else:
#             queryset = self.attendances.all()
#         attendances = []
#         for attendance in queryset:
#             attendances.append(attendance)
#             if delete:
#                 attendance.delete()
#         return attendances
# 
#     @callback('cancel_events')
#     def cancel_events(self, event_parts=None):
#         """
#         Cancels all event attendances to events in event_parts. If event_parts
#         is omited all attendances are cancled.
#         """
#         from woodstock.models import Event
#         attendances = self._cancel_events(event_parts)
#         # send emails
#         for event in Event.objects.filter(parts__in=list(a.event_part for a in attendances)):
#             event.send_unsubscribe_mail(self)
#         return attendances
# 
#     @callback('change_events')
#     def change_events(self, new_event_parts, old_event_parts=None):
#         """
#         Changes event attendances. If old_event_parts is omited all current
#         attendances are deleted.
#         """
#         from woodstock.models import Event
# 
#         old_attendances = self._cancel_events(old_event_parts, delete=False)
#         attendances = self._attend_events(new_event_parts)
#         if not attendances:
#             return False
#         for attendance in old_attendances:
#             attendance.delete()
#         # send emails
#         for event in Event.objects.for_attendances(attendances):
#             event.send_subscribe_mail(self)
#         return attendances
# 
#     def save(self, *args, **kwargs):
#         if settings.SUBSCRIPTION_NEEDS_INVITATION and self.invitee is None:
#             raise exceptions.PermissionDenied(_("Only invited Persons can register."))
#         super(Participant, self).save(*args, **kwargs)
# 
#     @classmethod
#     def register_extension(cls, register_fn):
#         register_fn(cls, ParticipantAdmin)
# 
#     callback_functions = []  # empty array
# 
#     @classmethod
#     def register_callback(cls, callback_fn, events):
#         cls.callback_functions.append({'fn': callback_fn, 'events': events})
# 
# Path: woodstock/models/invitee.py
# class Invitee(Person):
#     groups = models.ManyToManyField('woodstock.Group', related_name="invitations", blank=True)
# 
#     class Meta:
#         verbose_name = 'Einladung'
#         verbose_name_plural = 'Einladungen'
#         ordering = ('surname',)
#         app_label = 'woodstock'
# 
#     @classmethod
#     def register_extension(cls, register_fn):
#         register_fn(cls, InviteeAdmin)


from django.core.exceptions import PermissionDenied
from woodstock.models import Invitee, Participant


class WoodstockView(object):
    invitation_required = False
    registration_required = False

    def dispatch(self, request, *args, **kwargs):
        if self.invitation_required and not isinstance(request.user, Invitee):
            raise PermissionDenied

# Path: woodstock/models/participant.py
# class Participant(Person):
#     event_parts = models.ManyToManyField('woodstock.EventPart', related_name="participants",
#         blank=True, through="Attendance")
#     invitee = models.ForeignKey('woodstock.Invitee', blank=True, null=True,
#         default=None, related_name="participants")
# 
#     class Meta:
#         ordering = ('surname',)
#         verbose_name = 'Participant'
#         verbose_name_plural = 'Participants'
#         app_label = 'woodstock'
# 
#     def _attend_events(self, event_parts):
#         """
#         Attend events without callback and other functianlity.
#         """
#         attendances = []
#         success = True
#         for part in event_parts:
#             success &= not part.fully_booked()
#             attendances.append(Attendance.objects.create(participant=self,
#                 event_part=part, confirmed=True))
#         if success:
#             return attendances
#         # remove confirmation if something didn't went well
#         for attendance in attendances:
#             attendance.confirmed = False
#             attendance.save()
#         return False
# 
#     @callback('attend_events')
#     def attend_events(self, event_parts):
#         """
#         tries to attend all parts in event_parts
#         """
#         from woodstock.models import Event
#         if settings.SUBSCRIPTION_CONSUMES_INVITATION and self.attendances.count():
#             raise exceptions.PermissionDenied(_("You can only attend one event."))
#         if not settings.SUBSCRIPTION_ALLOW_MULTIPLE_EVENTS or not settings.SUBSCRIPTION_ALLOW_MULTIPLE_EVENTPARTS:
#             events = Event.objects.active().filter(
#                 models.Q(parts__attendances__confirmed=True, parts__attendances__participant=self)
#                 | models.Q(parts__in=event_parts)
#                 )
#             if not settings.SUBSCRIPTION_ALLOW_MULTIPLE_EVENTS and events.distinct().count() > 1:
#                 raise exceptions.PermissionDenied(_("You can only attend one event."))
#             if not settings.SUBSCRIPTION_ALLOW_MULTIPLE_EVENTPARTS and events.distinct().count() != events.count():
#                 raise exceptions.PermissionDenied(_("You can only attend one eventpart per event."))
#         attendances = self._attend_events(event_parts)
#         if not attendances:
#             return False
#         # send emails
#         for event in Event.objects.for_attendances(attendances):
#             event.send_subscribe_mail(self)
#         return attendances
# 
#     def _cancel_events(self, event_parts, delete=True):
#         """
#         Cancels event attendances
#         """
#         if event_parts:
#             queryset = self.attendances.filter(event_part__in=event_parts)
#         else:
#             queryset = self.attendances.all()
#         attendances = []
#         for attendance in queryset:
#             attendances.append(attendance)
#             if delete:
#                 attendance.delete()
#         return attendances
# 
#     @callback('cancel_events')
#     def cancel_events(self, event_parts=None):
#         """
#         Cancels all event attendances to events in event_parts. If event_parts
#         is omited all attendances are cancled.
#         """
#         from woodstock.models import Event
#         attendances = self._cancel_events(event_parts)
#         # send emails
#         for event in Event.objects.filter(parts__in=list(a.event_part for a in attendances)):
#             event.send_unsubscribe_mail(self)
#         return attendances
# 
#     @callback('change_events')
#     def change_events(self, new_event_parts, old_event_parts=None):
#         """
#         Changes event attendances. If old_event_parts is omited all current
#         attendances are deleted.
#         """
#         from woodstock.models import Event
# 
#         old_attendances = self._cancel_events(old_event_parts, delete=False)
#         attendances = self._attend_events(new_event_parts)
#         if not attendances:
#             return False
#         for attendance in old_attendances:
#             attendance.delete()
#         # send emails
#         for event in Event.objects.for_attendances(attendances):
#             event.send_subscribe_mail(self)
#         return attendances
# 
#     def save(self, *args, **kwargs):
#         if settings.SUBSCRIPTION_NEEDS_INVITATION and self.invitee is None:
#             raise exceptions.PermissionDenied(_("Only invited Persons can register."))
#         super(Participant, self).save(*args, **kwargs)
# 
#     @classmethod
#     def register_extension(cls, register_fn):
#         register_fn(cls, ParticipantAdmin)
# 
#     callback_functions = []  # empty array
# 
#     @classmethod
#     def register_callback(cls, callback_fn, events):
#         cls.callback_functions.append({'fn': callback_fn, 'events': events})
# 
# Path: woodstock/models/invitee.py
# class Invitee(Person):
#     groups = models.ManyToManyField('woodstock.Group', related_name="invitations", blank=True)
# 
#     class Meta:
#         verbose_name = 'Einladung'
#         verbose_name_plural = 'Einladungen'
#         ordering = ('surname',)
#         app_label = 'woodstock'
# 
#     @classmethod
#     def register_extension(cls, register_fn):
#         register_fn(cls, InviteeAdmin)


# Path: woodstock/views/base.py
from django.core.exceptions import PermissionDenied
from woodstock.models import Invitee, Participant



class WoodstockView(object):
    invitation_required = False
    registration_required = False

    def dispatch(self, request, *args, **kwargs):
        if self.invitation_required and not isinstance(request.user, Invitee):
            raise PermissionDenied

        if self.registration_required and not isinstance(request.user, Participant):