-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add video widget service and resource
- Loading branch information
1 parent
d6aa3be
commit 9351d32
Showing
6 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |