class UserRegistration(Resource): parser = reqparse.RequestParser() add_to_parser(parser, 'phone_number', int, required=True) add_to_parser(parser, 'country_code', str, required=True) @classmethod def post(cls): data = cls.parser.parse_args() if UserModel.find_by_phone_number(data['phone_number']) is not None: raise ExistingUserError('User already exists.') user = UserModel(**data) authy_service = AuthyService() if authy_service.request_phone_verification_code(user): user.save_to_db() return { **user.tokens, 'user_id': user.id, 'verified': user.verified }, 201 raise VerificationCodeNotSentError( 'There was an issue requesting the verification code. Try again.')
class Message(Resource): parser = reqparse.RequestParser() add_to_parser(parser, 'text', str) add_to_parser(parser, 'send_time', str) add_to_parser(parser, 'frequency', str) add_to_parser(parser, 'active', inputs.boolean) @classmethod def post(cls, user_id): """Create a daily message""" data = cls.parser.parse_args() message = MessageModel(user_id, **data) message.save_to_db() user = UserModel.find_by_user_id(user_id) add_job(message, user.phone_number) twilio = TwilioService() twilio.send_add_message_success_sms(user.formatted_phone_number, data) return {'success': 'ok', 'message': message.json}, 201 @classmethod def put(cls, user_id, message_id): """ Update message fields :param user_id - Int :param message_id - Int :return request status - Dict """ data = cls.parser.parse_args() valid_fields = {k: v for k, v in data.items() if v != None} message = MessageModel.find_by_message_id(message_id) for key in valid_fields.keys(): setattr(message, key, valid_fields[key]) message.save_to_db() return {'success': 'ok'}, 202 def delete(self, user_id, message_id): """Delete specific message""" message = MessageModel.find_by_message_id(message_id) message.delete_from_db() scheduler.remove_job(str(message_id)) return {'success': 'ok'}, 202
class UserVerify(Resource): parser = reqparse.RequestParser() add_to_parser(parser, 'verification_code', str, required=True) @classmethod def post(cls, user_id): data = cls.parser.parse_args() user = UserModel.find_by_user_id(user_id) if user is None: raise UserNotFoundError('Invalid user ID.') authy_service = AuthyService() confirmation = authy_service.confirm_phone_number( user, data['verification_code']) if not confirmation['success']: raise InvalidVerificationCodeError(confirmation['message']) user.verified = True user.save_to_db() twilio_service = TwilioService() twilio_service.send_registration_success_sms( user.formatted_phone_number) return {'success': 'ok', 'verified': user.verified}
class UserLogin(Resource): parser = reqparse.RequestParser() add_to_parser(parser, 'phone_number', int, required=True) @classmethod def post(cls): data = cls.parser.parse_args() user = UserModel.find_by_phone_number(data['phone_number']) if not user: raise InvalidLoginError( f"User with phone number {data['phone_number']} doesn't exist." ) return {**user.tokens, 'user_id': user.id, 'verified': user.verified}