def create_email(old_id=1, old_event_id=2, event_id=None, details='test event details', extra_txt='test extra text', replace_all=False, email_type=EVENT, created_at=None, send_starts_at=None, expires=None): if old_event_id: event = dao_get_event_by_old_id(old_event_id) if not event: event = create_event(old_id=old_event_id) create_event_date(event_id=str(event.id), event_datetime='2019-06-21 19:00') event_id = str(event.id) data = { 'event_id': event_id, 'old_id': old_id, 'old_event_id': old_event_id, 'details': details, 'extra_txt': extra_txt, 'replace_all': replace_all, 'email_type': email_type, 'created_at': created_at, 'send_starts_at': send_starts_at, 'expires': expires } email = Email(**data) dao_create_email(email) return email
def import_emails(): data = request.get_json(force=True) validate(data, post_import_emails_schema) errors = [] emails = [] for item in data: err = '' email = Email.query.filter(Email.old_id == item['id']).first() if not email: event_id = None email_type = EVENT if int(item['eventid']) < 0: if item['eventdetails'].startswith('New Acropolis'): email_type = MAGAZINE else: email_type = ANNOUNCEMENT expires = None if email_type == EVENT: event = dao_get_event_by_old_id(item['eventid']) if not event: err = u'event not found: {}'.format(item) current_app.logger.info(err) errors.append(err) continue event_id = str(event.id) expires = event.get_last_event_date() else: # default to 2 weeks expiry after email was created expires = datetime.strptime( item['timestamp'], "%Y-%m-%d %H:%M") + timedelta(weeks=2) email = Email( event_id=event_id, old_id=item['id'], old_event_id=item['eventid'], details=item['eventdetails'], extra_txt=item['extratxt'], replace_all=True if item['replaceAll'] == 'y' else False, email_type=email_type, created_at=item['timestamp'], send_starts_at=item['timestamp'], expires=expires) dao_create_email(email) emails.append(email) else: err = u'email already exists: {}'.format(email.old_id) current_app.logger.info(err) errors.append(err) res = {"emails": [e.serialize() for e in emails]} if errors: res['errors'] = errors return jsonify(res), 201 if emails else 400 if errors else 200
def create_email(): data = request.get_json(force=True) validate(data, post_create_email_schema) email = Email(**data) dao_create_email(email) return jsonify(email.serialize()), 201
def upload_magazine(magazine_id, pdf_data): current_app.logger.info('Upload magazine pdf: {}'.format(magazine_id)) try: magazine = dao_get_magazine_by_id(magazine_id) storage = Storage(current_app.config['STORAGE']) decoded_data = base64.b64decode(pdf_data) storage.upload_blob_from_base64string(magazine.filename, magazine.filename, decoded_data, content_type='application/pdf') try: topics = extract_topics(base64.b64decode(decoded_data)) except Exception as e: topics = [] current_app.logger.error("Error extracting topics: %r", e) dao_update_record(Magazine, magazine_id, topics=topics) email = dao_get_email_by_magazine_id(magazine_id) if not email: email = Email(magazine_id=magazine.id, email_state=READY, email_type=MAGAZINE) dao_create_email(email) emails_to = [user.email for user in dao_get_users()] subject = 'Please review {}'.format(magazine.title) # send email to admin users and ask them to log in in order to approve the email review_part = '<div>Please review this email: {}/emails/{}</div>'.format( current_app.config['FRONTEND_ADMIN_URL'], str(email.id)) magazine_html = get_email_html(MAGAZINE, magazine_id=magazine.id) response = send_smtp_email(emails_to, subject, review_part + magazine_html) if response != 200: current_app.logger.error( 'Error sending review email {}, for {}'.format( email.id, magazine.id)) except Exception as e: current_app.logger.error('Task error uploading magazine: {}'.format( str(e)))