Skip to content

Commit

Permalink
add video widget service and resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jadmsaadaot committed Jul 10, 2023
1 parent d6aa3be commit 9351d32
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 3 deletions.
2 changes: 1 addition & 1 deletion met-api/src/met_api/models/widget_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion met-api/src/met_api/resources/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion met-api/src/met_api/resources/widget_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
70 changes: 70 additions & 0 deletions met-api/src/met_api/resources/widget_video.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions met-api/src/met_api/schemas/widget_video.py
Original file line number Diff line number Diff line change
@@ -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')
43 changes: 43 additions & 0 deletions met-api/src/met_api/services/widget_video_service.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9351d32

Please sign in to comment.