Get Officials by year with query param year
This commit is contained in:
+11
-4
@@ -194,6 +194,12 @@ class Occupation(models.Model):
|
||||
end_date = models.DateField(_('End date'))
|
||||
role = models.ForeignKey('Role', on_delete=models.SET_NULL, null=True)
|
||||
|
||||
@staticmethod
|
||||
def occupations_by_year(year):
|
||||
return Occupation.objects.filter(
|
||||
end_date__gte=timezone.datetime(year, 1, 1)).filter(
|
||||
start_date__lte=timezone.datetime(year, 12, 31))
|
||||
|
||||
def __str__(self):
|
||||
return '{}: {} - {}'.format(self.role.name, self.start_date, self.end_date)
|
||||
|
||||
@@ -217,11 +223,12 @@ class Official(models.Model):
|
||||
|
||||
@property
|
||||
def current_roles(self):
|
||||
return self.role_history.all().filter(end_date__gte=timezone.now()).filter(start_date__lte=timezone.now())
|
||||
return self.role_history.all().filter(end_date__gte=timezone.now())
|
||||
|
||||
@property
|
||||
def roles_of_year(self, year):
|
||||
return self.role_history.all().filter(end_date__gte=timezone.datetime(year, 1, 1)).filter(start_date__lte=timezone.datetime(year, 12, 31))
|
||||
@staticmethod
|
||||
def official_by_year(year):
|
||||
return Official.objects.filter(
|
||||
role_history__in=Occupation.occupations_by_year(year)).distinct()
|
||||
|
||||
def __str__(self):
|
||||
return '{} {}'.format(self.first_name, self.last_name)
|
||||
|
||||
@@ -116,9 +116,9 @@ class OccupationSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class ContactsSerializer(serializers.ModelSerializer):
|
||||
current_roles = OccupationSerializer(many=True, read_only=True)
|
||||
role_history = OccupationSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Official
|
||||
fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'current_roles')
|
||||
fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'role_history')
|
||||
depth = 2
|
||||
|
||||
@@ -112,6 +112,12 @@ class ContactsViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
serializer_class = ContactsSerializer
|
||||
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||
|
||||
def get_queryset(self):
|
||||
year = self.request.query_params.get('year')
|
||||
if not year:
|
||||
return Official.official_by_year(timezone.now().year)
|
||||
return Official.official_by_year(int(year))
|
||||
|
||||
|
||||
class TagsViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Tag.objects.all()
|
||||
|
||||
Reference in New Issue
Block a user