def load(): file_path = os.path.join(os.path.dirname(__file__), 'test_data.json') f = open(file_path, 'r') content = f.read().decode('utf8') f.close() data = json.loads(content) users = data['users'] friends = data['friends'] for user in users: pw_md5 = hashlib.md5() pw_md5.update(user['password']) u = User(name=user['name'], password=pw_md5.hexdigest(), head_img_url=user['head_img_url'], last_login_time=datetime.now()) u.insert() for detail in user['user_details']: ud = UserDetail(user_id=u.id, field_name=detail['field_name'], field_value='%s,v' % detail['field_value'], updated_at=datetime.now()) ud.insert() ui = UserIndex(field_name=detail['field_name'], field_value=detail['field_value'], user_id=u.id) ui.insert() for friend in friends: user_id = UserIndex.get_user_id('phone', friend['user_phone']) friend_id = UserIndex.get_user_id('phone', friend['friend_phone']) f = Friend(user_id=user_id, friend_id=friend_id) f.insert() print "u_id: %d, f_id: %d" % (user_id, friend_id) f = Friend(user_id=friend_id, friend_id=user_id) f.insert() print "u_id: %d, f_id: %d" % (friend_id, user_id)
def post(self): phone = self.get_argument("phone") u = self.get_current_user() pw = '123456' pw_md5 = hashlib.md5() pw_md5.update(pw) u.password = pw_md5.hexdigest() u.update() ud = UserDetail(user_id=u.id, field_name='phone', field_value='%s,v' % phone, updated_at=datetime.now()) ud.insert() ui = UserIndex(field_name='phone', field_value=phone, user_id=u.id) ui.insert() result = {'success':True, 'user':{'id':u.id, 'password':pw}} self.write(json.dumps(result))
def auth_email(username, md5_pw): try: user_id = UserIndex.get_user_id('email', username) u = User.get(user_id) if u.password == md5_pw: return u except: return None return None
def post(self): name = self.get_argument("name") phone = self.get_argument("phone", None) # 保存数据 pw = '123456' pw_md5 = hashlib.md5() pw_md5.update(pw) u = User(name=name, password=pw_md5.hexdigest(), last_login_time=datetime.now()) u.insert() if phone: ud = UserDetail(user_id=u.id, field_name='phone', field_value='%s,v' % phone, updated_at=datetime.now()) ud.insert() ui = UserIndex(field_name='phone', field_value=phone, user_id=u.id) ui.insert() result = {'success':True, 'user':{'id':u.id, 'password':pw}} else: result = {'success':True} self.set_secure_cookie("user", str(u.id)) self.write(json.dumps(result))