def subscribe_member(): data = request.get_json(force=True) current_app.logger.info('Subscribe member: {}'.format(data)) validate(data, post_subscribe_member_schema) member = dao_get_member_by_email(data.get('email')) if member: return jsonify( {'error': 'member already subscribed: {}'.format(member.email)}), 400 member = Member(name=data['name'], email=data['email'], marketing_id=data['marketing_id'], active=True) dao_create_member(member) send_ga_event(f"Subscribed {member.id}", "members", "subscribe", f"{member.id}") basic_html = get_email_html( email_type=BASIC, title='Subscription', message= "Thank you{} for subscribing to New Acropolis events and magazines". format(' {}'.format(data.get('name', '')) if 'name' in data else ''), member_id=member.id) response = send_email(data['email'], 'New Acropolis subscription', basic_html) return jsonify(member.serialize())
def send_emails(email_id): members_not_sent_to = dao_get_members_not_sent_to(email_id) if current_app.config.get('EMAIL_RESTRICT'): limit = 1 elif current_app.config.get('ENVIRONMENT') == 'live': email_provider = get_email_provider() limit = email_provider.limit else: limit = current_app.config.get('EMAIL_LIMIT') current_app.logger.info( 'Task send_emails received %s, sending %d emails', str(email_id), len(members_not_sent_to) if len(members_not_sent_to) < limit else limit) email = dao_get_email_by_id(email_id) try: for index, (member_id, email_to) in enumerate(members_not_sent_to): if limit and index > limit - 1 or email.email_state != APPROVED: current_app.logger.info("Email stopped - {}".format( "not approved" if email.email_state != APPROVED else f"limit reached: {limit}")) break subject = email.get_subject() message = None if email.email_type == EVENT: message = get_email_html(email.email_type, event_id=email.event_id, details=email.details, extra_txt=email.extra_txt, member_id=member_id) elif email.email_type == MAGAZINE: message = get_email_html(MAGAZINE, magazine_id=email.magazine_id, member_id=member_id) email_status_code, email_provider_id = send_email( email_to, subject, message) dao_add_member_sent_to_email(email_id, member_id, status_code=email_status_code, email_provider_id=email_provider_id) send_ga_event( f"Sent {email.email_type} email, {subject} - {str(email.id)}", "email", "send success" if email_status_code in [200, 201, 202] else "send failed", f"{subject} - {email.id}") except InvalidRequest as e: if e.status_code == 429: current_app.logger.error("Email limit reached: %r", e.message) if "Minute" in e.message: send_periodic_emails.apply_async(countdown=60) raise
def it_doesnt_send_in_test_environment(self, app, mocker): mocker.patch.dict('app.application.config', { 'ENVIRONMENT': 'test' }) mock_post = mocker.patch('app.comms.stats.requests.post') send_ga_event("test", "test", "test", "test") assert not mock_post.called
def it_doesnt_send_stats_if_send_stats_disabled(self, app, mocker): mocker.patch.dict('app.application.config', { 'ENVIRONMENT': 'development', 'DISABLE_STATS': True }) mock_post = mocker.patch('app.comms.stats.requests.post') send_ga_event("test", "test", "test", "test") assert not mock_post.called
def send_email_stats(month=None, year=None): count = dao_get_emails_sent_count(month=month, year=year) send_ga_event("Emails sent count", "email", "send_success", "Email Stats", value=count) current_app.logger.info(f"Email count for {month}/{year}: {count}") return f'email count for {month}/{year} = {count}'
def download_pdf(magazine_id, category="magazine_download"): magazine = dao_get_magazine_by_id(magazine_id) send_ga_event("magazine download", category, "download", magazine.title) pdf_filename = 'pdfs/{}'.format(magazine.filename) storage = Storage(current_app.config['STORAGE']) pdf = BytesIO(storage.get_blob(pdf_filename)) return send_file(pdf, as_attachment=True, attachment_filename=magazine.filename, mimetype='application/pdf')
def send_num_subscribers_and_social_stats(inc_subscribers=True): current_app.logger.info( 'Task send_num_subscribers_and_social_stats received: {}') report_month = (datetime.today() - timedelta(weeks=2)).strftime('%B').lower() num_subscribers = num_new_subscribers = 0 if inc_subscribers: month_year = (datetime.today() - timedelta(weeks=2)).strftime('%m-%Y') num_subscribers = dao_get_active_member_count() send_ga_event("Number of subscribers", "members", f"num_subscribers_{report_month}", num_subscribers) num_new_subscribers = dao_get_active_member_count( month=month_year.split('-')[0], year=month_year.split('-')[1]) send_ga_event("Number of new subscribers", "members", f"num_new_subscribers_{report_month}", num_new_subscribers) facebook_count = get_facebook_count() if facebook_count: send_ga_event("Facebook followers count", "social", f"num_facebook_{report_month}", facebook_count) instagram_count = get_instagram_count() if instagram_count: send_ga_event("Instagram followers count", "social", f"num_instagram_{report_month}", instagram_count) return num_subscribers, num_new_subscribers, facebook_count, instagram_count
def unsubscribe_member(unsubcode): member = _get_member_from_unsubcode(unsubcode) dao_update_member(member.id, active=False) send_ga_event(f"Unsubscribed {member.id}", "members", "unsubscribe", f"{member.id}") basic_html = get_email_html( email_type=BASIC, title='Unsubscribe', message= "{}, you have successfully unsubscribed from New Acropolis events and magazines" .format(member.name)) send_email(member.email, 'New Acropolis unsubscription', basic_html) return jsonify({'message': '{} unsubscribed'.format(member.name)})