Advertisement
Woobinda

Code styles

Sep 13th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. @doc.summary('Update engagement by ID')
  2.     @doc.consumes(EngagementUpdateBody, location='body')
  3.     @doc.consumes(XClientHeader, location='header')
  4.     @last_modified('match_info', 'engagement_id')
  5.     async def patch(self, request, engagement_id):
  6.         """HTTP PATCH method for Engagement update.
  7.  
  8.        Args:
  9.            request (sanic.request.Request): Request object that should contain X-Client header.
  10.            engagement_id (str): String representation of the MongoDB engagement
  11.            document id.
  12.  
  13.        Returns:
  14.            sanic.response.HTTPResponse object with engagement document data:
  15.                {
  16.                    "data": {"isUpdated": True or False},
  17.                    "response_datetime": "2019-01-24T11:35:02.078577",
  18.                    "status": "success"
  19.                }
  20.  
  21.        Raises:
  22.            sanic.exceptions.InvalidUsage: If request doesn't contains X-Client header.
  23.  
  24.        """
  25.         try:
  26.             await parser.parse(EngagementUpdateSchema(strict=True), request, locations=('headers', "json"))
  27.         except Exception as e:
  28.             print(e)
  29.  
  30.         controller = request.app.controllers.EngagementsController
  31.         is_update_template = request.json.get('uploadTemplateId')
  32.         is_with_option_wizard = 'entities' in request.json
  33.         user_data = await self.get_session_data(request)
  34.         user_id = user_data.get("useracct_id")
  35.         force_update = util.strtobool(request.args.get('force_update', 'False'))
  36.         if is_update_template or force_update or not is_with_option_wizard:
  37.             result = await controller.patch_upload_template_by_engagement(request, engagement_id, user_id=user_id)
  38.         else:
  39.             if not request.json.get('entities'):
  40.                 raise BadRequestError("The field 'entities' is empty - there is must be at least one entity")
  41.             result = await controller.patch_engagement_by_id(request, engagement_id)
  42.         return self.get_response(result)
  43.  
  44.  
  45.  
  46.  
  47. @doc.summary('Update engagement by ID')
  48.     @doc.consumes(EngagementUpdateBody, location='body')
  49.     @doc.consumes(XClientHeader, location='header')
  50.     @last_modified('match_info', 'engagement_id')
  51.     async def patch(self, request, engagement_id):
  52.         """HTTP PATCH method for Engagement update.
  53.  
  54.        Args:
  55.            request (sanic.request.Request): Request object that should contain X-Client header.
  56.            engagement_id (str): String representation of the MongoDB engagement
  57.            document id.
  58.  
  59.        Returns:
  60.            sanic.response.HTTPResponse object with engagement document data:
  61.                {
  62.                    "data": {"isUpdated": True or False},
  63.                    "response_datetime": "2019-01-24T11:35:02.078577",
  64.                    "status": "success"
  65.                }
  66.  
  67.        Raises:
  68.            sanic.exceptions.InvalidUsage: If request doesn't contains X-Client header.
  69.  
  70.        """
  71.         try:
  72.             await parser.parse(EngagementUpdateSchema(strict=True),
  73.                                request,
  74.                                locations=('headers', "json"))
  75.         except Exception as e:
  76.             print(e)
  77.  
  78.         controller = request.app.controllers.EngagementsController
  79.         request_data = await self.get_session_data(request)
  80.         user_id = request_data.get("useracct_id")
  81.  
  82.         if not self.is_update_engagement(request_data):
  83.             result = await controller.patch_upload_template_by_engagement(
  84.                 request,
  85.                 engagement_id,
  86.                 user_id=user_id
  87.             )
  88.         else:
  89.             result = await controller.patch_engagement_by_id(request, engagement_id)
  90.  
  91.         return self.get_response(result)
  92.  
  93.     @staticmethod
  94.     def is_update_engagement(request_data: dict) -> bool:
  95.         is_with_optional_wizard = "entities" in request_data
  96.         is_upload_template = request_data.get("uploadTemplateId", False)
  97.         force_update = util.strtobool(request_data.get("force_update", "False"))
  98.  
  99.         if is_upload_template or force_update or not is_with_optional_wizard:
  100.             return False
  101.  
  102.         if not request_data.get('entities'):
  103.             raise BadRequestError(
  104.                 "The field 'entities' is empty - there is must be at least one entity"
  105.             )
  106.  
  107.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement