Skip to content

Commit

Permalink
Merge branch '#1965-results-myrsr' into #1897-refactor-results
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Feb 22, 2016
2 parents 697772e + e478f11 commit d433bfa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
2 changes: 2 additions & 0 deletions akvo/rest/serializers/indicator_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class IndicatorPeriodSerializer(BaseRSRSerializer):

parent_period = serializers.Field(source='parent_period.pk')
percent_accomplishment = serializers.Field(source='percent_accomplishment')

class Meta:
model = IndicatorPeriod
Expand All @@ -23,6 +24,7 @@ class IndicatorPeriodFrameworkSerializer(BaseRSRSerializer):

data = IndicatorPeriodDataFrameworkSerializer(many=True, required=False, allow_add_remove=True)
parent_period = serializers.Field(source='parent_period.pk')
percent_accomplishment = serializers.Field(source='percent_accomplishment')

class Meta:
model = IndicatorPeriod
47 changes: 20 additions & 27 deletions akvo/rsr/models/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,38 +402,31 @@ def update_actual_value(self, data, relative_data, comment=''):
self.actual_comment = comment
self.save(update_fields=['actual_comment'])

# Update parent period
# Update parent period (if not percentages)
parent = self.parent_period()
if parent:
if parent and self.indicator.measure != '2':
parent.update_actual_value(str(Decimal(self.actual_value) - old_actual), True)
except (InvalidOperation, TypeError):
pass

# OLD CODE: might be re-used later, if we're clear on how to calculate percentages
#
# @property
# def percent_accomplishment(self):
# """
# Return the percentage completed for this indicator period. If not possible to convert the
# values to numbers, return None.
# """
# if isinstance(self.target, Decimal) and isinstance(self.actual, Decimal) and \
# isinstance(self.baseline, Decimal):
# try:
# return round((self.actual - self.baseline) / (self.target - self.baseline) * 100, 1)
# except DivisionByZero:
# return round(self.actual / self.target * 100, 1) if self.target > 0 else None
# except (InvalidOperation, TypeError):
# return None
# return None
#
# @property
# def percent_accomplishment_100(self):
# """
# Similar to the percent_accomplishment property. However, it won't return any number bigger
# than 100.
# """
# return max(self.percent_accomplishment, 100) if self.percent_accomplishment else None
@property
def percent_accomplishment(self):
"""
Return the percentage completed for this indicator period. If not possible to convert the
values to numbers, return None.
"""
try:
return round(Decimal(self.actual_value) / Decimal(self.target_value) * 100, 1)
except (InvalidOperation, TypeError, DivisionByZero):
return None

@property
def percent_accomplishment_100(self):
"""
Similar to the percent_accomplishment property. However, it won't return any number bigger
than 100.
"""
return max(self.percent_accomplishment, 100) if self.percent_accomplishment else None

@property
def actual(self):
Expand Down
24 changes: 9 additions & 15 deletions akvo/rsr/static/scripts-src/my-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,21 +1024,15 @@ var IndicatorPeriodEntry = React.createClass({displayName: 'IndicatorPeriodEntry
},

renderPercentageComplete: function() {
// OLD CODE: might be re-used later, if we're clear on how to calculate percentages
//
//if (this.props.period.percent_accomplishment !== null) {
// return (
// <span className="percentage-complete"> ({this.props.period.percent_accomplishment}%)</span>
// );
//} else {
// return (
// <span />
// );
//}

return (
React.DOM.span(null )
);
if (this.props.period.percent_accomplishment !== null && this.props.selectedIndicator.measure !== '2') {
return (
React.DOM.span( {className:"percentage-complete"}, " (",this.props.period.percent_accomplishment,"%)")
);
} else {
return (
React.DOM.span(null )
);
}
},

render: function() {
Expand Down
34 changes: 18 additions & 16 deletions akvo/rsr/static/scripts-src/my-results.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1023,22 +1023,24 @@ var IndicatorPeriodEntry = React.createClass({
}
},

renderPercentageComplete: function() {
// OLD CODE: might be re-used later, if we're clear on how to calculate percentages
//
//if (this.props.period.percent_accomplishment !== null) {
// return (
// <span className="percentage-complete"> ({this.props.period.percent_accomplishment}%)</span>
// );
//} else {
// return (
// <span />
// );
//}
renderActualValue: function() {
var actualValue = this.props.period.actual_value;
if (this.props.selectedIndicator.measure === '2') {
actualValue += '%';
}
return actualValue;
},

return (
<span />
);
renderPercentageComplete: function() {
if (this.props.period.percent_accomplishment !== null && this.props.selectedIndicator.measure !== '2') {
return (
<span className="percentage-complete"> ({this.props.period.percent_accomplishment}%)</span>
);
} else {
return (
<span />
);
}
},

render: function() {
Expand All @@ -1047,7 +1049,7 @@ var IndicatorPeriodEntry = React.createClass({
{this.renderPeriodDisplay()}
<td className="target-td">{this.props.period.target_value}</td>
<td className="actual-td">
{this.props.period.actual_value}
{this.renderActualValue()}
{this.renderPercentageComplete()}
</td>
{this.renderActions()}
Expand Down

0 comments on commit d433bfa

Please sign in to comment.