def create_onsite_attendees_for_order(data): """ Creates on site ticket holders for an order and adds it into the request data. :param data: data initially passed in the POST request for order. :return: """ on_site_tickets = data.get('on_site_tickets') if not on_site_tickets: raise UnprocessableEntityError( {'pointer': 'data/attributes/on_site_tickets'}, 'on_site_tickets info missing') data['ticket_holders'] = [] for on_site_ticket in on_site_tickets: ticket_id = on_site_ticket['id'] quantity = int(on_site_ticket['quantity']) ticket = safe_query_without_soft_deleted_entries( Ticket, 'id', ticket_id, 'ticket_id') ticket_sold_count = get_count( db.session.query(TicketHolder.id).filter_by(ticket_id=int( ticket.id), deleted_at=None)) # Check if the ticket is already sold out or not. if ticket_sold_count + quantity > ticket.quantity: # delete the already created attendees. for holder in data['ticket_holders']: ticket_holder = (db.session.query(TicketHolder).filter( id == int(holder)).one()) db.session.delete(ticket_holder) try: db.session.commit() except Exception: logging.exception('DB Exception!') db.session.rollback() raise ConflictError( {'pointer': '/data/attributes/on_site_tickets'}, "Ticket with id: {} already sold out. You can buy at most {} tickets" .format(ticket_id, ticket.quantity - ticket_sold_count), ) for _ in range(1, quantity): ticket_holder = TicketHolder( firstname='onsite', lastname='attendee', email='*****@*****.**', ticket_id=ticket.id, event_id=data.get('event'), ) save_to_db(ticket_holder) data['ticket_holders'].append(ticket_holder.id) # delete from the data. del data['on_site_tickets']
def create_order(): data, errors = OrderAmountInputSchema().load(request.get_json()) if errors: return make_response(jsonify(errors), 422) tickets_dict = data['tickets'] order_amount = calculate_order_amount(tickets_dict, data.get('discount_code')) ticket_ids = {ticket['id'] for ticket in tickets_dict} ticket_map = {int(ticket['id']): ticket for ticket in tickets_dict} tickets = ( Ticket.query.filter_by(deleted_at=None).filter(Ticket.id.in_(ticket_ids)).all() ) if not tickets: raise UnprocessableEntityError( {'source': 'tickets'}, "Tickets missing in Order request", ) event = tickets[0].event try: attendees = [] for ticket in tickets: for _ in range(ticket_map[ticket.id]['quantity']): ticket.raise_if_unavailable() attendees.append( TicketHolder(firstname='', lastname='', ticket=ticket, event=event) ) db.session.commit() except Exception as e: db.session.rollback() raise e validate_attendees({attendee.id for attendee in attendees}) if data.get('amount') is not None and ( current_user.is_staff or has_access('is_coorganizer', event_id=event.id) ): # If organizer or admin has overrided the amount of order order_amount['total'] = data['amount'] order = Order( amount=order_amount['total'], event=event, discount_code_id=data.get('discount_code'), ticket_holders=attendees, ) db.session.commit() order.populate_and_save() order_tickets = OrderTicket.query.filter_by(order_id=order.id).all() for order_ticket in order_tickets: ticket_info = ticket_map[order_ticket.ticket.id] order_ticket.price = ticket_info.get('price') save_to_db(order_ticket) return OrderSchema().dump(order)
def create_order(): data, errors = OrderAmountInputSchema().load(request.get_json()) if errors: return make_response(jsonify(errors), 422) tickets_dict = data['tickets'] order_amount = calculate_order_amount(tickets_dict, data.get('discount_code')) ticket_ids = {ticket['id'] for ticket in tickets_dict} ticket_map = {int(ticket['id']): ticket for ticket in tickets_dict} tickets = ( Ticket.query.filter_by(deleted_at=None).filter(Ticket.id.in_(ticket_ids)).all() ) if not tickets: raise UnprocessableEntityError( {'source': 'tickets'}, "Tickets missing in Order request", ) event = tickets[0].event try: attendees = [] for ticket in tickets: for _ in range(ticket_map[ticket.id]['quantity']): ticket.raise_if_unavailable() attendees.append( TicketHolder(firstname='', lastname='', ticket=ticket, event=event) ) db.session.commit() except Exception as e: db.session.rollback() raise e validate_attendees({attendee.id for attendee in attendees}) order = Order( amount=order_amount['total'], event=event, discount_code_id=data.get('discount_code'), ticket_holders=attendees, ) db.session.commit() order.populate_and_save() return OrderSchema().dumps(order)
def prep_order(self): event, ticket, identifier = create_order(self) order = TicketingManager.get_order_by_identifier(identifier) order.user_id = self.super_admin.id order.brand = "Visa" order.completed_at = datetime.now() order.status = "completed" order.last4 = "1234" order.exp_month = "12" order.exp_year = "2050" order.paid_via = "stripe" order.payment_mode = "card" save_to_db(order) holder = TicketHolder(firstname="John", lastname="Doe", order_id=order.id, ticket_id=ticket.id) save_to_db(holder) return order
def initiate_order_payment(form, paid_via=None): identifier = form['identifier'] email = form['email'] order = TicketingManager.get_and_set_expiry(identifier) if order: user = DataGetter.get_or_create_user_by_email(email, form) order.user_id = user.id if not order.user.user_detail.firstname and not order.user.user_detail.lastname: order.user.user_detail.firstname = form['firstname'] order.user.user_detail.lastname = form['lastname'] if order.amount > 0 \ and (not order.paid_via or (order.paid_via and (order.paid_via == 'stripe' or order.paid_via == 'paypal'))): if paid_via: order.paid_via = paid_via country = form['country'] address = form['address'] city = form['city'] state = form['state'] zipcode = form['zipcode'] order.address = address order.city = city order.state = state order.country = country order.zipcode = zipcode if paid_via == 'transfer' or paid_via == 'onsite' or paid_via == 'cheque': order.status = 'placed' else: order.status = 'initialized' else: order.status = 'completed' order.completed_at = datetime.utcnow() if not order.paid_via: order.paid_via = 'free' invoice_id = order.get_invoice_number() order_url = url_for('ticketing.view_order_after_payment', order_identifier=order.identifier, _external=True) # add holders to user holders_firstnames = form.getlist('holders[firstname]') holders_lastnames = form.getlist('holders[lastname]') holders_ticket_ids = form.getlist('holders[ticket_id]') holders_emails = form.getlist('holders[email]') holders_occupations = form.getlist('holders[occupation]') holders_occupation_details = form.getlist('holders[occupation_detail]') holders_expertises = form.getlist('holders[expertise]') holders_genders = form.getlist('holders[gender]') holders_welcome_receptions = form.getlist('holders[welcome_reception]') holders_recruitments = form.getlist('holders[recruitment]') for i, firstname in enumerate(holders_firstnames): data = { 'firstname': firstname, 'lastname': get_or_default_string(holders_lastnames, i) } holder_user = DataGetter.get_or_create_user_by_email( get_or_default_string(holders_emails, i), data ) ticket_holder = TicketHolder(firstname=data['firstname'], lastname=data['lastname'], occupation=get_or_default_string(holders_occupations, i), occupation_detail=get_or_default_string(holders_occupation_details, i), expertise=get_or_default_string(holders_expertises, i), gender=get_or_default_string(holders_genders, i), welcome_reception=get_or_default_string(holders_welcome_receptions, i), recruitment=get_or_default_string(holders_recruitments, i), ticket_id=int(get_or_default_int(holders_ticket_ids, i)), email=holder_user.email, order_id=order.id) if data['firstname'] == '' or data['lastname'] == '' or holder_user.email == '': return abort(400) if order.status == "completed": send_email_for_after_purchase(holder_user.email, invoice_id, order_url, order.event.name, order.event.organizer_name) DataManager.add_attendee_role_to_event(holder_user, order.event_id) db.session.add(ticket_holder) # add attendee role to user if order.status == "completed": send_notif_for_after_purchase(order.user, invoice_id, order_url) trigger_after_purchase_notifications(email, order.event_id, order.event, invoice_id, order_url) send_email_for_after_purchase(email, invoice_id, order_url, order.event.name, order.event.organizer_name) DataManager.add_attendee_role_to_event(user, order.event_id) # save items save_to_db(order) return order else: return False
def initiate_order_payment(form, paid_via=None): identifier = form['identifier'] email = form['email'] order = TicketingManager.get_and_set_expiry(identifier) if order: user = TicketingManager.get_or_create_user_by_email(email, form) order.user_id = user.id if order.amount > 0 \ and (not order.paid_via or (order.paid_via and (order.paid_via == 'stripe' or order.paid_via == 'paypal'))): if paid_via: order.paid_via = paid_via country = form['country'] address = form['address'] city = form['city'] state = form['state'] zipcode = form['zipcode'] order.address = address order.city = city order.state = state order.country = country order.zipcode = zipcode if paid_via == 'transfer' or paid_via == 'onsite' or paid_via == 'cheque': order.status = 'placed' else: order.status = 'initialized' else: order.status = 'completed' invoice_id = order.get_invoice_number() order_url = url_for('ticketing.view_order_after_payment', order_identifier=order.identifier, _external=True) order.completed_at = datetime.utcnow() if not order.paid_via: order.paid_via = 'free' #add holders to user holders_firstnames = form.getlist('holders[firstname]') holders_lastnames = form.getlist('holders[lastname]') holders_ticket_ids = form.getlist('holders[ticket_id]') holders_emails = form.getlist('holders[email]') for i, firstname in enumerate(holders_firstnames): data = { 'firstname': firstname, 'lastname': holders_lastnames[i] } holder_user = TicketingManager.get_or_create_user_by_email( holders_emails[i], data) ticket_holder = TicketHolder(firstname=data['firstname'], lastname=data['lastname'], ticket_id=int( holders_ticket_ids[i]), email=holder_user.email, order_id=order.id) if order.status == "completed": send_email_for_after_purchase(holder_user.email, invoice_id, order_url, order.event.name, order.event.organizer_name) DataManager.add_attendee_role_to_event(holder_user, order.event_id) db.session.add(ticket_holder) # add attendee role to user if order.status == "completed": trigger_after_purchase_notifications(email, order.event_id, order.event, invoice_id, order_url) send_email_for_after_purchase(email, invoice_id, order_url, order.event.name, order.event.organizer_name) DataManager.add_attendee_role_to_event(user, order.event_id) # save items save_to_db(order) return order else: return False
def create_order(): data = request.get_json() tickets, discount_code = calculate_order_amount_wrapper(data) attendee = data['attendee'] for attribute in attendee: attendee[attribute.replace('-', '_')] = attendee.pop(attribute) schema = AttendeeSchema() json_api_attendee = { "data": { "attributes": data['attendee'], "type": "attendee" } } result = schema.load(json_api_attendee) if result.errors: return make_response(jsonify(result.errors), 422) ticket_ids = {int(ticket['id']) for ticket in tickets} quantity = {int(ticket['id']): ticket['quantity'] for ticket in tickets} ticket_list = (db.session.query(Ticket).filter( Ticket.id.in_(ticket_ids)).filter_by(event_id=data['event_id'], deleted_at=None).all()) ticket_ids_found = { ticket_information.id for ticket_information in ticket_list } tickets_not_found = ticket_ids - ticket_ids_found if tickets_not_found: return make_response( jsonify( status='Order Unsuccessful', error='Tickets with id {} were not found in Event {}.'.format( tickets_not_found, data['event_id']), ), 404, ) for ticket_info in ticket_list: if (ticket_info.quantity - get_count( db.session.query(TicketHolder.id).filter_by( ticket_id=int(ticket_info.id), deleted_at=None))) < quantity[ticket_info.id]: return make_response( jsonify(status='Order Unsuccessful', error='Ticket already sold out.'), 409, ) attendee_list = [] for ticket in tickets: for ticket_amount in range(ticket['quantity']): attendee = TicketHolder(**result[0], event_id=int(data['event_id']), ticket_id=int(ticket['id'])) db.session.add(attendee) attendee_list.append(attendee) ticket_pricing = calculate_order_amount(tickets, discount_code) if not has_access('is_coorganizer', event_id=data['event_id']): data['status'] = 'initializing' # create on site attendees # check if order already exists for this attendee. # check for free tickets and verified user order = Order( amount=ticket_pricing['total_amount'], user_id=current_user.id, event_id=int(data['event_id']), status=data['status'], ) db.session.add(order) db.session.commit() db.session.refresh(order) order_tickets = {} for holder in attendee_list: holder.order_id = order.id db.session.add(holder) if not order_tickets.get(holder.ticket_id): order_tickets[holder.ticket_id] = 1 else: order_tickets[holder.ticket_id] += 1 create_pdf_tickets_for_holder(order) for ticket in order_tickets: od = OrderTicket(order_id=order.id, ticket_id=ticket, quantity=order_tickets[ticket]) db.session.add(od) order.quantity = order.tickets_count db.session.add(order) db.session.commit() db.session.refresh(order) order_schema = OrderSchema() return order_schema.dump(order)