Skip to content

Commit

Permalink
Merge pull request #8 from prsnca/restful_api
Browse files Browse the repository at this point in the history
restful api
  • Loading branch information
prsnca authored Jan 16, 2017
2 parents c564a1b + 358a575 commit d45436d
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 24 deletions.
32 changes: 23 additions & 9 deletions committeeVotes/models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
# coding: utf-8

from django.db import models
from django.conf import settings


# Create your models here.
class Bill(models.Model):
name = models.CharField(max_length=500)
oknesset_url = models.CharField(max_length=100, blank=True, null=True)
passed = models.NullBooleanField()

def __unicode__(self):
return self.name


class VoteType(models.Model):
typeName = models.CharField(max_length=10)

def __unicode__(self):
return self.typeName


class Minister(models.Model):
name = models.CharField(max_length=30)
title = models.CharField(max_length=100, null=True, blank=True)
Expand All @@ -23,29 +30,36 @@ class Minister(models.Model):
phone = models.CharField(max_length=20, null=True, blank=True)
oknesset = models.CharField(max_length=100, null=True, blank=True)
coop = models.NullBooleanField(blank=True)

@property
def photo_url(self):
url_prefix=""
if 'http' not in self.photo:
url_prefix=settings.MEDIA_URL
return "{prefix}{photo}.jpg".format(prefix=url_prefix, photo=self.photo)

def __unicode__(self):
return self.name


class Meeting(models.Model):
took_place = models.DateField(unique=True)
proposed_bills = models.ManyToManyField(Bill, blank=True)
proposed_bills = models.ManyToManyField(Bill, blank=True, related_name="meeting")
voting_ministers = models.ManyToManyField(Minister, blank=True, related_name='meeting_voting_minister')
missing_ministers = models.ManyToManyField(Minister, blank=True, related_name='meeting_missing_minister')

def __unicode__(self):
return "Meeting #" + str(self.id) + u" - %s" % self.took_place


class Vote(models.Model):
vote = models.ForeignKey(VoteType)
meeting = models.ForeignKey(Meeting)
bill = models.ForeignKey(Bill)
bill = models.ForeignKey(Bill, related_name="votes")
minister = models.ForeignKey(Minister, related_name="votes")

class Meta:
unique_together = ("bill","minister")
unique_together = ("bill", "minister")

def __unicode__(self):
return self.minister.name + " voted " + self.vote.typeName




return self.minister.name + " voted " + self.vote.typeName
195 changes: 195 additions & 0 deletions committeeVotes/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# coding: utf-8

from committeeVotes.models import Bill, Meeting, Minister, Vote, VoteType
from rest_framework import serializers


class DynamicFieldsMixin(object):

"""
A serializer mixin that takes an additional `fields` argument that controls
which fields should be displayed.
Usage::
class MySerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer):
class Meta:
model = MyModel
Copied from https://gist.github.com/dbrgn/4e6fc1fe5922598592d6
"""

def __init__(self, *args, **kwargs):
super(DynamicFieldsMixin, self).__init__(*args, **kwargs)
if 'request' in self.context:
fields = self.context['request'].QUERY_PARAMS.get('fields')
if fields:
fields = fields.split(',')
# Drop any fields that are not specified in the `fields`
# argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)


class VoteSerializer(serializers.ModelSerializer):
vote = serializers.StringRelatedField()
minister = serializers.StringRelatedField()

class Meta:
model = Vote
fields = ('vote', 'minister')


class VoteListSerializer(serializers.BaseSerializer):
def to_representation(self, obj):
return obj.minister


class MinisterListSerializer(serializers.ModelSerializer):
class Meta:
model = Minister
fields = ('id', 'name', 'photo_url', 'coop')


class VoteBillSerializer(serializers.ModelSerializer):

class Meta:
model = Bill
fields = ('id', 'name')


class BillSerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer):

class Meta:
model = Bill
fields = ('id', 'name', 'oknesset_url', 'passed')


class BillVoteSerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer):
yay = serializers.SerializerMethodField()
nay = serializers.SerializerMethodField()
sustained = serializers.SerializerMethodField()
others = serializers.SerializerMethodField()

def get_votes(self, bill, voteTypeName):
voteType = VoteType.objects.get(typeName=voteTypeName)
votes = Vote.objects.select_related('minister').filter(vote=voteType, bill=bill)
voted_ministers = []
for v in votes:
voted_ministers.append(v.minister.id)
qs = Minister.objects.filter(id__in=voted_ministers)
serializer = MinisterListSerializer(instance=qs, many=True)
return serializer.data

def get_other_ministers(self, bill):
votes = Vote.objects.select_related('minister').filter(bill=bill)
voted_ministers = []
for v in votes:
voted_ministers.append(v.minister.id)
qs = Minister.objects.exclude(id__in=voted_ministers)
serializer = MinisterListSerializer(instance=qs, many=True)
return serializer.data

def get_yay(self, bill):
return self.get_votes(bill, u'בעד')

def get_nay(self, bill):
return self.get_votes(bill, u'נגד')

def get_sustained(self, bill):
return self.get_votes(bill, u'נמנע')

def get_others(self, bill):
return self.get_other_ministers(bill)

class Meta:
model = Bill
fields = ('id', 'name', 'oknesset_url', 'passed', 'yay', 'nay', 'sustained', 'others')


class MinisterSerializer(MinisterListSerializer):

class Meta(MinisterListSerializer.Meta):
fields = MinisterListSerializer.Meta.fields + ('title',
'facebook', 'twitter', 'mail', 'phone', 'oknesset')


class MinisterVoteSerializer(serializers.ModelSerializer):
bill = VoteBillSerializer()
vote = serializers.StringRelatedField()

class Meta:
model = Vote
fields = ('bill', 'vote')




class MeetingListSerializer(serializers.ModelSerializer):
bill_count = serializers.IntegerField(
source='proposed_bill_count',
read_only=True
)

class Meta:
model = Meeting
fields = ('id', 'took_place', 'bill_count')


class MinisterInMeetingSerializer(serializers.ModelSerializer):

class Meta:
model = Minister
fields = ('id', 'name', 'url')


class MeetingSerializer(serializers.ModelSerializer):
proposed_bills = BillSerializer(many=True, read_only=True)
voting_ministers = MinisterListSerializer(many=True, read_only=True)
missing_ministers = MinisterListSerializer(many=True, read_only=True)
non_voting_ministers = serializers.SerializerMethodField()

def get_non_voting_ministers(self, meeting):
voting_ministers = meeting.voting_ministers.all()
voting_ids = voting_ministers.values_list('id', flat=True)
missing_ministers = meeting.missing_ministers.all()
missing_ids = missing_ministers.values_list('id', flat=True)
qs = Minister.objects.exclude(id__in=voting_ids).exclude(id__in=missing_ids)
serializer = MinisterListSerializer(instance=qs, many=True)
return serializer.data

class Meta:
model = Meeting


class MeetingBillSerializer(serializers.ModelSerializer):

class Meta:
model = Meeting
fields = ('id', 'took_place')


class BillDetailSerializer(serializers.ModelSerializer):
votes = VoteSerializer(many=True, read_only=True)
meeting = MeetingBillSerializer(many=True, read_only=True)

class Meta:
model = Bill


class MeetingDetailSerializer(serializers.HyperlinkedModelSerializer):
proposed_bills = BillVoteSerializer(many=True, read_only=True)
voting_ministers = MinisterListSerializer(many=True, read_only=True)
missing_ministers = MinisterListSerializer(many=True, read_only=True)
non_voting_ministers = serializers.SerializerMethodField()

def get_non_voting_ministers(self, meeting):
voting_ministers = meeting.voting_ministers.all()
voting_ids = voting_ministers.values_list('id', flat=True)
missing_ministers = meeting.missing_ministers.all()
missing_ids = missing_ministers.values_list('id', flat=True)
qs = Minister.objects.exclude(id__in=voting_ids).exclude(id__in=missing_ids)
serializer = MinisterListSerializer(instance=qs, many=True)
return serializer.data

class Meta:
model = Meeting
8 changes: 1 addition & 7 deletions committeeVotes/templates/committeeVotes/minister_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,9 @@ <h3>
</div>
<div class="col-md-4 hidden-sm hidden-xs">
<div class="pull-left">
{% if 'http' in minister.photo %}
<img src="{{ minister.photo }}.jpg">
{% else %}
<img src="{{ MEDIA_URL }}{{ minister.photo }}.jpg">
{% endif %}
<img src="{{ minister.photo_url }}">
</div>
</div>
</div>
</div>


{% endblock %}
Loading

0 comments on commit d45436d

Please sign in to comment.