Ejemplo n.º 1
0
    def get(self):
        try:
            # Getting the cid from the jwt.
            current_customer = get_jwt_identity()

            # Getting the customer from the database through the model in models.py
            customer_object = Customer.find_by_cid(current_customer)

            # Checks if no object got returned in the query, then return 401 Unauthorized.
            if customer_object.customer_id == None:
                return {
                    "message":
                    "Invalid cid. The customer doesn't exist in our database"
                }, 401

            if customer_object.address_id == None:
                return {"message": "The customer has no address"}, 401

            customer_address = Address.find_by_address_id(
                customer_object.address_id)

            return {
                "message": "Address of the customer was found",
                "city": customer_address.city,
                "postcode": customer_address.postcode,
                "street_name": customer_address.street_name,
                "street_number": customer_address.street_number,
                "apartment_number": customer_address.apartment_number
            }, 202

        except Exception as err:
            return {
                "message": "Something went wrong on the server",
                "error": str(err)
            }, 500
Ejemplo n.º 2
0
    def post(self):
        data = registration_parser.parse_args()

        if not data["email"]:
            return {'message': 'Email is required'}

        if not data["password"]:
            return {'message': 'Password is required'}

        # Checking if the email is already in our database, returns message if it is. Countinues if not.
        if Customer.find_by_email(data['email']):
            return {'message': 'User {} already exists'.format(data['email'])}

        # Hashing password as soon as possible, Please dont add anything between the line above and below this comment
        data["password"] = Customer.generate_hash(data["password"])

        #TODO: Improve this \/
        cid = random.randint(10000000, 99999999)
        while Customer.find_by_cid(cid):
            if cid >= 99999999:
                cid = 10000000
            else:
                cid += 1

        aid = None
        if data["city"] or data["postcode"] or data["street_name"] or data[
                "street_number"] or data["apartment_number"]:
            aid = random.randint(10000000, 99999999)
            while Address.find_by_address_id(aid):
                if aid >= 99999999:
                    aid = 10000000
                else:
                    aid += 1

        # Making a new model with the email and password provided
        new_customer = Customer(customer_id=cid,
                                customer_email=data["email"],
                                customer_password=data["password"],
                                first_name=data["first_name"],
                                last_name=data["last_name"],
                                customer_birthday=data["birthday"],
                                customer_phone=data["phone"],
                                address_id=aid)

        if aid:
            new_address = Address(address_id=aid,
                                  city=data["city"],
                                  postcode=data["postcode"],
                                  street_name=data["street_name"],
                                  street_number=data["street_number"],
                                  apartment_number=data["apartment_number"])

        try:
            # Saving the new user to the database. the method is located in models.py
            new_customer.save_to_db()
            if aid:
                new_address.save_to_db()

            # Making tokens so the customer is logged in
            access_token = create_access_token(identity=cid)
            refresh_token = create_refresh_token(identity=cid)

            return {
                'message': 'Customer {} was created'.format(data['email']),
                'access_token': access_token,
                'refresh_token': refresh_token
            }, 201
        except Exception as err:
            return {'message': 'Something went wrong', "error": str(err)}, 500
Ejemplo n.º 3
0
    def post(self):
        data = edit_parser.parse_args()

        # Getting the cid from the jwt.
        current_customer = get_jwt_identity()

        # Getting the customer from the database through the model in models.py
        customer_object = Customer.find_by_cid(current_customer)

        # Hashing password as soon as possible, Please dont add anything between the line above and below this comment
        data["password"] = Customer.generate_hash(data["password"])

        # Checks if no object got returned in the query, then return 401 Unauthorized.
        if customer_object.customer_id == None:
            return {
                "message":
                "Invalid cid. The customer doesn't exist in our database"
            }, 401

        if data["password"]:
            customer_object.customer_password = data["password"]
        if data["first_name"]:
            customer_object.first_name = data["first_name"]
        if data["last_name"]:
            customer_object.last_name = data["last_name"]
        if data["birthday"]:
            customer_object.customer_birthday = data["birthday"]
        if data["phone"]:
            customer_object.customer_phone = data["phone"]

        customer_address = None
        new_address = None
        if data["city"] or data["postcode"] or data["street_name"] or data[
                "street_number"] or data["apartment_number"]:
            if customer_object.address_id:
                customer_address = Address.find_by_address_id(
                    customer_object.address_id)
                if data["city"]:
                    customer_address.city = data["city"]
                if data["postcode"]:
                    customer_address.postcode = data["postcode"]
                if data["street_name"]:
                    customer_address.street_name = data["street_name"]
                if data["street_number"]:
                    customer_address.street_number = data["street_number"]
                if data["apartment_number"]:
                    customer_address.apartment_number = data[
                        "apartment_number"]
            else:
                aid = random.randint(10000000, 99999999)
                while Address.find_by_address_id(aid):
                    if aid >= 99999999:
                        aid = 10000000
                    else:
                        aid += 1
                new_address = Address(
                    address_id=aid,
                    city=data["city"],
                    postcode=data["postcode"],
                    street_name=data["street_name"],
                    street_number=data["street_number"],
                    apartment_number=data["apartment_number"])
                current_customer.address_id = aid

        try:
            # Saving the new user to the database. the method is located in models.py
            if new_address: new_address.save_to_db()
            customer_object.commit()

            return {
                'message':
                'Customer {} was edited'.format(
                    customer_object.customer_email),
            }, 201
        except Exception as err:
            return {'message': 'Something went wrong', "error": str(err)}, 500