def upload(self, **kw): del kw['sprox_id'] # required by sprox kw['authors'] = [DBSession.query(User).get(id) for id in kw['authors']] pres = Presentation() kw['files'] = [] for f in ('file', 'file_2', 'file_3'): if kw[f] is not None: kw['files'].append( PresentationFile( presentation_id=pres.id, file=kw[f], description=kw['{}_description'.format(f)])) DBSession.add(kw['files'][-1]) del kw[f] del kw['{}_description'.format(f)] for k, v in kw.items(): setattr(pres, k, v) DBSession.add(pres) DBSession.flush() flash('Your presentation was successfully uploaded') redirect('/presentations')
def pmsync(): session = requests.Session() pmurl = tg.config.get("mailman.pipermail.url") r = session.get(pmurl) r.raise_for_status() for m in archivefiles_p.finditer(r.text): name = m.group(1) log.info("Syncing archive {}...".format(name)) ar = session.get(pmurl + "/" + name) lines = ar.text.splitlines() messages = [] msglines = [] for line, nextline in zip(lines, lines[1:] + ['']): m1 = fromline_p.match(line) m2 = fromline2_p.match(line) if line.startswith("From ") and nextline.startswith( "From: ") and msglines: messages.append(parse_message('\n'.join(msglines))) msglines = [] elif m1: msglines.append('From: "{}" <{}@{}>'.format( m1.group(3), m1.group(1), m1.group(2))) elif m2: msglines.append('From: {}@{}'.format(m2.group(1), m2.group(2))) else: msglines.append(line) messages.append(parse_message('\n'.join(msglines))) for message in messages: message_id = message.get('message-id') date = email.utils.parsedate_to_datetime( message.get('date', '1 Jan 1970 00:00:00 -0000')) if (DBSession.query(MailMessage).filter( MailMessage.message_id == message_id).count()): # this is already in our databases transaction.commit() return m = subject_p.match(message.get('subject', '')) subject = m.group(1) body = get_plaintext_body(message) attachments = [] while True: nbody, _, attstring = body.rpartition(nextpart) attachment = parse_attachment(attstring) if attachment is None: break if attachment is not False: attachments.append(attachment) body = nbody mm = MailMessage(message_id=message_id, from_=message.get('from'), date=date, subject=subject, body=body, parent_message_id=message.get('in-reply-to')) DBSession.add(mm) for f in reversed(attachments): DBSession.add(MailAttachment(message=mm, file=f)) DBSession.flush() transaction.commit()