<filename>service/resources/submission.py<gh_stars>1-10
"""Submission module"""
#pylint: disable=too-few-public-methods
import os
import json
from dateutil import tz
import falcon
import jsend
from tasks import bluebeam_export
from .models import create_submission, create_export
from .hooks import validate_access

LOCAL_TZ = tz.gettz("America/Los_Angeles")

@falcon.before(validate_access)
class Submission():
    """Submission class"""

    def on_post(self, _req, resp):
        #pylint: disable=no-self-use,no-member
        """
            Record post into the database
        """
        try:
            request_body = _req.bounded_stream.read()
            json_params = json.loads(request_body)

            # if there is a webhook, check its validity
            # valid if either:
            #   1. webhook has a type that has a corresponding url environment variable
            #   2. webhook has a callback url
            if '_webhook' in json_params:
                if 'type' in json_params['_webhook'] and\
                    not os.environ.get("WEBHOOK_{0}_URL".format(json_params['_webhook']['type'])):
                    raise ValueError("Webhook {0} not supported".format(
                        json_params['_webhook']['type']))
                if 'url' not in json_params['_webhook'] and 'type' not in json_params['_webhook']:
                    raise ValueError("Invalid webhook definition")

            export_obj = create_export(self.session)
            submission = create_submission(self.session, json_params, export_obj.guid)

            bluebeam_export.apply_async(
                args=(export_obj.guid,),
                serializer='pickle'
            )

            resp.status = falcon.HTTP_200
            resp.body = json.dumps(jsend.success({
                'submission_id': submission.id,
                'date_received': submission.date_received.astimezone(LOCAL_TZ).strftime(
                    "%Y/%m/%d, %H:%M:%S"
                )
            }))
        except Exception as err: # pylint: disable=broad-except
            print("error:")
            print("{0}".format(err))
            resp.status = falcon.HTTP_500
            resp.body = json.dumps(jsend.error("{0}".format(err)))

<filename>service/resources/submission.py<gh_stars>1-10
"""Submission module"""
#pylint: disable=too-few-public-methods
import os
import json
from dateutil import tz
import falcon
import jsend
from tasks import bluebeam_export
from .models import create_submission, create_export
from .hooks import validate_access

LOCAL_TZ = tz.gettz("America/Los_Angeles")

@falcon.before(validate_access)
class Submission():
    """Submission class"""

    def on_post(self, _req, resp):
        #pylint: disable=no-self-use,no-member
        """
            Record post into the database
        """
        try:
            request_body = _req.bounded_stream.read()
            json_params = json.loads(request_body)

            # if there is a webhook, check its validity
            # valid if either:
            #   1. webhook has a type that has a corresponding url environment variable
            #   2. webhook has a callback url
            if '_webhook' in json_params:
                if 'type' in json_params['_webhook'] and\
                    not os.environ.get("WEBHOOK_{0}_URL".format(json_params['_webhook']['type'])):
                    raise ValueError("Webhook {0} not supported".format(
                        json_params['_webhook']['type']))
                if 'url' not in json_params['_webhook'] and 'type' not in json_params['_webhook']:
                    raise ValueError("Invalid webhook definition")

            export_obj = create_export(self.session)
            submission = create_submission(self.session, json_params, export_obj.guid)

            bluebeam_export.apply_async(
                args=(export_obj.guid,),
                serializer='pickle'
            )

            resp.status = falcon.HTTP_200
            resp.body = json.dumps(jsend.success({
                'submission_id': submission.id,
                'date_received': submission.date_received.astimezone(LOCAL_TZ).strftime(
                    "%Y/%m/%d, %H:%M:%S"
                )
            }))
        except Exception as err: # pylint: disable=broad-except
            print("error:")
            print("{0}".format(err))
            resp.status = falcon.HTTP_500
            resp.body = json.dumps(jsend.error("{0}".format(err)))

Submission module #pylint: disable=too-few-public-methods Submission class #pylint: disable=no-self-use,no-member Record post into the database # if there is a webhook, check its validity # valid if either: #   1. webhook has a type that has a corresponding url environment variable #   2. webhook has a callback url # pylint: disable=broad-except