示例#1
0
 def get(self):
     session = self.backend.get_session()
     response = Response()
     try:
         username = self.get_argument('username', None, True)
         password = self.get_argument('password', None, True)
         user = User.register({'username': username, 'password': password})
         session.add(user)
         session.commit()
         response.add_data(user.as_dict())
     except IntegrityError:
         response.add_error_message('Username already exists')
     except Exception:
         response.add_error_message('Exception while adding a new user')
     finally:
         session.rollback()
     self.write(response.as_dict())
 def post(self, cls, slug=None):
     session = self.backend.get_session()
     response = Response()
     cls_obj = getattr(sys.modules[__name__], cls)
     if inspect.isclass(cls_obj):
         try:
             obj = cls_obj()
             obj.update(self.request.arguments)
             session.add(obj)
             session.commit()
             response.add_data(obj.as_dict())
         except:
             session.rollback()
             response.add_error_message('Exception while fetching cards')
     else:
         response.add_error_message('No such class %s' % cls)
     self.write(response.as_dict())
 def put(self, cls, slug=None):
     session = self.backend.get_session()
     response = Response()
     cls_obj = getattr(sys.modules[__name__], cls)
     if inspect.isclass(cls_obj):
         try:
             if slug:
                 obj = session.query(cls_obj).filter(cls_obj.id == slug).one()
                 obj.update(self.request.arguments)
                 session.add(obj)
                 session.commit()
                 response.add_data(obj.as_dict())
             else:
                 response.add_error_message('Missing %s id' % cls)
         except NoResultFound:
             response.add_error_message('Missing %s' % cls)
         except:
             response.add_error_message('Exception while fetching %s' % cls)
         finally:
             session.rollback()
     else:
         response.add_error_message('No such class %s' % cls)
     self.write(response.as_dict())
 def delete(self, cls, slug=None):
     session = self.backend.get_session()
     response = Response()
     cls_obj = getattr(sys.modules[__name__], cls)
     if inspect.isclass(cls_obj):
         try:
             if slug:
                 session.query(cls_obj).filter(cls_obj.id == slug).delete()
                 session.commit()
                 response.add_data('Deleted')
             else:
                 response.add_error_message('Missing %s id' % cls)
         except:
             response.add_error_message('Exception while fetching cards')
     else:
         response.add_error_message('No such class %s' % cls)
     self.write(response.as_dict())
 def get(self, cls, slug=None):
     session = self.backend.get_session()
     response = Response()
     cls_obj = getattr(sys.modules[__name__], cls)
     if inspect.isclass(cls_obj):
         try:
             objs = session.query(cls_obj)
             if slug:
                 obj = objs.filter(cls_obj.id == slug).one()
                 response.add_data(obj.as_dict())
             else:
                 response.add_data([obj.as_dict() for obj in objs])
         except NoResultFound:
             response.add_error_message('No such %s' % cls)
         except:
             response.add_error_message('Exception while fetching %s' % cls)
     else:
         response.add_error_message('No such class %s' % cls)
     self.write(response.as_dict())
示例#6
0
 def get(self):
     session = self.backend.get_session()
     response = Response()
     try:
         username = self.get_argument('username', None, True)
         password = self.get_argument('password', None, True)
         user = session.query(User).filter(User.username == username).one()
         if user.check_password(password):
             session.query(UserToken).filter(UserToken.user_id == user.id).delete()
             token = UserToken.generate_token_for_user(user)
             session.add(token)
             session.commit()
             response.add_data(token.as_dict())
         else:
             raise NoResultFound
     except NoResultFound:
         response.add_error_message('User does not exist')
     except IntegrityError:
         response.add_error_message('Token already exists. Retry')
     except Exception:
         response.add_error_message('Exception while generating a token')
     finally:
         session.rollback()
     self.write(response.as_dict())