Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update actual comment with update text #2368

Merged
merged 1 commit into from
Sep 23, 2016
Merged
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
24 changes: 24 additions & 0 deletions akvo/rsr/models/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,29 @@ def recalculate_children(self, save=True):

return new_value

def update_actual_comment(self, save=True):
"""
Set the actual comment to the text of the latest approved update.

:param save; Boolean, save period if True
:return Actual comment of period
"""

approved_updates = self.data.filter(status=IndicatorPeriodData.STATUS_APPROVED_CODE)
update_texts = [
'{}: {}'.format(update.last_modified_at.strftime('%d-%m-%Y'), update.text)
for update in approved_updates.order_by('-created_at')
]
actual_comment = ' | '.join(update_texts)
if len(actual_comment) >= 2000: # max_size
actual_comment = '{} ...'.format(actual_comment[:1995])

self.actual_comment = actual_comment
if save:
self.save()

return self.actual_comment

def is_calculated(self):
"""
When a period has got indicator updates, we consider the actual value to be a
Expand Down Expand Up @@ -778,6 +801,7 @@ def save(self, recalculate=True, *args, **kwargs):
# In case the status is approved, recalculate the period
if recalculate and self.status == self.STATUS_APPROVED_CODE:
self.period.recalculate_period()
self.period.update_actual_comment()

def delete(self, *args, **kwargs):
old_status = self.status
Expand Down