Esempio n. 1
0
    def get(self, supervisor_id):
        # Authorize user.
        id, role = fetch_token(request.headers.get("Authorization"))
        if id is not None and not isinstance(id, int):
            abort(401, status="error", message=id)
        if id is None:
            abort(401,
                  status="error",
                  message="You have to log in to access this resource")
        supervisor = PropertySupervisor.query.filter_by(id=supervisor_id)\
            .first()
        if supervisor is None:
            abort(404,
                  message="Supervisor with id = {} doesn't exist".format(
                      supervisor_id))
        comments = Comment.query.filter_by(supervisor_id=supervisor.id).all()

        # Match every user id to the corresponding user name.
        comments_user = []
        for comment in comments:
            user_name = user_schema.dump(comment.user).data["name"]
            comment = comment_schema.dump(comment).data
            comment["user_name"] = user_name
            comments_user.append(comment)

        return {"status": "success", "data": comments_user}, 200
Esempio n. 2
0
 def get(self):
     # Authorize user.
     id, role = fetch_token(request.headers.get("Authorization"))
     if id is not None and not isinstance(id, int):
         abort(401, status="error", message=id)
     if id is None:
         abort(401, status="error",
               message="You have to log in to access this resource")
     door_frame_types = DoorFrameType.query.all()
     door_frame_types = door_frame_types_schema\
         .dump(door_frame_types).data
     return {"status": "success", "data": door_frame_types}, 200
Esempio n. 3
0
    def post(self):
        try:
            # Authorize user.
            id, role = fetch_token(request.headers.get("Authorization"))
            if id is not None and not isinstance(id, int):
                abort(401, status="error", message=id)
            if id is None:
                abort(401,
                      status="error",
                      message="You have to log in to access this resource")
            if role == "supervisor":
                abort(401,
                      status="error",
                      message="Only users can use this resource")

            post_data = request.get_json()

            # Checking if property_id exists.
            property_id = post_data.get("property_id")
            if property_id is None:
                abort(400, status="error", message="property_id is required")

            # Checking if property_id is instance of the int class.
            if not isinstance(property_id, int):
                abort(400,
                      status="error",
                      message="property_id must be integer")

            # Checking if size is positive.
            if property_id < 0:
                abort(400,
                      status="error",
                      message="property_id must be positive")

            # Creating the new instance.
            new_favorite = Favorite(id, property_id)
            # Adding the new instance to database.
            db.session.add(new_favorite)
            # Commit changes.
            db.session.commit()
            return {
                "status": "success",
                "message": ("Favorite added"
                            " successfully")
            }, 200
        except IntegrityError:
            # Rollback changes.
            db.session.rollback()
            abort(400,
                  status="error",
                  message=("Please make sure that the"
                           " property exists and that user has not already"
                           " saved that property as favorite."))
Esempio n. 4
0
 def get(self):
     # Authorize user.
     id, role = fetch_token(request.headers.get("Authorization"))
     if id is not None and not isinstance(id, int):
         abort(401, status="error", message=id)
     if id is None:
         abort(401,
               status="error",
               message="You have to log in to access this resource")
     comments = Comment.query.all()
     comments = comments_schema\
         .dump(comments).data
     return {"status": "success", "data": comments}, 200
Esempio n. 5
0
    def get(self):
        # Authorize user.
        id, role = fetch_token(request.headers.get("Authorization"))
        if id is not None and not isinstance(id, int):
            abort(401, status="error", message=id)
        if id is None:
            abort(401, status="error",
                  message="You have to log in to access this resource")
        if role == "supervisor":
            abort(401, status="error",
                  message="Only users can use this resource")

        visits = Visit.query.filter(Visit.user_id == id)
        visits = visits_schema\
            .dump(visits).data
        return {"status": "success", "data": visits}, 200
Esempio n. 6
0
    def get(self):
        # Authorize user.
        id, role = fetch_token(request.headers.get("Authorization"))
        if id is not None and not isinstance(id, int):
            abort(401, status="error", message=id)
        if id is None:
            abort(401,
                  status="error",
                  message="You have to log in to access this resource")
        if role == "supervisor":
            abort(401,
                  status="error",
                  message="Only users can use this resource")

        user = User.query.filter_by(id=id).first()
        user = user_schema.dump(user).data
        return {"status": "success", "data": user}, 200
Esempio n. 7
0
    def get(self, property_id):
        # Authorize user.
        id, role = fetch_token(request.headers.get("Authorization"))
        if id is not None and not isinstance(id, int):
            abort(401, status="error", message=id)
        if id is None:
            abort(401,
                  status="error",
                  message="You have to log in to access this resource")
        property = Property.query.filter_by(id=property_id).first()
        if property is None:
            abort(404,
                  message="Property with id = {} doesn't exist".format(
                      property_id))
        users = property.favored_by
        users = users_schema.dump(users).data

        return {"status": "success", "data": users}, 200
Esempio n. 8
0
    def get(self):
        # Authorize user.
        id, role = fetch_token(request.headers.get("Authorization"))
        if id is not None and not isinstance(id, int):
            abort(401, status="error", message=id)
        if id is None:
            abort(401, status="error",
                  message="You have to log in to access this resource")
        if role == "supervisor":
            abort(401, status="error",
                  message="Only users can use this resource")

        user = User.query.filter_by(id=id).first()
        if user is None:
            abort(404, message="User with id = {} doesn't exist"
                  .format(id))
        properties = user.properties
        properties = properties_schema.dump(properties).data
        return {"status": "success", "data": properties}, 200
    def get(self):
        # Authorize user.
        id, role = fetch_token(request.headers.get("Authorization"))
        if id is not None and not isinstance(id, int):
            abort(401, status="error", message=id)
        if id is None:
            abort(401, status="error",
                  message="You have to log in to access this resource")
        if role == "user":
            abort(401, status="error",
                  message="Only supervisors can use this resource")

        comments = Comment.query.filter_by(supervisor_id=id).all()

        # Match every user id to the corresponding user name.
        comments_user = []
        for comment in comments:
            user_name = user_schema.dump(comment.user).data["name"]
            comment = comment_schema.dump(comment).data
            comment["user_name"] = user_name
            comments_user.append(comment)

        return {"status": "success", "data": comments_user}, 200
Esempio n. 10
0
    def post(self):
        try:
            # Authorize user.
            id, role = fetch_token(request.headers.get("Authorization"))
            if id is not None and not isinstance(id, int):
                abort(401, status="error", message=id)
            if id is None:
                abort(401,
                      status="error",
                      message="You have to log in to access this resource")
            if role == "user":
                abort(401,
                      status="error",
                      message="Only supervisors can use this resource")

            post_data = request.get_json()

            # Checking if size exists.
            size = post_data.get("size")
            if size is None:
                abort(400, status="error", message="size is required")

            # Checking if size is instance of the int class.
            if not isinstance(size, int):
                abort(400, status="error", message="size must be integer")

            # Checking if size is positive.
            if size < 0:
                abort(400, status="error", message="size must be positive")

            # Checking if actions exist.
            actions = post_data.get("actions")
            if actions is None:
                abort(400, status="error", message="actions are required")

            # Checking if actions is instance of the list class.
            if not isinstance(actions, list):
                abort(400, status="error", message="actions must be list")

            # Checking if actions has appropriate length.
            if len(actions) == 0 or len(actions) > 2:
                abort(400,
                      status="error",
                      message="actions must be have at most 2 items")

            # Checking if action has appropriate value.
            values = [item.name for item in PropertyActionEnum]

            action_used = []

            for action in actions:
                # Checking if action exists.
                if "action" not in action:
                    abort(400, status="error", message="action is required")
                if action["action"] not in values:
                    abort(400,
                          status="error",
                          message=(
                              "action must have one"
                              " of the following values: {}".format(values)))

                # Checking if action has already been used.
                if action["action"] in action_used:
                    abort(400,
                          status="error",
                          message="duplicate action {}".format(
                              action["action"]))

                action_used.append(action["action"])

                # Checking if price exists.
                if "price" not in action:
                    abort(400, status="error", message="price is required")

                # Checking if price is instance of the int class.
                if not isinstance(action["price"], int):
                    abort(400, status="error", message="price must be integer")

                # Checking if price is positive.
                if action["price"] < 0:
                    abort(400,
                          status="error",
                          message="price must be positive")

            # Checking if floor exists.
            floor = post_data.get("floor")
            if floor is None:
                abort(400, status="error", message="floor is required")

            # Checking if floor is instance of the int class.
            if not isinstance(floor, int):
                abort(400, status="error", message="floor must be integer")

            # Checking if floor is in the expected range.
            if floor < -1 or floor > 99:
                abort(400,
                      status="error",
                      message="size must be between -1 and 99")

            # Checking if postal_code exists.
            postal_code = post_data.get("postal_code")
            if postal_code is None:
                abort(400, status="error", message="postal_code is required")

            # Checking if postal_code is instance of the str class.
            if not isinstance(postal_code, str):
                abort(400,
                      status="error",
                      message="postal_code must be string")

            # Checking if postal_code has appropriate length.
            if len(postal_code) > 10:
                abort(400,
                      status="error",
                      message=("postal_code must be consists of at most"
                               " 10 characters"))

            # Checking if street_address exists.
            street_address = post_data.get("street_address")
            if street_address is None:
                abort(400,
                      status="error",
                      message="street_address is required")

            # Checking if street_address is instance of the str class.
            if not isinstance(street_address, str):
                abort(400,
                      status="error",
                      message="street_address must be string")

            # Checking if street_address has appropriate length.
            if len(street_address) > 35:
                abort(400,
                      status="error",
                      message=("street_address must be consists of at most"
                               " 35 characters"))

            # Checking if street_number exists.
            street_number = post_data.get("street_number")
            if street_number is not None:
                # Checking if street_number is instance of the int class.
                if not isinstance(street_number, int):
                    abort(400,
                          status="error",
                          message="street_number must be integer")
                # Checking if street_number is positive.
                if street_number < 0:
                    abort(400,
                          status="error",
                          message="street_number must be positive")

            # Checking if property_type exists.
            property_type = post_data.get("property_type")
            if property_type is None:
                abort(400, status="error", message="property_type is required")

            # Checking if property_type is instance of the str class.
            if not isinstance(property_type, str):
                abort(400,
                      status="error",
                      message="property_type must be string")

            # Checking if energy_certificate has appropriate value.
            values = [item.name for item in PropertyTypeEnum]

            if property_type not in values:
                abort(400,
                      status="error",
                      message=("property_type must have one"
                               " of the following values: {}".format(values)))

            # Checking if rooms exists.
            rooms = post_data.get("rooms")
            if rooms is not None:
                # Checking if rooms is instance of the int class.
                if not isinstance(rooms, int):
                    abort(400, status="error", message="rooms must be integer")

                # Checking if rooms is positive.
                if rooms < 0:
                    abort(400,
                          status="error",
                          message="rooms must be positive")

            # Checking if longitude exists.
            longitude = post_data.get("longitude")
            if longitude is None:
                abort(400, status="error", message="longitude is required")

            # Checking if longitude is instance of the float class.
            if not isinstance(longitude, float):
                abort(400, status="error", message="longitude must be float")

            # Checking if latitude exists.
            latitude = post_data.get("latitude")
            if latitude is None:
                abort(400, status="error", message="latitude is required")

            # Checking if latitude is instance of the float class.
            if not isinstance(latitude, float):
                abort(400, status="error", message="latitude must be float")

            # Checking if energy_certificate exists.
            energy_certificate = post_data.get("energy_certificate")
            if energy_certificate is not None:
                # Checking if energy_certificate is instance of the str class.
                if not isinstance(energy_certificate, str):
                    abort(400,
                          status="error",
                          message="energy_certificate must be string")

                # Checking if energy_certificate has appropriate value.
                values = [item.name for item in EnergyCertificateEnum]

                if energy_certificate not in values:
                    abort(400,
                          status="error",
                          message=(
                              "energy_certificate must have one"
                              " of the following values: {}".format(values)))

            # Checking if details exists.
            details = post_data.get("details")
            if details is not None:
                # Checking if details is instance of the str class.
                if not isinstance(details, str):
                    abort(400,
                          status="error",
                          message="details must be string")

                # Checking if details has appropriate length.
                if len(details) > 150:
                    abort(400,
                          status="error",
                          message=("details must be consists of at most"
                                   " 150 characters"))

            # Checking if city_id exists.
            city_id = post_data.get("city_id")
            if city_id is None:
                abort(400, status="error", message="city_id is required")

            # Checking if city_id is instance of the int class.
            if not isinstance(city_id, int):
                abort(400, status="error", message="city_id must be integer")

            # Checking if city_id exists in database.
            city = City.query.filter_by(id=city_id).first()
            if city is None:
                abort(400,
                      status="error",
                      message="city_id = {} does not exist".format(city_id))

            # Checking if heating_type_id exists.
            heating_type_id = post_data.get("heating_type_id")
            if heating_type_id is None:
                abort(400,
                      status="error",
                      message="heating_type_id is required")

            # Checking if heating_type_id is instance of the int class.
            if not isinstance(heating_type_id, int):
                abort(400,
                      status="error",
                      message="heating_type_id must be integer")

            # Checking if heating_type_id exists in database.
            heating_type = HeatingType.query.filter_by(id=heating_type_id)\
                                      .first()
            if heating_type is None:
                abort(400,
                      status="error",
                      message="heating_type = {} does not exist".format(
                          heating_type_id))

            # Checking if door_frame_id exists.
            door_frame_id = post_data.get("door_frame_id")
            if door_frame_id is None:
                abort(400, status="error", message="door_frame_id is required")

            # Checking if door_frame_iddoor_frame_id is instance of the
            # int class.
            if not isinstance(door_frame_id, int):
                abort(400,
                      status="error",
                      message="door_frame_id must be integer")

            # Checking if door_frame_id exists in database.
            door_frame = DoorFrameType.query.filter_by(id=door_frame_id) \
                                      .first()
            if door_frame is None:
                abort(400,
                      status="error",
                      message="door_frame_id = {} does not exist".format(
                          door_frame_id))

            # Creating Property instance.
            property = Property(floor, postal_code, street_address,
                                street_number, property_type, size, rooms,
                                longitude, latitude, energy_certificate,
                                city_id, id, heating_type_id, door_frame_id,
                                details)

            # Saving Property instance.
            db.session.add(property)
            db.session.commit()

            # Creating PropertyAction instance.
            for action in actions:
                property_action = PropertyAction(property_id=property.id,
                                                 action=action["action"],
                                                 price=action["price"])

                # Saving PropertyAction instance.
                db.session.add(property_action)

            db.session.commit()

            return {
                "status": "success",
                "message": "Property added successfully"
            }, 200

        except IntegrityError:
            # Rollback changes.
            db.session.rollback()
            abort(400,
                  status="error",
                  message="Unexpected error. Please try again later")
Esempio n. 11
0
    def post(self):
        # Authorize user.
        id, role = fetch_token(request.headers.get("Authorization"))
        if id is not None and not isinstance(id, int):
            abort(401, status="error", message=id)
        if id is None:
            abort(401, status="error",
                  message="You have to log in to access this resource")

        post_data = request.get_json()

        # Tuple for dynamically storing filters.
        filters = ()

        # Checking if max_size is instance of the int class.
        max_size = post_data.get("max_size")
        if max_size is not None and not isinstance(max_size, int):
            abort(400, status="error", message="max_size must be integer")

        if max_size is not None:
            filters += (Property.size <= max_size, )

        # Checking if min_size is instance of the int class.
        min_size = post_data.get("min_size")
        if max_size is not None and not isinstance(max_size, int):
            abort(400, status="error", message="max_size must be integer")

        if min_size is not None:
            filters += (Property.size >= min_size, )

        # Checking if max_price is instance of the int class.
        max_price = post_data.get("max_price")
        if max_price is not None and not isinstance(max_price, int):
            abort(400, status="error", message="max_price must be integer")

        # Checking if min_price is instance of the int class.
        min_price = post_data.get("min_price")
        if min_price is not None and not isinstance(min_price, int):
            abort(400, status="error", message="min_price must be integer")

        # Checking if max_floor is instance of the int class.
        max_floor = post_data.get("max_floor")
        if max_floor is not None and not isinstance(max_floor, int):
            abort(400, status="error", message="max_floor must be integer")

        if max_floor is not None:
            filters += (Property.floor <= max_floor, )

        # Checking if min_floor is instance of the int class.
        min_floor = post_data.get("min_floor")
        if min_floor is not None and not isinstance(min_floor, int):
            abort(400, status="error", message="min_floor must be integer")

        if min_floor is not None:
            filters += (Property.floor >= min_floor, )

        # Checking if max_rooms is instance of the int class.
        max_rooms = post_data.get("max_rooms")
        if max_rooms is not None and not isinstance(max_rooms, int):
            abort(400, status="error", message="max_rooms must be integer")

        if max_rooms is not None:
            filters += (Property.rooms <= max_rooms, )

        # Checking if min_rooms is instance of the int class.
        min_rooms = post_data.get("min_rooms")
        if min_rooms is not None and not isinstance(min_rooms, int):
            abort(400, status="error", message="min_rooms must be integer")

        if min_rooms is not None:
            filters += (Property.rooms >= min_rooms, )

        # Checking if city is instance of the str class.
        city = post_data.get("city")
        if city is not None and not isinstance(city, str):
            abort(400, status="error", message="city must be string")

        if city is not None:
            city_instance = City.query.filter_by(name=city).first()
            if city_instance is not None:
                filters += (Property.city_id == city_instance.id, )
            else:
                abort(400, status="error",
                      message=("city {} does not exist in the database"
                               .format(city)))

        action = post_data.get("action")

        # Checking if action exists.
        if action is None:
            abort(400, status="error", message="action is required")

        # Checking if action is instance of the str class.
        if action is not None and not isinstance(action, str):
            abort(400, status="error", message="action must be string")

        # Running the actual query
        properties = Property.query.filter(*filters)

        # Iterate over every property and check if action, max and min price
        # requirements are met.
        result_properties = []
        for property in properties:
            favorite = False
            for property_action in property.actions:
                property_action = property_action_schema\
                    .dump(property_action).data
                if property_action["action"] == action:
                    # Check if property is favored by user.
                    for user in property.favored_by:
                        if id == user.id:
                            favorite = True
                            break
                    # Adding the price and favorite fields to property.
                    property = property_with_price_schema.dump(property).data
                    property["price"] = property_action["price"]
                    property["favorite"] = favorite
                    if min_price is not None and max_price is not None:
                        if property_action["price"] >= min_price \
                                and property_action["price"] <= max_price:
                            result_properties.append(property)
                    elif min_price is not None:
                        if property_action["price"] >= min_price:
                            result_properties.append(property)
                    elif max_price is not None:
                        if property_action["price"] <= max_price:
                            result_properties.append(property)
                    else:
                        result_properties.append(property)

        return {"status": "success", "data": result_properties}, 200