Beispiel #1
0
    def __init__(self, output):
        self.content = output

        try:
            self.json = output
            print(self.json)
        except:
            log.exception('ERROR at RWrapper JSON load')
            self.json = {}

        self.code = self.json.get('code', None)

        self.token = self.json.get('token')
        self.form_data = self.json['forms'][
            'model'] if 'forms' in self.json else {}

        if 'object_key' in self.form_data:
            self.object_key = self.form_data['object_key']
        else:
            self.object_key = self.json.get('object_id', None)

        if self.code and int(self.code) >= 400:
            self.raw()
            raise HTTPError(self.code, (self.json.get('title', '') +
                                        self.json.get('description', '') +
                                        self.json.get('error', '')))
Beispiel #2
0
    def process_request(self, req, resp):
        # req.stream corresponds to the WSGI wsgi.input environ variable,
        # and allows you to read bytes from the request body.
        #
        # See also: PEP 3333
        if req.content_length in (None, 0):
            # Nothing to do
            req.context['data'] = req.params.copy()
            req.context['result'] = {}
            return
        else:
            req.context['result'] = {}

        body = req.stream.read()
        if not body:
            raise falcon.HTTPBadRequest('Empty request body',
                                        'A valid JSON document is required.')

        try:
            json_data = body.decode('utf-8')
            req.context['data'] = json.loads(json_data)
            try:
                log.info("REQUEST DATA: %s" % json_data)
            except:
                log.exception("ERR: REQUEST DATA CANT BE LOGGED ")
        except (ValueError, UnicodeDecodeError):
            raise falcon.HTTPError(
                falcon.HTTP_753, 'Malformed JSON',
                'Could not decode the request body. The '
                'JSON was incorrect or not encoded as '
                'UTF-8.')
    def process_request(self, req, resp):
        # req.stream corresponds to the WSGI wsgi.input environ variable,
        # and allows you to read bytes from the request body.
        #
        # See also: PEP 3333
        if req.content_length in (None, 0):
            # Nothing to do
            req.context['data'] = req.params.copy()
            req.context['result'] = {}
            return
        else:
            req.context['result'] = {}

        body = req.stream.read()
        if not body:
            raise falcon.HTTPBadRequest('Empty request body',
                                        'A valid JSON document is required.')

        try:
            json_data = body.decode('utf-8')
            req.context['data'] = json.loads(json_data)
            try:
                log.info("REQUEST DATA: %s" % json_data)
            except:
                log.exception("ERR: REQUEST DATA CANT BE LOGGED ")
        except (ValueError, UnicodeDecodeError):
            raise falcon.HTTPError(falcon.HTTP_753,
                                   'Malformed JSON',
                                   'Could not decode the request body. The '
                                   'JSON was incorrect or not encoded as '
                                   'UTF-8.')
Beispiel #4
0
    def handle_message(self, ch, method, properties, body):
        """
        this is a pika.basic_consumer callback
        handles client inputs, runs appropriate workflows and views

        Args:
            ch: amqp channel
            method: amqp method
            properties:
            body: message body
        """
        input = {}
        headers = {}
        try:
            self.sessid = method.routing_key

            input = json_decode(body)
            data = input['data']

            # since this comes as "path" we dont know if it's view or workflow yet
            # TODO: just a workaround till we modify ui to
            if 'path' in data:
                if data['path'] in VIEW_METHODS:
                    data['view'] = data['path']
                else:
                    data['wf'] = data['path']
            session = Session(self.sessid)

            headers = {'remote_ip': input['_zops_remote_ip'],
                       'source': input['_zops_source']}

            if 'wf' in data:
                output = self._handle_workflow(session, data, headers)
            elif 'job' in data:

                self._handle_job(session, data, headers)
                return
            else:
                output = self._handle_view(session, data, headers)

        except HTTPError as e:
            import sys
            if hasattr(sys, '_called_from_test'):
                raise
            output = {"cmd": "error", "error": self._prepare_error_msg(e.message), "code": e.code}
            log.exception("Http error occurred")
        except:
            self.current = Current(session=session, input=data)
            self.current.headers = headers
            import sys
            if hasattr(sys, '_called_from_test'):
                raise
            err = traceback.format_exc()
            output = {"cmd": "error", "error": self._prepare_error_msg(err), "code": 500}
            log.exception("Worker error occurred with messsage body:\n%s" % body)
        if 'callbackID' in input:
            output['callbackID'] = input['callbackID']
        log.info("OUTPUT for %s: %s" % (self.sessid, output))
        output['reply_timestamp'] = time()
        self.send_output(output)
 def parse_input_data(self, node):
     data = DotDict()
     try:
         for nod in self._get_input_nodes(node):
             data.update(self._parse_input_node(nod))
     except Exception as e:
         log.exception("Error while processing node: %s" % node)
     return data
Beispiel #6
0
 def parse_input_data(self, node):
     data = DotDict()
     try:
         for nod in self._get_input_nodes(node):
             data.update(self._parse_input_node(nod))
     except Exception as e:
         log.exception("Error while processing node: %s" % node)
     return data
Beispiel #7
0
 def on_modified(event):
     if not is_background:
         print("Restarting worker due to change in %s" % event.src_path)
     log.info("modified %s" % event.src_path)
     try:
         kill_children()
         run_children()
     except:
         log.exception("Error while restarting worker")
Beispiel #8
0
    def process_response(self, req, resp, resource):
        if 'result' not in req.context:
            return
        req.context['result']['is_login'] = '******' in req.env['session']
        # print(":::::body: %s\n\n:::::result: %s" % (resp.body, req.context['result']))
        if resp.body is None and req.context['result']:
            resp.body = json.dumps(req.context['result'])


        try:
            log.info("RESPONSE: %s" % resp.body)
        except:
            log.exception("ERR: RESPONSE CANT BE LOGGED ")
Beispiel #9
0
def get_notifications(current):
    """
        Returns last N notifications for current user


        .. code-block:: python

            #  request:
                {
                'view':'_zops_unread_messages',
                'amount': int, # Optional, defaults to 8
                }

            #  response:
                {
                'status': 'OK',
                'code': 200,
                'notifications': [{'title':string,
                                   'body': string,
                                   'channel_key': key,
                                   'type': int,
                                   'url': string, # could be a in app JS URL prefixed with "#" or
                                                  # full blown URL prefixed with "http"
                                   'message_key': key,
                                   'timestamp': datetime},],
                }
        """
    current.output = {
        'status': 'OK',
        'code': 200,
        'notifications': [],
    }
    amount = current.input.get('amount', 8)
    try:
        notif_sbs = current.user.subscriptions.objects.get(channel_id=current.user.prv_exchange)
    except MultipleObjectsReturned:
        # FIXME: This should not happen,
        log.exception("MULTIPLE PRV EXCHANGES!!!!")
        sbs = current.user.subscriptions.objects.filter(channel_id=current.user.prv_exchange)
        sbs[0].delete()
        notif_sbs = sbs[1]
    for msg in notif_sbs.channel.message_set.objects.all()[:amount]:
        current.output['notifications'].insert(0, {
            'title': msg.msg_title,
            'body': msg.body,
            'type': msg.typ,
            'url': msg.url,
            'channel_key': msg.channel.key,
            'message_key': msg.key,
            'timestamp': msg.updated_at})
Beispiel #10
0
    def process_response(self, req, resp, resource):
        if 'result' not in req.context:
            return
        req.context['result']['is_login'] = '******' in req.env['session']
        if settings.DEBUG:
            req.context['result']['_debug_queries'] = sys._debug_db_queries
            sys._debug_db_queries = []
        if resp.body is None and req.context['result']:
            resp.body = json.dumps(req.context['result'])

        try:
            log.debug("RESPONSE: %s" % resp.body)
        except:
            log.exception("ERR: RESPONSE CANT BE LOGGED ")
    def process_response(self, req, resp, resource):
        if 'result' not in req.context:
            return
        req.context['result']['is_login'] = '******' in req.env['session']
        if settings.DEBUG:
            req.context['result']['_debug_queries'] = sys._debug_db_queries
            sys._debug_db_queries = []
        if resp.body is None and req.context['result']:
            resp.body = json.dumps(req.context['result'])

        try:
            log.debug("RESPONSE: %s" % resp.body)
        except:
            log.exception("ERR: RESPONSE CANT BE LOGGED ")
Beispiel #12
0
    def _parse_input_data(self, node):
        """
        Parses inputOutput part camunda modeller extensions.
        Args:
            node: SpiffWorkflow Node object.

        Returns:
            Data dict.
        """
        data = DotDict()
        try:
            for nod in self._get_input_nodes(node):
                data.update(self._parse_input_node(nod))
        except Exception as e:
            log.exception("Error while processing node: %s" % node)
        return data
Beispiel #13
0
def list_channels(current):
    """
        List channel memberships of current user


        .. code-block:: python

            #  request:
                {
                'view':'_zops_list_channels',
                }

            #  response:
                {
                'channels': [
                    {'name': string, # name of channel
                     'key': key,     # key of channel
                     'unread': int,  # unread message count
                     'type': int,    # channel type,
                                     # 15: public channels (chat room/broadcast channel distinction
                                                         comes from "read_only" flag)
                                     # 10: direct channels
                                     # 5: one and only private channel which is "Notifications"
                     'read_only': boolean,
                                     # true if this is a read-only subscription to a broadcast channel
                                     # false if it's a public chat room

                     'actions':[('action name', 'view name'),]
                    },]
                }
        """
    current.output = {
        'status': 'OK',
        'code': 200,
        'channels': []}
    for sbs in current.user.subscriptions.objects.filter(is_visible=True):
        try:
            current.output['channels'].append(sbs.get_channel_listing())
        except ObjectDoesNotExist:
            # FIXME: This should not happen,
            log.exception("UNPAIRED DIRECT EXCHANGES!!!!")
            sbs.delete()
Beispiel #14
0
    def __init__(self, *args):
        self.content = list(args[0])
        self.code = args[1]
        self.headers = list(args[2])
        try:
            self.json = json.loads(self.content[0].decode('utf-8'))
        except:
            log.exception('ERROR at RWrapper JSON load')
            self.json = {}

        self.token = self.json.get('token')

        if int(self.code[:3]) >= 400:
            self.raw()
            if self.code in CODE_EXCEPTION:
                raise CODE_EXCEPTION[self.code](title=self.json.get('title'),
                                                description=self.json.get('description'))
            else:
                raise falcon.HTTPError(title=self.json.get('title'),
                                       description=self.json.get('description'))
Beispiel #15
0
    def stc(self, response, request=None):
        """
        STC means Success Test Callback. Looks for 200 or 201 codes in response code.

        Args:
            response:
            request:
        """
        try:
            if not response['code'] in (200, 201):
                print("FAILED: Response not successful: \n")
                if not self.process_error_reponse(response):
                    print("\nRESP:\n%s")
                print("\nREQ:\n %s" % (response, request))
            else:
                return True
        except Exception as e:
            log.exception(
                "\n===========>\nFAILED API REQUEST\n<===========\n%s\n" % e)
            log.info("Response: \n%s\n\n" % response)
Beispiel #16
0
    def process_response(self, req, resp, resource):
        """
        Serializes ``req.context['result']`` to resp.body as JSON.

        If :attr:`~zengine.settings.DEBUG` is True,
        ``sys._debug_db_queries`` (set by pyoko) added to response.

        """
        if 'result' not in req.context:
            return
        req.context['result']['is_login'] = '******' in req.env['session']
        if settings.DEBUG:
            req.context['result']['_debug_queries'] = sys._debug_db_queries
            sys._debug_db_queries = []
        if resp.body is None and req.context['result']:
            resp.body = json.dumps(req.context['result'])

        try:
            log.debug("RESPONSE: %s" % resp.body)
        except:
            log.exception("ERR: RESPONSE CANT BE LOGGED ")
Beispiel #17
0
def unread_count(current):
    """
        Number of unread messages for current user


        .. code-block:: python

            #  request:
                {
                'view':'_zops_unread_count',
                }

            #  response:
                {
                'status': 'OK',
                'code': 200,
                'notifications': int,
                'messages': int,
                }
        """
    unread_ntf = 0
    unread_msg = 0
    for sbs in current.user.subscriptions.objects.filter(is_visible=True):
        try:
            if sbs.channel.key == current.user.prv_exchange:
                unread_ntf += sbs.unread_count()
            else:
                unread_msg += sbs.unread_count()
        except ObjectDoesNotExist:
            # FIXME: This should not happen,
            log.exception("MULTIPLE PRV EXCHANGES!!!!")
            sbs.delete()
    current.output = {
        'status': 'OK',
        'code': 200,
        'notifications': unread_ntf,
        'messages': unread_msg
    }
Beispiel #18
0
 def list_view(self):
     # TODO: add pagination
     # TODO: investigate and if neccessary add sequrity/sanity checks for search params
     brief = 'brief' in self.input
     query = self.object.objects.filter()
     if 'filters' in self.input:
         query = query.filter(**self.input['filters'])
     self.output['client_cmd'] = 'list_objects'
     self.output['nobjects'] = []
     self.output['objects'] = []
     if self.object.Meta.list_fields and not brief:  # add list headers
         list_headers = []
         for f in self.object.Meta.list_fields:
             if callable(getattr(self.object, f)):
                 list_headers.append(getattr(self.object, f).title)
             else:
                 list_headers.append(self.object._fields[f].title)
         self.output['nobjects'].append(list_headers)
     make_it_brief = brief or not self.object.Meta.list_fields
     if make_it_brief:
         self.output['nobjects'].append('-1')
     for obj in query:
         if ('deleted_obj' in self.current.task_data and self.current.task_data[
             'deleted_obj'] == obj.key):
             del self.current.task_data['deleted_obj']
             continue
         self.output['nobjects'].append(self.get_list_obj(obj, make_it_brief))
         self.output['objects'].append({"data": obj.clean_field_values(), "key": obj.key})
     if 'added_obj' in self.current.task_data:
         try:
                 new_obj = self.object.objects.get(self.current.task_data['added_obj'])
                 self.output['nobjects'].insert(1, self.get_list_obj(new_obj, make_it_brief))
                 self.output['objects'].insert(0, {"data": new_obj.clean_field_values(), "key": new_obj.key})
         except:
             log.exception("ERROR while adding newly created object to object listing")
         del self.current.task_data['added_obj']
     self.output
Beispiel #19
0
    def check_for_lane_permission(self):
        """
        One or more permissions can be associated with a lane
        of a workflow. In a similar way, a lane can be
        restricted with relation to other lanes of the workflow.

        This method called on lane changes and checks user has
        required permissions and relations.

        Raises:
             HTTPForbidden: if the current user hasn't got the
              required permissions and proper relations

        """
        # TODO: Cache lane_data in app memory
        if self.current.lane_permission:
            log.debug("HAS LANE PERM: %s" % self.current.lane_permission)
            perm = self.current.lane_permission
            if not self.current.has_permission(perm):
                raise HTTPError(
                    403, "You don't have required lane permission: %s" % perm)

        if self.current.lane_relations:
            context = self.get_pool_context()
            log.debug("HAS LANE RELS: %s" % self.current.lane_relations)
            try:
                cond_result = eval(self.current.lane_relations, context)
            except:
                log.exception("CONDITION EVAL ERROR : %s || %s" %
                              (self.current.lane_relations, context))
                raise
            if not cond_result:
                log.debug("LANE RELATION ERR: %s %s" %
                          (self.current.lane_relations, context))
                raise HTTPError(
                    403, "You aren't qualified for this lane: %s" %
                    self.current.lane_relations)