def post(self): errors = [] template = "login.html" post_values = ['name', 'pwd'] args = {} for v in post_values: # Get nessary argument # Use None as default if argument is not supplied args[v] = self.get_argument(v, None) try: # BUG: can not chain like this "member = Member().reload()" member = Member() member.reload(args['name'], args['pwd']) self.clear_all_cookies() self.set_cookie(name="auth", value=member.auth, expires_days=365) self.redirect("/") except exception.NameError: errors.append(u"请填写用户名") except exception.AuthError: errors.append(u"用户名或密码错误") if len(errors) > 0: # No need to go on either name or pwd is None self.render(template, title="Login", master=None, errors=errors)
def post(self): errors = [] template = "login.html" post_values = ['name','pwd'] args = {} for v in post_values: # Get nessary argument # Use None as default if argument is not supplied args[v] = self.get_argument(v, None) try: # BUG: can not chain like this "member = Member().reload()" member = Member() member.reload(args['name'], args['pwd']) self.clear_all_cookies() self.set_cookie(name = "auth", value = member.auth, expires_days = 365) self.redirect("/") except exception.NameError: errors.append(u"请填写用户名") except exception.AuthError: errors.append(u"用户名或密码错误") if len(errors) > 0: # No need to go on either name or pwd is None self.render(template, title = "Login", master = None, errors = errors)
def my_all_messages(uid): # get all messages with sender id or receiver id is uid msgs = db.message.find({ "peer": { "$all": [ int(uid), ] } }).sort("date", 1) t = [] for msg in msgs: # You are the receiver if msg['receiver'] == int(uid) and msg['status'] == cst.UNREAD: # Mark as readed msg['status'] = cst.READ db.message.save(msg) # Reject some if msg['receiver'] == int(uid) and msg['reject'] == True: continue # Jump Over it m = Member(db.member.find_one({"uid": msg['sender']})) msg['sender'] = dict(name=m.name, uid=m.uid, avatar=m.avatar) msg['date'] = (msg['date'] + datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M") t.append(msg) return t
def get_new_messages(uid): # You are the receiver msgs = db.message.find({ "receiver": int(uid), "status": cst.UNREAD }).sort("date", 1) t = [] for msg in msgs: m = Member(db.member.find_one({"uid": msg['sender']})) msg['sender'] = dict(name=m.name, uid=m.uid, avatar=m.avatar) msg['date'] = (msg['date'] + datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M") t.append(msg) return t
def post(self): errors = [] # errors message container member = Member() post_values = ['name','pwd','cpwd','email'] args = {} for v in post_values: # Get nessary argument # Use None as default if argument is not supplied args[v] = self.get_argument(v, None) # Set user name try: member.set_name(args['name']) except exception.NameError: errors.append(u"请填写用户名") except exception.DupKeyError: errors.append(u"用户名已经被使用") except exception.PatternMatchError: errors.append(u"你填写的用户名中有不被支持的字符") # Set user password if args['pwd'] != args['cpwd']: errors.append(u"两次输入的密码不一致") elif args['pwd'] is None and args['cpwd'] is None: errors.append(u"请填写密码") else: member.set_password(args['pwd']) # set user email try: member.set_email(args['email']) except ValueError: errors.append(u"请填写邮箱") except exception.DupKeyError: errors.append(u"邮箱已经被使用") except exception.PatternMatchError: errors.append(u"邮件地址格式不正确") if errors: self.render("register.html", title = u"注册", errors = errors, master = None) else: member.set_secret_key(randomwords(20)) member.verify() member.put() self.set_cookie(name="auth", value=member.auth, expires_days = 365) self.redirect('/')
def get_member_by_name(_name): entity = db.member.find_one({"name": _name}) if entity: return Member(entity=entity) else: return None
def user_entity(self, uid=None, name=None): if uid: return Member(db.member.find_one({"uid": int(uid)})) elif name: return Member(db.member.find_one({"name": name}))
def current_user_entity(self): # For write return Member(db.member.find_one({"auth": self.get_cookie('auth')}))
def post(self): errors = [] # errors message container member = Member() post_values = ['name', 'pwd', 'cpwd', 'email'] args = {} for v in post_values: # Get nessary argument # Use None as default if argument is not supplied args[v] = self.get_argument(v, None) # Set user name try: member.set_name(args['name']) except exception.NameError: errors.append(u"请填写用户名") except exception.DupKeyError: errors.append(u"用户名已经被使用") except exception.PatternMatchError: errors.append(u"你填写的用户名中有不被支持的字符") # Set user password if args['pwd'] != args['cpwd']: errors.append(u"两次输入的密码不一致") elif args['pwd'] is None and args['cpwd'] is None: errors.append(u"请填写密码") else: member.set_password(args['pwd']) # set user email try: member.set_email(args['email']) except ValueError: errors.append(u"请填写邮箱") except exception.DupKeyError: errors.append(u"邮箱已经被使用") except exception.PatternMatchError: errors.append(u"邮件地址格式不正确") if errors: self.render("register.html", title=u"注册", errors=errors, master=None) else: member.set_secret_key(randomwords(20)) member.verify() member.put() self.set_cookie(name="auth", value=member.auth, expires_days=365) self.redirect('/')