def post(self): logging.debug(self.fsid) if Mgdb().find_login(self.jsonbody): logging.debug('登录成功') fsname = self.jsonbody[name] fsid = Mgdb().get_fsid(fsname) self.set_cookie('fsid', fsid, gvars.domain, expires_days=30) self.set_cookie('fsname', fsname, gvars.domain, expires_days=30) self.write_ok("登录成功") else: logging.debug('登录失败') self.write_failure("登录失败")
def send_comes(name): avatar = Mgdb().get_avatar(name) send = {'type': 2, 'from': name, 'time': get_time()} if avatar: send['msg'] = avatar for handler in online_users.values(): if handler: handler.write_message(json.dumps(send))
def post(self): if check_register(self.jsonbody) is True: if Mgdb().add_login(self.jsonbody) is True: self.write_ok("注册成功") else: self.write_failure("用户名或邮箱已存在") else: self.write_failure("信息填写有误")
def get(self): name = self.get_argument('name') user = Mgdb().find_user({'name': name}) if user: if 'avatar' in user: user['avatar'] = gvars.image_url + user['avatar'] self.write_dict(user) else: self.write_failure('用户不存在')
def get(self): names_avatars = [] for fsid in online_users.keys(): if fsid != self.fsid: if fsid in ROBOTS: name = fsid avatar = Mgdb().get_avatar(name) if not avatar: avatar = '' else: user = Mgdb().find_user({"fsid": fsid}) name = user['name'] if 'avatar' in user: avatar = gvars.image_url + user['avatar'] else: avatar = '' names_avatars.append({'name': name, 'avatar': avatar}) d = {'online_users': names_avatars} self.write_dict(d)
def post(self): email = json.loads(self.request.body.decode('utf-8')) if Mgdb().find_email(email): logging.debug('email already exist') else: v_code = sys_email.send_verify_code(email[mail]) if v_code is not None: emails_codes[email[mail]] = v_code else: logging.debug('send email failed')
def post(self): files = self.request.files.get('file') if files: avatar = files[0] ext = os.path.splitext(avatar['filename'])[1] filename = str(time.time()) + ext avatar_file = get_image_file(filename) with open(avatar_file, 'wb') as f: f.write(avatar['body']) logging.warn('upload: ' + avatar_file) Mgdb().update_avatar(self.name, filename) self.write_ok("上传成功")
def send_msg2name(from_name, to_name, msg): """ A发送给B一条消息, 系统要给A和B各发一条 A收到{type: -1, from: B} B收到{type: 1, from: A} """ send = {'msg': msg, 'time': get_time()} from_fsid = Mgdb().get_fsid(from_name) to_fsid = Mgdb().get_fsid(to_name) if from_fsid in online_users: from_handler = online_users[from_fsid] if from_handler: send['type'] = -1 send['from'] = to_name from_handler.write_message(json.dumps(send)) if to_fsid in online_users: to_handler = online_users[to_fsid] if to_handler: send['type'] = 1 send['from'] = from_name to_handler.write_message(json.dumps(send))
def on_message(self, message): if isinstance(message, unicode): # 普通消息 msg = json.loads(message) if msg.has_key('type') and msg.has_key('to') and msg.has_key( 'msg'): # 验证发送过来的cookie if msg['type'] == 99: fsid = msg['msg'][:32] name = msg['msg'][32:] if Mgdb().find_user({'fsid': fsid, 'name': name}): self.fsid = fsid self.name = name logging.warn('on_open: ' + str(id(self))) send_comes(name) # 移除同一用户旧的handler if fsid in online_users.keys(): online_users.pop(fsid) send_comes(name) online_users[fsid] = self elif self.fsid in online_users: # image:// + 图片url if msg['msg'] == 'image://' and self.temp: msg['msg'] += gvars.image_url + self.temp self.temp = None if msg['type'] == 0: # all send_msg2all(self.name, msg['msg']) elif msg['type'] == 1: # name if msg['to'] in ROBOTS: # robot send_msg2name(self.name, msg['to'], msg['msg']) text = tuling.tuling_chat(self.fsid, msg['msg']) send_msg2name(msg['to'], self.name, text) else: # person send_msg2name(self.name, msg['to'], msg['msg']) else: # 二进制文件, 先缓存到 self.temp, 接收到下一条文本消息再处理 self.temp = str(time.time()) + '.jpg' temp = os.path.join(gvars.image_dir, self.temp) with open(temp, 'wb') as f: f.write(message)
def post(self): logging.debug(self.fsid) if Mgdb().find_login(self.jsonbody): logging.debug('登录成功') fsname = self.jsonbody[name] fsid = Mgdb().get_fsid(fsname) self.set_cookie('fsid', fsid, gvars.domain, expires_days=30) self.set_cookie('fsname', fsname, gvars.domain, expires_days=30) self.write_ok("登录成功") else: logging.debug('登录失败') self.write_failure("登录失败") class LogoutHandler(BaseHandler): ''' 退出登录 ''' def get(self): self.write_ok("退出登录成功") if self.name: logging.warn('logout: ' + self.name) if __name__ == "__main__": # run on local pass r = {u'username': u'\u8d85\u54e5', u'password': u'chao'} logging.debug(Mgdb().find_login(r))
def post(self): if Mgdb().update_user(self.name, self.jsonbody): self.write_ok('更新成功') else: self.write_failure('更新失败')
class Post: def __init__(self): self.db = Mgdb() def std_post(self,post): start = post """ std = { 'title':"", 'author':"", 'post':'', 'tags':"", 'time':"", 'username':'' 'permalink':'' 'id':1 } """ if not start.has_key('time') or start['time'] == None: start['time'] = time.strftime("%Y-%m-%d %H:%M", time.localtime()) else: start['last_mod_time'] = time.strftime("%Y-%m-%d %H:%M", time.localtime()) if not start.has_key('author') or start['author'] == None: start['author'] = config.AUTHOR if not start.has_key('title') or start['title'] == None: start['title'] = "New Post" if not start.has_key('posts') or start['posts'] == None: start['post'] = "Hello flask" if not start.has_key('username') or start['username'] == None: start['username'] = "******" if not start.has_key('id') or start['id'] == None: start['id'] = self.db.count_document()['count'] + 1 if not start.has_key('permalink') or start['permalink'] == None: start['permalink'] = ""+ time.strftime("%Y-%m-%d-", time.localtime())+ start['title'].replace(' ',"-") return start def add_markdown(self,post_list): #add markdown key if post_list == None: return post_list start = [] for post in post_list: post['mk_posts'] = markdown2.markdown(post.get('posts')) start.append(post) return start #查 def get_all_Post(self): all_post = [ {'title':"This is my First Post",'contents':"im am super man"}, {'title':"This is my sec Post",'contents':"just a test"}, ] post = self.db.get_document({}) post_list = [{"title":"hello",'posts':'test'}] result_posts = [] if post['result']: post_list = post['documents'] for post in post_list: post['mk_posts'] = markdown2.markdown(post.get('posts','no')) result_posts.append(post) return result_posts else: print "no data" return [{"title":"hello",'posts':'<h1>xxxxxxxxxxxxx</h1>'}] #查id def get_Post_byID(self,id=None): pass #search by permalink def get_Post_byPermalink(self,permalink): post = self.db.get_document({'permalink':permalink})['documents'] posts = self.add_markdown(post) if posts != None: return posts else: return [{'title':'no this post'}] #增 def new_Post(self,post_contents_dict={}): result = {'result':False,'reason':"sucess put in db"} start = post_contents_dict if start['posts'] != u'' and start['title'] != u'': print self.std_post(post_contents_dict) self.db.insert_document(self.std_post(post_contents_dict)) else: print "wuxiao data" return result #删 def del_Post(self,_id=None): print _id try: # use db del result = self.db.del_document({'_id':ObjectId(_id)}) if result['result']: print result['reason'] return True else: return False except Exception: print "objectid error" return False # def get_Post_by_Pagenumber(self,current_page,limit=config.PAGE_NUMBERS,dict={}): #pass documents with get documents if current_page == 1: skip = 0 elif current_page > 1: skip = (current_page -1) * limit print skip else: print "error" print "current_page:",current_page print "limit:",limit print "skip:",skip post = self.db.get_document(dict,limit=limit,skip=skip)['documents'] posts = self.add_markdown(post) print posts print "list length:",len(posts) if posts != None: return posts else: return [{'title':'no this post'}] #get by _id #search by permalink def get_by_id(self,id): try: post = self.db.get_document({'_id':ObjectId(id)})['documents'] posts = self.add_markdown(post) if posts != None: return posts else: return [{'title':'no this post'}] except Exception: print "error" return [{'title':'no this post'}] def update_by_id(self,id,new={}): try: new = self.std_post(new) result = self.db.update_document({'_id':ObjectId(id)},new) result = True if True: return True else: return False except Exception: print "objectid error" return [{'title':'no this post'}]
def __init__(self): self.db = Mgdb()