Skip to content

Commit

Permalink
ACT-453: Simplify target frequency labels (#490)
Browse files Browse the repository at this point in the history
* rename existing labels

* add new reporting frequencies

* add migrations

Co-authored-by: Andrew <[email protected]>
  • Loading branch information
Kimaiyo077 and andrewtpham authored Apr 25, 2020
1 parent c0590c8 commit b1f9c5b
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 44 deletions.
23 changes: 23 additions & 0 deletions indicators/migrations/0012_auto_20200424_1649.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.10 on 2020-04-24 13:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('indicators', '0011_merge_20200312_2019'),
]

operations = [
migrations.AlterField(
model_name='historicalindicator',
name='target_frequency',
field=models.IntegerField(blank=True, choices=[(1, 'Life of Program only'), (3, 'Every Year'), (4, 'Every Six Months'), (5, 'Every Four Months'), (6, 'Every Three Months'), (7, 'Every Month'), (8, 'Every Week'), (9, 'Every Two Weeks')], help_text=' ', null=True, verbose_name='Target frequency'),
),
migrations.AlterField(
model_name='indicator',
name='target_frequency',
field=models.IntegerField(blank=True, choices=[(1, 'Life of Program only'), (3, 'Every Year'), (4, 'Every Six Months'), (5, 'Every Four Months'), (6, 'Every Three Months'), (7, 'Every Month'), (8, 'Every Week'), (9, 'Every Two Weeks')], help_text=' ', null=True, verbose_name='Target frequency'),
),
]
22 changes: 10 additions & 12 deletions indicators/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,23 @@ def get_queryset(self):

class Indicator(models.Model):
LOP = 1
MID_END = 2
ANNUAL = 3
SEMI_ANNUAL = 4
TRI_ANNUAL = 5
QUARTERLY = 6
MONTHLY = 7
EVENT = 8
WEEKLY = 8
BIWEEKLY = 9

TARGET_FREQUENCIES = (
(LOP, 'Life of Program only'),
(MID_END, 'Midline and endline'),
(ANNUAL, 'Annual'),
(SEMI_ANNUAL, 'Semi-annual'),
(TRI_ANNUAL, 'Tri-annual'),
(QUARTERLY, 'Quarterly'),
(MONTHLY, 'Monthly'),
(EVENT, 'Event')
(ANNUAL, 'Every Year'),
(SEMI_ANNUAL, 'Every Six Months'),
(TRI_ANNUAL, 'Every Four Months'),
(QUARTERLY, 'Every Three Months'),
(MONTHLY, 'Every Month'),
(WEEKLY, 'Every Week'),
(BIWEEKLY, 'Every Two Weeks')
)

indicator_key = models.UUIDField(
Expand Down Expand Up @@ -452,9 +452,7 @@ class PeriodicTarget(models.Model):
edit_date = models.DateTimeField(null=True, blank=True, auto_now=True)

def __str__(self):
if self.indicator.target_frequency == Indicator.LOP \
or self.indicator.target_frequency == Indicator.EVENT \
or self.indicator.target_frequency == Indicator.MID_END:
if self.indicator.target_frequency == Indicator.LOP:
return self.period
if self.start_date and self.end_date:
return "%s (%s - %s)" % (
Expand Down
13 changes: 8 additions & 5 deletions indicators/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
DisaggregationType, DisaggregationValue
)
from workflow.models import Documentation
from workflow.serializers import ProgramSerializer


class DisaggregationLabelSerializer(serializers.ModelSerializer):
Expand All @@ -24,10 +25,11 @@ class Meta:

class IndicatorSerializer(serializers.ModelSerializer):
disaggregation = DisaggregationTypeSerializer(read_only=True, many=True)
program = ProgramSerializer(read_only=True, many=True)

class Meta:
model = Indicator
fields = ['id', 'name', 'lop_target', 'disaggregation']
fields = ['id', 'name', 'lop_target', 'baseline', 'rationale_for_target', 'disaggregation', 'program']


class DisaggregationValueSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -62,10 +64,11 @@ class Meta:
fields = ['id', 'period', 'start_date', 'end_date', 'target', 'collecteddata_set', 'indicator']

def get_indicator(self, obj):
return {"indicator_id": obj.indicator.id,
return {"id": obj.indicator.id,
"baseline": obj.indicator.baseline,
"indicator_lop": obj.indicator.lop_target,
"rationale": obj.indicator.rationale_for_target, }
"lop_target": obj.indicator.lop_target,
"rationale_for_target": obj.indicator.rationale_for_target,
}


class DataCollectionFrequencySerializer(serializers.ModelSerializer):
Expand All @@ -89,4 +92,4 @@ class Meta:
class DocumentationSerializer(serializers.ModelSerializer):
class Meta:
model = Documentation
fields = '__all__'
fields = '__all__'
5 changes: 4 additions & 1 deletion indicators/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
program_indicator_report, indicator_data_report, IndicatorExport, service_json,
collected_data_json, program_indicators_json, IndicatorReportData, IndicatorDataExport, ObjectiveView, objectives_list, objectives_tree,
LevelView, DisaggregationTypeDeleteView, DisaggregationLabelDeleteView,
IndicatorTarget, IndicatorTypeView, DataCollectionFrequencyView, PeriodicTargetCreateView)
IndicatorTarget, IndicatorTypeView, DataCollectionFrequencyView, PeriodicTargetCreateView, IndicatorDataView)

urlpatterns = [

Expand Down Expand Up @@ -155,4 +155,7 @@
PeriodicTargetCreateView.as_view(),
name='periodic_target_view'
),

# Indicator data view
path('indicator_data', IndicatorDataView.as_view(), name='indicator_data_view'),
]
21 changes: 20 additions & 1 deletion indicators/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

from workflow.models import (
Program, SiteProfile, Country, Sector, ActivitySites, FormGuidance,
Documentation
Documentation, Organization
)
from workflow.mixins import AjaxableResponseMixin
from workflow.admin import CountryResource
Expand Down Expand Up @@ -2242,6 +2242,8 @@ def post(self, request, *args, **kwargs):
indicator = Indicator.objects.filter(id=all_data['indicator_id'])
indicator.update(**indicator_data)
return Response({'data': PeriodicTargetSerializer(self.get_queryset(), many=True).data}, status=status.HTTP_201_CREATED)

print(serialized.errors)
return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)

def patch(self, request, *args, **kwargs):
Expand Down Expand Up @@ -2285,3 +2287,20 @@ def delete(self, request, *args, **kwargs):
indicator.update(**indicator_data)
return Response({'data': PeriodicTargetSerializer(self.get_queryset(), many=True).data},
status=status.HTTP_200_OK)


class IndicatorDataView(GView):
"""
View to fetch indicator data
"""
def get(self, request):
try:
organization = Organization.objects.get(id=request.user.activity_user.organization.id)
indicators = Indicator.objects.filter(program__organization=organization)

return JsonResponse({
'level_1_label': organization.level_1_label,
'indicators': IndicatorSerializer(indicators, many=True).data
})
except Exception as e:
return JsonResponse(dict(error=str(e)))
1 change: 1 addition & 0 deletions static/vue.js/collected_data_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ $(document).ready(() => {
this.documentation = ''
this.disaggregations = {}
this.show_disaggregations = false
console.log(this.collectedData)

if(this.showModal) {
let self = this;
Expand Down
130 changes: 109 additions & 21 deletions static/vue.js/indicators/target_period.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,45 @@ new Vue({
showTable: false,
disabledClass: false,
targets: [],
level_1_label: '',
sum: 0,
target_value: {},
target_period_data: [],
frequencies: [{"id": "3", "text":"Annual"},
{"id": "4", "text":"Semi-annual"},
{"id": "5", "text":"Tri-annual"},
{"id": "6", "text":"Quarterly"},
{"id": "7", "text":"Monthly"}],
frequencies: null,
indicatorList: []
},
beforeMount: function() {
this.makeRequest('GET', '/indicators/periodic_target/')
.then(response => {
response.data.forEach(target => {
this.target_period_data.push(target)
})
})
})
.catch(e => {
toastr.error('There was a problem loading targets from the database');
});
});

this.makeRequest('GET', '/indicators/indicator_data')
.then(response => {
console.log(response)
if(response.data){
this.level_1_label = response.data.level_1_label
this.frequencies = [{"id": "1", "text":`Life of ${this.level_1_label}`},
{"id": "3", "text":"Every Year"},
{"id": "4", "text":"Every Six Months"},
{"id": "5", "text":"Every Four Months"},
{"id": "6", "text":"Every Three Months"},
{"id": "7", "text":"Every Month"},
{"id": "8", "text":"Every Week"},
{"id": "9", "text":"Every Two Weeks"}]

this.indicatorList = response.data.indicators

}
})
.catch(e => {
toastr.error('There was a problem loading Indicators from the database');
});
},
methods: {
makeRequest(method, url, data = null) {
Expand Down Expand Up @@ -85,7 +105,12 @@ new Vue({
case "7":
this.generateMonthlyTargets(periodStarts);
break;

case "8":
this.generateWeeklyTargets(periodStarts);
break;
case "9":
this.generateBiWeeklyTargets(periodStarts);
break;
default:
break;
}
Expand All @@ -112,6 +137,68 @@ new Vue({

showFields: function(){
this.show = true

if (this.target_frequency == "1"){
this.generateLOPTargets();
}
},

generateLOPTargets: function(){
this.showTable = true
this.disabledClass = true
const period = `Life of ${this.level_1_label}`
const pk = 1

this.indicatorList.forEach(indicator => {
if (indicator.id == this.indicator_id){
const program = indicator.program[0]
const start_date = moment(program.start_date).format("MMM DD, YYYY");
const end_date = moment(program.end_date).format("MMM DD, YYYY");
this.target_frequency_start = moment(program.start_date).format("YYYY-MM");
this.targets = [...this.targets, { pk, start_date, end_date, period}];
}
})

},

generateWeeklyTargets: function(periodStart){
for (let i = 1; i <= this.number_of_target_periods; i++) {
const period = `Week ${i}`
const pk = `${i}`
let periodEnds = moment(periodStart)
let start_date = moment(periodStart)
.startOf("day")
.format("MMM DD, YYYY");
let end_date = moment(periodEnds)
.add(7, "days")
.endOf("day")
.format("MMM DD, YYYY");

this.targets = [...this.targets, { pk, start_date, end_date, period}];

periodStart = moment(periodEnds).add(8, "days");
}

},

generateBiWeeklyTargets: function(periodStart){
for (let i = 1; i <= this.number_of_target_periods; i++) {
const period = `Weeks ${i}`
const pk = `${i}`
let periodEnds = moment(periodStart)
let start_date = moment(periodStart)
.startOf("day")
.format("MMM DD, YYYY");
let end_date = moment(periodEnds)
.add(14 ,"days")
.endOf("day")
.format("MMM DD, YYYY");

this.targets = [...this.targets, { pk, start_date, end_date, period}];

periodStart = moment(periodEnds).add(15, "days");
}

},

generateYearlyTargets: function(periodStart){
Expand All @@ -127,10 +214,7 @@ new Vue({
.endOf("month")
.format("MMM DD, YYYY");

console.log(pk)
console.log(this.targets)
this.targets = [...this.targets, { pk, start_date, end_date, period}];
console.log(this.targets)

periodStart = moment(periodEnds).add(12, "months");
}
Expand Down Expand Up @@ -235,12 +319,14 @@ new Vue({
this.modalHeader = "Add Target Periods"

this.target_period_data.forEach(target =>{
if(target.indicator.indicator_id == this.indicator_id){
console.log(target)
if(target.indicator.id == this.indicator_id){
console.log("gere")
this.isEdit = true
this.overall_target = target.indicator.indicator_lop
this.sum = target.indicator.indicator_lop
this.overall_target = target.indicator.lop_target
this.sum = target.indicator.lop_target
this.baseline= target.indicator.baseline
this.rationale= target.indicator.rationale
this.rationale= target.indicator.rationale_for_target
const pk = target.id
const start_date = target.start_date
const end_date = target.end_date
Expand Down Expand Up @@ -271,6 +357,12 @@ new Vue({
this.target_frequency = "7"
}else if(this.targets[0].period.includes('Quarter')){
this.target_frequency = "6"
}else if(this.targets[0].period.includes('Weeks')){
this.target_frequency = "9"
}else if(this.targets[0].period.includes('Week')){
this.target_frequency = "8"
}else if(this.targets[0].period.includes('life')){
this.target_frequency = "1"
}


Expand Down Expand Up @@ -308,10 +400,7 @@ new Vue({
for (var key in this.target_value) {
if (this.target_value.hasOwnProperty(key)) {
this.targets.forEach((target =>{
console.log(typeof(key))
console.log(target)
if (parseInt(key) === parseInt(target.pk)){
console.log("sound")
target["target"] = this.target_value[key]
}
}))
Expand All @@ -321,6 +410,7 @@ new Vue({

const id = this.indicator_id
this.targets = this.targets.map(function (obj) {
console.log(obj)
obj['indicator_id'] = obj['id'];
obj['start_date'] = moment(obj['start_date']).format("YYYY-MM-DD")
obj['end_date'] = moment(obj['end_date']).format("YYYY-MM-DD")
Expand All @@ -336,6 +426,7 @@ new Vue({
periodic_targets: this.targets
}

console.log(data)
try {
const response = await this.makeRequest(
'POST',
Expand All @@ -362,10 +453,7 @@ new Vue({
for (var key in this.target_value) {
if (this.target_value.hasOwnProperty(key)) {
this.targets.forEach((target =>{
console.log(typeof(key))
console.log(target)
if (parseInt(key) === parseInt(target.pk)){
console.log("sound")
target["target"] = this.target_value[key]
}
}))
Expand Down
Loading

0 comments on commit b1f9c5b

Please sign in to comment.