def send_after_event_mail(): from app.instance import current_app as app with app.app_context(): events = Event.query.filter_by(state='published', deleted_at=None).all() for event in events: organizers = get_user_event_roles_by_role_name( event.id, 'organizer') speakers = Speaker.query.filter_by(event_id=event.id, deleted_at=None).all() owner = get_user_event_roles_by_role_name(event.id, 'owner').first() current_time = datetime.datetime.now(pytz.timezone(event.timezone)) time_difference = current_time - event.ends_at time_difference_minutes = (time_difference.days * 24 * 60) + \ (time_difference.seconds / 60) frontend_url = get_settings()['frontend_url'] if current_time > event.ends_at and time_difference_minutes < 1440: for speaker in speakers: if not speaker.is_email_overridden: send_email_after_event(speaker.user.email, event.name, frontend_url) send_notif_after_event(speaker.user, event.name) for organizer in organizers: send_email_after_event(organizer.user.email, event.name, frontend_url) send_notif_after_event(organizer.user, event.name) if owner: send_email_after_event(owner.user.email, event.name, frontend_url) send_notif_after_event(owner.user, event.name)
def send_event_fee_notification_followup(): from app.instance import current_app as app with app.app_context(): incomplete_invoices = EventInvoice.query.filter( EventInvoice.status != 'paid' ).all() for incomplete_invoice in incomplete_invoices: if incomplete_invoice.amount > 0: prev_month = monthdelta(incomplete_invoice.created_at, 1).strftime( "%b %Y" ) # Displayed as Aug 2016 app_name = get_settings()['app_name'] frontend_url = get_settings()['frontend_url'] link = '{}/event-invoice/{}/review'.format( frontend_url, incomplete_invoice.identifier ) send_followup_email_for_monthly_fee_payment( incomplete_invoice.user.email, incomplete_invoice.event.name, prev_month, incomplete_invoice.amount, app_name, link, ) send_followup_notif_monthly_fee_payment( incomplete_invoice.user, incomplete_invoice.event.name, prev_month, incomplete_invoice.amount, app_name, link, incomplete_invoice.event.id, )
def expire_pending_tickets(): from app.instance import current_app as app with app.app_context(): db.session.query(Order).filter(Order.status == 'pending', (Order.created_at + datetime.timedelta(minutes=30)) <= datetime.datetime.now()).\ update({'status': 'expired'}) db.session.commit()
def initialize_db(credentials): with app.app_context(): populate_data = True inspector = reflection.Inspector.from_engine(db.engine) table_name = 'events' table_names = inspector.get_table_names() print("[LOG] Existing tables:") print("[LOG] " + ','.join(table_names)) if table_name not in table_names: print("[LOG] Table not found. Attempting creation") try: db.engine.execute('create extension if not exists citext') db.create_all() stamp() except Exception: populate_data = False print( "[LOG] Could not create tables. Either database does not exist or tables already created" ) if populate_data: credentials = credentials.split(":") admin_email = os.environ.get('SUPER_ADMIN_EMAIL', credentials[0]) admin_password = os.environ.get('SUPER_ADMIN_PASSWORD', credentials[1]) create_super_admin(admin_email, admin_password) populate() else: print( "[LOG] Tables already exist. Skipping data population & creation." )
def send_after_event_mail(): from app.instance import current_app as app with app.app_context(): events = Event.query.filter_by(state='published', deleted_at=None).all() for event in events: organizers = get_user_event_roles_by_role_name(event.id, 'organizer') speakers = Speaker.query.filter_by(event_id=event.id, deleted_at=None).all() owner = get_user_event_roles_by_role_name(event.id, 'owner').first() current_time = datetime.datetime.now(pytz.timezone(event.timezone)) time_difference = current_time - event.ends_at time_difference_minutes = (time_difference.days * 24 * 60) + ( time_difference.seconds / 60 ) frontend_url = get_settings()['frontend_url'] if current_time > event.ends_at and time_difference_minutes < 1440: unique_emails = set() user_objects = [] for speaker in speakers: if not speaker.is_email_overridden: unique_emails.add(speaker.user.email) user_objects.append(speaker.user) for organizer in organizers: unique_emails.add(organizer.user.email) user_objects.append(organizer.user) if owner: unique_emails.add(owner.user.email) user_objects.append(owner.user) for email in unique_emails: send_email_after_event(email, event.name, frontend_url) # Unique user's dict based on their id. unique_users_dict = make_dict(user_objects, "id") for user in unique_users_dict.values(): send_notif_after_event(user, event.name)
def send_event_fee_notification(): from app.instance import current_app as app with app.app_context(): events = Event.query.filter_by(deleted_at=None, state='published').all() for event in events: latest_invoice = EventInvoice.query.filter_by( event_id=event.id).order_by( EventInvoice.created_at.desc()).first() if latest_invoice: orders = Order.query \ .filter_by(event_id=event.id) \ .filter_by(status='completed') \ .filter(Order.completed_at > latest_invoice.created_at).all() else: orders = Order.query.filter_by(event_id=event.id).filter_by( status='completed').all() fee_total = 0 for order in orders: for ticket in order.tickets: if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0: fee = ticket.price * ( get_fee(event.payment_country, order.event.payment_currency) / 100.0) fee_total += fee if fee_total > 0: owner = get_user_event_roles_by_role_name(event.id, 'owner').first() new_invoice = EventInvoice(amount=fee_total, event_id=event.id, user_id=owner.user.id) if event.discount_code_id and event.discount_code: r = relativedelta(datetime.datetime.utcnow(), event.created_at) if r <= event.discount_code.valid_till: new_invoice.amount = fee_total - \ (fee_total * (event.discount_code.value / 100.0)) new_invoice.discount_code_id = event.discount_code_id save_to_db(new_invoice) prev_month = monthdelta(new_invoice.created_at, 1).strftime( "%b %Y") # Displayed as Aug 2016 app_name = get_settings()['app_name'] frontend_url = get_settings()['frontend_url'] link = '{}/invoices/{}'.format(frontend_url, new_invoice.identifier) send_email_for_monthly_fee_payment(new_invoice.user.email, event.name, prev_month, new_invoice.amount, app_name, link) send_notif_monthly_fee_payment(new_invoice.user, event.name, prev_month, new_invoice.amount, app_name, link, new_invoice.event_id)
def send_event_fee_notification_followup(): from app.instance import current_app as app with app.app_context(): incomplete_invoices = EventInvoice.query.filter( EventInvoice.amount > 0, EventInvoice.status != 'paid' ).all() for incomplete_invoice in incomplete_invoices: incomplete_invoice.send_notification(follow_up=True)
def send_monthly_event_invoice(): from app.instance import current_app as app with app.app_context(): events = Event.query.filter_by(deleted_at=None, state='published').all() for event in events: # calculate net & gross revenues user = event.owner admin_info = get_settings() currency = event.payment_currency try: ticket_fee_object = (db.session.query(TicketFees).filter_by( currency=currency).one()) except NoResultFound: logger.error('Ticket Fee not found for event id {id}'.format( id=event.id)) continue ticket_fee_percentage = ticket_fee_object.service_fee ticket_fee_maximum = ticket_fee_object.maximum_fee orders = Order.query.filter_by(event=event).all() gross_revenue = event.calc_monthly_revenue() invoice_amount = gross_revenue * (ticket_fee_percentage / 100) if invoice_amount > ticket_fee_maximum: invoice_amount = ticket_fee_maximum net_revenue = gross_revenue - invoice_amount payment_details = { 'tickets_sold': event.tickets_sold, 'gross_revenue': gross_revenue, 'net_revenue': net_revenue, 'amount_payable': invoice_amount, } # save invoice as pdf pdf = create_save_pdf( render_template( 'pdf/event_invoice.html', orders=orders, user=user, admin_info=admin_info, currency=currency, event=event, ticket_fee_object=ticket_fee_object, payment_details=payment_details, net_revenue=net_revenue, ), UPLOAD_PATHS['pdf']['event_invoice'], dir_path='/static/uploads/pdf/event_invoices/', identifier=event.identifier, ) # save event_invoice info to DB event_invoice = EventInvoice(amount=invoice_amount, invoice_pdf_url=pdf, event_id=event.id) save_to_db(event_invoice)
def change_session_state_on_event_completion(): from app.instance import current_app as app with app.app_context(): sessions_to_be_changed = Session.query.join(Event).filter(Session.state == 'pending')\ .filter(Event.ends_at < datetime.datetime.now()) for session in sessions_to_be_changed: session.state = 'rejected' save_to_db( session, 'Changed {} session state to rejected'.format(session.title))
def event_invoices_mark_due(): from app.instance import current_app as app with app.app_context(): db.session.query(EventInvoice).filter( EventInvoice.status == 'upcoming', Event.id == EventInvoice.event_id, Event.ends_at >= datetime.datetime.now(), (EventInvoice.created_at + datetime.timedelta(days=30) <= datetime.datetime.now())).update({EventInvoice.status: 'due'}, synchronize_session=False)
def delete_ticket_holders_no_order_id(): from app.instance import current_app as app with app.app_context(): order_expiry_time = get_settings()['order_expiry_time'] TicketHolder.query.filter( TicketHolder.order_id == None, TicketHolder.deleted_at.is_(None), TicketHolder.created_at + datetime.timedelta(minutes=order_expiry_time) < datetime.datetime.utcnow()).delete(synchronize_session=False) db.session.commit()
# gather all data first before dropping anything. # some DBs lock after things have been dropped in # a transaction. metadata = MetaData() tbs = [] all_fks = [] for table_name in inspector.get_table_names(): fks = [] for fk in inspector.get_foreign_keys(table_name): if not fk['name']: continue fks.append(ForeignKeyConstraint((), (), name=fk['name'])) t = Table(table_name, metadata, *fks) tbs.append(t) all_fks.extend(fks) for fkc in all_fks: conn.execute(DropConstraint(fkc)) for table in tbs: conn.execute(DropTable(table)) trans.commit() if __name__ == "__main__": with current_app.app_context(): db_drop_everything(db)
def prepare_db(credentials='[email protected]:fossasia'): with app.app_context(): initialize_db(credentials)