# Path: kobra/models.py
# class DiscountRegistration(models.Model):
#     id = IdField()
# 
#     discount = models.ForeignKey(
#         'Discount',
#         related_name='discount_registrations',
#         on_delete=models.PROTECT)
#     student = models.ForeignKey(
#         'Student',
#         related_name='discount_registrations',
#         on_delete=models.PROTECT)
# 
#     timestamp = models.DateTimeField(
#         default=now)
# 
#     class Meta:
#         ordering = ['-timestamp']
# 
#         permissions = [
#             ['view_discountregistration', _('Can view discount registration')]
#         ]
# 
# class Event(models.Model):
#     id = IdField()
# 
#     name = NameField()
# 
#     organization = models.ForeignKey(
#         'Organization',
#         related_name='events')
# 
#     class Meta:
#         ordering = ['name']
# 
#         permissions = [
#             ['view_event', _('Can view event')]
#         ]
# 
#     def __str__(self):
#         return '{} / {}'.format(self.organization, self.name)
# 
# class TicketType(models.Model):
#     id = IdField()
# 
#     name = NameField()
#     event = models.ForeignKey(
#         'Event',
#         related_name='ticket_types',
#         verbose_name=_('event'))
# 
#     personal_discount_limit = models.PositiveIntegerField(
#         default=1,
#         null=True,
#         blank=True,
#         verbose_name=_('personal discount limit'),
#         help_text=_('The maximum number of discount registrations per person '
#                     'for this ticket type. This should almost always be 1. '
#                     'Blank means no limit. Setting this incorrectly can make '
#                     'your organization liable for repayment.'))
# 
#     class Meta:
#         ordering = ['name']
# 
#         permissions = [
#             ['view_tickettype', _('Can view ticket type')]
#         ]
# 
#         unique_together = [['event', 'name']]
# 
#     def __str__(self):
#         return '{} / {}'.format(self.event, self.name)
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
#         for union in Union.objects.all():
#             Discount.objects.get_or_create(ticket_type=self, union=union)


import django_filters
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework import filters
from ...models import DiscountRegistration, Event, TicketType
# -*- coding: utf-8 -*-



class DiscountPermissionFilter(DjangoFilterBackend):
    def filter_queryset(self, request, queryset, view):
        return queryset.filter(
            ticket_type__event__organization__admins=request.user)


class DiscountRegistrationFilter(FilterSet):
    event = django_filters.ModelChoiceFilter(
        name='discount__ticket_type__event',
        queryset=Event.objects.all())

    class Meta:
        model = DiscountRegistration
        fields = [
            'event',
            'student'
        ]


class DiscountRegistrationPermissionFilter(DjangoFilterBackend):
    def filter_queryset(self, request, queryset, view):

# Path: kobra/models.py
# class DiscountRegistration(models.Model):
#     id = IdField()
# 
#     discount = models.ForeignKey(
#         'Discount',
#         related_name='discount_registrations',
#         on_delete=models.PROTECT)
#     student = models.ForeignKey(
#         'Student',
#         related_name='discount_registrations',
#         on_delete=models.PROTECT)
# 
#     timestamp = models.DateTimeField(
#         default=now)
# 
#     class Meta:
#         ordering = ['-timestamp']
# 
#         permissions = [
#             ['view_discountregistration', _('Can view discount registration')]
#         ]
# 
# class Event(models.Model):
#     id = IdField()
# 
#     name = NameField()
# 
#     organization = models.ForeignKey(
#         'Organization',
#         related_name='events')
# 
#     class Meta:
#         ordering = ['name']
# 
#         permissions = [
#             ['view_event', _('Can view event')]
#         ]
# 
#     def __str__(self):
#         return '{} / {}'.format(self.organization, self.name)
# 
# class TicketType(models.Model):
#     id = IdField()
# 
#     name = NameField()
#     event = models.ForeignKey(
#         'Event',
#         related_name='ticket_types',
#         verbose_name=_('event'))
# 
#     personal_discount_limit = models.PositiveIntegerField(
#         default=1,
#         null=True,
#         blank=True,
#         verbose_name=_('personal discount limit'),
#         help_text=_('The maximum number of discount registrations per person '
#                     'for this ticket type. This should almost always be 1. '
#                     'Blank means no limit. Setting this incorrectly can make '
#                     'your organization liable for repayment.'))
# 
#     class Meta:
#         ordering = ['name']
# 
#         permissions = [
#             ['view_tickettype', _('Can view ticket type')]
#         ]
# 
#         unique_together = [['event', 'name']]
# 
#     def __str__(self):
#         return '{} / {}'.format(self.event, self.name)
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
#         for union in Union.objects.all():
#             Discount.objects.get_or_create(ticket_type=self, union=union)


# Path: kobra/api/v1/filters.py
import django_filters
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework import filters
from ...models import DiscountRegistration, Event, TicketType

# -*- coding: utf-8 -*-



class DiscountPermissionFilter(DjangoFilterBackend):
    def filter_queryset(self, request, queryset, view):
        return queryset.filter(
            ticket_type__event__organization__admins=request.user)


class DiscountRegistrationFilter(FilterSet):
    event = django_filters.ModelChoiceFilter(
        name='discount__ticket_type__event',
        queryset=Event.objects.all())

    class Meta:
        model = DiscountRegistration
        fields = [
            'event',
            'student'
        ]


class DiscountRegistrationPermissionFilter(DjangoFilterBackend):
    def filter_queryset(self, request, queryset, view):

        return queryset.filter(