def login(self): username, password = self['username'], self['password'] salt_hash = yield db.row( """ select id, salt, hash from users where username = %(username)s""", {'username': username}) if not salt_hash: raise Stink("no user named %s" % username) user_id, salt, hash = tuple( (salt_hash[key] for key in ('id', 'salt', 'hash'))) if hashlib.sha256(password + salt).hexdigest() == hash: token = os.urandom(16).encode('hex') yield db.query( """ insert into tokens (user_id, token) values (%(user_id)s, %(token)s)""", { 'user_id': user_id, 'token': token }) self.set_cookie('token', token) else: raise Stink("bad password") self.request.user = yield db.value( """select user_id from tokens where token = %(token)s""", {'token': token}) defer.returnValue({ 'message': 'logged in', 'token': token, 'user': { 'id': self.request.user } })
def test_update(self): row = db.insert('data', {'type': 'weight'}) row['value'] = 150 db.update('data', row) row2 = db.row('select * from data') assert_equal(150, row2['value'])
def is_owner(self): user_id, page = (self.request.user, self['page']) data = yield db.row(""" select users.id as user_id, pages.published from users, pages where pages.user_id = users.id and pages.id = %(page)s""", {'page': page}) if not data: raise Stink("no page set %s" % page) if data['user_id'] != user_id: raise Stink("you don't own page %s" % page) defer.returnValue(data['published'])
def test_insert(self): row_dict = {'type': 'weight'} returned = db.insert('data', row_dict) assert_equal(returned, row_dict) assert_equal(1, row_dict['id']) row = db.row('select * from data') assert_equal(1, row['id']) assert_equal('weight', row['type'])
def is_owner(self): user_id, page = (self.request.user, self['page']) data = yield db.row( """ select users.id as user_id, pages.published from users, pages where pages.user_id = users.id and pages.id = %(page)s""", {'page': page}) if not data: raise Stink("no page set %s" % page) if data['user_id'] != user_id: raise Stink("you don't own page %s" % page) defer.returnValue(data['published'])
def login(self): username, password = self['username'], self['password'] salt_hash = yield db.row(""" select id, salt, hash from users where username = %(username)s""", {'username':username}) if not salt_hash: raise Stink("no user named %s" % username) user_id, salt, hash = tuple((salt_hash[key] for key in ('id', 'salt', 'hash'))) if hashlib.sha256(password + salt).hexdigest() == hash: token = os.urandom(16).encode('hex') yield db.query(""" insert into tokens (user_id, token) values (%(user_id)s, %(token)s)""", {'user_id':user_id, 'token':token}) self.set_cookie('token', token) else: raise Stink("bad password") self.request.user = yield db.value("""select user_id from tokens where token = %(token)s""", {'token':token}) defer.returnValue({'message' : 'logged in', 'token':token, 'user': {'id': self.request.user}})
def data(params): if params.get('post'): db.insert('data', { 'type': 'weight', 'value': params['post']['value'], 'user_id': 1, 'date': date.isoformat(date.today()) }) return elif len(params['path']) > 1: dt = params['path'][1] if dt == 'today': dt = date.isoformat(date.today()) return db.row('select type, date, value from data where user_id=%s and type=%s and date=%s', (1, 'weight', dt)) else: # The whole data set return db.rows('select type, date, value from data where user_id=%s and type=%s', (1, 'weight'))
def check_user(user_id, first_name, username, chat_id): chuser = db.row("users", {"user_id = ": str(user_id)}) print(chuser) if chuser == None: tz = pytz.timezone("Europe/Moscow") now = datetime.datetime.now(tz).strftime('%d.%m.%Y.%H:%M') print(str(now)) user = {"user_id": str(user_id), "first_name": first_name, "username": username, "chat_id": str(chat_id), "time_add_chat": str(now) } db.insert("users", user) print("add") else: user = list_to_dict(chuser) print("user") pprint(user) return user
def test_execute(self): db.execute('insert into data (type) values (%s)', 'weight') row = db.row('select id, type from data') assert_equal({'id': 1, 'type': 'weight'}, row)
def test_row(self): db.insert('data', {'type': 'weight'}) row = db.row('select id, type from data') assert_equal({'id': 1, 'type': 'weight'}, row)
def read(): nameFile = input("Ingresa la ruta de tu archivo: ") archivo = open(nameFile) lines = archivo.readlines() archivo.close() for i in lines: i = i.rstrip("\n") #catch the name tem = i.split("=") nameRow = tem[0].rstrip( " ") # <----------- indica el nombre de la lista #catch the operation sort sortRow = False if ("ORDENAR" in tem[1]): sortRow = True # <----------------- indica si hay que hacer ordenamiento tem[1] = tem[1].replace("ORDENAR", "") tem[1] = tem[1].rstrip(" ") tem[1] = tem[1].rstrip(",") #catch the operation search searchRow = False valueSearchRow = None if ("BUSCAR" in tem[1]): searchRow = True # <--------------- indica si hay que realizar una busqueda tem[1] = tem[1].replace("BUSCAR", "") l = tem[1].rstrip(" ") l = (len(tem[1])) valueSearchRow = tem[1][ l - 1] # <---------- indica que valor debemos buscar tem[1] = tem[1].rstrip(str(valueSearchRow)) tem[1] = tem[1].rstrip(" ") tem[1] = tem[1].rstrip(",") #catch the values valuesTemp = tem[1].split(",") valuesRow = [] # <---------- lista con los valores for j in valuesTemp: aux = int(j.strip()) valuesRow.append(aux) #---------------------------------------- SEARCH -------------------------------------------- positionRow = [] if searchRow == True: cont = 1 for n in valuesRow: #print("valores: ",n) #print("valor de busqueda: ",valueSearchRow) if int(n) == int(valueSearchRow): positionRow.append(cont) cont = cont + 1 #add to vector rows all the information fila = db.row(nameRow, valuesRow, sortRow, searchRow, valueSearchRow, positionRow) db.arrayRows.append(fila) #fuera del for for i in db.arrayRows: if i.sort == True: vd = i.values.copy() vo = sortOnMyOwn(vd) i.orderedValues = vo os.system('clear') print("\n") print( "-------------------------------------Datos cargados con exito-------------------------------------\n" ) aaaa = input("Presiona enter para continuar: ") '''