Skip to content

Commit

Permalink
Merge b854ebd into f1667a5
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMwashuma committed May 19, 2021
2 parents f1667a5 + b854ebd commit a3eb44b
Show file tree
Hide file tree
Showing 85 changed files with 11,301 additions and 862 deletions.
4 changes: 4 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exclude_paths:
- "docs/*"
- "tally_ho/libs/*"
- "tally_ho/apps/tally/static/js/data_table.js"
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
&& python manage.py create_groups --settings=tally_ho.settings.docker
&& python manage.py create_demo_users --settings=tally_ho.settings.docker
&& python manage.py create_quarantine_checks --settings=tally_ho.settings.docker
&& DJANGO_SETTINGS_MODULE=tally_ho.settings.docker gunicorn --chdir /code --bind :8000 tally_ho.wsgi:application"
&& DJANGO_SETTINGS_MODULE=tally_ho.settings.docker gunicorn --limit-request-line 6000 --chdir /code --bind :8000 tally_ho.wsgi:application"
volumes:
- .:/code
- static_volume:/code/tally_ho/static
Expand Down
19 changes: 18 additions & 1 deletion tally_ho/apps/tally/forms/create_station_form.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from django import forms
from django.forms import ModelForm

from django.forms import (
ModelForm,
ModelChoiceField
)
from tally_ho.apps.tally.models.station import Station
from tally_ho.apps.tally.models.center import Center
from tally_ho.apps.tally.models.sub_constituency import SubConstituency

disable_copy_input = {
'onCopy': 'return false;',
Expand All @@ -27,3 +33,14 @@ class Meta:
widgets = {
"tally": forms.HiddenInput()
}

def __init__(self, *args, **kwargs):
super(CreateStationForm, self).__init__(*args, **kwargs)

if self.initial.get('tally'):
self.fields['center'] = ModelChoiceField(
queryset=Center.objects.filter(
tally__id=11))
self.fields['sub_constituency'] = ModelChoiceField(
queryset=SubConstituency.objects.filter(
tally__id=self.initial['tally']))
2 changes: 1 addition & 1 deletion tally_ho/apps/tally/forms/edit_user_profile_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __init__(self, *args, **kwargs):

def clean(self):
if self.is_valid():
lower_case_form_data(self, EditUserProfileForm, ['username'])
lower_case_form_data(self, EditAdminProfileForm, ['username'])

def save(self):
user = super(EditAdminProfileForm, self).save()
Expand Down
26 changes: 22 additions & 4 deletions tally_ho/apps/tally/forms/quarantine_form.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import forms
from django.forms import ModelForm

from tally_ho.apps.tally.models.quarantine_check import QuarantineCheck
Expand All @@ -9,18 +10,35 @@
'onDrop': 'return false;',
'onPaste': 'return false;',
'autocomplete': 'off',
'class': 'form-control required'
'class': 'form-control'
}


class QuarantineCheckForm(ModelForm):
class Meta:
model = QuarantineCheck
fields = ['name', 'value', 'percentage', 'active']
fields = ['name', 'value', 'percentage', 'active', 'description']

value = forms.CharField(required=False)
percentage = forms.CharField(required=False)

def __init__(self, *args, **kargs):
super(QuarantineCheckForm, self).__init__(*args, **kargs)
self.fields['name'].widget.attrs.update({'class': 'form-control'})
self.fields['value'].widget.attrs.update({'class': 'form-control'})
self.fields['percentage'].widget.attrs.update(

if self.initial['value'] == 0:
self.fields['value'].widget.attrs.update(
{'class': 'form-control', 'disabled': ''})
else:
self.fields['value'].widget.attrs.update(
{'class': 'form-control'})

if self.initial['percentage'] == 0:
self.fields['percentage'].widget.attrs.update(
{'class': 'form-control', 'disabled': ''})
else:
self.fields['percentage'].widget.attrs.update(
{'class': 'form-control'})

self.fields['description'].widget.attrs.update(
{'class': 'form-control'})
18 changes: 18 additions & 0 deletions tally_ho/apps/tally/migrations/0034_quarantinecheck_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.1 on 2020-11-13 07:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tally', '0033_auto_20201028_1543'),
]

operations = [
migrations.AddField(
model_name='quarantinecheck',
name='description',
field=models.TextField(blank=True, null=True),
),
]
23 changes: 23 additions & 0 deletions tally_ho/apps/tally/migrations/0035_auto_20201206_0709.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.1.1 on 2020-12-06 07:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tally', '0034_quarantinecheck_description'),
]

operations = [
migrations.AlterField(
model_name='quarantinecheck',
name='percentage',
field=models.FloatField(default=0),
),
migrations.AlterField(
model_name='quarantinecheck',
name='value',
field=models.FloatField(default=0),
),
]
48 changes: 48 additions & 0 deletions tally_ho/apps/tally/migrations/0036_allcandidatesvotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 2.1.1 on 2021-02-01 13:45

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tally', '0035_auto_20201206_0709'),
]

operations = [
migrations.CreateModel(
name='AllCandidatesVotes',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tally_id', models.IntegerField()),
('full_name', models.CharField(max_length=255)),
('ballot_number', models.IntegerField()),
('candidate_id', models.IntegerField()),
('candidate_active', models.BooleanField(default=False)),
('stations', models.PositiveIntegerField(default=0)),
('center_ids', django.contrib.postgres.fields.ArrayField(
base_field=models.IntegerField(), size=None)),
('station_numbers', django.contrib.postgres.fields.ArrayField(base_field=models.PositiveSmallIntegerField(blank=True, null=True), size=None)),
('stations_completed', models.PositiveIntegerField(default=0)),
('votes', models.PositiveIntegerField(default=0)),
('total_votes', models.PositiveIntegerField(default=0)),
('all_candidate_votes', models.PositiveIntegerField(default=0)),
('candidate_votes_included_quarantine', models.PositiveIntegerField(default=0)),
('stations_complete_percent', models.IntegerField()),
],
options={
'managed': False,
},
),
migrations.RunSQL(
"""
CREATE MATERIALIZED VIEW tally_allcandidatesvotes AS
SELECT "tally_candidate"."full_name", "tally_candidate"."tally_id" AS "tally_id", "tally_candidate"."id" AS "candidate_id", "tally_ballot"."number" AS "ballot_number", "tally_candidate"."active" AS "candidate_active", COUNT("tally_resultform"."id") FILTER (WHERE ("tally_resultform"."ballot_id" IS NOT NULL AND "tally_resultform"."center_id" IS NOT NULL AND "tally_resultform"."station_number" IS NOT NULL AND "tally_resultform"."tally_id" = ("tally_candidate"."tally_id"))) AS "stations", COUNT("tally_resultform"."id") FILTER (WHERE ("tally_resultform"."ballot_id" IS NOT NULL AND "tally_resultform"."center_id" IS NOT NULL AND "tally_resultform"."form_state" = 0 AND "tally_resultform"."station_number" IS NOT NULL AND "tally_resultform"."tally_id" = ("tally_candidate"."tally_id"))) AS "stations_completed", (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND U3."form_state" = 0) LIMIT 1) AS "votes", CASE WHEN (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND U3."form_state" = 0) LIMIT 1) IS NOT NULL THEN (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND U3."form_state" = 0) LIMIT 1) ELSE 0 END AS "total_votes", (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND (U3."form_state" = 0 OR U3."form_state" = 2)) LIMIT 1) AS "all_candidate_votes", CASE WHEN (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND (U3."form_state" = 0 OR U3."form_state" = 2)) LIMIT 1) IS NOT NULL THEN (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND (U3."form_state" = 0 OR U3."form_state" = 2)) LIMIT 1) ELSE 0 END AS "candidate_votes_included_quarantine", CASE WHEN COUNT("tally_resultform"."id") FILTER (WHERE ("tally_resultform"."ballot_id" IS NOT NULL AND "tally_resultform"."center_id" IS NOT NULL AND "tally_resultform"."station_number" IS NOT NULL AND "tally_resultform"."tally_id" = ("tally_candidate"."tally_id"))) > 0 THEN ROUND(CAST(((100 * CAST(COUNT("tally_resultform"."id") FILTER (WHERE ("tally_resultform"."ballot_id" IS NOT NULL AND "tally_resultform"."center_id" IS NOT NULL AND "tally_resultform"."form_state" = 0 AND "tally_resultform"."station_number" IS NOT NULL AND "tally_resultform"."tally_id" = ("tally_candidate"."tally_id"))) AS FLOAT)) / CAST(COUNT("tally_resultform"."id") FILTER (WHERE ("tally_resultform"."ballot_id" IS NOT NULL AND "tally_resultform"."center_id" IS NOT NULL AND "tally_resultform"."station_number" IS NOT NULL AND "tally_resultform"."tally_id" = ("tally_candidate"."tally_id"))) AS FLOAT)) AS numeric), 3) ELSE 0 END AS "stations_complete_percent", ARRAY_AGG(DISTINCT "tally_center"."id") AS "center_ids", ARRAY_AGG(DISTINCT "tally_resultform"."station_number") AS "station_numbers" FROM "tally_candidate" INNER JOIN "tally_ballot" ON ("tally_candidate"."ballot_id" = "tally_ballot"."id") LEFT OUTER JOIN "tally_resultform" ON ("tally_ballot"."id" = "tally_resultform"."ballot_id") LEFT OUTER JOIN "tally_center" ON ("tally_resultform"."center_id" = "tally_center"."id") GROUP BY "tally_candidate"."id", "tally_ballot"."number", (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND U3."form_state" = 0) LIMIT 1), CASE WHEN (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND U3."form_state" = 0) LIMIT 1) IS NOT NULL THEN (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND U3."form_state" = 0) LIMIT 1) ELSE 0 END, (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND (U3."form_state" = 0 OR U3."form_state" = 2)) LIMIT 1), CASE WHEN (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND (U3."form_state" = 0 OR U3."form_state" = 2)) LIMIT 1) IS NOT NULL THEN (SELECT CASE WHEN U0."votes" IS NOT NULL THEN U0."votes" ELSE 0 END AS "candidate_votes" FROM "tally_result" U0 INNER JOIN "tally_candidate" U1 ON (U0."candidate_id" = U1."id") INNER JOIN "tally_resultform" U3 ON (U0."result_form_id" = U3."id") WHERE (U0."active" = true AND U0."candidate_id" = ("tally_candidate"."id") AND U1."tally_id" = ("tally_candidate"."tally_id") AND U0."entry_version" = 2 AND (U3."form_state" = 0 OR U3."form_state" = 2)) LIMIT 1) ELSE 0 END;
CREATE UNIQUE INDEX tally_allcandidatesvotes_pk ON tally_allcandidatesvotes(candidate_id);
""",
"""
DROP MATERIALIZED VIEW tally_allcandidatesvotes;
"""
),
]
30 changes: 30 additions & 0 deletions tally_ho/apps/tally/models/all_candidates_votes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.db import models
from django.contrib.postgres.fields import ArrayField

import reversion


class AllCandidatesVotes(models.Model):
class Meta:
app_label = 'tally'
managed = False

tally_id = models.IntegerField()
full_name = models.CharField(max_length=255)
ballot_number = models.IntegerField()
candidate_id = models.IntegerField()
candidate_active = models.BooleanField(default=False)
stations = models.PositiveIntegerField(default=0)
center_ids = ArrayField(models.IntegerField())
station_numbers = ArrayField(models.PositiveSmallIntegerField(
blank=True, null=True))
stations_completed = models.PositiveIntegerField(default=0)
votes = models.PositiveIntegerField(default=0)
total_votes = models.PositiveIntegerField(default=0)
all_candidate_votes = models.PositiveIntegerField(default=0)
candidate_votes_included_quarantine = models.PositiveIntegerField(
default=0)
stations_complete_percent = models.IntegerField()


reversion.register(AllCandidatesVotes)
5 changes: 3 additions & 2 deletions tally_ho/apps/tally/models/quarantine_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class Meta:

name = models.CharField(max_length=256, unique=True)
method = models.CharField(max_length=256, unique=True)
value = models.FloatField()
percentage = models.FloatField(default=100)
value = models.FloatField(default=0)
percentage = models.FloatField(default=0)
active = models.BooleanField(default=False)
description = models.TextField(null=True, blank=True)

def local_name(self):
return _(self.name)
Expand Down
12 changes: 6 additions & 6 deletions tally_ho/apps/tally/models/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def save(self, *args, **kwargs):

super(UserProfile, self).save(*args, **kwargs)

@property
def get_edit_link(self):
return get_edit_user_link(self) if self else None
def get_edit_link(self, **kwargs):
role = kwargs.get('role', 'user')
return get_edit_user_link(self, role=role) if self else None

@property
def get_edit_tally_link(self):
return get_edit_user_link(self, True) if self else None
def get_edit_tally_link(self, **kwargs):
role = kwargs.get('role', 'user')
return get_edit_user_link(self, True, role=role) if self else None

@property
def is_administrator(self):
Expand Down

0 comments on commit a3eb44b

Please sign in to comment.