diff --git a/met-api/src/met_api/models/widget_video.py b/met-api/src/met_api/models/widget_video.py index 5526568ae..6ba049c4a 100644 --- a/met-api/src/met_api/models/widget_video.py +++ b/met-api/src/met_api/models/widget_video.py @@ -25,7 +25,7 @@ def get_video(cls, widget_id) -> list[WidgetVideo]: """Get video.""" widget_video = db.session.query(WidgetVideo) \ .filter(WidgetVideo.widget_id == widget_id) \ - .all() + .first() return widget_video @classmethod diff --git a/met-api/src/met_api/resources/document.py b/met-api/src/met_api/resources/document.py index 302308598..abc4b96eb 100644 --- a/met-api/src/met_api/resources/document.py +++ b/met-api/src/met_api/resources/document.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""API endpoints for managing a FOI Requests resource.""" +"""API endpoints for managing documents resource.""" from http import HTTPStatus diff --git a/met-api/src/met_api/resources/widget_map.py b/met-api/src/met_api/resources/widget_map.py index 3945ee78d..fa2a786c2 100644 --- a/met-api/src/met_api/resources/widget_map.py +++ b/met-api/src/met_api/resources/widget_map.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""API endpoints for managing a FOI Requests resource.""" +"""API endpoints for managing map resource.""" import json from http import HTTPStatus diff --git a/met-api/src/met_api/resources/widget_video.py b/met-api/src/met_api/resources/widget_video.py new file mode 100644 index 000000000..7d3591ecd --- /dev/null +++ b/met-api/src/met_api/resources/widget_video.py @@ -0,0 +1,70 @@ +# Copyright © 2021 Province of British Columbia +# +# Licensed under the Apache License, Version 2.0 (the 'License'); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an 'AS IS' BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""API endpoints for managing a video widget resource.""" +from http import HTTPStatus + +from flask import request +from flask_cors import cross_origin +from flask_restx import Namespace, Resource + +from met_api.auth import jwt as _jwt +from met_api.exceptions.business_exception import BusinessException +from met_api.schemas.widget_video import WidgetVideoSchema +from met_api.services.widget_video_service import WidgetVideoService +from met_api.utils.roles import Role +from met_api.utils.util import allowedorigins, cors_preflight + + +API = Namespace('widget_videos', description='Endpoints for Video Widget Management') +"""Widget Videos""" + + +@cors_preflight('GET, POST, PATCH, OPTIONS') +@API.route('') +class Video(Resource): + """Resource for managing video widgets.""" + + @staticmethod + @cross_origin(origins=allowedorigins()) + def get(widget_id): + """Get video widget.""" + try: + widget_video = WidgetVideoService().get_video(widget_id) + return WidgetVideoSchema().dump(widget_video), HTTPStatus.OK + except BusinessException as err: + return str(err), err.status_code + + @staticmethod + @cross_origin(origins=allowedorigins()) + @_jwt.has_one_of_roles([Role.EDIT_ENGAGEMENT.value]) + def post(widget_id): + """Create video widget.""" + try: + request_json = request.get_json() + widget_video = WidgetVideoService().create_video(widget_id, request_json) + return WidgetVideoSchema().dump(widget_video), HTTPStatus.OK + except BusinessException as err: + return str(err), err.status_code + + @staticmethod + @cross_origin(origins=allowedorigins()) + @_jwt.has_one_of_roles([Role.EDIT_ENGAGEMENT.value]) + def patch(widget_id): + """Update video widget.""" + request_json = request.get_json() + try: + widget_video = WidgetVideoService().update_video(widget_id, request_json) + return WidgetVideoSchema().dump(widget_video), HTTPStatus.OK + except BusinessException as err: + return str(err), err.status_code diff --git a/met-api/src/met_api/schemas/widget_video.py b/met-api/src/met_api/schemas/widget_video.py new file mode 100644 index 000000000..5f8b3af2c --- /dev/null +++ b/met-api/src/met_api/schemas/widget_video.py @@ -0,0 +1,28 @@ +# Copyright © 2019 Province of British Columbia +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Manager for widget video schema.""" + +from met_api.models.widget_video import WidgetVideo as WidgetVideoModel + +from marshmallow import Schema + + +class WidgetVideoSchema(Schema): # pylint: disable=too-many-ancestors, too-few-public-methods + """This is the schema for the video model.""" + + class Meta: # pylint: disable=too-few-public-methods + """Videos all of the Widget Video fields to a default schema.""" + + model = WidgetVideoModel + fields = ('id', 'widget_id', 'engagement_id', 'video_url', 'description') diff --git a/met-api/src/met_api/services/widget_video_service.py b/met-api/src/met_api/services/widget_video_service.py new file mode 100644 index 000000000..abb2404f8 --- /dev/null +++ b/met-api/src/met_api/services/widget_video_service.py @@ -0,0 +1,43 @@ +"""Service for Widget Video management.""" +from http import HTTPStatus + +from met_api.exceptions.business_exception import BusinessException +from met_api.models.widget_video import WidgetVideo as WidgetVideoModel + + +class WidgetVideoService: + """Widget Video management service.""" + + @staticmethod + def get_video(widget_id): + """Get video by widget id.""" + widget_video = WidgetVideoModel.get_video(widget_id) + return widget_video + + @staticmethod + def create_video(widget_id, video_details: dict): + """Create video for the widget.""" + video_data = dict(video_details) + widget_video = WidgetVideoService._create_video_model(widget_id, video_data) + widget_video.commit() + return widget_video + + @staticmethod + def update_video(widget_id, request_json): + """Update video widget.""" + widget_video: WidgetVideoModel = WidgetVideoModel.get_video(widget_id) + if widget_video.widget_id != widget_id: + raise BusinessException( + error='Invalid widgets and video', + status_code=HTTPStatus.BAD_REQUEST) + return WidgetVideoModel.update_video(widget_id, request_json) + + @staticmethod + def _create_video_model(widget_id, video_data: dict): + video_model: WidgetVideoModel = WidgetVideoModel() + video_model.widget_id = widget_id + video_model.engagement_id = video_data.get('engagement_id') + video_model.video_url = video_data.get('video_url') + video_model.description = video_data.get('description') + video_model.flush() + return video_model