Exemple #1
0
    def put(self):
        """ Update resource data.

        Returns:
            response (flask.Response): Flask response object
        """
        try:
            request_data = request.get_json()
            db_data = Resources.query.filter_by(
                id=request_data.get("id")).first()
            if db_data is None:
                return error_response(
                    f"Resource with given ID: {request_data['id']} was not found",
                    msg="Resource not found",
                    err_code=404,
                )
            db_data.active = request_data.get("active")
            db.session.commit()
            return {
                "success": True,
                "message": f"Resource with ID {db_data.id} updated",
            }
        except (KeyError, AttributeError):
            return error_response(
                "ID is the only supported filter at this moment",
                msg="Unsupported filter",
                err_code=403,
            )
        except Exception as err:
            return error_response(err.__repr__())
Exemple #2
0
    def get(self):
        """ Get slots endpoint.

        Returns:
            slots_list (list): list of slots dictionaries
        """
        try:
            if request.get_json() is not None:
                return error_response(
                    "JSON body is not accepted in this endpoint",
                    msg="Invalid input",
                    err_code=406,
                )
            from_date = request.args.get("from")
            to_date = request.args.get("to")
            resources = request.args.get("resources")
            if from_date and to_date and resources is not None:
                slots_obj_list = Slots.query.filter_by(
                    timestamp=from_date,
                    timestamp_end=to_date,
                    available_resources=resources,
                ).all()
            elif from_date and to_date is not None:
                slots_obj_list = Slots.query.filter(
                    and_(func.date(Slots.timestamp) >= from_date),
                    func.date(Slots.timestamp) <= to_date,
                ).all()
            elif from_date is not None:
                slots_obj_list = Slots.query.filter(
                    func.date(Slots.timestamp) == from_date).all()
            elif to_date is not None:
                slots_obj_list = Slots.query.filter(
                    func.date(Slots.timestamp_end) == to_date).all()
            elif resources is not None:
                slots_obj_list = Slots.query.filter(
                    Slots.available_resources.in_(resources)).all()
            else:
                slots_obj_list = Slots.query.all()

            slots_list = list()
            for slot in slots_obj_list:
                slot_dict = {
                    "id": slot.id,
                    "timestamp": str(slot.timestamp),
                    "timestamp_end": str(slot.timestamp_end),
                    "formatted_timestamp": str(slot.formatted_timestamp),
                    "formatted_timestamp_end":
                    str(slot.formatted_timestamp_end),
                    "free": slot.free,
                    "available_resources": slot.available_resources,
                    "maximum_capacity": slot.maximum_capacity,
                }
                slots_list.append(slot_dict)
            return jsonify(slots_list)
        except Exception as err:
            return error_response(err.__repr__())
Exemple #3
0
    def put(self):
        """ Update booking data.

        Returns:
            response (flask.Response): Flask response object
        """
        try:
            request_data = request.get_json()
            db_data = Bookings.query.filter_by(
                id=request_data.get("id")).first()
            if db_data is None:
                return error_response(
                    f"Booking with given ID: {request_data['id']} was not found",
                    msg="Booking not found",
                    err_code=404,
                )

            possible_request_params = [
                request_data.get("resource_id"),
                request_data.get("booked_from"),
                request_data.get("booked_to"),
            ]
            if not any(possible_request_params):
                return error_response(
                    f"'resource_id', 'booked_from' or 'booked_to' JSON arguments should be provided",
                    msg="Invalid input",
                    err_code=406,
                )
            if request_data.get("resource_id"):
                db_data.resource_id = request_data.get("resource_id")
            if request_data.get("booked_from"):
                db_data.booked_from = datetime.strptime(
                    request_data.get("booked_from"), "%Y-%m-%d %H:%M:%S")
            if request_data.get("booked_to"):
                db_data.booked_to = datetime.strptime(
                    request_data.get("booked_to"), "%Y-%m-%d %H:%M:%S")
            db.session.commit()
            return {
                "success": True,
                "message": f"Booking with ID {db_data.id} updated"
            }
        except (KeyError, AttributeError):
            return error_response(
                "ID is the only supported filter at this moment",
                msg="Unsupported filter",
                err_code=403,
            )
        except Exception as err:
            return error_response(err.__repr__())
Exemple #4
0
    def post(self):
        """ Add single booking.

        Returns:
            response (flask.Response): Flask response object
        """
        request_data = request.get_json()
        try:
            db_data = Bookings(
                resource_id=request_data.get("resource_id"),
                user_id=request_data.get("user_id"),
                booked_from=datetime.strptime(request_data.get("booked_from"),
                                              "%Y-%m-%d %H:%M:%S"),
                booked_to=datetime.strptime(request_data.get("booked_to"),
                                            "%Y-%m-%d %H:%M:%S"),
                notes=request_data.get("notes"),
            )
            db.session.add(db_data)
            db.session.commit()
            return {
                "success":
                True,
                "message":
                f"Booking of user {request_data.get('resource_id')} "
                f"with resource {request_data.get('user_id')} "
                f"from {request_data.get('booked_from')} to "
                f"{request_data.get('booked_to')} added",
            }
        except Exception as err:
            return error_response(err.__repr__())
Exemple #5
0
    def post(self):
        """ Add single resource.

        Returns:
            response (flask.Response): Flask response object
        """
        request_data = request.get_json()
        try:
            db_data = Resources(
                title=request_data.get("title"),
                created_at=datetime.now(),
                updated_at=datetime.now(),
                active=request_data.get("active"),
                intervals=request_data.get("intervals"),
                opening_hours_mon=request_data.get("opening_hours_mon"),
                opening_hours_tue=request_data.get("opening_hours_tue"),
                opening_hours_wed=request_data.get("opening_hours_wed"),
                opening_hours_thu=request_data.get("opening_hours_thu"),
                opening_hours_fri=request_data.get("opening_hours_fri"),
                opening_hours_sat=request_data.get("opening_hours_sat"),
                opening_hours_sun=request_data.get("opening_hours_sun"),
            )
            db.session.add(db_data)
            db.session.commit()
            return {
                "success": True,
                "message": f"Resource {request_data.get('title')} added",
            }
        except Exception as err:
            return error_response(err.__repr__())
Exemple #6
0
    def get(self):
        """ Get all resources.

        Returns:
            resources_list (list): list of resources dictionaries
        """
        try:
            if request.get_json() is not None:
                return error_response(
                    "JSON body is not accepted in this endpoint",
                    msg="Invalid input",
                    err_code=406,
                )
            resource_id = request.args.get("id")
            title = request.args.get("title")
            if resource_id and title is not None:
                resources_obj_list = Resources.query.filter_by(
                    id=resource_id, title=title).all()
            elif resource_id is not None:
                resources_obj_list = Resources.query.filter_by(
                    id=resource_id).all()
            elif title is not None:
                resources_obj_list = Resources.query.filter_by(
                    title=title).all()
            else:
                resources_obj_list = Resources.query.all()
            resources_list = list()
            for res in resources_obj_list:
                res_dict = {
                    "id": res.id,
                    "title": res.title,
                    "created_at": str(res.created_at),
                    "updated_at": str(res.updated_at),
                    "active": res.active,
                    "intervals": res.intervals,
                    "opening_hours_mon": res.opening_hours_mon,
                    "opening_hours_tue": res.opening_hours_tue,
                    "opening_hours_wed": res.opening_hours_wed,
                    "opening_hours_thu": res.opening_hours_thu,
                    "opening_hours_fri": res.opening_hours_fri,
                    "opening_hours_sat": res.opening_hours_sat,
                    "opening_hours_sun": res.opening_hours_sun,
                }
                resources_list.append(res_dict)
            return jsonify(resources_list)
        except Exception as err:
            return error_response(err.__repr__())
Exemple #7
0
    def get(self):
        """ Get bookings data.

        Returns:
            bookings_list (list): list of bookings dictionaries
        """
        try:
            if request.get_json() is not None:
                return error_response(
                    "JSON body is not accepted in this endpoint",
                    msg="Invalid input",
                    err_code=406,
                )
            booking_id = request.args.get("id")
            resource_id = request.args.get("resource-id")
            user_id = request.args.get("user-id")
            if booking_id and user_id and resource_id is not None:
                bookings_obj_list = Bookings.query.filter_by(
                    id=booking_id, user_id=user_id,
                    resource_id=resource_id).all()
            elif booking_id is not None:
                bookings_obj_list = Bookings.query.filter_by(
                    id=booking_id).all()
            elif user_id is not None:
                bookings_obj_list = Bookings.query.filter_by(
                    user_id=user_id).all()
            elif resource_id is not None:
                bookings_obj_list = Bookings.query.filter_by(
                    resource_id=resource_id).all()
            else:
                bookings_obj_list = Bookings.query.all()

            bookings_list = list()
            for booking in bookings_obj_list:
                booking_dict = {
                    "id": booking.id,
                    "resource_id": booking.resource_id,
                    "user_id": booking.user_id,
                    "booked_from": booking.booked_from,
                    "booked_to": booking.booked_to,
                    "notes": booking.notes,
                }
                bookings_list.append(booking_dict)
            return jsonify(bookings_list)
        except Exception as err:
            return error_response(err.__repr__())
Exemple #8
0
    def delete(self):
        """ Delete given booking using its ID.

        Args:
            id (str): booking ID

        Returns:
            response (flask.Response): Flask response object
        """
        if request.get_json() is not None:
            return error_response(
                "JSON body is not accepted in this endpoint",
                msg="Invalid input",
                err_code=406,
            )
        if request.args.get("id"):
            booking_id = request.args.get("id")
        else:
            return error_response("Improper URL parameters provided",
                                  msg="Invalid input",
                                  err_code=406)
        try:
            db_data = Bookings.query.filter_by(id=booking_id).first()
            if db_data is None:
                return error_response(
                    f"Booking with given ID: {booking_id} was not found",
                    msg="Booking not found",
                    err_code=404,
                )
            db.session.delete(db_data)
            db.session.commit()
            response = jsonify(
                dict(success=True,
                     message=f"Booking with ID {booking_id} removed"))
            return response
        except Exception as err:
            return error_response(err.__repr__())