From 8d42cefa7c48dbc857829dcc55f6bb0b176716fa Mon Sep 17 00:00:00 2001 From: Odumosu Dipo Date: Fri, 18 Sep 2020 01:00:59 +0100 Subject: [PATCH] fix: various fixes this pull request attempts to address: - nditech/apollo-issues#89, where the "recent phone" was inaccurate - nditech/apollo-issues#90, where the "db phone" and "recent phone" were to be updated - nditech/apollo-issues#91, where a specific order for participant phone numbers was to be set - nditech/apollo-issues#92, where the "db phone" and "recent phone" for location submission export was to match the observer export --- apollo/formsframework/forms.py | 2 ++ apollo/participants/models.py | 7 ++++++- apollo/participants/services.py | 4 ++-- apollo/submissions/services.py | 8 ++------ 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/apollo/formsframework/forms.py b/apollo/formsframework/forms.py index 33e68cf10..79abc5983 100644 --- a/apollo/formsframework/forms.py +++ b/apollo/formsframework/forms.py @@ -240,6 +240,7 @@ def save(self, commit=True): submission.sender_verified = phone_contact.verified if commit: + phone_contact.touch() phone_contact.save() else: if submission.id is None: @@ -254,6 +255,7 @@ def save(self, commit=True): number=phone_num, participant_id=participant.id) if commit: + phone_contact.touch() phone_contact.save() if commit: diff --git a/apollo/participants/models.py b/apollo/participants/models.py index c295d5e7d..0e7e4514c 100644 --- a/apollo/participants/models.py +++ b/apollo/participants/models.py @@ -329,7 +329,9 @@ def phones(self): if not hasattr(self, '_phones'): phones = PhoneContact.query.filter_by( participant_id=self.id - ).order_by(PhoneContact.updated).all() + ).order_by( + PhoneContact.verified.desc(), + PhoneContact.updated.desc()).all() self._phones = phones return self._phones @@ -391,6 +393,9 @@ class PhoneContact(BaseModel): participant = db.relationship('Participant') + def touch(self): + self.updated = utils.current_timestamp() + class ContactHistory(BaseModel): id = db.Column(db.Integer, nullable=False, primary_key=True) diff --git a/apollo/participants/services.py b/apollo/participants/services.py index 44211fefc..72a71543d 100644 --- a/apollo/participants/services.py +++ b/apollo/participants/services.py @@ -12,7 +12,7 @@ from apollo.participants.models import ( ParticipantSet, Participant, ParticipantGroup, ParticipantGroupType, ParticipantPartner, - ParticipantRole, PhoneContact, Sample, samples_participants) + ParticipantRole, PhoneContact, samples_participants) number_regex = re.compile('[^0-9]') @@ -59,7 +59,7 @@ def export_list(self, query): output_buffer.close() for participant in query: - phones = participant.phone_contacts + phones = participant.phones if phones: phone_numbers = [p.number for p in phones[:3]] phone_numbers += [''] * (3 - len(phone_numbers)) diff --git a/apollo/submissions/services.py b/apollo/submissions/services.py index c1c6f71fa..3256f1b00 100644 --- a/apollo/submissions/services.py +++ b/apollo/submissions/services.py @@ -90,9 +90,7 @@ def export_list(self, query): if submission.participant else '', submission.participant.primary_phone if submission.participant else '', - sorted(submission.participant.phone_contacts, key=lambda p: p.updated, reverse=True)[0].number # noqa - if submission.participant and - submission.participant.phone_contacts else '', + submission.last_phone_number, ] + [ submission.location.make_path().get(loc_type.name, '') for loc_type in location_types @@ -137,9 +135,7 @@ def export_list(self, query): if sib.participant else '', sib.participant.primary_phone if sib.participant else '', - sib.participant.phone_contacts[0].number - if sib.participant and sib.participant.phone_contacts - else '', + sib.last_phone_number, ] + [ sib.location.make_path().get(loc_type.name, '') for loc_type in location_types