Esempio n. 1
0
 def serialize(self, content):
     """convert content to JSON."""
     warnings.warn(
         'use `class Handler(BasicHandler, JsonMixin)`',
         DeprecationWarning,
         stacklevel=2,
     )
     return hujson2.dumps(content)
Esempio n. 2
0
    def note(category, message=None, data=None, typ=None):
        """bei Bedarf strukturiert loggen, Sentry breadcrumbs."""
        assert category in [
            'http',
            'navigation',
            'user',
            'rpc',
            'input',
            'external',
            'storage',
            'auth',
            'flow',
        ]
        if not data:
            data = {}

        # see https://docs.sentry.io/clients/python/breadcrumbs/
        # and https://github.com/getsentry/sentry/blob/master/src/sentry/static/sentry/less/group-detail.less
        # for valid values
        if category == 'auth':
            category == 'user'
            warnings.warn('use `user` instead of `auth`',
                          DeprecationWarning,
                          stacklevel=2)
        if category == 'http':
            category = 'rpc'
            typ = 'http'

        # 'flatten' data
        jsondata = hujson2.dumps(data)
        data = hujson2.loads(jsondata)
        if len(jsondata) > 10000:
            # shorten data
            try:
                if hasattr(data, 'items'):
                    for key, value in data.items():
                        data[key] = value[:200]
                else:
                    data = str(data)[:1024]
            except Exception as e:
                data = {'error': 'data too big', 'exception': str(e)}

        logger.debug('note: %s: %s %r', category, message, data)
        raven.breadcrumbs.record(data=data,
                                 category=category,
                                 type=typ,
                                 message=message)
Esempio n. 3
0
def taskqueue_add_multi_payload(name, url, payloadlist, **kwargs):
    """like taskqueue_add_multi() but transmit a json encoded payload instead a query parameter.

    In the Task handler you can get the data via ``zdata = json.loads(self.request.body)``.
    See http://code.google.com/appengine/docs/python/taskqueue/tasks.html
    """
    tasks = []
    for payload in payloadlist:
        payload = hujson2.dumps(payload)
        payload = zlib.compress(payload)
        tasks.append(taskqueue.Task(url=url, payload=payload, **kwargs))
        # Patch Addition to Taskqueue
        if len(tasks) >= 50:
            taskqueue.Queue(name=name).add(tasks)
            tasks = []
    if tasks:
        taskqueue.Queue(name=name).add(tasks)
    LOGGER.debug('%d tasks queued to %s', len(payloadlist), url)
Esempio n. 4
0
 def serialize(self, content):
     """convert content to JSON."""
     return hujson2.dumps(content)
Esempio n. 5
0
    def post(self):
        r"""Create or update a credential.

        Use it like this

            curl -u $uid:$secret -X POST -F admin=True \
                -F text='fuer das Einspeisen von SoftM Daten' -F email='*****@*****.**' \
                http://example.appspot.com/gaetk2/auth/credentials
            {
             "secret": "aJNKCDUZW5PIBT23LYX7XXVFENA",
             "uid": "u66666o26ec4b"
            }
        """
        # The data can be submitted either as a json encoded body or form encoded
        if self.request.headers.get('Content-Type',
                                    '').startswith('application/json'):
            data = hujson2.loads(self.request.body)
        else:
            data = self.request

        admin = str(data.get('admin', '')).lower() == 'true'
        text = data.get('text', '')
        uid = data.get('uid')
        email = data.get('email')
        permissions = data.get('permissions', '')
        if isinstance(permissions, basestring):
            permissions = permissions.split(',')

        if uid:
            credential = self.get_credential(uid)
        else:
            credential = None

        if credential:
            # if a credential already exists we only have to modify it
            credential.admin = admin
            credential.text = text
            credential.email = email
            credential.permissions = []
            for permission in permissions:
                if permission not in getattr(gaetkconfig,
                                             'ALLOWED_PERMISSIONS', []):
                    raise exc.HTTP400_BadRequest('invalid permission %r' %
                                                 permission)
                credential.permissions.append(permission)
            credential.put()
        else:
            # if not, we generate a new one
            credential = gaetk_Credential.create(admin=admin,
                                                 text=text,
                                                 email=email)

        self.response.headers['Content-Type'] = 'application/json'
        self.response.set_status(201)
        self.response.out.write(
            hujson2.dumps(
                dict(uid=credential.uid,
                     secret=credential.secret,
                     admin=credential.admin,
                     text=credential.text,
                     email=credential.email,
                     permissions=credential.permissions,
                     created_at=credential.created_at,
                     updated_at=credential.updated_at)))