def serialize(self, user_urn): """Return object data in easily serializeable format""" message = { 'urn_to': [], 'urn_from': '', 'msg_id': self.msg_id, 'subject': self.subject, 'body': self.body, 'thread_id': self.thread_id, 'sent_date': self.sent_date, 'read_date': self.read_date, 'collection_case': self.collection_case, 'reporting_unit': self.reporting_unit, 'survey': self.survey, '_links': '', 'labels': [] } if User(user_urn).is_respondent: actor = user_urn else: actor = self.survey for row in self.statuses: if row.actor == actor: message['labels'].append(row.label) if row.label == 'INBOX': message['urn_to'].append(row.actor) elif row.label == 'SENT': message['urn_from'] = row.actor return message
def remove_label(label, message, user_urn): """delete a label from status table""" actor = user_urn if User(user_urn).is_respondent else message['survey'] try: query = "DELETE FROM status WHERE label = '{0}' and msg_id = '{1}' and actor = '{2}'". \ format(label, message['msg_id'], actor) db.get_engine(app=db.get_app()).execute(query) return True except Exception as e: logger.error(e) raise (InternalServerError( description="Error retrieving messages from database"))
def add_label(label, message, user_urn): """add a label to status table""" actor = user_urn if User(user_urn).is_respondent else message['survey'] try: query = "INSERT INTO status (label, msg_id, actor) VALUES ('{0}','{1}','{2}')". \ format(label, message['msg_id'], actor) db.get_engine(app=db.get_app()).execute(query) return True except Exception as e: logger.error(e) raise (InternalServerError( description="Error retrieving messages from database"))
def message_save(self, message, is_draft, draft_id): """Saves the message to the database along with the subsequent status and audit""" save = Saver() save.save_message(message.data, datetime.now(timezone.utc)) if User(message.data.urn_from).is_respondent: save.save_msg_status(message.data.urn_from, message.data.msg_id, Labels.SENT.value) save.save_msg_status(message.data.survey, message.data.msg_id, Labels.INBOX.value) save.save_msg_status(message.data.survey, message.data.msg_id, Labels.UNREAD.value) else: save.save_msg_status(message.data.survey, message.data.msg_id, Labels.SENT.value) save.save_msg_audit(message.data.msg_id, message.data.urn_from) save.save_msg_status(message.data.urn_to, message.data.msg_id, Labels.INBOX.value) save.save_msg_status(message.data.urn_to, message.data.msg_id, Labels.UNREAD.value) if is_draft is True: self.del_draft_labels(draft_id) return MessageSend._alert_recipients(message.data.msg_id)
def test_is_respondent_true(self): """test uses is_respondent property of User with respondent urn""" self.assertTrue(User('respondent.00000.00000').is_respondent)
def test_is_internal_false(self): """test uses is_internal property of User with respondent urn""" self.assertFalse(User('respondent.00000.00000').is_internal)
def test_is_internal_true(self): """test uses is_internal property of User with internal urn""" self.assertTrue(User('internal.00000.00000').is_internal)
def test_is_respondent_false(self): """test uses is_respondent property of User with internal urn""" self.assertFalse(User('internal.00000.00000').is_respondent)
def _save_draft_status(saver, msg_id, person, survey, label): """Save labels with correct actor for internal and respondent""" actor = survey if User(person).is_internal else person if person is not None and len(person) != 0: saver.save_msg_status(actor, msg_id, label)