コード例 #1
0
class AdminSignUpView(MethodView):
    def __init__(self):
        super().__init__()
        self.helpers = Helpers()
        self.success = Success()
        self.error = Error()

    @admin_only
    def post(self, user_id):
        """This method handles the registration route for an admin"""

        json_data = request.get_json(force=True)
        email = json_data.get('email')
        password = json_data.get('password')
        # check if user exists
        user = User.query.filter_by(email=json_data.get('email')).first()

        if not user:
            try:
                # check if password exists
                if not json_data.get('password'):
                    return self.error.bad_request('No password provided')
                if len(password) < 8:
                    return self.error.bad_request('Password too short')
                # check if email exists
                if not json_data.get('email'):
                    return self.error.bad_request('No email provided')
                if not self.helpers.email_valid(email):
                    return self.error.bad_request('Invalid email')

                user = User(email=email, password=password, admin=True)
                user.save()
                return self.success.create_resource(
                    'User created successfully')
            except Exception as e:
                return self.error.internal_server_error(
                    'Error occurred {}'.format(e))
        else:
            return self.error.causes_conflict('User already exists')
コード例 #2
0
class LoginView(MethodView):
    """This is a view for handling user login and assigning of tokens"""
    def __init__(self):
        super().__init__()
        self.helpers = Helpers()
        self.success = Success()
        self.error = Error()

    def post(self):
        """A method for handling the log in request endpoint"""
        json_data = request.get_json(force=True)
        email = json_data.get('email')
        password = json_data.get('password')
        try:
            # check if password exists
            if not email:
                return self.error.bad_request('No password provided')
            if len(password) < 8:
                return self.error.bad_request('Password too short')
            # check if email exists
            if not email:
                return self.error.bad_request('No email provided')
            if not self.helpers.email_valid(email):
                return self.error.bad_request('Invalid email')
            # Get the user object
            user = User.query.filter_by(email=email).first()
            # Authenticate the user
            if user and user.is_password_valid(password):
                # Generate the access token to be used in header
                access_token = user.generate_token(user.id)
                if access_token:
                    token = access_token.decode()
                    return make_response(jsonify({"token": token})), 200
            else:
                return self.error.unauthorized('Invalid email or password')
        except Exception as e:
            return str(e)
コード例 #3
0
class OrdersView(MethodView):
    """This is for handling order requests"""
    def __init__(self):
        super().__init__()
        self.success = Success()
        self.error = Error()
        self.helpers = Helpers()

    @token_required
    def post(self, user_id):
        """For adding order to database"""
        json_data = request.get_json(force=True)
        name = json_data.get('name')
        email = json_data.get('email')
        phone_number = json_data.get('phone_number')
        problem_statement = json_data.get('problem_statement')
        leading_channel = json_data.get('leading_channel')
        project_type = json_data.get('project_type')
        preferred_software = json_data.get('preferred_software')
        description = json_data.get('description')

        try:
            if not name or not email or not phone_number or not problem_statement or not leading_channel \
                    or not project_type or not preferred_software or not description:
                return self.error.bad_request('Please enter all fields')
            if not self.helpers.email_valid(email=email):
                return self.error.bad_request('Invalid email')
            order = Order(name=name,
                          email=email,
                          phone_number=phone_number,
                          problem_statement=problem_statement,
                          leading_channel=leading_channel,
                          project_type=project_type,
                          preferred_software=preferred_software,
                          description=description)
            order.save()
            return jsonify({'message': 'Success', 'id': order.id}), 201
        except Exception as e:
            return self.error.internal_server_error(
                'Error occurred {}'.format(e))

    def get(self):
        """This is a method for getting all orders from the database"""
        try:
            orders = Order.query.all()
            order_data = []

            #  make the data json serializable
            for order in orders:
                order_data.append({
                    'id': order.id,
                    'name': order.name,
                    'email': order.email,
                    'phone_number': order.phone_number,
                    'problem_statement': order.problem_statement,
                    'leading_channel': order.leading_channel,
                    'project_type': order.project_type,
                    'preferred_software': order.preferred_software,
                    'description': order.description,
                    'done': order.done
                })
            return jsonify({'data': order_data}), 200
        except Exception as e:
            return self.error.internal_server_error('Error occurred'.format(e))