Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

feat: add non-reporting centres view #50

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions locations/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.db.models import Q
from rest_framework import generics
from locations.models import Location

from locations.models import Location, LocationType
from locations.serializers import LocationSerializer


Expand Down Expand Up @@ -41,8 +43,13 @@ def get_queryset(self):
if not type_names:
return queryset.none()

types = type_names.split(u',')
queryset = queryset.filter(type__name__in=types)
types = [t.lower() for t in type_names.split(u',')]
terms = Q()
for typ in types:
terms |= Q(name__iexact=typ)

loc_types = LocationType.objects.filter(terms)
queryset = queryset.filter(type__in=loc_types)

# filter on parent id
parent_id = self.request.query_params.get(u'parent')
Expand Down
27 changes: 25 additions & 2 deletions locations/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import calendar

from django import forms
from .models import Location

from br.utils import get_report_year_range
from locations.models import Location


def generate_edit_form(location, data=None):
state_choices = Location.objects.filter(type__name='state').values_list(
state_choices = Location.objects.filter(type__name='State').values_list(
'id', 'name')

def clean_location_field(form, field_name, location_type):
Expand Down Expand Up @@ -64,3 +68,22 @@ class CenterCreationForm(forms.Form):
name = forms.CharField()
lga = forms.ModelChoiceField(queryset=Location.objects.filter(
type__name=u'LGA'))


def _get_year_choices():
choices = [('', '----- Select year -----')]
year_range = get_report_year_range()
choices.extend([(yr, yr) for yr in range(year_range[0], year_range[1] + 1)])
return choices


def _get_month_choices():
choices = [('', '----- Select month -----')]
choices.extend([(i, calendar.month_abbr[i]) for i in range(1, 13)])
return choices


class NonReportingCentresFilterForm(forms.Form):
location = forms.ModelChoiceField(queryset=Location.objects.filter(type__name__in=['State', 'LGA']), required=False)
year = forms.ChoiceField(choices=_get_year_choices, required=False)
month = forms.ChoiceField(choices=_get_month_choices, required=False)
11 changes: 11 additions & 0 deletions locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ def get_stock(self):
stock = None

return stock

def latest_birth_report_time(self):
if self.type.name != 'RC':
return None

try:
report = self.birthregistration_records.latest('time')

return report.time
except Exception:
return None


def get_locations_graph(reverse=False):
Expand Down
9 changes: 5 additions & 4 deletions locations/templates/locations/center_edit.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends 'base/layout.html' %}
{% load pipeline staticfiles %}
{% load pipeline staticfiles widget_tweaks %}
{% block stylesheets %}
{% stylesheet 'centers' %}
{% endblock %}
Expand All @@ -13,15 +13,15 @@
{{ form.id }}
<div class="form-group">
<label for="{{ form.name.name }}">{{ form.name.label }}</label>
<input type="text" class="form-control" id="{{ form.name.auto_id }}" name="{{ form.name.name }}" value="{{ form.name.value }}">
{% render_field form.name class='form-control' %}
</div>
<div class="form-group">
<label for="{{ form.code.name }}">{{ form.code.label }}</label>
<input type="text" class="form-control" id="{{ form.code.auto_id }}" name="{{ form.code.name }}" value="{{ form.code.value }}">
{% render_field form.code class='form-control' %}
</div>
<div class="form-group">
<label for="{{ form.state.name }}">{{ form.state.label }}</label>
{{ form.state }}
{% render_field form.state class='form-control' %}
</div>
<div class="form-group">
<label for="{{ form.lga.id_for_label }}">{{ form.lga.label|upper }}</label>
Expand All @@ -46,6 +46,7 @@
</div>
{% endblock %}
{% block scripts %}
{{ block.super }}
{% javascript 'centers' %}
<script type="text/javascript">
$(document).ready(function() {
Expand Down
136 changes: 136 additions & 0 deletions locations/templates/locations/non_reporting_centre_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{% extends 'base/layout.html' %}{% load bootstrap_pagination location_tags static widget_tweaks %}
{% block title %}Birth Registration Statistics &middot; {{ page_title }}{% endblock %}
{% block usermenu %}
{% include 'common/usermenu.html' %}
{% endblock %}
{% block stylesheets %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static 'css/select2/select2.min.css' %}">
<style type="text/css">
.select2-container.form-control {
display: inline-block;
}
</style>
{% endblock %}
{% block masthead %}
<div class="row">
<div class="col-lg-12 title">
<ol class="breadcrumb">
<li><a href="{% url "br:dashboard" %}">Birth Registration</a></li>
<li class="active">{{ page_title }}</li>
</ol>
<h1 class="page-title">{{ page_title }}</h1>
</div>
</div>
{% endblock %}
{% block content %}
<div class="row">
<div class="col-lg-12">
<section class="widget">
<form class="form-horizontal form-inline" role="form" id="filter_form">
<fieldset>
<div class="form-group row">
<div class="col-md-12">
<select name="{{ filter_form.location.name }}" id="{{ filter_form.location.id_for_label }}" data-placeholder="Location">
{% if filter_form.cleaned_data.location %}<option value="{{ filter_form.cleaned_data.location.pk }}">{{ filter_form.cleaned_data.location.name }}</option>{% endif %}
</select>
{% render_field filter_form.year class="form-control" %}
{% render_field filter_form.month class="form-control" %}
<button type="submit" class="btn btn-primary" id="filter_button">Filter</button>
<a class="btn btn-inverse" href="{% url 'locations:non_reporting_center_list' %}">Clear</a>
<a class="btn btn-primary-outline" href="{% url 'locations:non_reporting_center_list' %}?export=1&year={{ year }}&month={{ month }}&location={{ location }}">Export</a>
</div>
</div>
</fieldset>
</form>
</section>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<section class="widget">
<div class="widget-table">
<table class="table table-striped table-bordered table-hover">
<caption>{{ caption }}</caption>
<thead>
<tr>
<th>State</th>
<th>LGA</th>
<th>Name</th>
<th>Code</th>
<th>Active?</th>
<th>Last report received</th>
<th></th>
</tr>
</thead>
<tbody>
{% for centre in centres %}
{% with ancestors=centre.get_ancestors %}
<tr>
<td>{{ ancestors|extract_ancestor_name:'State' }}</td>
<td>{{ ancestors|extract_ancestor_name:'LGA' }}</td>
<td>{{ centre.name }}</td>
<td>{{ centre.code }}</td>
{% if centre.active %}
<td class="success"><i class="glyphicon glyphicon-ok"></i></td>
{% else %}
<td class="danger"><i class="glyphicon glyphicon-remove"></i></td>
{% endif %}
<td>{{ centre.latest_birth_report_time|date:'SHORT_DATE_FORMAT' }}</td>
<td><a href="{% url 'locations:center_edit' pk=centre.pk %}">Edit</a></td>
</tr>
{% endwith %}
{% empty %}
<tr>
<td class="table-warning text-xs-center" colspan="7">No non-reporting centers found</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</section>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="text-xs-center">
{% bootstrap_paginate page_obj range=10 show_first_last="true" %}
</div>
</div>
</div>
{% endblock content %}
{% block scripts %}
{{ block.super }}
<script src="{% static "js/select2/select2.min.js" %}"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
var endpoint = '{% url "api:location_list_typed" %}?type=state,lga';
$('#{{ filter_form.location.id_for_label }}').select2({
ajax: {
url: endpoint,
data: function(params) {
return {q: params.term};
},
dataType: 'json',
quietMillis: 250,
processResults: function(data, params) {
return {results: data.results};
}
},
width: '30em',
allowClear: true,
minimumInputLength: 2,
cache: true,
templateResult: function(item) {
return item.name + ' (' + item.type + ')';
},
templateSelection: function(item) {
if (item.text)
return item.text;
else
return item.name + ' (' + item.type + ')';
}
});
});
</script>
{% endblock %}
9 changes: 5 additions & 4 deletions locations/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4
from django.conf.urls import url
from locations.views import *
from locations import views

urlpatterns = [
url(r'^center/new/?$', CenterCreationView.as_view(), name='center_add'),
url(r'^centers/?$', CenterListView.as_view(), name='center_list'),
url(r'^center/(?P<pk>\d+)/?$', CenterUpdateView.as_view(), name='center_edit'),
url(r'^center/new/?$', views.CenterCreationView.as_view(), name='center_add'),
url(r'^centers/?$', views.CenterListView.as_view(), name='center_list'),
url(r'^non-reporting-centers/?$', views.NonReportingCentresView.as_view(), name='non_reporting_center_list'),
url(r'^center/(?P<pk>\d+)/?$', views.CenterUpdateView.as_view(), name='center_edit'),
]
Loading