|
| 1 | +from django.contrib import admin |
| 2 | +from django.db import models |
| 3 | +from django.utils.translation import ugettext_lazy as _ |
| 4 | + |
| 5 | +from pyconbalkan.conference.models import Conference |
| 6 | + |
| 7 | + |
| 8 | +def _get_default_conference(): |
| 9 | + return Conference.objects.first().id |
| 10 | + |
| 11 | + |
| 12 | +class AbstractConference(models.Model): |
| 13 | + conference = models.ForeignKey( |
| 14 | + Conference, on_delete=models.CASCADE, default=_get_default_conference |
| 15 | + ) |
| 16 | + |
| 17 | + class Meta: |
| 18 | + abstract = True |
| 19 | + |
| 20 | + |
| 21 | +class ConferenceFilter(admin.SimpleListFilter): |
| 22 | + title = _('Conference year') |
| 23 | + parameter_name = 'conference' |
| 24 | + |
| 25 | + def choices(self, changelist): |
| 26 | + for lookup, title in self.lookup_choices: |
| 27 | + yield { |
| 28 | + 'selected': self.value() is lookup or self.value() == str(lookup), |
| 29 | + 'query_string': changelist.get_query_string( |
| 30 | + {self.parameter_name: lookup} |
| 31 | + ), |
| 32 | + 'display': title, |
| 33 | + } |
| 34 | + |
| 35 | + def lookups(self, request, model_admin): |
| 36 | + choices = [] |
| 37 | + for _ in Conference.objects.all(): |
| 38 | + if _ == request.conference: |
| 39 | + choices.append((None, "{} Current".format(_))) |
| 40 | + else: |
| 41 | + choices.append((_.id, str(_))) |
| 42 | + return choices |
| 43 | + |
| 44 | + def queryset(self, request, queryset): |
| 45 | + return queryset.filter(conference=self.value() or request.conference.pk) |
| 46 | + |
| 47 | + |
| 48 | +class ConferenceAbstractAdmin(admin.ModelAdmin): |
| 49 | + list_filter = (ConferenceFilter,) |
0 commit comments