Skip to content

Commit

Permalink
Adjust migrations and filter by published more
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Oct 25, 2023
1 parent 2d14656 commit a35d807
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
16 changes: 10 additions & 6 deletions ureport/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def create_org(self, subdomain, user):
self.assertEqual(Org.objects.filter(domain=subdomain).count(), 1)
return Org.objects.get(domain=subdomain)

def create_poll(self, title, is_featured=False, has_synced=True):
def create_poll(self, title, is_featured=False, has_synced=True, published=True):
now = timezone.now()
return Poll.objects.create(
flow_uuid=six.text_type(randint(1000, 9999)),
Expand All @@ -133,6 +133,7 @@ def create_poll(self, title, is_featured=False, has_synced=True):
poll_date=now,
org=self.uganda,
is_featured=is_featured,
published=published,
has_synced=has_synced,
created_by=self.superuser,
modified_by=self.superuser,
Expand Down Expand Up @@ -372,20 +373,23 @@ def test_polls_by_org_list(self):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["count"], Poll.objects.filter(org=self.uganda, is_active=True, has_synced=True).count()
response.data["count"],
Poll.objects.filter(org=self.uganda, is_active=True, published=True, has_synced=True).count(),
)
self.assertTrue(response.data["results"][0]["created_on"] > response.data["results"][1]["created_on"])

response = self.client.get(url2)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["count"], Poll.objects.filter(org=self.nigeria, is_active=True, has_synced=True).count()
response.data["count"],
Poll.objects.filter(org=self.nigeria, is_active=True, published=True, has_synced=True).count(),
)

response = self.client.get(f"{url}?sort=modified_on")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["count"], Poll.objects.filter(org=self.uganda, is_active=True, has_synced=True).count()
response.data["count"],
Poll.objects.filter(org=self.uganda, is_active=True, published=True, has_synced=True).count(),
)
self.assertTrue(response.data["results"][0]["modified_on"] > response.data["results"][1]["modified_on"])

Expand All @@ -400,7 +404,7 @@ def test_polls_by_org_list_with_fields_parameter(self):
url = "/api/v1/polls/org/%d/?fields=%s" % (self.uganda.pk, "title")
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
count_polls = Poll.objects.filter(org=self.uganda, is_active=True, has_synced=True).count()
count_polls = Poll.objects.filter(org=self.uganda, is_active=True, published=True, has_synced=True).count()
self.assertEqual(response.data["count"], count_polls)
polls = [self.second_featured_poll, self.first_featured_poll, self.another_poll, self.reg_poll]
for i in range(count_polls):
Expand All @@ -418,7 +422,7 @@ def test_polls_by_org_list_with_exclude_parameter(self):
)
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
count_polls = Poll.objects.filter(org=self.uganda, is_active=True, has_synced=True).count()
count_polls = Poll.objects.filter(org=self.uganda, is_active=True, published=True, has_synced=True).count()
poll = self.reg_poll
self.assertEqual(response.data["count"], count_polls)
self.assertDictEqual(
Expand Down
2 changes: 1 addition & 1 deletion ureport/polls/migrations/0075_populate_published.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def populate_poll_published(apps, schema_editor): # pragma: no cover
Poll.objects.filter(is_active=False).update(published=False)

# make all poll have is_active=True
Poll.objects.filter(is_active=False).update(is_active=True)
Poll.objects.filter(is_active=False).exclude(flow_uuid="").update(is_active=True)


class Migration(migrations.Migration):
Expand Down
1 change: 1 addition & 0 deletions ureport/polls/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def pre_save(self, obj):
flow_uuid=obj.flow_uuid,
backend=obj.backend,
is_active=True,
published=True,
created_on__gte=five_minutes_ago,
).first()
if similar_poll:
Expand Down
4 changes: 3 additions & 1 deletion ureport/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,9 @@ def get_average_response_rate(cls, org):
def calculate_average_response_rate(cls, org):
key = f"org:{org.id}:average_response_rate"

poll_ids = list(Poll.objects.filter(org_id=org.id, is_active=True).only("id").values_list("id", flat=True))
poll_ids = list(
Poll.objects.filter(org_id=org.id, published=True, is_active=True).only("id").values_list("id", flat=True)
)

flow_result_ids = list(
PollQuestion.objects.filter(is_active=True, poll_id__in=poll_ids).values_list("flow_result_id", flat=True)
Expand Down
2 changes: 1 addition & 1 deletion ureport/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ def populate_contact_activity(org):
start_date = now - timedelta(days=365)

flows = list(
Poll.objects.filter(org_id=org.id, poll_date__gte=start_date)
Poll.objects.filter(org_id=org.id, published=True, poll_date__gte=start_date)
.only("flow_uuid")
.values_list("flow_uuid", flat=True)
)
Expand Down

0 comments on commit a35d807

Please sign in to comment.