def update_from_params(cls, data): def proxy(data, field): if field == 'image': resolver = MediaResolverFactory.produce('image', data[field]) return resolver.Resolve() return data[field] try: if 'id' in data: with DBConnection() as session: vid = data['id'] entity = session.db.query(EntitySession).filter_by( vid=vid).all() if len(entity): for _ in entity: for field in cls.editable_items_list: setattr( _, field, proxy(data, field) if field in data else getattr(_, field, None)) session.db.commit() return vid except: return None return None
def add(self): with DBConnection() as session: session.db.add(self) session.db.commit() return self.vid return None
def delete(cls, vid): with DBConnection() as session: res = session.db.query(cls).filter_by(vid=vid).all() if len(res): [session.db.delete(_) for _ in res] session.db.commit() else: raise FileNotFoundError('%s was not found' % cls.__name__)
def stats(cls, id, type): with DBConnection() as session: if type == 'slider': res = list((session.db.query( func.count(EntityVote.value).filter( and_(EntityVote.value >= 0, EntityVote.value <= 20)).label('count0'), func.count(EntityVote.value).filter( and_(EntityVote.value > 20, EntityVote.value <= 40)).label('count1'), func.count(EntityVote.value).filter( and_(EntityVote.value > 40, EntityVote.value <= 60)).label('count2'), func.count(EntityVote.value).filter( and_(EntityVote.value > 60, EntityVote.value <= 80)).label('count3'), func.count(EntityVote.value).filter( and_(EntityVote.value > 80, EntityVote.value <= 100)).label('count4'), ).filter_by(session=id).all())[0]) all_res = sum(res) if all_res > 0: res = [int(float(_) / float(all_res) * 100) for _ in res] else: pos_size = \ session.db.query( func.count(EntityVote.value) ).filter_by(session=id).filter(EntityVote.value != 0).all()[0][0] neg_size = \ session.db.query( func.count(EntityVote.value) ).filter_by(session=id).filter(EntityVote.value == 0).all()[0][0] summ = pos_size + neg_size if summ > 0: pos = int(float(pos_size) / summ * 100.0) res = [pos, 100 - pos] else: res = [0, 0] return [int(_) for _ in res]
def reset_session(**request_handler_args): resp = request_handler_args['resp'] id = getIntPathParam("id", **request_handler_args) if id is not None: try: with DBConnection() as db_session: fp = db_session.db.query(EntityVote).filter_by(session=id).all() db_session.db.query(EntityVote).filter_by(session=id).delete() db_session.db.commit() resp.status = falcon.HTTP_200 cache.invalidate(get_session_objects, 'get_session_func', id) for _ in fp: cache.invalidate(get_vote_objects, 'get_vote_func', id, _.user) return except FileNotFoundError: resp.status = falcon.HTTP_404 return resp.status = falcon.HTTP_400
return # passed access token is valid return logging.getLogger().setLevel(logging.DEBUG) args = utils.RegisterLaunchArguments() cfgPath = args.cfgpath profile = args.profile # configure with open(cfgPath) as f: cfg = utils.GetAuthProfile(json.load(f), profile, args) DBConnection.configure(**cfg['each_db']) if 'oidc' in cfg: cfg_oidc = cfg['oidc'] # wsgi_app = api = falcon.API(middleware=[CORS(), Auth(), MultipartMiddleware()]) wsgi_app = api = falcon.API(middleware=[CORS(), MultipartMiddleware()]) server = SpecServer(operation_handlers=operation_handlers) if 'server_host' in cfg: with open('swagger.json') as f: swagger_json = json.loads(f.read(), object_pairs_hook=OrderedDict) server_host = cfg['server_host'] base_name = server_host swagger_json['host'] = server_host
def get(cls): with DBConnection() as session: return session.db.query(cls)