Exemple #1
0
 def list(self, app_id):
     try:
         with session_scope() as session:
             data = session.query(self.Model).filter_by(app_id=app_id).all()
             data = map(lambda row: row.as_dict(), data)
             return list(data)
     except SQLAlchemyError as err:
         raise err
     except ValidationError as err:
         raise err
     except DataError as err:
         err.message = err.to_primitive()
         raise err
     except Exception as err:
         raise err
Exemple #2
0
 def get_permissions(self, app_id, role_id):
     try:
         redis = RedisCash()
         permission_data = redis.app_prefix_get(role_id, app_id)
         if permission_data is None:
             with session_scope() as session:
                 permission_data = session.query(self.Model).filter_by(
                     id=role_id, app_id=app_id).first()
                 permission_data = permission_data.role_resources_as_dict()
                 redis.app_prefix_set(role_id, permission_data, app_id)
         return permission_data
     except SQLAlchemyError as err:
         raise err
     except Exception as err:
         raise err
Exemple #3
0
 def create(self, **kwargs):
     try:
         with session_scope() as session:
             row = self.Model(**kwargs)
             session.add(row)
             session.flush()
             session.refresh(row)
             return row._asdict()
     except SQLAlchemyError as err:
         raise err
     except ValidationError as err:
         raise err
     except DataError as err:
         err.message = err.to_primitive()
         raise err
     except Exception as err:
         raise err
Exemple #4
0
 def get(self, **kwargs):
     try:
         with session_scope() as session:
             data = []
             for value in session.query(Role.id).join(
                     self.Model).filter_by(**kwargs).all():
                 data.append(value.id)
             return data
     except SQLAlchemyError as err:
         raise err
     except ValidationError as err:
         raise err
     except DataError as err:
         err.message = err.to_primitive()
         raise err
     except Exception as err:
         raise err
Exemple #5
0
 def create(self, app_id, **kwargs):
     try:
         with session_scope() as session:
             redis = RedisCash()
             row = self.Model(**kwargs)
             session.add(row)
             session.flush()
             session.refresh(row)
             redis.app_prefix_clear(kwargs['role_id'], app_id)
             return row._asdict()
     except SQLAlchemyError as err:
         raise err
     except ValidationError as err:
         raise err
     except DataError as err:
         err.message = err.to_primitive()
         raise err
     except Exception as err:
         raise err
Exemple #6
0
 def update(self, model_id, app_id, **kwargs):
     try:
         with session_scope() as session:
             row = session.query(self.Model).filter_by(
                 id=model_id, app_id=app_id).first()
             if row is None:
                 raise NoResultFound(
                     "{model_name} with id: {model_id}, app_id: {app_id} not found "
                     .format(model_name=self.ModelName,
                             model_id=model_id,
                             app_id=app_id))
             for (key, value) in kwargs.items():
                 setattr(row, key, value)
             return row._asdict()
     except SQLAlchemyError as err:
         raise err
     except ValidationError as err:
         raise err
     except DataError as err:
         err.message = err.to_primitive()
         raise err
     except Exception as err:
         raise err
Exemple #7
0
 def delete(self, model_id, app_id):
     try:
         with session_scope() as session:
             query = session.query(self.Model).filter_by(id=model_id,
                                                         app_id=app_id)
             row = query.first()
             if row is None:
                 raise NoResultFound(
                     "{model_name} with id: {model_id}, app_id: {app_id} not found "
                     .format(model_name=self.ModelName,
                             model_id=model_id,
                             app_id=app_id))
             query.delete()
             return row._asdict()
     except SQLAlchemyError as err:
         raise err
     except ValidationError as err:
         raise err
     except DataError as err:
         err.message = err.to_primitive()
         raise err
     except Exception as err:
         raise err