def get(city_id, dbconnection: dbutils.DBConnection = None) -> City: if isinstance(city_id, str) and len(city_id.split(',')) > 0: city_id = splitstrlist(city_id) if len(city_id) == 1: city_id = city_id[0] if isinstance(city_id, list) and len(city_id) == 0: return list() if isinstance(city_id, int) and city_id != 0: dbconnection.execute( "SELECT * FROM cities WHERE city_id='{}'".format(city_id), dbutils.dbfields['cities']) elif isinstance(city_id, list): dbconnection.execute( "SELECT * FROM cities WHERE city_id IN (" + ','.join(map(str, city_id)) + ")", dbutils.dbfields['cities']) elif city_id == 0: dbconnection.execute("SELECT * FROM cities", dbutils.dbfields['cities']) if len(dbconnection.last()) == 0: return list() cities = dbconnection.last() cities = list(map(lambda x: City(x), cities)) if isinstance(city_id, int) and city_id != 0: return cities[0] elif isinstance(city_id, list) or city_id == 0: return cities
def get(sport_id, dbconnection:dbutils.DBConnection=None) -> list: if isinstance(sport_id, str) and len(sport_id.split(',')) > 0: sport_id = splitstrlist(sport_id) if len(sport_id) == 1: sport_id = sport_id[0] if isinstance(sport_id, list) and len(sport_id) == 0: return list() if isinstance(sport_id, int) and sport_id != 0: dbconnection.execute("SELECT * FROM sport_types WHERE sport_id='{}'".format(sport_id), dbutils.dbfields['sport_types']) elif isinstance(sport_id, list): dbconnection.execute( "SELECT * FROM sport_types WHERE sport_id IN (" + ','.join(map(str, sport_id)) + ")", dbutils.dbfields['sport_types']) elif sport_id == 0: dbconnection.execute("SELECT * FROM sport_types", dbutils.dbfields['sport_types']) if len(dbconnection.last()) == 0: return list() sport_types = dbconnection.last() sport_types = list(map(lambda x: SportType(x), sport_types)) if isinstance(sport_id, int) and sport_id != 0: return sport_types[0] elif isinstance(sport_id, list) or sport_id == 0: return sport_types
def get(court_id, city_id: int = 1, dbconnection: dbutils.DBConnection = None) -> Court: if isinstance(court_id, str) and len(court_id.split(',')) > 0: court_id = splitstrlist(court_id) if len(court_id) == 1: court_id = court_id[0] if isinstance(court_id, list) and len(court_id) == 0: return list() if isinstance(court_id, int) and court_id != 0: dbconnection.execute( "SELECT * FROM courts WHERE court_id={} AND city_id={}".format( court_id, city_id), dbutils.dbfields['courts']) elif isinstance(court_id, list): dbconnection.execute( "SELECT * FROM courts WHERE court_id IN (" + ','.join(map(str, court_id)) + ") AND city_id={}".format(city_id), dbutils.dbfields['courts']) elif court_id == 0: dbconnection.execute( "SELECT * FROM courts WHERE city_id={}".format(city_id), dbutils.dbfields['courts']) if len(dbconnection.last()) == 0: return list() courts = dbconnection.last() courts = list(map(lambda x: Court(x, dbconnection=dbconnection), courts)) if isinstance(court_id, int) and court_id != 0: return courts[0] elif isinstance(court_id, list) or court_id == 0: return courts
def get_post(post_id: int, dbconnection: dbutils.DBConnection = None) -> BlogPost: dbconnection.execute( "SELECT * FROM blog.posts WHERE post_id={}".format(post_id), _fields['posts']) if len(dbconnection.last()) == 0: return None return BlogPost(dbconnection.last()[0], dbconnection=dbconnection)
def get(city_id, dbconnection:dbutils.DBConnection=None) -> City: if isinstance(city_id, str) and len(city_id.split(',')) > 0: city_id = splitstrlist(city_id) if len(city_id) == 1: city_id = city_id[0] if isinstance(city_id, list) and len(city_id) == 0: return list() if isinstance(city_id, int) and city_id != 0: dbconnection.execute("SELECT * FROM cities WHERE city_id='{}'".format(city_id), dbutils.dbfields['cities']) elif isinstance(city_id, list): dbconnection.execute("SELECT * FROM cities WHERE city_id IN (" + ','.join(map(str, city_id)) + ")", dbutils.dbfields['cities']) elif city_id == 0: dbconnection.execute("SELECT * FROM cities", dbutils.dbfields['cities']) if len(dbconnection.last()) == 0: return list() cities = dbconnection.last() cities = list(map(lambda x: City(x), cities)) if isinstance(city_id, int) and city_id != 0: return cities[0] elif isinstance(city_id, list) or city_id == 0: return cities
def get(amplua_id, dbconnection: dbutils.DBConnection = None) -> Amplua: # FIXME вроде как str передается иногда if isinstance(amplua_id, list) and len(amplua_id) == 0: return list() if isinstance(amplua_id, int) and amplua_id != 0: dbconnection.execute( "SELECT * FROM ampluas WHERE amplua_id='{}'".format(amplua_id), dbutils.dbfields['ampluas']) elif isinstance(amplua_id, list): dbconnection.execute( "SELECT * FROM ampluas WHERE amplua_id IN (" + ','.join(map(str, amplua_id)) + ")", dbutils.dbfields['ampluas']) elif amplua_id == 0: dbconnection.execute("SELECT * FROM ampluas", dbutils.dbfields['ampluas']) if len(dbconnection.last()) == 0: return list() ampluas = dbconnection.last() ampluas = list(map(lambda x: Amplua(x, dbconnection=dbconnection), ampluas)) if isinstance(amplua_id, int) and amplua_id != 0: return ampluas[0] elif isinstance(amplua_id, list) or amplua_id == 0: return ampluas
def get_tag(tag_id: int, dbconnection: dbutils.DBConnection = None) -> BlogPost: dbconnection.execute( "SELECT * FROM blog.tags WHERE tag_id={}".format(tag_id), _fields['posts']) if len(dbconnection.last()) == 0: return None return Tag(dbconnection.last()[0])
def add_game_finances(game_id:int, dbconnection:dbutils.DBConnection=None): finances = calc_game(game_id, dbconnection=dbconnection) sql = "INSERT INTO finances VALUES ({})".format(', '.join(["'{}'".format(finances[i]) for i in dbutils.dbfields['finances']])) dbconnection.execute(sql) dbconnection.execute("SELECT percents FROM responsible_salary WHERE user_id={}".format(finances['responsible_user_id'])) if len(dbconnection.last())>0: percents = dbconnection.last()[0][0] dbconnection.execute("INSERT INTO responsible_games_salary VALUES ({}, {}, {}, {}, {})".format( finances['responsible_user_id'], game_id, finances['profit'], percents, finances['responsible_salary'])) dbconnection.execute("SELECT * FROM finance_balance WHERE user_id!=0 AND YEAR(date)={} AND MONTH(date)={}".format( finances['datetime'].date().year, finances['datetime'].date().month), dbutils.dbfields['finance_balance']) if len(dbconnection.last())==0: dbconnection.execute("INSERT INTO finance_balance VALUES ('Александр Горохов', 0, 3, '{year}-{month}-01', 50), ('Виталий Харченко', 0, 1, '{year}-{month}-01', 50)".format( year=finances['datetime'].date().year, month=finances['datetime'].date().month)) owners = dbconnection.last() balance = dbconnection.execute("SELECT * FROM finance_balance WHERE user_id=0", dbutils.dbfields['finance_balance'])[0] for owner in owners: percents = owner['percents'] money = round(finances['real_profit']*(percents/100)) if owner['user_id']==finances['responsible_user_id']: money += finances['responsible_salary'] dbconnection.execute("UPDATE finance_balance SET value=value+{} WHERE user_id={} AND YEAR(date)={} AND MONTH(date)={}".format( money, owner['user_id'], finances['datetime'].date().year, finances['datetime'].date().month)) percents = balance['percents'] money = finances['real_profit']*(percents/100) dbconnection.execute("UPDATE finance_balance SET value=value+{} WHERE user_id=0".format(money))
def get(type_id, dbconnection:dbutils.DBConnection=None) -> GameType: if isinstance(type_id, str) and len(type_id.split(',')) > 0: type_id = splitstrlist(type_id) if len(type_id) == 1: type_id = type_id[0] if isinstance(type_id, list) and len(type_id) == 0: return list() if isinstance(type_id, int) and type_id != 0: dbconnection.execute("SELECT * FROM game_types WHERE type_id='{}'".format(type_id), dbutils.dbfields['game_types']) elif isinstance(type_id, list): dbconnection.execute( "SELECT * FROM game_types WHERE type_id IN (" + ','.join(map(str, type_id)) + ")", dbutils.dbfields['game_types']) elif type_id == 0: dbconnection.execute("SELECT * FROM game_types", dbutils.dbfields['game_types']) if len(dbconnection.last()) == 0: return list() game_types = dbconnection.last() game_types = list(map(lambda x: GameType(x, dbconnection=dbconnection), game_types)) if isinstance(type_id, int) and type_id != 0: return game_types[0] elif isinstance(type_id, list) or type_id == 0: return game_types
def get(user_id: int, all: bool = False, type: int = -1, dbconnection: dbutils.DBConnection = None) -> list: query = "SELECT * FROM notifications WHERE datetime<NOW() AND user_id={}".format( user_id) options = list() if not all: options.append("`read`=0") if type >= 0: options.append("type={}".format(type)) if len(options) > 0: query += ' AND ' query += ' AND '.join(options) query += ' ORDER BY DATETIME DESC' if all: query += ' LIMIT 40' dbconnection.execute(query, dbutils.dbfields['notifications']) if len(dbconnection.last()) == 0: return list() notifications = dbconnection.last() notifications = list( map(lambda x: Notification(x, dbconnection=dbconnection), notifications)) return notifications
def get_subscribed(command_id: int, dbconnection: dbutils.DBConnection = None) -> list: dbconnection.execute( "SELECT user_id FROM usergames WHERE comand_id='{}'".format( command_id)) if len(dbconnection.last()) == 0: return list() return list(map(lambda x: x[0], dbconnection.last()))
def get_posts_by_tags(tag_ids: list, dbconnection: dbutils.DBConnection = None) -> list: dbconnection.execute( "SELECT * FROM blog.posts WHERE post_id IN (SELECT post_id FROM blog.post_tags WHERE tag_id IN ({})) AND status>=0" .format(', '.join(tag_ids)), _fields['posts']) if len(dbconnection.last()) == 0: return list() return list(map(lambda x: Tag(x), dbconnection.last()))
def unsubscribe(user_id: int, game_id: int, dbconnection: dbutils.DBConnection = None): dbconnection.execute( "SELECT status FROM usergames WHERE user_id={} AND game_id={}".format( user_id, game_id)) if len(dbconnection.last()) == 0: return status = dbconnection.last()[0][0] if status == 2 or status == 1: if status == 2: delete_future_notifications(user_id, game_id, dbconnection=dbconnection) usergames_unsubscribe(user_id, game_id, dbconnection=dbconnection) elif status == 0: return game = get_by_id(game_id, dbconnection=dbconnection) if game.reserved() > 0 and len(game.reserved_people()) > 0: for user_id in game.reserved_people(): utils.spool_func( notificating.site.subscribed, user_id, 'В игре "{}" освободилось место!'.format( create_link.game(game)), 1, game_id) utils.spool_func(notificating.mail.tpl.notify_reserved, game, user_id)
def get_by_id(game_id, dbconnection: dbutils.DBConnection = None) -> Game: if isinstance(game_id, str) and len(game_id.split(',')) > 0: game_id = splitstrlist(game_id) if len(game_id) == 1: game_id = game_id[0] if isinstance(game_id, list) and len(game_id) == 0: return list() if isinstance(game_id, int): dbconnection.execute( "SELECT * FROM games WHERE game_id={}".format(game_id), dbutils.dbfields['games']) elif isinstance(game_id, list): dbconnection.execute( "SELECT * FROM games WHERE game_id IN (" + ','.join(map(str, game_id)) + ")", dbutils.dbfields['games']) if len(dbconnection.last()) == 0: return list() games = dbconnection.last() games = list(map(lambda x: Game(x, dbconnection=dbconnection), games)) if isinstance(game_id, int): return games[0] elif isinstance(game_id, list): return games
def get_moderating_posts(dbconnection: dbutils.DBConnection = None) -> list: dbconnection.execute("SELECT * FROM blog.posts WHERE status=0", _fields['posts']) if len(dbconnection.last()) == 0: return list() return list( map(lambda x: BlogPost(x, dbconnection=dbconnection), dbconnection.last()))
def get_reserved_to_game(game_id: int, dbconnection: dbutils.DBConnection = None) -> list: dbconnection.execute( "SELECT user_id FROM usergames WHERE game_id='{}' AND status=1".format( game_id)) return list( map(lambda x: x[0], dbconnection.last())) if len(dbconnection.last()) > 0 else list()
def get_subscribed_games(user_id: int, dbconnection: dbutils.DBConnection = None) -> list: dbconnection.execute( "SELECT game_id FROM usergames WHERE user_id='{}' AND status=2".format( user_id)) return list( map(lambda x: x[0], dbconnection.last())) if len(dbconnection.last()) > 0 else list()
def get_organizer_games(user_id: int, dbconnection: dbutils.DBConnection = None) -> list(): dbconnection.execute( "SELECT game_id FROM games WHERE created_by='{}' and deleted=0".format( user_id)) return list( map(lambda x: x[0], dbconnection.last())) if len(dbconnection.last()) > 0 else list()
def get_responsible_games(user_id: int, dbconnection: dbutils.DBConnection = None) -> list: dbconnection.execute( "SELECT game_id FROM games WHERE responsible_user_id='{}' and deleted=0" .format(user_id)) return list( map(lambda x: x[0], dbconnection.last())) if len(dbconnection.last()) > 0 else list()
def delete_future_notifications(user_id: int, game_id: int, dbconnection: dbutils.DBConnection = None): dbconnection.execute( "SELECT notification_id FROM notifications WHERE type=1 AND game_id={} AND user_id={} AND DATETIME>NOW()" .format(game_id, user_id)) if len(dbconnection.last()) == 0: return notifications.delete(list(map(lambda x: x[0], dbconnection.last())))
def create(email:str, dbconnection:dbutils.DBConnection=None) -> str: dbconnection.execute("SELECT activated, token FROM activation WHERE email='{}'".format(email)) if len(dbconnection.last())>0: if dbconnection.last()[0][0]==1: raise ValueError('{} already activated'.format(email), email, dbconnection.last()[0][1]) elif dbconnection.last()[0][0]==2: raise KeyError('{} already registrated'.format(email), email) else: dbconnection.execute("DELETE FROM activation WHERE email='{}'".format(email)) token = modules.generate_token() dbconnection.execute("INSERT INTO activation (email, token) VALUES ('{}', '{}')".format(email, token)) return token
def get(user_id, userlevel: int = -1, count: slice = slice(0, 20), dbconnection: dbutils.DBConnection = None) -> User: if isinstance(user_id, str) and len(user_id.split(',')) > 0: user_id = splitstrlist(user_id) if len(user_id) == 1: user_id = user_id[0] if isinstance(user_id, list) and len(user_id) == 0: return list() sql = "SELECT * FROM users WHERE user_id='{}'".format(user_id) if isinstance(user_id, int) and user_id != 0: sql = "SELECT * FROM users WHERE user_id='{}'".format(user_id) elif isinstance(user_id, list): sql = "SELECT * FROM users WHERE user_id IN (" + ','.join( map(str, user_id)) + ")" elif user_id == 0: sql = "SELECT * FROM users" if userlevel >= 0 or isinstance(userlevel, set): if user_id == 0: sql += ' WHERE ' else: sql += ' AND ' if isinstance(userlevel, int): sql += " LOCATE('|{}|', userlevel) ".format(userlevel) else: ' AND '.join( map(lambda x: "LOCATE('|{}|', userlevel)".format(x), userlevel)) sql += " ORDER BY played_games DESC" if user_id == 0: sql += " LIMIT {}, {}".format(count.start if count.start else 0, count.stop) dbconnection.execute(sql, dbutils.dbfields['users']) if len(dbconnection.last()) == 0: return list() users = dbconnection.last() users = list(map(lambda x: User(x, dbconnection=dbconnection), users)) if isinstance(user_id, int) and user_id != 0: return users[0] elif isinstance(user_id, list) or user_id == 0: return users
def are_friends(user_id_1: int, user_id_2: int, dbconnection: dbutils.DBConnection = None) -> bool: dbconnection.execute( "SELECT * FROM friends WHERE user_id1={} AND user_id2={}".format( user_id_1, user_id_2)) return len(dbconnection.last()) != 0
def add(dbconnection: dbutils.DBConnection = None, **kwargs) -> int: sql = 'INSERT INTO games ({dbkeylist}) VALUES ({dbvaluelist})' keylist = list(kwargs.keys()) sql = sql.format(dbkeylist=', '.join(keylist), dbvaluelist=', '.join( ["'{}'".format(str(kwargs[key])) for key in keylist])) dbconnection.execute(sql) dbconnection.execute('SELECT last_insert_id() FROM games') return dbconnection.last()[0][0]
def create(email: str, dbconnection: dbutils.DBConnection = None) -> str: dbconnection.execute( "SELECT activated, token FROM activation WHERE email='{}'".format( email)) if len(dbconnection.last()) > 0: if dbconnection.last()[0][0] == 1: raise ValueError('{} already activated'.format(email), email, dbconnection.last()[0][1]) elif dbconnection.last()[0][0] == 2: raise KeyError('{} already registrated'.format(email), email) else: dbconnection.execute( "DELETE FROM activation WHERE email='{}'".format(email)) token = modules.generate_token() dbconnection.execute( "INSERT INTO activation (email, token) VALUES ('{}', '{}')".format( email, token)) return token
def get_comands(comand_id:(list, int), dbconnection:dbutils.DBConnection=None) -> Comand: if isinstance(comand_id, list) and len(comand_id) == 0: return list() if isinstance(comand_id, int): dbconnection.execute("SELECT * FROM comands WHERE comand_id='{}'".format(comand_id)) elif isinstance(comand_id, list): dbconnection.execute( "SELECT * FROM comands WHERE comand_id IN ({})".format(','.join(list(map(str, comand_id))))) return list(map(lambda x: Comand(x, dbconnection), dbconnection.last()))
def get(user_id:int, all:bool=False, type:int=-1, dbconnection:dbutils.DBConnection=None) -> list: query = "SELECT * FROM notifications WHERE datetime<NOW() AND user_id={}".format(user_id) options = list() if not all: options.append("`read`=0") if type >= 0: options.append("type={}".format(type)) if len(options) > 0: query += ' AND ' query += ' AND '.join(options) query += ' ORDER BY DATETIME DESC' if all: query += ' LIMIT 40' dbconnection.execute(query, dbutils.dbfields['notifications']) if len(dbconnection.last()) == 0: return list() notifications = dbconnection.last() notifications = list(map(lambda x: Notification(x, dbconnection=dbconnection), notifications)) return notifications
def get(user_id, userlevel:int=-1, count:slice=slice(0, 20), dbconnection:dbutils.DBConnection=None) -> User: if isinstance(user_id, str) and len(user_id.split(',')) > 0: user_id = splitstrlist(user_id) if len(user_id) == 1: user_id = user_id[0] if isinstance(user_id, list) and len(user_id) == 0: return list() sql = "SELECT * FROM users WHERE user_id='{}'".format(user_id) if isinstance(user_id, int) and user_id != 0: sql = "SELECT * FROM users WHERE user_id='{}'".format(user_id) elif isinstance(user_id, list): sql = "SELECT * FROM users WHERE user_id IN (" + ','.join(map(str, user_id)) + ")" elif user_id == 0: sql = "SELECT * FROM users" if userlevel >= 0 or isinstance(userlevel, set): if user_id==0: sql += ' WHERE ' else: sql += ' AND ' if isinstance(userlevel, int): sql += " LOCATE('|{}|', userlevel) ".format(userlevel) else: ' AND '.join(map(lambda x: "LOCATE('|{}|', userlevel)".format(x), userlevel)) sql += " ORDER BY played_games DESC" if user_id == 0: sql += " LIMIT {}, {}".format(count.start if count.start else 0, count.stop) dbconnection.execute(sql, dbutils.dbfields['users']) if len(dbconnection.last()) == 0: return list() users = dbconnection.last() users = list(map(lambda x: User(x, dbconnection=dbconnection), users)) if isinstance(user_id, int) and user_id != 0: return users[0] elif isinstance(user_id, list) or user_id == 0: return users
def court_game_intersection(court_id: int, datetime: str, duration: int, dbconnection: dbutils.DBConnection = None) -> Game: sql = ( " SELECT * FROM games WHERE court_id='{court_id}'" " AND deleted=0 AND (" " (datetime BETWEEN '{datetime}' AND '{datetime}' + INTERVAL '{duration}' MINUTE)" " OR " " (datetime + INTERVAL duration MINUTE BETWEEN '{datetime}' AND '{datetime}' + INTERVAL '{duration}' MINUTE))" " AND " " datetime!='{datetime}' AND datetime!=('{datetime}' + INTERVAL '{duration}' MINUTE)" " AND " " (datetime + INTERVAL duration MINUTE)!='{datetime}' AND (datetime + INTERVAL duration MINUTE)!=('{datetime}' + INTERVAL '{duration}' MINUTE)" ) sql = sql.format(court_id=court_id, datetime=datetime, duration=duration) dbconnection.execute(sql, dbutils.dbfields['games']) if len(dbconnection.last()) > 0: return Game(dbconnection.last()[0], dbconnection=dbconnection) return None
def calc_game(game_id:int, dbconnection:dbutils.DBConnection=None) -> dict: game = dbconnection.execute("SELECT * FROM games WHERE game_id={}".format(game_id), dbutils.dbfields['games'])[0] game = Game(game, dbconnection=dbconnection) reports = dbconnection.execute("SELECT * FROM reports WHERE game_id={}".format(game_id), dbutils.dbfields['reports']) additional_charges = dbconnection.execute("SELECT * FROM additional_charges WHERE game_id={}".format(game_id), dbutils.dbfields['additional_charges']) finances = dict() finances['game_id'] = game_id finances['datetime'] = game.datetime() finances['capacity'] = game.capacity() finances['cost'] = game.cost() finances['sport_id'] = game.sport_type() finances['responsible_user_id'] = game.responsible_user_id() finances['created_by'] = game.created_by() finances['visited'] = len(list(filter(lambda x: x['status']!=0, reports))) finances['empty'] = finances['capacity']-finances['visited'] if finances['empty']<0: finances['empty']=0 finances['lost_empty'] = finances['empty']*finances['cost'] finances['notvisited'] = len(list(filter(lambda x: x['status']==0, reports))) finances['lost_notvisited'] = finances['notvisited']*finances['cost'] finances['notpayed'] = len(list(filter(lambda x: x['status']==1, reports))) finances['lost_notpayed'] = finances['notpayed']*finances['cost'] finances['playedpayed'] = len(list(filter(lambda x: x['status']==2, reports))) finances['real_income'] = finances['playedpayed']*finances['cost'] finances['ideal_income'] = finances['cost']*finances['capacity'] if finances['capacity']>0 else finances['real_income'] finances['rent_charges'] = game.court_id(True).cost()*(game.duration()/60) finances['additional_charges'] = sum([i['cost'] for i in additional_charges]) finances['profit'] = finances['real_income']-finances['additional_charges']-finances['rent_charges'] dbconnection.execute("SELECT percents FROM responsible_salary WHERE user_id={}".format(game.responsible_user_id())) finances['responsible_salary'] = 0 if len(dbconnection.last())>0: finances['responsible_salary'] = round((finances['profit'])*(dbconnection.last()[0][0]/100)) finances['profit'] -= finances['responsible_salary'] finances['real_profit'] = finances['profit'] + finances['rent_charges'] return finances
def get(amplua_id, dbconnection:dbutils.DBConnection=None) -> Amplua: # FIXME вроде как str передается иногда if isinstance(amplua_id, list) and len(amplua_id) == 0: return list() if isinstance(amplua_id, int) and amplua_id != 0: dbconnection.execute("SELECT * FROM ampluas WHERE amplua_id='{}'".format(amplua_id), dbutils.dbfields['ampluas']) elif isinstance(amplua_id, list): dbconnection.execute("SELECT * FROM ampluas WHERE amplua_id IN (" + ','.join(map(str, amplua_id)) + ")", dbutils.dbfields['ampluas']) elif amplua_id == 0: dbconnection.execute("SELECT * FROM ampluas", dbutils.dbfields['ampluas']) if len(dbconnection.last()) == 0: return list() ampluas = dbconnection.last() ampluas = list(map(lambda x: Amplua(x, dbconnection=dbconnection), ampluas)) if isinstance(amplua_id, int) and amplua_id != 0: return ampluas[0] elif isinstance(amplua_id, list) or amplua_id == 0: return ampluas
def get_comands(comand_id: (list, int), dbconnection: dbutils.DBConnection = None) -> Comand: if isinstance(comand_id, list) and len(comand_id) == 0: return list() if isinstance(comand_id, int): dbconnection.execute( "SELECT * FROM comands WHERE comand_id='{}'".format(comand_id)) elif isinstance(comand_id, list): dbconnection.execute( "SELECT * FROM comands WHERE comand_id IN ({})".format(','.join( list(map(str, comand_id))))) return list(map(lambda x: Comand(x, dbconnection), dbconnection.last()))
def user_game_intersection(user_id: int, game: Game, dbconnection: dbutils.DBConnection = None) -> Game: sql = ( " SELECT * FROM games WHERE game_id!='{game_id}'" " AND game_id IN (SELECT game_id FROM usergames WHERE user_id='{user_id}' AND status=2)" " AND deleted=0 AND (" " (datetime BETWEEN '{datetime}' AND '{datetime}' + INTERVAL '{duration}' MINUTE)" " OR " " (datetime + INTERVAL duration MINUTE BETWEEN '{datetime}' AND '{datetime}' + INTERVAL '{duration}' MINUTE))" " AND " " datetime!='{datetime}' AND datetime!=('{datetime}' + INTERVAL '{duration}' MINUTE)" " AND " " (datetime + INTERVAL duration MINUTE)!='{datetime}' AND (datetime + INTERVAL duration MINUTE)!=('{datetime}' + INTERVAL '{duration}' MINUTE)" ) sql = sql.format(datetime=game.datetime, duration=game.duration(), user_id=user_id, game_id=game.game_id()) dbconnection.execute(sql, dbutils.dbfields['games']) if len(dbconnection.last()) > 0: return Game(dbconnection.last()[0], dbconnection=dbconnection) return None
def subscribe(user_id: int, game_id: int, reserved: bool = False, dbconnection: dbutils.DBConnection = None): dbconnection.execute( "SELECT status FROM usergames WHERE user_id={} AND game_id={}".format( user_id, game_id)) if len(dbconnection.last()) == 0: if reserved: usergames_reserve(user_id, game_id, dbconnection=dbconnection) else: usergames_subscribe(user_id, game_id, dbconnection=dbconnection) else: status = dbconnection.last()[0][0] if status == 2 or status == 1 and reserved: return if (status == 1 or status == 0) and not reserved: usergames_subscribe(user_id, game_id, dbconnection=dbconnection) write_future_notifications(user_id, game_id, dbconnection=dbconnection) if status == 0 and reserved: usergames_reserve(user_id, game_id, dbconnection=dbconnection)
def usergames_set(user_id: int, game_id: int, status: int, dbconnection: dbutils.DBConnection = None): dbconnection.execute( "SELECT COUNT(*) FROM usergames WHERE user_id={} AND game_id={}". format(user_id, game_id)) if dbconnection.last()[0][0] == 0: sql = "INSERT INTO usergames (user_id, game_id, status) VALUES ({}, {}, {})".format( user_id, game_id, status) else: sql = "UPDATE usergames SET status={} WHERE user_id={} AND game_id={}".format( status, user_id, game_id) dbconnection.execute(sql) games_cache.drop(game_id) cacher.drop_by_table_name('usergames', 'game_id', game_id)
def get(game_id:int, dbconnection:dbutils.DBConnection=None) -> dict: # registered: user_id:int -> status:int, unregistered: name:str -> (status:int, phone:str) resp = {'registered': dict(), 'unregistered': dict(), 'additional':list()} dbconnection.execute("SELECT user_id, status FROM reports WHERE user_id>0 AND game_id={}".format(game_id)) if len(dbconnection.last()) > 0: for user in dbconnection.last(): user_id, status = user resp['registered'][user_id] = status dbconnection.execute("SELECT name, phone, status FROM reports WHERE user_id=0 AND game_id={}".format(game_id)) if len(dbconnection.last()) > 0: for user in dbconnection.last(): name, phone, status = user[0], user[1], user[2] resp['unregistered'][name] = (status, phone) dbconnection.execute("SELECT description, cost FROM additional_charges WHERE game_id={}".format(game_id)) if len(dbconnection.last()) > 0: for i in dbconnection.last(): resp['additional'].append(i) return resp
def get(token:str, dbconnection:dbutils.DBConnection=None) -> str: dbconnection.execute("SELECT email FROM activation WHERE token='{}'".format(token)) if len(dbconnection.last())==0: raise ValueError('No email for <{}>'.format(token)) return dbconnection.last()[0][0]
def reported(game_id:int, dbconnection:dbutils.DBConnection=None) -> bool: dbconnection.execute("SELECT COUNT(user_id) FROM reports WHERE game_id={}".format(game_id)) return dbconnection.last()[0][0] > 0
def banned(user_id:int, dbconnection:dbutils.DBConnection=None) -> bool: dbconnection.execute("SELECT user_id FROM banned WHERE user_id={}".format(user_id)) return len(dbconnection.last()) != 0
def get_tags(post_id:int, dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT * FROM blog.tags WHERE tag_id IN (SELECT tag_id FROM blog.post_tags WHERE post_id={})".format(post_id), _fields['tags']) if len(dbconnection.last())==0: return list() return list(map(lambda x: Tag(x), dbconnection.last()))
def get_comand_ids(game_id: int, dbconnection: dbutils.DBConnection = None) -> list: dbconnection.execute( "SELECT comand_id FROM comands WHERE game_id='{}'".format(game_id)) if len(dbconnection.last()) == 0: return list() return list(map(lambda x: x[0], dbconnection.last()))
def are_friends(user_id_1:int, user_id_2:int, dbconnection:dbutils.DBConnection=None) -> bool: dbconnection.execute("SELECT * FROM friends WHERE user_id1={} AND user_id2={}".format(user_id_1, user_id_2)) return len(dbconnection.last()) != 0
def get_tag(tag_id:int, dbconnection:dbutils.DBConnection=None) -> BlogPost: dbconnection.execute("SELECT * FROM blog.tags WHERE tag_id={}".format(tag_id), _fields['posts']) if len(dbconnection.last())==0: return None return Tag(dbconnection.last()[0])
def get_all_tags(dbconnection: dbutils.DBConnection = None) -> list: dbconnection.execute("SELECT * FROM blog.tags", _fields['tags']) return list(map(lambda x: Tag(x), dbconnection.last()))
def get_subscribed(command_id:int, dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT user_id FROM usergames WHERE comand_id='{}'".format(command_id)) if len(dbconnection.last()) == 0: return list() return list(map(lambda x: x[0], dbconnection.last()))
def get(game_id:int, dbconnection:dbutils.DBConnection=None) -> GameFinance: dbconnection.execute("SELECT * FROM finances WHERE game_id={}".format(game_id), dbutils.dbfields['finances']) if len(dbconnection.last())==0: return None return GameFinance(dbconnection.last()[0])
def get_all(dbconnection:dbutils.DBConnection=None) -> GameFinance: dbconnection.execute("SELECT * FROM finances", dbutils.dbfields['finances']) return list(map(lambda x: GameFinance(x, dbconnection), dbconnection.last()))
def get_moderating_posts(dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT * FROM blog.posts WHERE status=0", _fields['posts']) if len(dbconnection.last())==0: return list() return list(map(lambda x: BlogPost(x, dbconnection=dbconnection), dbconnection.last()))
def get_comand_ids(game_id:int, dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT comand_id FROM comands WHERE game_id='{}'".format(game_id)) if len(dbconnection.last()) == 0: return list() return list(map(lambda x: x[0], dbconnection.last()))
def get_by_date(month:int=0, year:int=0, dbconnection:dbutils.DBConnection=None) -> GameFinance: dbconnection.execute("SELECT * FROM finances WHERE MONTH(datetime)={} AND YEAR(datetime)={}".format( 'MONTH(NOW())' if month==0 else month, 'YEAR(NOW())' if year==0 else year), dbutils.dbfields['finances']) return list(map(lambda x: GameFinance(x, dbconnection), dbconnection.last()))
def get_posts_by_tags(tag_ids:list, dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT * FROM blog.posts WHERE post_id IN (SELECT post_id FROM blog.post_tags WHERE tag_id IN ({})) AND status>=0".format(', '.join(tag_ids)), _fields['posts']) if len(dbconnection.last())==0: return list() return list(map(lambda x: Tag(x), dbconnection.last()))
def get_additional_charges(game_id:int, dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT * FROM additional_charges WHERE game_id={}".format(game_id), dbutils.dbfields['additional_charges']) return dbconnection.last()
def get_post(post_id:int, dbconnection:dbutils.DBConnection=None) -> BlogPost: dbconnection.execute("SELECT * FROM blog.posts WHERE post_id={}".format(post_id), _fields['posts']) if len(dbconnection.last())==0: return None return BlogPost(dbconnection.last()[0], dbconnection=dbconnection)
def get_all_outlays(dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT * FROM outlays", dbutils.dbfields['outlays']) return list(map(Outlay, dbconnection.last()))
def get_all_tags(dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT * FROM blog.tags", _fields['tags']) return list(map(lambda x: Tag(x), dbconnection.last()))
def get_outlays_by_date(month:int=0, year:int=0, dbconnection:dbutils.DBConnection=None) -> list: dbconnection.execute("SELECT * FROM outlays WHERE MONTH(datetime)={} AND YEAR(datetime)={}".format( 'NOW()' if month==0 else month, 'NOW()' if year==0 else year), dbutils.dbfields['outlays']) return list(map(Outlay, dbconnection.last()))