def get_user(): page, per_page, offset = get_page_items() paginate = request.args.get('paginate', 'true').lower() paginate = False if paginate == 'false' else True user_ids = request.args.get('userIds', None) if user_ids: user_ids = [ user_id for user_id in user_ids.split(',') if ObjectId.is_valid(user_id) ] users = UserService.get_users(user_ids=user_ids, paginate=False) else: q = request.args.get('q', '') emails = [ email for email in request.args.get('emails', '').split(',') if AuthService.validate_email(email) ] users = UserService.get_users(query=q, page=page, limit=per_page, emails=emails, paginate=paginate) if users.get('data'): return ResponseDto(status=0, result=users).to_json() return ResponseDto(status=-1, msg='No User Found.').to_json()
def set_user_last_notification_time(): user = request.form.get("user") last_accessed_time = request.form.get("notification-time") if user and last_accessed_time and last_accessed_time.isdigit(): key_for_last_notification_time = '{}{}'.format( KEY_PREFIX_LAST_USER_NOTIFICATION_AT, user) last_accessed_time_in_redis = redis.get(key_for_last_notification_time) if not last_accessed_time_in_redis or long( last_accessed_time_in_redis) < long(last_accessed_time): redis.set(key_for_last_notification_time, last_accessed_time) return ResponseDto(status=0, msg='success').to_json() return ResponseDto(status=-1, msg='args invalid').to_json()
def get_slot(): page, per_page, offset = get_page_items() paginate = request.args.get('paginate', 'true').lower() paginate = False if paginate == 'false' else True slot_ids = request.args.get('slotIds', None) order_by = request.args.get('orderBy', '-created_at') if slot_ids: slot_ids = [ slot_id for slot_id in slot_ids.split(',') if ObjectId.is_valid(slot_id) ] slots = CalendarService.get_slots(slot_ids=slot_ids, paginate=False, order_by=order_by) else: start_ts = request.args.get('startTimestamp', None) end_ts = request.args.get('endTimestamp', None) user = request.args.get('userEmail', None) if user: user = UserService.get_users(emails=[user], data_objects=True, paginate=False).first() if user is None: return ResponseDto( status=-1, msg="User is not present with this email.").to_json() booked_by = request.args.get('bookedBy', None) if booked_by: booked_by = UserService.get_users(emails=[booked_by], data_objects=True, paginate=False).first() if booked_by is None: return ResponseDto( status=-1, msg="Booking user is not present with this email." ).to_json() slots = CalendarService.get_slots(page=page, limit=per_page, user=user, booked_by=booked_by, end_ts=end_ts, order_by=order_by, start_ts=start_ts, paginate=paginate) if slots.get('data'): return ResponseDto(status=0, result=slots).to_json() return ResponseDto(status=-1, msg='No Slot Found.').to_json()
def login(): email = request.form.get('email', '') password = request.form.get('password', '') remember = True if request.form.get('remember') else False if not email.strip() or not password.strip( ) or not AuthService.validate_email(email): return ResponseDto(status=-1, msg='Data Incomplete/Invalid.').to_json() response = AuthService.login(email=email, password=password, remember=remember) if not response['value']: return ResponseDto(status=-1, msg=response['error']).to_json() return ResponseDto(status=0, msg=response['msg'], result=response['data']).to_json()
def delete_slot(): slot_id = request.json.get('slotId', None) if not slot_id: return ResponseDto(status=-1, msg='No ID provided.').to_json() slot_resp = CalendarService.validate_slot_id_and_get_instance(slot_id) if slot_resp["err"] is not None: return ResponseDto(status=-1, msg=slot_resp["err"]).to_json() validated = CalendarService.validate_remove_slot(slot_resp["instance"]) if not validated['value']: return ResponseDto(status=-1, msg=validated['error']).to_json() response = CalendarService.remove_slot(slot_resp["instance"]) if response['status']: return ResponseDto(status=0, msg='Remove successful').to_json() return ResponseDto(status=-1, msg='Remove failed').to_json()
def update_slot(): slot_data = request.json.get('slotData', {}) if not slot_data: return ResponseDto(status=-1, msg="Slot Data missing").to_json() if not slot_data.get("id", None): return ResponseDto(status=-1, msg='No ID provided.').to_json() slot_resp = CalendarService.validate_slot_id_and_get_instance( slot_data.get("id")) if slot_resp["err"] is not None: return ResponseDto(status=-1, msg=slot_resp["err"]).to_json() possible_fields = ["startTimestamp", "endTimestamp", "bookedBy"] if not set(possible_fields).intersection(set(slot_data.keys())): return ResponseDto(status=0, msg="Nothing to update.").to_json() validated = CalendarService.check_data_validations( slot_data, exempted_slot=[slot_resp['instance']]) if not validated['value']: return ResponseDto(status=-1, msg=validated['error']).to_json() response = slot_resp["instance"].update(slot_data) if response['status']: return ResponseDto(status=0, msg='Update operation successful', result={ "data": response['data'] }).to_json() return ResponseDto(status=-1, msg='Update operation failed').to_json()
def register(): email = request.form.get('email', '') name = request.form.get('name', '') password = request.form.get('password', '') confirm = request.form.get('confirmPassword', '') if not email.strip() or not password.strip() or not name.strip() or not confirm.strip() or password != confirm\ or not AuthService.validate_email(email): return ResponseDto( status=-1, msg='Data Incomplete/Invalid or Password Mismatch').to_json() response = AuthService.register_user(email=email, name=name, password=password) if not response['value']: return ResponseDto(status=-1, msg=response['error']).to_json() return ResponseDto(status=0, msg=response['msg'], result=response['data']).to_json()
def get_user_data(): users = request.form.get("users") start_time = request.form.get("start-time") end_time = request.form.get("end-time") if users: users = users.split(',') user_time_preference_list = [] response_dto = ResponseDto(result=user_time_preference_list) today_seven_pm_time = int( datetime.datetime.combine(datetime.date.today(), datetime.time(13, 30)).strftime("%s")) for user in users: user_time_preference_list.append({ 'user_id': user, 'suggested_time': today_seven_pm_time + randint(0, USER_NOTIFICATION_TIME_RANGE) }) return response_dto.to_json() return ResponseDto(status=-1, msg='user list not present').to_json()
def create_slot(): slot_data = request.json.get('slotData') if not slot_data: return ResponseDto(status=-1, msg="Slot Data missing").to_json() necessary_fields = ["userEmail", "startTimestamp", "endTimestamp"] error, msg = False, '' for field in necessary_fields: if field not in slot_data: error = True msg = field + " not provided" if error: return ResponseDto(status=-1, msg=msg).to_json() validated = CalendarService.check_data_validations(slot_data) if not validated['value']: return ResponseDto(status=-1, msg=validated['error']).to_json() response = CalendarService.create_slot(slot_data) if response: return ResponseDto(status=0, msg='Create operation successful', result=response).to_json() return ResponseDto(status=-1, msg='Slot create operation failed').to_json()
def service_unavailable(e): logging.exception(e) return ResponseDto(status=-1, msg="Something went bad.")
def page_not_found(e): logging.exception(e) return ResponseDto(status=-1, msg="Page Not Found").to_json()
def handle_error(error): logging.exception(error) return ResponseDto(status=-1, msg="Unauthorized User").to_json()
def unauthorized(): if not current_user.is_authenticated: return ResponseDto(status=-1, msg="Unauthorized User").to_json() else: return redirect('/')
def health_check(): return ResponseDto(status=0, msg='All good').to_json(), 200
def logout(): AuthService.logout() return ResponseDto(status=0, msg='Logout Successful').to_json()