From 501f7bc5f30db67a5d185580be1a5f15e5e2fdf4 Mon Sep 17 00:00:00 2001 From: Matheus Fernandes Date: Tue, 9 Jul 2019 15:35:47 -0300 Subject: [PATCH 1/3] Add counters to bill serializer Signed-off-by: Matheus Fernandes --- wikilegis/api/resources.py | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/wikilegis/api/resources.py b/wikilegis/api/resources.py index 90759362..d6def927 100644 --- a/wikilegis/api/resources.py +++ b/wikilegis/api/resources.py @@ -78,6 +78,47 @@ class BillResource(ModelResource): comments = fields.ToManyField('api.resources.CommentResource', 'comments') votes = fields.ToManyField('api.resources.UpDownVoteResource', 'votes') + def dehydrate(self, bundle): + segment_upvotes = 0 + segment_downvotes = 0 + segment_votes = 0 + additive_count = 0 + supress_count = 0 + modifier_count = 0 + ids = [] + + for segment in bundle.obj.segments.all(): + additive_count += segment.additive_amendments_count + supress_count += segment.supress_amendments_count + modifier_count += segment.modifier_amendments_count + + ids += list(segment.modifier_amendments.values_list( + 'author__id', flat=True + )) + ids += list(segment.additive_amendments.values_list( + 'author__id', flat=True + )) + ids += list(segment.supress_amendments.values_list( + 'author__id', flat=True + )) + + for vote in segment.votes.all(): + ids.append(vote.user.id) + if vote.vote: + segment_upvotes += 1 + else: + segment_downvotes += 1 + segment_votes += 1 + + bundle.data['segment_upvotes'] = segment_upvotes + bundle.data['segment_downvotes'] = segment_downvotes + bundle.data['segment_votes'] = segment_votes + bundle.data['additive_amendments_count'] = additive_count + bundle.data['supress_amendments_count'] = supress_count + bundle.data['modifier_amendments_count'] = modifier_count + bundle.data['participants_count'] = len(set(ids)) + return bundle + class Meta: queryset = core_models.Bill.objects.filter( allowed_users__isnull=True, From 2e6e1f7e71ff2a6323165a2a75a66c3a3209f263 Mon Sep 17 00:00:00 2001 From: Matheus Fernandes Date: Fri, 12 Jul 2019 16:14:13 -0300 Subject: [PATCH 2/3] Refactor counter extrator Signed-off-by: Matheus Fernandes --- wikilegis/api/resources.py | 77 +++++++++++++++------------- wikilegis/api/statistics.py | 78 +++++++++++++++++++++++++++++ wikilegis/wikilegis/settings/api.py | 2 +- 3 files changed, 121 insertions(+), 36 deletions(-) create mode 100644 wikilegis/api/statistics.py diff --git a/wikilegis/api/resources.py b/wikilegis/api/resources.py index d6def927..ec8ab4e2 100644 --- a/wikilegis/api/resources.py +++ b/wikilegis/api/resources.py @@ -7,6 +7,7 @@ from tastypie import fields from core import models as core_models +from api import statistics class UserResource(ModelResource): @@ -78,45 +79,50 @@ class BillResource(ModelResource): comments = fields.ToManyField('api.resources.CommentResource', 'comments') votes = fields.ToManyField('api.resources.UpDownVoteResource', 'votes') - def dehydrate(self, bundle): - segment_upvotes = 0 - segment_downvotes = 0 + def alter_list_data_to_serialize(self, request, data): segment_votes = 0 + amendment_votes = 0 + bill_votes = 0 additive_count = 0 supress_count = 0 modifier_count = 0 - ids = [] - - for segment in bundle.obj.segments.all(): - additive_count += segment.additive_amendments_count - supress_count += segment.supress_amendments_count - modifier_count += segment.modifier_amendments_count - - ids += list(segment.modifier_amendments.values_list( - 'author__id', flat=True - )) - ids += list(segment.additive_amendments.values_list( - 'author__id', flat=True - )) - ids += list(segment.supress_amendments.values_list( - 'author__id', flat=True - )) - - for vote in segment.votes.all(): - ids.append(vote.user.id) - if vote.vote: - segment_upvotes += 1 - else: - segment_downvotes += 1 - segment_votes += 1 - - bundle.data['segment_upvotes'] = segment_upvotes - bundle.data['segment_downvotes'] = segment_downvotes - bundle.data['segment_votes'] = segment_votes - bundle.data['additive_amendments_count'] = additive_count - bundle.data['supress_amendments_count'] = supress_count - bundle.data['modifier_amendments_count'] = modifier_count - bundle.data['participants_count'] = len(set(ids)) + comments_count = 0 + participants_count = 0 + + for bill in self.get_object_list(request): + stats = statistics.bill_stats(bill) + bill_votes += stats['bill_votes'] + additive_count += stats['additive_count'] + supress_count += stats['supress_count'] + modifier_count += stats['modifier_count'] + comments_count += stats['comments_count'] + segment_votes += stats['segment_votes'] + amendment_votes += stats['amendment_votes'] + participants_count += stats['participants_count'] + + data['meta']['segment_votes'] = segment_votes + data['meta']['amendment_votes'] = amendment_votes + data['meta']['comments_count'] = comments_count + data['meta']['bill_votes'] = bill_votes + data['meta']['additive_count'] = additive_count + data['meta']['supress_count'] = supress_count + data['meta']['modifier_count'] = modifier_count + data['meta']['amendments_count'] = ( + additive_count + supress_count + modifier_count + ) + data['meta']['participants_count'] = participants_count + return data + + def dehydrate(self, bundle): + stats = statistics.bill_stats(bundle.obj) + + bundle.data['amendment_votes'] = stats['amendment_votes'] + bundle.data['segment_votes'] = stats['segment_votes'] + bundle.data['comments_count'] = stats['comments_count'] + bundle.data['additive_amendments_count'] = stats['additive_count'] + bundle.data['supress_amendments_count'] = stats['supress_count'] + bundle.data['modifier_amendments_count'] = stats['modifier_count'] + bundle.data['participants_count'] = stats['participants_count'] return bundle class Meta: @@ -133,6 +139,7 @@ class Meta: 'status': ALL, 'id': ALL, 'closing_date': ALL, + 'created': ALL, } ordering = ['closing_date', 'status', 'modified'] diff --git a/wikilegis/api/statistics.py b/wikilegis/api/statistics.py new file mode 100644 index 00000000..3c7f8ed1 --- /dev/null +++ b/wikilegis/api/statistics.py @@ -0,0 +1,78 @@ + + +def segment_stats(segment): + ids = [] + comments_count = segment.comments_count + amendment_votes = 0 + + ids += list(segment.modifier_amendments.values_list( + 'author__id', flat=True + )) + for amendment in segment.modifier_amendments.all(): + ids += list(amendment.comments.values_list('author__id', flat=True)) + comments_count += amendment.comments_count + amendment_votes += amendment.votes_count + + ids += list(segment.additive_amendments.values_list( + 'author__id', flat=True + )) + for amendment in segment.additive_amendments.all(): + ids += list(amendment.comments.values_list('author__id', flat=True)) + comments_count += amendment.comments_count + amendment_votes += amendment.votes_count + + ids += list(segment.supress_amendments.values_list( + 'author__id', flat=True + )) + for amendment in segment.supress_amendments.all(): + ids += list(amendment.comments.values_list('author__id', flat=True)) + comments_count += amendment.comments_count + amendment_votes += amendment.votes_count + + ids += list(segment.comments.values_list('author__id', flat=True)) + + return { + 'participants': len(set(ids)), + 'participants_ids': set(ids), + 'amendment_votes': amendment_votes, + 'segment_votes': segment.votes_count, + 'additive_amendments': segment.additive_amendments_count, + 'supress_amendments': segment.supress_amendments_count, + 'modifier_amendments': segment.modifier_amendments_count, + 'comments_count': comments_count + } + + +def bill_stats(bill): + ids = [] + segment_votes = 0 + amendment_votes = 0 + bill_votes = bill.votes_count + additive_count = 0 + supress_count = 0 + modifier_count = 0 + comments_count = 0 + + for segment in bill.segments.all(): + stats = segment_stats(segment) + ids += list(stats['participants_ids']) + segment_votes += stats['segment_votes'] + amendment_votes += stats['amendment_votes'] + additive_count += stats['additive_amendments'] + modifier_count += stats['modifier_amendments'] + supress_count += stats['supress_amendments'] + comments_count += stats['comments_count'] + + ids += list(bill.votes.values_list('user__id', flat=True)) + + return { + 'participants_count': len(set(ids)), + 'segment_votes': segment_votes, + 'amendment_votes': amendment_votes, + 'comments_count': comments_count, + 'bill_votes': bill_votes, + 'additive_count': additive_count, + 'supress_count': supress_count, + 'modifier_count': modifier_count, + 'amendments_count': additive_count + supress_count + modifier_count, + } diff --git a/wikilegis/wikilegis/settings/api.py b/wikilegis/wikilegis/settings/api.py index 99157095..79e44a31 100644 --- a/wikilegis/wikilegis/settings/api.py +++ b/wikilegis/wikilegis/settings/api.py @@ -2,4 +2,4 @@ API_KEY = config('API_KEY', default='apikey') -TASTYPIE_DEFAULT_FORMATS = ['json'] +TASTYPIE_DEFAULT_FORMATS = ['json'] \ No newline at end of file From e12a3bfb9e4cd613c0cb4f823cbd706d8d39a42b Mon Sep 17 00:00:00 2001 From: Matheus Fernandes Date: Wed, 17 Jul 2019 15:27:53 -0300 Subject: [PATCH 3/3] Add user statistics Signed-off-by: Matheus Fernandes --- wikilegis/api/resources.py | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/wikilegis/api/resources.py b/wikilegis/api/resources.py index ec8ab4e2..0e64fd07 100644 --- a/wikilegis/api/resources.py +++ b/wikilegis/api/resources.py @@ -17,6 +17,80 @@ def dehydrate(self, bundle): bundle.data.pop('is_staff', None) bundle.data.pop('is_superuser', None) + bundle.data['votes_count'] = bundle.obj.updownvote_set.count() + bundle.data['comments_count'] = bundle.obj.comment_set.count() + bundle.data['additive_count'] = bundle.obj.additiveamendment_set.count() + bundle.data['modifier_count'] = bundle.obj.modifieramendment_set.count() + bundle.data['supress_count'] = bundle.obj.supressamendment_set.count() + + bill_count = 0 + for bill in core_models.Bill.objects.all(): + if bill.votes.filter(user=bundle.obj).count() > 0: + bill_count += 1 + continue + + for segment in bill.segments.all(): + if segment.votes.filter(user=bundle.obj).count() > 0: + bill_count += 1 + break + + if segment.comments.filter(author=bundle.obj).count() > 0: + bill_count += 1 + break + + has_participation = False + for amendment in segment.additive_amendments.all(): + if amendment.author == bundle.obj: + has_participation = True + break + else: + if amendment.votes.filter(user=bundle.obj).count() > 0: + has_participation = True + break + + if amendment.comments.filter(author=bundle.obj).count() > 0: + has_participation = True + break + + if has_participation: + bill_count += 1 + break + + for amendment in segment.modifier_amendments.all(): + if amendment.author == bundle.obj: + has_participation = True + break + else: + if amendment.votes.filter(user=bundle.obj).count() > 0: + has_participation = True + break + + if amendment.comments.filter(author=bundle.obj).count() > 0: + has_participation = True + break + + if has_participation: + bill_count += 1 + break + + for amendment in segment.supress_amendments.all(): + if amendment.author == bundle.obj: + has_participation = True + break + else: + if amendment.votes.filter(user=bundle.obj).count() > 0: + has_participation = True + break + + if amendment.comments.filter(author=bundle.obj).count() > 0: + has_participation = True + break + + if has_participation: + bill_count += 1 + break + bundle.data['bill_participations'] = bill_count + key = bundle.request.GET.get('api_key', None) if key != settings.API_KEY: del bundle.data['email']