def attendee_signup(args): if 'signup_code' in args and len(args['signup_code']) > 0: attendee = Attendee.query.filter_by( email=args['email'], signup_code=args['signup_code']).first() if attendee is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Email not found') attendee.update(**args) attendee.signup_code = '' db.session.commit() return jsonify({ 'message': 'Attendee created successfully', 'data': attendee.serialize(), 'token': jwttoken.encode(attendee.id, 'Attendee') }), 201 else: attendee = Attendee.query.filter_by(email=args['email']).first() if attendee is not None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Duplicated email') attendee = Attendee(firstname=args['firstname'], lastname=args['lastname'], email=args['email'], phone=args['phone']) attendee.set_password(password=args['password']) db.session.add(attendee) db.session.commit() return jsonify({ 'message': 'Attendee created successfully', 'data': attendee.serialize(), 'token': jwttoken.encode(attendee.id, 'Attendee') }), 201
def event_get_info(user, user_type, event_id): event = Event.query.filter_by(id=event_id).first() if event is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Event not found') # owner = Organizer.query.filter_by(id=event.owner_id).first() # location = Location.query.filter_by(id=event.location_id).first() if event.type == 'private' and user_type == 'Attendee': for re in event.reservations: if re.attendee_id == user.id: return jsonify({ 'result': { 'detail': event.serialize(), 'nummber_of_attendees': len(event.reservations), 'contact': event.owner.email, 'location_name': event.location.name_location, 'location_address': event.location.address } }), 200 raise Error(status_code=StatusCode.FORBIDDEN, error_message='Permission denied') else: return jsonify({ 'result': { 'detail': event.serialize(), 'nummber_of_attendees': len(event.reservations), 'contact': event.owner.email, 'location_name': event.location.name_location, 'location_address': event.location.address } }), 200
def event_delete(user, user_type, event_id): if user_type != 'Organizer': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') event = Event.query.filter_by(id=event_id, owner_id=user.id).first() if event is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Location not found') db.session.delete(event) db.session.commit() return jsonify({'message': 'Event deleted successfully'}), 201
def event_update(user, user_type, event_id, args): if user_type != 'Organizer': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') event = Event.query.filter_by(id=event_id, owner_id=user.id).first() if event is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Event not found') event.update(**args) db.session.commit() return jsonify({ 'message': 'Location updated successfully', 'data': event.serialize() }), 201
def attendee_login(args): attendee = Attendee.query.filter_by(email=args['email']).first() if attendee and attendee.check_password(args['password']): return jsonify({'token': jwttoken.encode(attendee.id, 'Attendee')}), 200 raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid email or password.')
def organizer_login(args): organizer = Organizer.query.filter_by(email=args['email']).first() if organizer and organizer.check_password(args['password']): return jsonify({'token': jwttoken.encode(organizer.id, 'Organizer')}), 200 raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid email or password.')
def reservation_delete(user, user_type, event_id): if user_type != 'Attendee': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') event = Event.query.filter_by(id=event_id).first() if event is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Event not found') if user_type == 'Attendee' and event.type == 'private': found = False for re in event.reservations: if re.attendee_id == user.id: found = True if found is False: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Permission denied') reservation = Reservation.query.filter_by(event_id=event.id, attendee_id=user.id).first() db.session.delete(reservation) db.session.commit() return jsonify({ 'message': 'Reservation deleted successfully' }), 201
def organizer_update_info(user, user_type, args): if user_type != 'Organizer': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') user.update(**args) if 'password' in args and len(args['password']) > 0: user.set_password(args['password']) db.session.commit() return jsonify({ 'message': 'Organizer updated successfully', 'data': user.serialize() }), 201
def event_create(user, user_type, args): if user_type != 'Organizer': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') event = Event.query.filter_by(title=args['title']).first() if event is not None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Duplicated event') location = Location.query.filter_by(id=args['location_id'], owner_id=user.id).first() if location is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Location not belongs to owner') if args['type'] != 'public' and args['type'] != 'private': raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Invalid type of event') event = Event( title=args['title'], description=args['description'], category=args['category'], # start_date=datetime.datetime.strptime(args['start_date'], '%d-%m-%Y %H:%M'), # end_date=datetime.datetime.strptime(args['end_Date'], '%d-%m-%Y %H:%M'), start_date=args['start_date'], end_date=args['end_date'], location_id=location.id, owner_id=user.id, type=args['type'], capacity=args['capacity']) db.session.add(event) db.session.commit() return jsonify({ 'message': 'Event created successfully', 'data': event.serialize() }), 201
def event_confirm(user, user_type, event_id): if user_type != 'Attendee': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') event = Event.query.filter_by(id=event_id).first() if event is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Event not found') if datetime.datetime.now() > event.end_date: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Expired event') reservations = event.reservations # if len(reservations) == event.capacity: # raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Out of slot') reservation = Reservation.query.filter_by(event_id=event_id, attendee_id=user.id, status='PENDING').first() if reservation is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Reservation not found') existing_slots = Reservation.query.filter_by(event_id=event_id, status='INVITED').all() if len(existing_slots) == event.capacity: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Full slots') reservation.status = 'INVITED' db.session.commit() return jsonify({'message': 'Confirmed'}), 201
def attendee_get_by_event(user, user_type, event_id): event = Event.query.filter_by(id=event_id).first() if event is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Event not found') if user_type == 'Attendee' and event.type == 'private': found = False for re in event.reservations: if re.attendee_id == user.id: found = True if found is False: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Permission denied') result = [] for re in event.reservations: tmp = {} at = re.attendees tmp['user'] = at.serialize() tmp['status'] = re.status tmp['user_id'] = at.id result.append(tmp) return jsonify(result), 200
def event_upload_image(user, user_type, event_id): if user_type != 'Organizer': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') event = Event.query.filter_by(id=event_id, owner_id=user.id).first() if event is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Event not found') if 'image' not in request.files: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Need image part') img = request.files['image'] if img.filename == '': raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Not selected image') if img and allowed_image(img.filename): new_file_name = str(datetime.datetime.now()) + '_' + str(random.randint(0, 9999999)) \ + '.' + img.filename.rsplit('.', 1)[1].lower() img.save(os.path.join('uploads', new_file_name)) event.img = new_file_name db.session.commit() return jsonify({'message': 'Image uploaded'}), 201 else: return jsonify({'message': 'Extension not allowed'})
def organizer_register(args): organizer = Organizer.query.filter_by(email=args['email']).first() if organizer is not None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Duplicated email') organizer = Organizer(firstname=args['firstname'], lastname=args['lastname'], email=args['email'], phone=args['phone']) organizer.set_password(password=args['password']) db.session.add(organizer) db.session.commit() return jsonify({ 'message': 'Organizer created successfully', 'data': organizer.serialize(), 'token': jwttoken.encode(organizer.id, 'Organizer') }), 201
def event_get_by_organizer(user, user_type): if user_type != 'Organizer': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') result = [] for ev in user.events: tmp = { 'detail': ev.serialize(), 'nummber_of_attendees': len(ev.reservations), 'contact': ev.owner.email, 'location_name': ev.location.name_location, 'location_address': ev.location.address } result.append(tmp) return jsonify(result), 200
def event_get_public_by_attendee(user, user_type, attendee_id): if user_type != 'Attendee' or user.id != attendee_id: raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') reserves = user.reservations result = [] for re in reserves: ev = re.events if ev.type == 'public': tmp = { 'detail': ev.serialize(), 'nummber_of_attendees': len(ev.reservations), 'contact': ev.owner.email, 'location_name': ev.location.name_location, 'location_address': ev.location.address } result.append(tmp) return jsonify(result), 200
def location_get_by_owner(owner_id): owner = Organizer.query.filter_by(id=owner_id).first() if owner is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Owner not found') page = None if request.args.get('page') is None else int( request.args.get('page')) result = Location.query.filter_by(owner_id=owner_id).paginate(page=page, per_page=15) has_next = 'YES' if page is not None and page == -(-result.total // 15) + 1: has_next = None elif page is None: has_next = None return jsonify({ 'owner_id': owner_id, 'current_page': page, 'next_page_url': has_next, 'data': [x.serialize() for x in result.items] }), 200
def organizer_get_info(user, user_type): if user_type != 'Organizer': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') return jsonify({'result': user.serialize()}), 200
def event_booking_handle(user, user_type, event_id): event = Event.query.filter_by(id=event_id).first() if event is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Event not found') if datetime.datetime.now() > event.end_date: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Expired event') if event.type == 'public': if user_type != 'Attendee': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') if len(event.reservations) == event.capacity: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Full slot') reservation = Reservation(status='INVITED', event_id=event.id, attendee_id=user.id) db.session.add(reservation) db.session.commit() return jsonify({ 'result': reservation.serialize() }), 201 elif event.type == 'private': result = [] if user_type != 'Organizer': raise Error(status_code=StatusCode.UNAUTHORIZED, error_message='Invalid token') if 'csv_file' not in request.files: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Need csv_file part') csv_file = request.files['csv_file'] new_file_name = 'tmp/' + ''.join(random.choices(string.ascii_uppercase + string.digits, k=32)) + '.csv' csv_file.save(new_file_name) if csv_file.filename == '': raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Not selected csv') if csv_file and allowed_csv(csv_file.filename): # csv_reader = csv.reader(open(new_file_name)) # row_count = sum(1 for row in csv_reader) - 1 # if len(event.reservations) + row_count > event.capacity: # raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Too many invitations') list_inv = open(new_file_name).read().replace('"', '').split('\n')[1:] if len(event.reservations) + len(list_inv) > event.capacity: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Too many invitations') for row in list_inv: user_mail = row existing_user = Attendee.query.filter_by(email=user_mail).first() if existing_user is not None: existing_re = Reservation.query.filter_by(event_id=event.id, attendee_id=user.id).first() if existing_re is None: reservation = Reservation(status='PENDING', event_id=event.id, attendee_id=existing_user.id) db.session.add(reservation) db.session.commit() else: reservation = existing_re else: rand_str = ''.join(random.choices(string.ascii_uppercase + string.digits, k=32)) link = app.config['URL_MAIL'] + '?signup_code=' + rand_str + '&mail=' + user_mail new_user = Attendee(firstname='', lastname='', email=user_mail, phone='', signup_code=rand_str, password_hash='', password_salt='') db.session.add(new_user) db.session.commit() reservation = Reservation(status='PENDING', event_id=event.id, attendee_id=new_user.id) db.session.add(reservation) db.session.commit() message = 'Here is your confirm link: {}'.format(link) send_email(subject='Your confirm link', recipients=[user_mail], text_body=message, html_body=None) result.append(reservation) return jsonify({ 'result': [x.serialize() for x in result] }), 201
def organizer_get_specific_info(organizer_id): organizer = Organizer.query.filter_by(id=organizer_id).first() if organizer is None: raise Error(status_code=StatusCode.BAD_REQUEST, error_message='Organizer not found.') return jsonify(organizer.serialize()), 200