def login(req): if authutil.is_logined(req): return authutil.api_error(500,'已经是登陆状态') form = LoginForm(req.POST) if form.is_valid(): email = form.cleaned_data['email'].encode('utf-8') password = form.cleaned_data['password'].encode('utf-8') remember = form.cleaned_data['remember'] try: ukey = oocrpc.backend.Login({'Email':email,'Password':password}) userinfo = oocrpc.backend.GetUserInfo(ukey) except oocrpc.RpcError,ex: if ex.message.startswith('EmailError'): return HttpResponse(simplejson.dumps({'emailerror':'邮箱不存在'})) elif ex.message.startswith('PasswordError'): return HttpResponse(simplejson.dumps({'passworderror':'密码不正确'})) else: # todo add log return HttpResponse(simplejson.dumps({'internalerror':'服务器开小差了'})) retdict = { 'ukey':ukey, 'userinfo':userinfo } resp = HttpResponse(retdict) if remember: timeout = 3600*24*30*6 # half of a year else: timeout = None authutil.set_logined(req,resp,ukey,timeout) return resp
def signup(req): if authutil.is_logined(req): return authutil.api_error(500,'已经是登陆状态') form = SignupForm(req.POST) if form.is_valid(): email = form.cleaned_data['email'].encode('utf-8') password = form.cleaned_data['password'].encode('utf-8') nickname = form.cleaned_data['nickname'].encode('utf-8') try: ukey = oocrpc.backend.RegisterUser({'Nickname':nickname,'Email':email,'Password':password}) except oocrpc.RpcError, ex: if ex.message.startswith('EmailError'): return HttpResponse(simplejson.dumps({'emailerror':'邮箱已经存在'})) elif ex.message.startswith('NicknameError'): return HttpResponse(simplejson.dumps({'nicknameerror':'用户昵称重复'})) else: return HttpResponse(simplejson.dumps({'internalerror':'服务器开小差了'})) resp = HttpResponse(simplejson.dumps({'ok':1})) timeout = 3600*24*30*6 authutil.set_logined(req,resp,ukey,timeout) return resp