-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deseng 464 : Poll widget UI #2367
Changes from all commits
0cf44a6
41680d6
a28a0b8
bfd0fff
5e2a50a
e2cebe5
6177797
7c7f8f5
d6780cc
d620ab5
4d4b138
eedbd91
da2ce7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,10 +2,13 @@ | |||||
from http import HTTPStatus | ||||||
|
||||||
from sqlalchemy.exc import SQLAlchemyError | ||||||
|
||||||
from met_api.constants.engagement_status import Status as EngagementStatus | ||||||
from met_api.constants.membership_type import MembershipType | ||||||
from met_api.exceptions.business_exception import BusinessException | ||||||
from met_api.models.widget_poll import Poll as PollModel | ||||||
from met_api.services import authorization | ||||||
from met_api.services.engagement_service import EngagementService | ||||||
from met_api.services.poll_answers_service import PollAnswerService | ||||||
from met_api.services.poll_response_service import PollResponseService | ||||||
from met_api.utils.roles import Role | ||||||
|
@@ -24,7 +27,9 @@ def get_poll_by_id(poll_id: int): | |||||
"""Get poll by poll ID.""" | ||||||
poll = PollModel.query.get(poll_id) | ||||||
if not poll: | ||||||
raise BusinessException('Poll widget not found', HTTPStatus.NOT_FOUND) | ||||||
raise BusinessException( | ||||||
'Poll widget not found', HTTPStatus.NOT_FOUND | ||||||
) | ||||||
return poll | ||||||
|
||||||
@staticmethod | ||||||
|
@@ -33,7 +38,9 @@ def create_poll(widget_id: int, poll_details: dict): | |||||
try: | ||||||
eng_id = poll_details.get('engagement_id') | ||||||
WidgetPollService._check_authorization(eng_id) | ||||||
return WidgetPollService._create_poll_model(widget_id, poll_details) | ||||||
return WidgetPollService._create_poll_model( | ||||||
widget_id, poll_details | ||||||
) | ||||||
except SQLAlchemyError as exc: | ||||||
raise BusinessException(str(exc), HTTPStatus.BAD_REQUEST) from exc | ||||||
|
||||||
|
@@ -45,9 +52,13 @@ def update_poll(widget_id: int, poll_widget_id: int, poll_data: dict): | |||||
WidgetPollService._check_authorization(widget_poll.engagement_id) | ||||||
|
||||||
if widget_poll.widget_id != widget_id: | ||||||
raise BusinessException('Invalid widget ID', HTTPStatus.BAD_REQUEST) | ||||||
raise BusinessException( | ||||||
'Invalid widget ID', HTTPStatus.BAD_REQUEST | ||||||
) | ||||||
|
||||||
return WidgetPollService._update_poll_model(poll_widget_id, poll_data) | ||||||
return WidgetPollService._update_poll_model( | ||||||
poll_widget_id, poll_data | ||||||
) | ||||||
except SQLAlchemyError as exc: | ||||||
raise BusinessException(str(exc), HTTPStatus.BAD_REQUEST) from exc | ||||||
|
||||||
|
@@ -77,6 +88,21 @@ def is_poll_active(poll_id: int) -> bool: | |||||
except SQLAlchemyError as exc: | ||||||
raise BusinessException(str(exc), HTTPStatus.BAD_REQUEST) from exc | ||||||
|
||||||
@staticmethod | ||||||
def is_poll_engagement_published(poll_id: int) -> bool: | ||||||
"""Check if the poll is published.""" | ||||||
try: | ||||||
poll = WidgetPollService.get_poll_by_id(poll_id) | ||||||
engagement = EngagementService().get_engagement(poll.engagement_id) | ||||||
pub_val = EngagementStatus.Published.value | ||||||
# Return False immediately if engagement is None | ||||||
if engagement is None: | ||||||
return False | ||||||
# Check if the engagement's status matches the published value | ||||||
return engagement.get('status_id') == pub_val | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the max length of the line by flake8 is 79. |
||||||
except SQLAlchemyError as exc: | ||||||
raise BusinessException(str(exc), HTTPStatus.BAD_REQUEST) from exc | ||||||
|
||||||
@staticmethod | ||||||
def _create_poll_model(widget_id: int, poll_data: dict): | ||||||
"""Private method to create poll model.""" | ||||||
|
@@ -94,12 +120,23 @@ def _update_poll_model(poll_id: int, poll_data: dict): | |||||
@staticmethod | ||||||
def _check_authorization(engagement_id): | ||||||
"""Check user authorization.""" | ||||||
authorization.check_auth(one_of_roles=(MembershipType.TEAM_MEMBER.name, Role.EDIT_ENGAGEMENT.value), | ||||||
engagement_id=engagement_id) | ||||||
authorization.check_auth( | ||||||
one_of_roles=( | ||||||
MembershipType.TEAM_MEMBER.name, | ||||||
Role.EDIT_ENGAGEMENT.value, | ||||||
), | ||||||
engagement_id=engagement_id, | ||||||
) | ||||||
|
||||||
@staticmethod | ||||||
def _handle_poll_answers(poll_id: int, poll_data: dict): | ||||||
"""Handle poll answers creation and deletion.""" | ||||||
PollAnswerService.delete_poll_answers(poll_id) | ||||||
answers_data = poll_data.get('answers', []) | ||||||
PollAnswerService.create_bulk_poll_answers(poll_id, answers_data) | ||||||
try: | ||||||
if 'answers' in poll_data and len(poll_data['answers']) > 0: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the looks of this, if a user tried to save a poll with no answers then it would fail silently, correct? This probably won't happen that often but we should probably notify them that their save was unsuccessful. Does that happen somewhere? Or does the frontend block saving if no answers are provided? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function ensures that if there are no changes to the answers in the API, there is no need to update the answers. Especially, while patching There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, it's clear now! |
||||||
PollAnswerService.delete_poll_answers(poll_id) | ||||||
answers_data = poll_data.get('answers', []) | ||||||
PollAnswerService.create_bulk_poll_answers( | ||||||
poll_id, answers_data | ||||||
) | ||||||
except SQLAlchemyError as exc: | ||||||
raise BusinessException(str(exc), HTTPStatus.BAD_REQUEST) from exc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you save this to a variable to keep the line length short on line 102?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. as the max length of line by flake8 is 79