def set_questions(self, texts): """Sets and saves the questions using a sequence of texts The function updates the questions relationship so the text of its questions match the sequence of texts provided and saves the result to the database. It removes any question whose text is not in texts. It does nothing to questions whose text is in texts. It adds a new question for each text in texts that is not in questions. All duplicates questions are removed. Args: questions: a sequence of strings representing the text of each question """ old_texts = set([q.text for q in self.questions]) texts = set(texts) for text in texts - old_texts: q = Question(text, self) db_session.add(q) for text in old_texts - texts: for q in self.questions: if q.text == text: db_session.delete(q) break db_session.commit()
def main(config_filename): app = create_app(config_filename) mailbox = DailyLogResponseMailbox(app.config['MAIL_SERVER'], app.config['MAIL_USER'], app.config['MAIL_PASSWORD']) uids = mailbox.uids()[:app.config['MAILS_PER_PROCESS']] for uid in uids: email = mailbox.get_email(uid) user = User.query.filter_by(email=email.sender).first() # todo error checking that the user does not exist # todo error checking that the email is not a valid dailylogemail answers = email.get_answers([q.text for q in user.questions]) date = email.get_date() for i in xrange(len(answers)): answer = Answer(answers[i], date, user, user.questions[i]) db_session.add(answer) db_session.commit() # todo get rid of the uids that errored out mailbox.delete(uids)