Use OccupationSerializer for the Contacts API
This commit is contained in:
@@ -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'),
|
||||
),
|
||||
]
|
||||
+3
-7
@@ -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()
|
||||
|
||||
|
||||
+9
-10
@@ -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')
|
||||
|
||||
+4
-4
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user