예제 #1
0
파일: api.py 프로젝트: selimppc/jasminsms
 def call_jasmin(self, url, params=None):
     try:
         r = requests.get('%s/%s' % (old_api_uri, url), params=params)
     except requests.exceptions.ConnectionError as e:
         raise falcon.HTTPInternalServerError(
             'Jasmin httpapi connection error',
             'Could not connect to Jasmin http api (%s): %s' %
             (old_api_uri, e))
     except Exception as e:
         raise falcon.HTTPInternalServerError(
             'Jasmin httpapi unknown error', str(e))
     else:
         return r.status_code, r.content.strip('"')
예제 #2
0
 def __init__(self,
              schema,
              target='content',
              title='Request data failed validation'):
     if isinstance(schema, str):
         self.schema = yaml.safe_load(schema)
     elif isinstance(schema, dict):
         self.schema = schema
     else:
         raise falcon.HTTPInternalServerError('Bad JSON schema')
     if target not in ['content', 'params']:
         raise falcon.HTTPInternalServerError('Invalid schema target')
     self.target = target
     self.title = title
예제 #3
0
    def on_get(self, req, resp, action, conn_id):
        # Retrieve URL
        web_server_url = self.retrieve_config('base', 'web_server')

        if 'Error' in web_server_url:
            resp.status = falcon.HTTP_500
            raise falcon.HTTPInternalServerError("Internal Server Error",
                                                 "Missing Configuration File")
        else:
            if action == 'delete':
                # Form the request URL towards Airflow
                req_url = '{}/admin/rest_api/api?api=connections&delete=true&conn_id={}'.format(
                    web_server_url, conn_id)
            else:
                self.return_error(
                    resp, falcon.HTTP_400,
                    'Invalid Paremeters for Deleting Airflow Connection')
                return

            response = requests.get(req_url).json()

            # Return output
            if response["output"]["stderr"]:
                resp.status = falcon.HTTP_400
                resp.body = response["output"]["stderr"]
                return
            else:
                resp.status = falcon.HTTP_200
                resp.body = response["output"]["stdout"]
예제 #4
0
    def on_patch(self, req, resp):
        """Method takes either sparql statement or predicate and object 
        and updates the Resource.

        Args:
            req -- Request
            resp -- Response
        """
        doc_uuid = req.get_param('uuid')
        if not doc_uuid:
            raise falcon.HTTPMissingParam('uuid')
        predicate = req.get_param('predicate') or None
        if not predicate:
            raise falcon.HTTPMissingParam('predicate')
        object_ = req.get_param('object') or None
        if not object_:
            raise falcon.HTTPMissingParam('object')
        doc_type = req.get_param('doc_type') or None
        if self.__update__(doc_id=doc_uuid,
                           doc_type=doc_type,
                           field=predicate,
                           value=object_):
            resp.status = falcon.HTTP_202
            resp.body = json.dumps(True)
        else:
            raise falcon.HTTPInternalServerError(
                "Error with PATCH for {}".format(doc_uuid),
                "Failed setting {} to {}".format(predicate, object_))
예제 #5
0
    def on_post(self, req, resp):
        """Creates a diary based on a PDF template"""
        LOGGER = logging.getLogger()

        resp.set_header('Content-Type', 'text/json')

        raw_json = req.stream.read().decode('utf-8')
        content = json.loads(raw_json, encoding='utf-8')

        try:
            # Parse parameters
            pdf_template = content.get("pdf_template")
            pages = int(content.get("pages"))
            starting_date = content.get("date")
            email = content.get("email")
            font = content.get("font")

            a4_diary = create.create_a4_diary(pdf_template,
                                              pages,
                                              starting_date,
                                              email=email,
                                              font=font)

            a5_booklet = create.convert_to_a5_booklet(a4_diary)
            resp.body = json.dumps([str(a4_diary), str(a5_booklet)])
            LOGGER.info("Document created {}".format(pdf_template))
        except Exception as e:
            LOGGER.error("Error creating document", exc_info=True)
            raise falcon.HTTPInternalServerError(
                title="Error creating document: " + str(type(e)),
                description=(str(e) +
                             ','.join(traceback.format_tb(e.__traceback__))))
예제 #6
0
 def _serialize_json_to_string(self, resp):
     if self._has_json(resp):
         if not isinstance(resp.json, dict) and \
            not isinstance(resp.json, list):
             raise falcon.HTTPInternalServerError()
         return json.dumps(resp.json)
     return json.dumps({})
예제 #7
0
    async def wrapper(self, req, resp, *args, **kwargs):
        if req_schema is not None:
            m = await req.get_media()

            try:
                jsonschema.validate(m,
                                    req_schema,
                                    format_checker=jsonschema.FormatChecker())
            except jsonschema.ValidationError as e:
                raise falcon.HTTPBadRequest('Request data failed validation',
                                            description=e.message)

        result = await func(self, req, resp, *args, **kwargs)

        if resp_schema is not None:
            try:
                jsonschema.validate(resp.media,
                                    resp_schema,
                                    format_checker=jsonschema.FormatChecker())
            except jsonschema.ValidationError:
                raise falcon.HTTPInternalServerError(
                    'Response data failed validation'
                    # Do not return 'e.message' in the response to
                    # prevent info about possible internal response
                    # formatting bugs from leaking out to users.
                )

        return result
예제 #8
0
파일: server.py 프로젝트: ctreese/clueless
    def on_get(self, req, resp, player_id):

        if not self.gs.gameStarted:
            raise falcon.HTTPInternalServerError(
                "Game Not Started",
                "Please start the game before issuing commands")
        if (player_id not in self.gs.get_activePlayers()):
            raise falcon.HTTPBadRequest(
                "Invalid Player Name",
                "That player name is not currently playing")

        output = {'characters': [], 'weapons': [], 'rooms': []}
        for card in self.gs.players[player_id].hand:
            if card.type is 1:
                output['characters'].append(card.name)
            elif card.type is 2:
                output['weapons'].append(card.name)
            elif card.type is 3:
                output['rooms'].append(card.name)
            else:
                raise falcon.HTTPBadRequest("Invalid Card Used", )

        resp.set_header('Powered-By', 'Falcon')
        resp.body = json.dumps(output)
        resp.status = falcon.HTTP_200
예제 #9
0
    def process_response(self, req, resp, resource, req_succeeded):
        if 'result' not in req.context or (resp.data and len(resp.data) > 0):
            # If there's no response to process or a different middleware set the response to something, do nothing in this one.
            return

        result = req.context['result']
        if 'data' in result and not 'meta' in result:
            resp.body = json.dumps(result['data'])
        else:
            resp.body = json.dumps(result)

        schema = _get_response_schema(resource, req)
        if schema is None:
            return

        try:
            jsonschema.validate(req.context['result'], schema)
        except jsonschema.exceptions.ValidationError as error:
            method_name = {
                'POST': 'on_post',
                'PUT': 'on_put',
                'PATCH': 'on_patch',
                'GET': 'on_get',
                'DELETE': 'on_delete'
            }[req.method]
            self.logger.error(
                'Blocking proposed response from being sent from {0}.{1}.{2} to client as it does not match the defined schema: {3}'
                .format(resource.__module__, resource.__class__.__name__,
                        method_name, str(error)))
            raise falcon.HTTPInternalServerError('Internal Server Error',
                                                 'Undisclosed')
예제 #10
0
    def process_request(self, req, resp):
        token = req.get_header('Authorization')

        if not token:
            raise falcon.HTTPUnauthorized('Auth token required',
                                          '',
                                          '',
                                          href='http://docs.example.com/auth')

        auth_headers = {'Authorization': token}
        auth_response = auth_client.post(headers=auth_headers)

        if not auth_response:
            raise falcon.HTTPInternalServerError('Server error', 'There was a server error')

        if auth_response.status_code == 401:
            raise falcon.HTTPUnauthorized('Authorization failed',
                                          '',
                                          '',
                                          href='http://docs.example.com/auth')

        user_details = auth_response.json()

        if not self._has_permission(user_details):
            metrics.incr('authorization.permission_denied')
            description = 'You do not have permission to access this resource.'

            raise falcon.HTTPForbidden('Permission denied',
                                       description,
                                       href='http://docs.example.com/auth')

        metrics.incr('authorization.authorization_success')

        req.context['auth_header'] = auth_headers
        req.context['user_details'] = user_details
예제 #11
0
파일: util.py 프로젝트: dilipbm/falcon_ask
def dispatch_request(req):
    # Check if req.stream is consumed, otherwise get POST data.
    body = req.context.get('doc') or _get_json_body(req)
    logger.debug('Got body: %s' % body)

    req_type = get_req_type(body)
    intent_maps = req.context['intent_maps']

    if req_type == 'LaunchRequest':
        return req.context['welcome']
    elif req_type == 'IntentRequest':
        intent = body['request']['intent']['name']
        try:
            intent_fn = intent_maps[intent]
        except KeyError:
            logger.exception(
                'No intent function mapping found for intent: %s' % intent)
            try:
                intent_fn = intent_maps['UnknownIntent']
            except KeyError:
                logger.exception('UnknownIntent mapping also not found.')
                raise falcon.HTTPInternalServerError()
        return intent_fn(body)
    elif req_type == 'SessionEndedRequest':
        logger.info('SessionEndedRequest received. Session ended.')
        return None

    logger.error('Invalid request type: %s', req_type)
    raise falcon.HTTPBadRequest()
예제 #12
0
    def on_post(self,
                req,
                resp,
                dag_id,
                run_id=None,
                conf=None,
                execution_date=None):
        # Retrieve URL
        web_server_url = self.retrieve_config('base', 'web_server')

        if 'Error' in web_server_url:
            resp.status = falcon.HTTP_500
            raise falcon.HTTPInternalServerError("Internal Server Error",
                                                 "Missing Configuration File")
        else:
            req_url = '{}/api/experimental/dags/{}/dag_runs'.format(
                web_server_url, dag_id)

            response = requests.post(req_url,
                                     json={
                                         "run_id": run_id,
                                         "conf": conf,
                                         "execution_date": execution_date,
                                     })

            if response.ok:
                resp.status = falcon.HTTP_200
            else:
                self.return_error(resp, falcon.HTTP_400, 'Fail to Execute Dag')
                return
예제 #13
0
    def __add_stub__(self, label, namespace, pid=None):
        """Internal method takes label, namespace, and an optional pid
        creates a new Islandora Object and returns JSON result

        Args:
            label -- Label for new Islandora Object
            namespace -- Namespace for repository
            pid -- Optional pid
        """
        data = {"label": label} 
        if pid:
            data['pid'] = pid
        else:
            data["namespace"] = namespace
        add_url = "{}/islandora/rest/v1/object".format(self.base_url)
        add_object_result = requests.post(add_url, data=data, auth=self.auth)
        if add_object_result.status_code > 399:
            print("Failed to add {} data={} auth={}".format(add_url, data, self.auth))
            raise falcon.HTTPInternalServerError(
                "Failed to add Islandora object",
                "Failed with url={}, islandora status code={}\n{}".format(
                    add_url,
                    add_object_result.status_code,
                    add_object_result.text))
        return add_object_result.json()
예제 #14
0
 def on_post(self, req, resp, pid, dsid=None):
     env = req.env
     env.setdefault('QUERY_STRING', '')
     form = cgi.FieldStorage(fp=req.stream, environ=env)
     file_item = form['userfile']
     data = {"dsid": dsid or "FILE_UPLOAD"}
     if "state" in form:
         data["state"] = form["state"].value
     else:
         data["state"] = "A"
     if "control_group" in form:
         data["controlGroup"] = form['control_group'].value
     else:
          data["controlGroup"] = "M"
     if "label" in form:
         data["label"] = form["label"].value
     else:
         data["label"] = file_item.name
     if "mime_type" in form:
         data["mimeType"] = form["mime_type"].value
     else:
         data["mimeType"] = 'application/octet-stream'
     if self.__add__(data, file_item.file, pid, dsid):
         resp.status = falcon.HTTP_201
         resp.body = json.dumps({
             "message": "Added {} datastream to {}".format(dsid, pid)})
     else:
         desc = "{} datastream was not added object".format(dsid)
         desc += "{}\nusing POST URL={}".format(pid, self.rest_url)
         raise falcon.HTTPInternalServerError(
             "Failed to add datastream to object",
             desc)
예제 #15
0
    def on_get(self, req, resp, customer_id):

        try:

            # This is the parameter which is going to get passed.
            customerId = customer_id
            customerPersonalInfo = self.__getCustomerPersonInfo(customerId)

            resp.status = falcon.HTTP_200
            resp.content_type = falcon.MEDIA_JSON
            resp.body = json.dumps({
                "result": "success",
                "details": customerPersonalInfo
            })

        except Exception as error:

            print(error)
            resp.status = falcon.HTTP_500
            resp.body = json.dumps({
                "result":
                "failed",
                "details":
                falcon.HTTPInternalServerError("Unable to create task")
            })
예제 #16
0
    def on_post(self,req,resp):
        """
        Handle POST requests.
        """
        username = req.media.get('username')
        password = req.media.get('password')

        # Check if parameters not empty
        if None in (username, password):
            raise falcon.HTTPBadRequest('Bad Request', 'Invalid Parameters')

        # Hash user password
        hashed = pbkdf2_sha256.encrypt(password, rounds=200000, salt_size=16)

        # Now create main user for account
        user = User(username=username, password=hashed)

        self.db_conn.add(user)

        # Attempt database changes commit
        try:
            # Create User
            self.db_conn.commit()

        except SQLAlchemyError as e:

            # Rollback Changes
            self.db_conn.rollback()

            # Send error
            logger.error(f'Database Error: (Code: {e.orig.args[0]} Message: {e.orig.args[1]})')
            raise falcon.HTTPInternalServerError('Internal Server Error', 'An error ocurred while communicating with the database.')

        resp.media = {'success': 'user_created'}
        resp.status = falcon.HTTP_201
예제 #17
0
파일: wsgi.py 프로젝트: nghiadt16/CALplus
 def _error_handler(self, exc, request, response, params):
     """Handler error"""
     if isinstance(exc, falcon.HTTPError):
         raise exc
     LOG.exception(exc)
     raise falcon.HTTPInternalServerError('Internal server error',
                                          six.text_type(exc))
예제 #18
0
파일: app.py 프로젝트: nwfsc-fram/warehouse
def handle_http_error(exception, req, resp, params):
    """
    Simple Falcon responder to log exceptions, before responding to web client

    >>> normal_exception = falcon.HTTPNotFound(description='friendly msg')
    >>> normal_exception.__cause__ = Exception('technical info')
    >>> handle_http_error( normal_exception, None, None, None)
    Traceback (most recent call last):
      ...
    falcon.errors.HTTPNotFound
    >>> unhandled_exception = Exception("I was not expected!")
    >>> handle_http_error( unhandled_exception, None, None, None)
    Traceback (most recent call last):
      ...
    falcon.errors.HTTPInternalServerError
    """
    if not isinstance(exception, falcon.HTTPError):
        #Warning! exception is not an HTTP response, be careful not to expose
        # security sensitive internal information
        logging.exception( Exception("Unsanitized exception occurred!", exception))#TODO: make this a locally defined class
        sanitized = falcon.HTTPInternalServerError(title='500',description="Please try again")
        raise get_datatables_editor_exception(sanitized)
    # else, intentionally raised (safe) web error
    #internally log cause
    if exception.__cause__:
        logging.exception(exception.__cause__)
    logging.exception(exception) #Falcon stack does not seem to include cause.
    # raise safe exception to client
    raise get_datatables_editor_exception(exception)
예제 #19
0
파일: server.py 프로젝트: ctreese/clueless
    def on_post(self, req, resp, player_id):
        resp.set_header('Powered-By', 'Falcon')
        #resp.body = '{}'

        if len(self.gs.playerList) < 2:
            raise falcon.HTTPInternalServerError(
                "Insufficent Players Registered",
                "Not enough players are currently registered.  Please register at least 2 players."
            )
        elif (player_id not in self.gs.get_playerlist()):
            raise falcon.HTTPBadRequest(
                "Invalid Player Name",
                "That player name is not currently playing")
        if not self.gs.gameStarted:
            character_list = ''
            self.gs.init_gamestate()
            resp.body = json.dumps(
                {'info': " ".join(self.gs.playerListActive)})
            resp.status = falcon.HTTP_201
            print("The suspect is: " + self.gs.board.caseFile.suspect.name)
            print("The room is: " + self.gs.board.caseFile.room.name)
            print("The weapon is: " + self.gs.board.caseFile.weapon.name,
                  flush=True)
        else:
            resp.body = json.dumps({'info': "Game has started already."})
            resp.status = falcon.HTTP_200
예제 #20
0
    def on_get(self, req, resp):
        token = req.get_param('token', True)
        data = {}
        for key in self.data_keys:
            data[key] = req.get_param(key, True)

        if not self.validate_token(token, data):
            raise falcon.HTTPForbidden('Invalid token for these given values',
                                       '')

        endpoint = self.config['iris']['hook']['gmail_one_click']

        try:
            result = self.iclient.post(endpoint, data)
        except MaxRetryError:
            logger.exception('Hitting iris-api failed for gmail oneclick')
        else:
            if result.status == 204:
                resp.status = falcon.HTTP_204
                return
            else:
                logger.error(
                    'Unexpected status code from api %s for gmail oneclick',
                    result.status)

        raise falcon.HTTPInternalServerError('Internal Server Error',
                                             'Invalid response from API')
예제 #21
0
    def on_get(self, req, resp):
        """
        Handles GET requests for ELB HealthCheck Resource
        :param req:
        :param resp:
        :return:
        """
        uptime = int(time.time()) - self.start_time

        if self.load_balancer.check_if_model_to_workers_map_is_empty():
            resp.status = falcon.HTTP_500
            resp.body = "Model To Workers Map is Empty"
            raise falcon.HTTPInternalServerError(
                'Internal Server Error', 'Model To Workers Map is Empty! ')

        resp.status = falcon.HTTP_200
        count = self.load_balancer.model_id_request_count

        # TODO requests and capacity have to be calculated. They are hardcoded for now

        resp.body = json.dumps({
            'uptime': uptime,
            'requests': count,
            'capacity': 100
        })
예제 #22
0
    def process_resource(self, req, resp, resource, params):
        if not isinstance(resource, self.resource):
            return

        allowed_methods = ['POST']
        if req.method not in allowed_methods:
            raise falcon.HTTPMethodNotAllowed(allowed_methods)

        # TODO: Validate application ID if intended for this service.

        if self.validate:
            body = req.context.get('doc') or util._get_json_body(req)
            valid_cert = self.validate_request_certificate(req, body)
            valid_ts = self.validate_request_timestamp(body)
            if not valid_cert or not valid_ts:
                logger.error('Failed to validate request.')
                raise falcon.HTTPForbidden()

        welcome = getattr(resource, 'welcome', None)
        req.context['welcome'] = welcome

        intent_maps = getattr(resource, 'intent_maps', None)
        if intent_maps is None:
            logger.error('Missing attribute "intent_maps" in resource.')
            raise falcon.HTTPInternalServerError()
        req.context['intent_maps'] = intent_maps
예제 #23
0
    def on_post(self, req, resp):
        """
        Encodes a diary (tif or zip file) based on a rubric (created in the web interface) and a blank 
        page of the diary (tif or zip file) present in TEMPLATE_DIR
        """
        LOGGER = logging.getLogger()

        resp.set_header('Content-Type', 'text/json')
        raw_json = req.stream.read().decode('utf-8')
        content = json.loads(raw_json, encoding='utf-8')

        try:
            rubric = content.get("rubric")
            diary_path = content.get("diary")
            date = content.get("date")

            encoded_diary = encode.encode_diary(diary_path, TEMPLATE_DIR,
                                                rubric, date)
            resp.body = json.dumps(str(encoded_diary))
            LOGGER.info("Document encoded {}".format(encoded_diary.stem))
        except Exception as e:
            LOGGER.error("Error encoding document", exc_info=True)
            raise falcon.HTTPInternalServerError(
                title="Error encoding document: " + str(type(e)),
                description=(str(e) +
                             ','.join(traceback.format_tb(e.__traceback__))))
예제 #24
0
    def process_response(self, req, resp, resource):
        if 'result' not in req.context:
            return

        resp.body = json.dumps(req.context['result'])

        schema = _get_response_schema(resource, req)
        if schema is None:
            return

        try:
            schema.validate(req.context['result'])
        except jsonschema.exceptions.ValidationError as error:
            method_name = {
                'POST': 'on_post',
                'PUT': 'on_put',
                'PATCH': 'on_patch',
                'GET': 'on_get',
                'DELETE': 'on_delete'
            }[req.method]
            self.logger.error(
                'Blocking proposed response from being sent from {0}.{1}.{2} to client as it does not match the defined schema: {3}'
                .format(resource.__module__, resource.__class__.__name__,
                        method_name, str(error)))
            raise falcon.HTTPInternalServerError('Internal Server Error',
                                                 'Undisclosed')
예제 #25
0
    def handle(cls, ex, req, resp, params):  # pylint: disable=W0613
        """
        Rollbacks the DB Session.

        In addition, handles the exceptions, logs it and raises the proper status code response.
        """
        DBSESSION.rollback()

        logger = logging.getLogger()
        log_msg = f"{req.method} for {req.relative_uri} {cls.get_payload(req)}"

        if isinstance(ex, falcon.HTTPError):
            logger.info(
                f"Treated error during: {log_msg}. Response status = {ex.status}"
            )
            pass
        elif isinstance(ex, ModelExceptions):
            logger.info(
                f"Treated error during: {log_msg}. Response status = 400")
            ex = falcon.HTTPBadRequest(description=ex.args)
        elif isinstance(ex, ObjectNotFound):
            logger.info(
                f"Treated error during: {log_msg}. Response status = 404")
            ex = falcon.HTTPNotFound(description=ex.args)
        elif isinstance(ex, IntegrityError):
            logger.exception(
                f"Database integrity error during: {log_msg}. Response status = 500"
            )
            ex = falcon.HTTPBadRequest()
        else:
            logger.exception(
                f"Unexpected error during: {log_msg}. Response status = 500")
            ex = falcon.HTTPInternalServerError()
        raise ex
예제 #26
0
def on_post(self, request, response):
    request._parse_form_urlencoded()
    user = None
    if request.auth:
        user = authenticate_by_token(request.auth)

    if user is None:
        raise falcon.HTTPUnauthorized('Login required', 'Please login',
                                      ['Basic realm="Login Required"'])

    if 'owner' not in request.params:
        request.params['owner'] = user.id
    new_item = self.model(**request.params)
    if user.can('read', new_item):
        with db.atomic():
            try:
                new_item.save()
            except:
                raise falcon.HTTPInternalServerError('Internal error', 'The \
    requested operation cannot be completed')
        response.status = falcon.HTTP_CREATED
        path = '{}/{}'.format(request.path, new_item.id)
        s = hinder(new_item.__dict__['_data'], path=path)
        response.body = json.dumps(s)
    else:
        raise falcon.HTTPForbidden('Forbidden access', 'You do not have the \
required permissions for this action')
예제 #27
0
    def on_get(self, req, resp, action, conn_id, protocol, host, port):
        # Retrieve URL
        web_server_url = self.retrieve_config('base', 'web_server')

        if 'Error' in web_server_url:
            resp.status = falcon.HTTP_500
            raise falcon.HTTPInternalServerError("Internal Server Error",
                                                 "Missing Configuration File")
        else:
            if action == 'add':
                # Concatenate to form the connection URL
                netloc = ''.join([host, ':', port])
                url = (protocol, netloc, '', '', '')
                conn_uri = urlparse.urlunsplit(url)

                # Form the request URL towards Airflow
                req_url = '{}/admin/rest_api/api?api=connections&add=true&conn_id={}&conn_uri={}'.format(
                    web_server_url, conn_id, conn_uri)
            else:
                self.return_error(
                    resp, falcon.HTTP_400,
                    'Invalid Paremeters for Adding Airflow Connection')
                return

            response = requests.get(req_url).json()

            # Return output
            if response["output"]["stderr"]:
                resp.status = falcon.HTTP_400
                resp.body = response["output"]["stderr"]
                return
            else:
                resp.status = falcon.HTTP_200
                resp.body = response["output"]["stdout"]
예제 #28
0
def on_patch_resource(self, request, response, id=0):
    user = None
    if request.auth:
        user = authenticate_by_token(request.auth)

    if user is None:
        raise falcon.HTTPUnauthorized('Login required', 'Please login',
                                      ['Basic realm="Login Required"'])

    try:
        item = self.model.get(getattr(self.model, 'id') == id)
    except:
        description = 'The resource you are looking for does not exist'
        raise falcon.HTTPNotFound(title='Not found', description=description)

    if user.can('edit', item):
        stream = request.stream.read().decode('UTF-8')
        parsed_stream = json.loads(stream)
        for key in parsed_stream:
            setattr(item, key, parsed_stream[key])
        with db.atomic():
            try:
                item.save()
            except:
                raise falcon.HTTPInternalServerError('Internal error', 'The \
    requested operation cannot be completed')
        item_dict = item_to_dictionary(self.model, item)
        s = hinder(item_dict, path=request.path)
        response.body = json.dumps(s)
    else:
        raise falcon.HTTPForbidden('Forbidden access', 'You do not have the \
required permissions for this action')
    def on_post(self, req, resp, *args, **kwargs):
        super(ResourceDeleteUserToken, self).on_post(req, resp, *args,
                                                     **kwargs)

        current_user = req.context["auth_user"]
        selected_token_string = req.media["token"]
        selected_token = self.db_session.query(UserToken).filter(
            UserToken.token == selected_token_string).one_or_none()

        if selected_token is not None:
            if selected_token.user.id == current_user.id:
                try:
                    self.db_session.delete(selected_token)
                    self.db_session.commit()

                    resp.status = falcon.HTTP_200
                except Exception as e:
                    mylogger.critical("{}:{}".format(
                        messages.error_removing_user_token, e))
                    raise falcon.HTTPInternalServerError()
            else:
                raise falcon.HTTPUnauthorized(
                    description=messages.token_doesnt_belongs_current_user)
        else:
            raise falcon.HTTPUnauthorized(description=messages.token_not_found)
예제 #30
0
def on_delete_resource(self, request, response, id=0):
    user = None
    if request.auth:
        user = authenticate_by_token(request.auth)

    if user is None:
        raise falcon.HTTPUnauthorized('Login required', 'Please login',
                                      ['Basic realm="Login Required"'])

    try:
        item = self.model.get(getattr(self.model, 'id') == id)
    except:
        description = 'The resource you are looking for does not exist'
        raise falcon.HTTPNotFound(title='Not found', description=description)

    if user.can('eliminate', item):
        try:
            with db.atomic():
                item.delete_instance()
        except:
            raise falcon.HTTPInternalServerError('Internal error', 'The \
requested operation cannot be completed')
        response.status = falcon.HTTP_NO_CONTENT
    else:
        raise falcon.HTTPForbidden('Forbidden access', 'You do not have the \
required permissions for this action')