From e5cc62bbbf096d31ee65a39aa49daabdf4cf65d9 Mon Sep 17 00:00:00 2001 From: Aarni Halinen Date: Thu, 10 Oct 2019 18:37:15 +0300 Subject: [PATCH] Use OccupationSerializer for the Contacts API --- webapp/migrations/0058_auto_20191010_1837.py | 18 ++++++++++++++++++ webapp/models.py | 10 +++------- webapp/serializers.py | 19 +++++++++---------- webapp/views.py | 8 ++++---- 4 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 webapp/migrations/0058_auto_20191010_1837.py diff --git a/webapp/migrations/0058_auto_20191010_1837.py b/webapp/migrations/0058_auto_20191010_1837.py new file mode 100644 index 0000000..7df01b1 --- /dev/null +++ b/webapp/migrations/0058_auto_20191010_1837.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.5 on 2019-10-10 15:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('webapp', '0057_auto_20190926_2102'), + ] + + operations = [ + migrations.AlterField( + model_name='official', + name='role_history', + field=models.ManyToManyField(blank=True, related_name='officials', to='webapp.Occupation'), + ), + ] diff --git a/webapp/models.py b/webapp/models.py index 52c141a..4c8d331 100644 --- a/webapp/models.py +++ b/webapp/models.py @@ -195,7 +195,7 @@ class Occupation(models.Model): role = models.ForeignKey('Role', on_delete=models.SET_NULL, null=True) @staticmethod - def occupations_by_year(year): + def by_year(year): return Occupation.objects.filter( end_date__gte=timezone.datetime(year, 1, 1)).filter( start_date__lte=timezone.datetime(year, 12, 31)) @@ -219,14 +219,10 @@ class Official(models.Model): last_name = models.CharField(_('Last name'), max_length=150) email = models.EmailField(_('Email address')) phone_number = PhoneNumberField(_('Phone number')) - role_history = models.ManyToManyField('Occupation', blank=True) - - @property - def current_roles(self): - return self.role_history.all().filter(end_date__gte=timezone.now()) + role_history = models.ManyToManyField('Occupation', 'officials', blank=True) @staticmethod - def official_by_year(year): + def by_year(year): return Official.objects.filter( role_history__in=Occupation.occupations_by_year(year)).distinct() diff --git a/webapp/serializers.py b/webapp/serializers.py index bd3ad13..03f8f1f 100644 --- a/webapp/serializers.py +++ b/webapp/serializers.py @@ -107,18 +107,17 @@ class RoleSerializer(serializers.ModelSerializer): fields = ('name', 'description', 'committee') +class ContactsSerializer(serializers.ModelSerializer): + class Meta: + model = Official + fields = ('first_name', 'last_name', 'email', 'phone_number') + depth = 2 + + class OccupationSerializer(serializers.ModelSerializer): role = RoleSerializer(read_only=True) + officials = ContactsSerializer(many=True, read_only=True) class Meta: model = Occupation - fields = ('role', 'start_date', 'end_date') - - -class ContactsSerializer(serializers.ModelSerializer): - role_history = OccupationSerializer(many=True, read_only=True) - - class Meta: - model = Official - fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'role_history') - depth = 2 + fields = ('role', 'start_date', 'end_date', 'officials') diff --git a/webapp/views.py b/webapp/views.py index 1aeab93..e024e4c 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -108,15 +108,15 @@ class FeedViewSet(viewsets.ModelViewSet): class ContactsViewSet(viewsets.ReadOnlyModelViewSet): - queryset = Official.objects.all() - serializer_class = ContactsSerializer + queryset = Occupation.objects.all() + serializer_class = OccupationSerializer 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)) + return Occupation.by_year(timezone.now().year) + return Occupation.by_year(int(year)) class TagsViewSet(viewsets.ReadOnlyModelViewSet):