def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- user = kwargs['user'] request_data = kwargs['request_data'] mandal_id = request_data['mandal_id'] date = request_data['date'] total_confirmed = request_data['total_confirmed'] total_deaths = request_data['total_deaths'] total_recovered = request_data['total_recovered'] storage = MandalStorageImplementation() presenter = PresenterImplementation() interactor = UpdateStatistics(storage=storage, presenter=presenter) try: interactor.update_statistics(mandal_id=mandal_id, user=user, date=date, total_confirmed=total_confirmed, total_deaths=total_deaths, total_recovered=total_recovered) except InvalidMandalId: raise NotFound(*INVALID_MANDAL_ID) except InvalidDetailsForTotalConfirmed: raise BadRequest(*INVALID_TOTAL_CONFIRMED) except InvalidDetailsForTotalDeaths: raise BadRequest(*INVALID_TOTAL_DEATHS) except InvalidDetailsForTotalRecovered: raise BadRequest(*INVALID_TOTAL_RECOVERED) except StatNotFound: raise BadRequest(*DETAILS_NOT_FOUND) except UserNotAdmin: raise Forbidden(*USER_NOT_ADMIN) return HttpResponse(status=200)
def api_wrapper(*args, **kwargs): user = kwargs['user'] post_id = kwargs['post_id'] try: delete_post(user_id=user.id, post_id=post_id) except InvalidPostException: raise BadRequest(*INVALID_POST) except UserCannotDeletePostException: raise BadRequest(*USER_CANNOT_DELETE_POST) response = HttpResponse(status=202) return response
def api_wrapper(*args, **kwargs): user = kwargs['user'] comment_id = kwargs['comment_id'] request_data = kwargs['request_data'] reaction_type = request_data['reaction_type'] try: react_to_comment(user_id=user.id, comment_id=comment_id, reaction_type=reaction_type) except InvalidCommentException: raise BadRequest(*INVALID_COMMENT) except InvalidReactionTypeException: raise BadRequest(*INVALID_REACTION_TYPE)
def api_wrapper(*args, **kwargs): user = kwargs['user'] post_id = kwargs['post_id'] request_data = kwargs['request_data'] reaction_type = request_data['reaction_type'] try: react_to_post(user_id=user.id, post_id=post_id, reaction_type=reaction_type) except InvalidPostException: raise BadRequest(*INVALID_POST) except InvalidReactionTypeException: raise BadRequest(*INVALID_REACTION_TYPE)
def api_wrapper(*args, **kwargs): post_id = kwargs['post_id'] from fb_post.models_utility_functions import Post, ObjectDoesNotExist try: Post.objects.get(id=post_id).delete() return except ObjectDoesNotExist: from django_swagger_utils.drf_server.exceptions import BadRequest raise BadRequest('Invalid post id', 'INVALID_POST_ID')
def api_wrapper(*args, **kwargs): post_id = kwargs['post_id'] try: dict_of_metrics = get_reaction_metrics(post_id=post_id) except InvalidPostException: raise BadRequest(*INVALID_POST) data = json.dumps({'reaction_metrics': dict_of_metrics}) response = HttpResponse(data, status=201) return response
def validate_tournament_category_unlocked( self, tournament_category, user_ids, user_id_wise_username=None, team_hash_key_wise_user_ids=None): user_id_wise_team_hash_key = {} if team_hash_key_wise_user_ids: for team_hash_key, team_user_ids in \ team_hash_key_wise_user_ids.items(): for user_id in team_user_ids: user_id_wise_team_hash_key[user_id] = team_hash_key locked_participant_names = list() for user_id in user_ids: unlocked_t_categories = self.get_unlocked_tournament_categories( user_id=user_id) if tournament_category not in unlocked_t_categories: if user_id_wise_username: locked_participant_names.append( user_id_wise_username[user_id]) else: locked_participant_names.append( user_id_wise_team_hash_key[user_id]) if not locked_participant_names: return from django_swagger_utils.drf_server.exceptions import BadRequest if len(locked_participant_names) == 1: from ib_tournament.constants.exception_messages import \ TOURNAMENT_CATEGORY_LOCKED_FOR_PARTICIPANT raise BadRequest( TOURNAMENT_CATEGORY_LOCKED_FOR_PARTICIPANT[0].format( locked_participant_names[0]), TOURNAMENT_CATEGORY_LOCKED_FOR_PARTICIPANT[1]) else: from ib_tournament.constants.exception_messages import \ TOURNAMENT_CATEGORY_LOCKED_FOR_PARTICIPANTS raise BadRequest( TOURNAMENT_CATEGORY_LOCKED_FOR_PARTICIPANTS[0].format( ", ".join(locked_participant_names)), TOURNAMENT_CATEGORY_LOCKED_FOR_PARTICIPANTS[1])
def api_wrapper(*args, **kwargs): comment_id = kwargs['comment_id'] try: replies_details_list = get_replies_for_comment(comment_id=comment_id) except InvalidCommentException: raise BadRequest(*INVALID_COMMENT) replies_details_list_json = json.dumps(replies_details_list) response = HttpResponse(replies_details_list_json, status=200) return response
def api_wrapper(*args, **kwargs): post_id = kwargs['postid'] try: delete_post(post_id=post_id) from django.http.response import HttpResponse return HttpResponse(status=200) except ObjectDoesNotExist: from django_swagger_utils.drf_server.exceptions import BadRequest raise BadRequest('Invalid post id', 'INVALID_POST_ID')
def api_wrapper(*args, **kwargs): user = kwargs['user'] comment_id = kwargs['comment_id'] request_data = kwargs['request_data'] reply_content = request_data['content'] try: reply_id = reply_to_comment( user_id=user.id, comment_id=comment_id, reply_content=reply_content ) except InvalidReplyContent: raise BadRequest(*INVALID_REPLY_CONTENT) except InvalidCommentException: raise BadRequest(*INVALID_COMMENT) coment_id_json = json.dumps({"comment_id": reply_id}) response = HttpResponse(coment_id_json, status=201) return response
def api_wrapper(*args, **kwargs): post_id = kwargs['post_id'] try: post_reaction_deatails = get_reactions_to_post(post_id=post_id) except InvalidPostException: raise BadRequest(*INVALID_POST) post_reaction_deatails_json = json.dumps(post_reaction_deatails) response = HttpResponse(post_reaction_deatails_json, status=200) return response
def api_wrapper(*args, **kwargs): user = kwargs['user'] request_data = kwargs['request_data'] post_id = request_data['post_id'] comment_content = request_data['content'] try: comment_id = create_comment(user_id=user.id, post_id=post_id, comment_content=comment_content) except InvalidPostException: raise BadRequest(*INVALID_POST) except InvalidCommentContent: raise BadRequest(*INVALID_COMMENT_CONTENT) json_data = json.dumps({"comment_id": comment_id}) response = HttpResponse(json_data, status=201) return response
def test_update_task_state_with_unchecked_mandatory_checkist(self): #arrange from .raw_inputs import task_dto,\ project_dto,\ update_task_state_input_data_with_unchecked_mandatory_checklist,\ update_task_state_query_dto_with_unchecked_mandatory_checklist from .expected_responses import mandatory_checklist_dtos from project_management_portal.constants.exception_messages\ import CHECKLIST_NOT_SATISFIED update_task_state_input_data\ = update_task_state_input_data_with_unchecked_mandatory_checklist project_id = update_task_state_input_data.get('project_id') task_id = update_task_state_input_data['task_id'] to_state_id = update_task_state_input_data['to_state_id'] task_storage = create_autospec(TaskStorageInterface) project_storage = create_autospec(ProjectStorageInterface) task_presenter = create_autospec(TaskPresenterInterface) project_presenter = create_autospec(ProjectPresenterInterface) interactor = TaskStateTransitionInteractor( task_storage=task_storage, project_storage=project_storage) project_storage.validate_project_id.return_value = project_dto task_storage.validate_task_id.return_value = task_dto project_storage.validate_state_transition.return_value = True project_storage.get_transition_mandatory_checklist\ .return_value = mandatory_checklist_dtos project_presenter.raise_checklist_not_satisfied_exception\ .side_effect = BadRequest(*CHECKLIST_NOT_SATISFIED) expected_error_message\ = "Check list mandatory fields not checked" #act with pytest.raises(BadRequest) as error: interactor.update_task_state_wrapper(update_task_state_input_data, project_presenter, task_presenter) #assert assert error.value.message == expected_error_message project_storage.validate_project_id.assert_called_once_with( project_id=project_id) task_storage.validate_task_id.assert_called_once_with(task_id=task_id) project_storage.validate_state_transition.assert_called_once_with( project_dto.workflow_id, task_dto.state_id, to_state_id) project_storage.get_transition_mandatory_checklist\ .assert_called_once_with( update_task_state_query_dto_with_unchecked_mandatory_checklist ) project_presenter.raise_checklist_not_satisfied_exception\ .assert_called_once()
def api_wrapper(*args, **kwargs): user = kwargs['user'] post_id = kwargs['post_id'] request_data = kwargs['request_data'] comment_content = request_data['content'] is_comment_content_invalid = not comment_content if is_comment_content_invalid: raise BadRequest(*INVALID_COMMENT_CONTENT) try: comment = create_comment(user_id=user.id, post_id=post_id, comment_content=comment_content) except: raise BadRequest(*INVALID_POST) data = json.dumps({'comment_id': comment.id}) response = HttpResponse(data, status=201) return response
def api_wrapper(*args, **kwargs): post_id = kwargs['post_id'] try: metrics_dict = get_reaction_metrics(post_id=post_id) except InvalidPostException: raise BadRequest(*INVALID_POST) metrics_dict_json = json.dumps(metrics_dict) response = HttpResponse(metrics_dict_json, status=200) return response
def api_wrapper(*args, **kwargs): post_id = kwargs["post_id"] try: delete_post(post_id) from django.http.response import HttpResponse import json return HttpResponse(json.dumps({"status": "post_deleted"}), status=201) except ObjectDoesNotExist: from django_swagger_utils.drf_server.exceptions import BadRequest raise BadRequest('Invalid post id', 'INVALID_POST_ID')
def api_wrapper(*args, **kwargs): comment_id = kwargs['comment_id'] offset = kwargs['request_query_params']['offset'] limit = kwargs['request_query_params']['limit'] from fb_post.models_utility_functions import get_replies_for_comment, SuspiciousOperation try: replies = get_replies_for_comment(comment_id, offset, limit) from django.http.response import HttpResponse print(replies) return replies except SuspiciousOperation: from django_swagger_utils.drf_server.exceptions import BadRequest raise BadRequest('Invalid comment id', 'INVALID_COMMENT_ID')
def api_wrapper(*args, **kwargs): user = kwargs['user'] request_data = kwargs['request_data'] post_content = request_data['content'] is_post_content_invalid = not post_content if is_post_content_invalid: raise BadRequest(*INVALID_POST_CONTENT) post = create_post(user_id=user.id, post_content=post_content) data = json.dumps({'post_id': post.id}) response = HttpResponse(data, status=201) return response
def api_wrapper(*args, **kwargs): comment_id = kwargs["comment_id"] request_query_params = kwargs["request_query_params"] offset = request_query_params.offset limit = request_query_params.limit try: replies = get_replies_for_comment(comment_id=comment_id, offset=offset, limit=limit) from django.http.response import HttpResponse return HttpResponse(str(replies), status=201) except SuspiciousOperation: from django_swagger_utils.drf_server.exceptions import BadRequest raise BadRequest('Invalid comment id', 'INVALID_COMMENT_ID')
def api_wrapper(*args, **kwargs): post_id = kwargs['post_id'] from fb.presenters.json_presenter import JsonPresenter from fb.storages.storage import StorageImplementer from fb.interactors.post_interactor import PostInteractor json_presenter = JsonPresenter() storage = StorageImplementer() post_interactor = PostInteractor(json_presenter, storage) from django_swagger_utils.drf_server.exceptions import BadRequest try: post_interactor.delete_post(post_id) return except BadRequest: raise BadRequest('Invalid post id', 'INVALID_POST_ID')
def api_wrapper(*args, **kwargs): user = kwargs['user'] request_data = kwargs['request_data'] post_content = request_data['content'] try: post_id = create_post( user_id = user.id, post_content = post_content ) except InvalidPostContent: raise BadRequest(*INVALID_POST_CONTENT) data = json.dumps ({"post_id": post_id}) response = HttpResponse(data, status=200) return response
def api_wrapper(*args, **kwargs): comment_id = kwargs['commentid'] query_params = kwargs['request_query_params'] offset = query_params.offset limit = query_params.limit try: replies = get_replies_for_comment(comment_id=comment_id, offset=offset, limit=limit) response = {"replies": replies} # import json return response except SuspiciousOperation: from django_swagger_utils.drf_server.exceptions import BadRequest raise BadRequest('Invalid comment id', 'INVALID_COMMENT_ID')
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- comment_id = kwargs['comment_id'] user = kwargs['user'] request_data = kwargs['request_data'] reaction_type = request_data['reaction_type'] try: react_to_comment(user_id=user.id, comment_id=comment_id, reaction_type=reaction_type) except InvalidCommentException: raise NotFound(*INVALID_COMMENT_ID) except InvalidReactionTypeException: raise BadRequest(*INVALID_REACTION_TYPE) return HttpResponse(status=200) """
def api_wrapper(*args, **kwargs): from fb.presenters.json_presenter import JsonPresenter from fb.storages.storage import StorageImplementer from fb.interactors.comment_interactor import CommentInteractor comment_id = kwargs['comment_id'] offset = kwargs['request_query_params']['offset'] limit = kwargs['request_query_params']['limit'] json_presenter = JsonPresenter() storage = StorageImplementer() comment_interactor = CommentInteractor(json_presenter, storage) try: response = comment_interactor.get_comment_replies( comment_id, offset, limit) return response except SuspiciousOperation: from django_swagger_utils.drf_server.exceptions import BadRequest raise BadRequest('Invalid comment id', 'INVALID_COMMENT_ID')
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- # till_date = kwargs['till_date'] query = kwargs['request_query_params'] till_date = query['till_date'] storage = StateStorageImplementation() presenter = PresenterImplementation() try: interactor = GetStateWiseCumulativeReport(storage=storage, presenter=presenter) except InvalidDate: raise BadRequest(*INVALID_DATE) daily_cumulative_report = interactor.get_state_wise_cumulative_report( till_date) data = json.dumps(daily_cumulative_report) return HttpResponse(data, status=200)
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- comment_id = kwargs['comment_id'] user = kwargs['user'] request_data = kwargs['request_data'] comment_content=request_data['content'] try: reply_comment_id = reply_to_comment( user_id=user.id, comment_id=comment_id, reply_content=comment_content ) except InvalidCommentException: raise NotFound(*INVALID_COMMENT_ID) except InvalidReplyContent: raise BadRequest(*INVALID_REPLY_CONTENT) else: data = json.dumps({"comment_id": reply_comment_id}) response = HttpResponse(data, status=201) return response """
def raise_checklist_not_satisfied_exception(self): raise BadRequest(*CHECKLIST_NOT_SATISFIED)
def raise_invalid_offset_value_exception(self): raise BadRequest(*INVALID_OFFSET)
def raise_invalid_limit_value_exception(self): raise BadRequest(*INVALID_LIMIT)
def raise_exception_for_invalid_username(self): raise BadRequest(*INVALID_USERNAME)