def get_says(request): """获得一组说说""" u_name = session.has_login(request) if not u_name: return HttpResponse("{'error' : 'Y', 'info' : '请先登录'}") if request.method == 'GET': if 'sayIDs' in request.GET: sayIDs = request.GET['sayIDs'] sayIDs = sayIDs.split(',') ret = "{'error' : 'N', 'says' : [" for i in range(len(sayIDs)): #只能是数字 if sayIDs[i].isdigit(): sayIDs[i] = int(sayIDs[i]) #验证权限 if says.is_author(sayIDs[i], u_name): res = says.get_text_and_time(sayIDs[i]) if res: #对单引号进行转义 sayText = res[0].replace('\'', '`') ret += "{'sayId' : %d, 'sayText' : '%s', 'time' : '%s'}," % (sayIDs[i], sayText, res[1]) ret += "]}" return HttpResponse(ret) return HttpResponse("{'error' : 'Y', 'info' : '未定义错误'}")
def get_last_one(request): u_name = session.has_login(request) if not u_name: return HttpResponse('') last_one = says.get_last_one_text_time(u_name) if last_one: sayText = last_one[1].replace('\'', '`') ret = "{'sayId' : %s, 'sayText' : '%s', 'time' : '%s'}" % (last_one[0], sayText, last_one[2]) return HttpResponse(ret) return HttpResponse('')
def public_say(request): """存储用户的说说""" u_name = session.has_login(request) if not u_name: return HttpResponseRedirect('/login/') if request.method == 'POST': if 'say' in request.POST: says.save(u_name, request.POST['say']) return HttpResponse('说说发布成功') return HttpResponse('你懂的')
def all_say(request): """获取所有的说说的ID""" u_name = session.has_login(request) #用户还没有登录,返回错误信息 if not u_name: ret = "{'error' : 'Y', 'info' : '请先登录'}" return HttpResponse(ret) #合法用户,返回ID sayIds = says.get_all_sayId(u_name) ret = "{'error' : 'N', 'sayIds' : %s}" % sayIds.__repr__() return HttpResponse(ret)
def friend_request(request): u_name = session.has_login(request) if not u_name: return HttpResponse("{'type' : '%d', 'info' : '请先登陆'}" % error.NO_LOGIN) if request.method == 'GET': ret = "{'type' : 'FR', 'list' : [" li = add_friend.getAllRequest(u_name) if li: for user in li: ret += "{'u_name' : '%s', 'r_name' : '%s'}," % user ret += "]}" return HttpResponse(ret) return HttpResponse("{'type' : '%d', 'info' : '未使用GET方式'}" % error.NO_GET_ERROR)
def get_say(request): u_name = session.has_login(request) if not u_name: return HttpResponse('') if request.method == 'GET': if 'sayID' in request.GET: sayID = request.GET['sayID'] #sayID必须是数字 if sayID.isdigit(): sayID = int(sayID) #请求是否权限允许 if says.is_author(sayID, u_name): ret = says.get_text(sayID) return HttpResponse(ret) return HttpResponse('')
def friend_confirm(request): u_name = session.has_login(request) if not u_name: return HttpResponse('请先登陆') if request.method == 'GET': if 'type' in request.GET and 'friendFrom' in request.GET: confirm_type = request.GET['type'] friendFrom = request.GET['friendFrom'] if confirm_type == 'disagree': add_friend.delete(friendFrom, u_name) return HttpResponse('确认成功') if add_friend.confirm(friendFrom, u_name): friends.save(friendFrom, u_name) return HttpResponse('确认成功') return HttpResponse('')
def add_friends(request): """添加好友请求""" u_name = session.has_login(request) if not u_name: return HttpResponse('请先登录') if request.method == 'POST': if 'friendName' in request.POST: friendTo = request.POST['friendName'] if security.is_valid_u_name(u_name): if friends.is_friend(u_name, friendTo): return HttpResponse('已是好友') else: add_friend.add_friend(u_name, friendTo) return HttpResponse('请求已发出') return HttpResponse('你懂的')
def register(request): #如果用户已经登录 if session.has_login(request): return HttpResponseRedirect('/mainpage/') form = Form() if request.method == 'POST': if ('u_name' in request.POST and 'u_pwd' in request.POST and 'u_confirm' in request.POST and 'u_email' in request.POST): form = Form(request.POST['u_name'], request.POST['u_pwd'], request.POST['u_confirm'], request.POST['u_email']) if form.valid(): form.save() response = HttpResponseRedirect('/mainpage/') cookie.set_cookie(response, request.POST['u_name']) return response return render_to_response('register.html', {'user' : form})
def login(request): """处理用户登录验证""" #如果用户已经登录 if session.has_login(request): return HttpResponseRedirect('/mainpage/') u_name = '' u_pwd = '' error = '' if 'u_name' in request.POST and 'u_pwd' in request.POST: u_name = request.POST['u_name'] u_pwd = request.POST['u_pwd'] login = Log_User(u_name, u_pwd) if login.validate(): response = HttpResponseRedirect('/mainpage/') cookie.set_cookie(response, login.u_name) return response else: error = '用户名或者密码有误,请重新登录' return render_to_response('login.html', {'error' : error, 'u_name' : u_name, 'u_pwd' : u_pwd})
def info(request): """相应用户信息查询和修改的请求""" u_name = session.has_login(request) if not u_name: return HttpResponseRedirect('/login/') year_to_now = range(1900, time.localtime().tm_year + 1) months = range(1, 13) days = range(1, 32) user_info = User_Info('', '', '', '', '', '', '', '', '') if request.method == 'POST': post = request.POST if ('r_name' in post and 'sex' in post and 'birth_year' in post and 'birth_month' in post and 'birth_day' in post and 'school_type' in post and 'school' in post and 'grade' in post): r_name = post['r_name'] sex = post['sex'] birth_year = post['birth_year'] birth_month = post['birth_month'] birth_day = post['birth_day'] school_type = post['school_type'] school_name = post['school'] school_grade = post['grade'] user_info = User_Info(u_name, r_name, sex, birth_year, birth_month, birth_day, school_type, school_name, school_grade) if user_info.is_validate(): user_info.save() return HttpResponse('信息修改成功') return HttpResponse(('信息修改失败,由于:%s %s' % (user_info.r_name_error, user_info.school_name_error))) #user_info = User_Info(u_name, r_name, sex, birth_year, birth_month, # birth_day, school_type, school_name, school_grade) else: info = base_info.getInfo(u_name) if info: user_info = User_Info(*info) else: user_info = User_Info() return render_to_response('info.html', {'year_to_now' : year_to_now, 'months' : months, 'days' : days, 'user' : user_info})
def say(request): u_name = session.has_login(request) if not u_name: return HttpResponseRedirect('/login/') return render_to_response('say.html')