def command_start(m): cid = m.chat.id check = find_user_by_tg(m.chat.username.lower()) if (check): if (check.chatid): bot.send_message( cid, "I already know you, no need for me to scan you again!") else: check.chatid = cid check.save() bot.send_message(cid, "Hello, stranger, let me scan you...") bot.send_message(cid, "Scanning complete, I know you now") else: check = User(tname=m.chat.username.lower(), chatid=cid) check.save()
def api_register_user(*, email, name, passwd): if not name or not name.strip(): raise APIValueError('name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email') if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd') users = yield from User.findAll('`email`=?', email) if len(users) > 0: raise APIError('register:failed', 'email', 'Email is already in use') uid = next_id() sha1_passwd = '%s:%s' % (uid, passwd) user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest()) yield from user.save() r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def auth_user(uid, url): check = find_user_by_vk(uid) if (check): if (check.vkid and check.tname and check.chatid): return check, 'ok' if (re.fullmatch(PATTERN, url)): tname = url[5:].lower() this_user = find_user_by_tg(tname) if (not this_user): #1 this_user = User(vkid=uid, tname=tname, chatid=None) this_user.save() return None, "для завершения регистрации перейдите по ссылке t.me/LetterFromVkBot и введите /start" elif (not this_user.chatid): #значит уже прошел #1 return None, "для завершения регистрации перейдите по ссылке t.me/LetterFromVkBot и введите /start \n без этого я не смогу работать с вами" elif (not this_user.vkid ): #значит пользователь регистрировался в телеге this_user.vkid = uid this_user.save() return this_user, 'ok' #вот тут я не уверен, нужно ли возвращать его else: return None, "ссылка не прошла regex"
def test(): yield from create_pool(user='******', password='******', database='awesome') u = User(name='Test', email='*****@*****.**', passwd='1234567890', image='about:blank') yield from u.save()
import orm
def registerUser(self, msgObject, userName): #(long userID,long password, string[] sensorsPresent): #msg contains userid+capabilities ''' To do: Parse the incoming message and store the capabilities of a particular user in the DB ''' print "Inside RegisterUser" foundFlag = False try: u = User.get(User.username==userName) foundFlag = True except User.DoesNotExist: if str(msgObject['ActivityRecognition'])=='present': activity = True else: activity = False if str(msgObject['DownloadAllowed'])=='yes': download = True else: download = False if 'Location' in msgObject: lAr = msgObject['Location'] location = str(lAr[0]) latitude = str(lAr[1]) longitude = str(lAr[2]) else: location = 'Hardcoded' latitude = 0.0 longitude = 0.0 u = User(username=str(userName), RegistrationDate=datetime.datetime.now(), ActivityRecognition=activity, DownloadAllowed=download, Location=location, Latitude=latitude, Longitude=longitude) u.save() #print "User Saved" '''Update location anyways, if the key is present ''' if 'Location' in msgObject: lAr = msgObject['Location'] location = str(lAr[0]) latitude = str(lAr[1]) longitude = str(lAr[2]) u.Longitude = longitude u.Latitude = latitude u.Location = location u.save() if foundFlag: #User already exists. Should handle it differently. #Means it could be a updation message. Delete all his previous Sensor Relation entries and fill new ones for now. print "User already existed! Deleting previous relation entries!" surtoD = SensorUserRel.select().where(SensorUserRel.user==u) for s in surtoD: s.delete_instance() sensorQueue = [] numSensor = eval(str(msgObject['noSensors'])) for i in range(1, numSensor+1): ''' TODO: Check for fields to be empty before parsing them with eval! ''' sName = "sensor" + str(i) try: s = Sensor.get(Sensor.SensorType==str(msgObject[sName][0]), Sensor.maxRange==eval(str(msgObject[sName][1])), Sensor.minDelay==eval(str(msgObject[sName][2])), Sensor.power==eval(str(msgObject[sName][3])), Sensor.resolution==eval(str(msgObject[sName][4]))) except Sensor.DoesNotExist: s = Sensor(name="temp", SensorType=str(msgObject[sName][0]), maxRange=eval(str(msgObject[sName][1])), minDelay = eval(str(msgObject[sName][2])), power = eval(str(msgObject[sName][3])), resolution=eval(str(msgObject[sName][4]))) s.save() sensorQueue += [s] for s in sensorQueue: sur = SensorUserRel(user=u, sensor=s) sur.save() print "Registered" + str(userName) + " Successfully!" self.msgHandler.send_message(mto=str(self.rMessage['from']).split("/")[0], msubject="Registration Successful!", mbody="Thank you! Registration/Updation Successful!") return
def save(): user = User(2, 'zhang') user.save()