Example #1
0
 def routing(self, environ):
     # according to PEP 3333 the native string representing PATH_INFO
     # (and others) can only contain unicode codepoints from 0 to 255,
     # which is why we need to decode to latin-1 instead of utf-8 here.
     # We transform it back to UTF-8
     path_info = environ['PATH_INFO'].encode('latin-1').decode('utf-8')
     route = self.lookup(path_info, environ)
     if route is NotFound:
         return reply(404)
     if route is NotSupported:
         return reply(405)
     return self.process_endpoint(environ, route)
Example #2
0
 def search(self, environ, overhead):
     # Create
     query = overhead.data['search']
     listing = []
     with SQLAlchemySession(overhead.engine) as session:
         for user in session.query(User).filter(User.nname == query).all():
             listing.append(
                 dict(
                     oid=str(user.oid),
                     vname=user.vname,
                     nname=user.nname,
                 ))
     return reply(200,
                  text=json.dumps(listing, sort_keys=True),
                  content_type="application/json")
     return reply(200, text="ALL GOOD", content_type="application/json")
Example #3
0
        def method_validation(*args):
            if isinstance(args[0], View):
                inst, environ, overhead = args
            else:
                inst = None
                environ, overhead = args

            extracted, errors = self.process_action(environ)
            if errors:
                return reply(400,
                             text=json.dumps(errors),
                             content_type="application/json")

            else:
                if self.as_dict:
                    result = extracted
                else:
                    names = tuple((field['model'] for field in self.fields))
                    DataClass = namedtuple(action.__name__, names)
                    result = DataClass(**extracted)

                if overhead is None:
                    overhead = result
                else:
                    assert isinstance(overhead, BaseOverhead)
                    overhead.set_data(result)

                if inst is not None:
                    return action(inst, environ, overhead)
                return action(environ, overhead)

            return result
Example #4
0
 def permissive_options(environ, overhead):
     r = reply(204)
     r.headers["Access-Control-Allow-Credentials"] = "true"
     r.headers["Access-Control-Allow-Methods"] = ",".join(allowed)
     r.headers["Access-Control-Allow-Headers"] = (
         "Authorization, Content-Type, X-Requested-With")
     return r
Example #5
0
 def helper(self, environ, overhead):
     # Create
     with SQLAlchemySession(overhead.engine) as session:
         print(session)
         session.query(User).all()
         import pdb
         pdb.set_trace()
         print("all good")
     return reply(200, text="ALL GOOD", content_type="application/json")
Example #6
0
 def create(self, environ, overhead):
     with SQLAlchemySession(overhead.engine) as session:
         user = User(**overhead.data)
         session.add(user)
     listing = {
         'status': 'ok',
     }
     return reply(200,
                  text=json.dumps(listing),
                  content_type="application/json")
Example #7
0
 def list(self, environ, overhead):
     with SQLAlchemySession(overhead.engine) as session:
         listing = [
             dict(oid=str(user.oid),
                  vname=user.vname.strip(),
                  nname=user.nname.strip())
             for user in session.query(User).slice(0, 5000).all()
         ]
     return reply(200,
                  text=json.dumps(listing, sort_keys=True),
                  content_type="application/json")
Example #8
0
    def delete(self, environ, overhead):
        # delete

        with SQLAlchemySession(overhead.engine) as session:
            session.query(User).get(overhead.parameters['id']).delete()
        listing = {
            'status': 'ok',
            'deleted_user': overhead.parameters['id'],
        }
        return reply(200,
                     text=json.dumps(listing, sort_keys=True),
                     content_type="application/json")
Example #9
0
 def jwt_protection(inst, environ, overhead):
     header = environ.get('HTTP_AUTHORIZATION')
     if header is not None and header.startswith('Bearer '):
         token = header[7:]
         try:
             payload = overhead.jwt_service.check_token(token)
             if payload is not None:
                 overhead.identity = environ['jwt.payload'] = payload
                 return method(inst, environ, overhead)
         except TokenException:
             # Do we need some kind of log ?
             pass
     return reply(401)
Example #10
0
    def update(self, environ, overhead):
        updated = True
        with SQLAlchemySession(overhead.engine) as session:
            user = session.query(User).get(overhead.parameters['id'])
            if user is not None:
                for field, value in overhead.data.items():
                    setattr(user, field, value)
            else:
                updated = False

        listing = {
            'status': updated and 'ok' or 'error',
            'updated_user': overhead.parameters['id'],
        }
        return reply(200,
                     text=json.dumps(listing, sort_keys=True),
                     content_type="application/json")
Example #11
0
    def get(self, environ, overhead):
        data = None
        with SQLAlchemySession(overhead.engine) as session:
            user = session.query(User).get(overhead.parameters['id'])
            if user is not None:
                data = dict(
                    oid=str(user.oid),
                    vname=user.vname,
                    nname=user.nname,
                )

        if data is None:
            listing = {
                'status': 'error',
            }
        else:
            listing = {
                'status': 'ok',
                'model': data,
            }
        return reply(200,
                     text=json.dumps(listing, sort_keys=True),
                     content_type="application/json")
Example #12
0
 def personal_info(self, environ, overhead):
     user = USERS.get(overhead.identity['user'])
     if user is not None:
         info = json.dumps(user['profile'], indent=4, sort_keys=True)
         return reply(200, text=info, content_type="application/json")
     return reply(404)
Example #13
0
 def schema(self, environ, overhead):
     return reply(200, text=SCHEMA_STRING, content_type="application/json")