def reg(request): #注册用户 #对应的网页模版地址为 mysite/templates/mysite/reg.html if chkAdminCookies(request) == None: return msg(request, "mysite:index", "无权限查看!") if request.method == 'POST': form = RegForm(request.POST) if form.is_valid(): username = request.POST['username'].strip() idsn = request.POST['idsn'].strip() phone = request.POST['phone'].strip() type = int(request.POST['type']) qry_usrs = User.objects.all().filter(idsn = idsn) if len(qry_usrs) != 0: qry_usrs[0].username = username qry_usrs[0].phone = phone qry_usrs[0].save() msg(request, "mysite:reg", '用户已存在并重新修改!') newusr = User(username = username, idsn = idsn, phone = phone, type = type, suffix = idsn[14:18]) newusr.save() return msg(request, "mysite:reg", "注册成功!") return msg(request, "mysite:reg", "关键项未填!") form = RegForm() usrs = User.objects.all() c = { 'form': form, 'usrs': usrs, } return render_to_response('mysite/reg.html', c, context_instance=RequestContext(request))
class Restrict(TestCase): def setUp(self): self.my_admin_data = {'username': '******', 'password': '******'} self.my_admin = User(username=self.my_admin_data['username'], is_staff=True, is_superuser=True) self.my_admin.set_password(self.my_admin_data['password']) self.my_admin.save() def test_get_as_anonymous_user(self): response = self.client.get(reverse(f'{APP_NAME}:restrict')) self.assertEqual(response.status_code, 302) def test_get_as_anonymous_user_following_redirects(self): response = self.client.get(reverse(f'{APP_NAME}:restrict'), follow=True) self.assertEqual(response.status_code, 200) def test_get_as_superuser(self): self.client.login(username=self.my_admin_data['username'], password=self.my_admin_data['password']) response = self.client.get(reverse(f'{APP_NAME}:restrict')) self.assertEqual(response.status_code, 200)
def handlereg(request): c={} username=request.POST['username'] password=request.POST['password'] email=request.POST['email'] if check_format(request) == 0: return msg(request,"mysite:reg","注册格式不对!") exist_user_list = User.objects.all().filter( username=username ) if len(exist_user_list) != 0: return msg(request,"mysite:reg","用户已经存在.") exist_user_list = User.objects.all().filter( email=email ) if len(exist_user_list) != 0: return msg(request,"mysite:reg","邮箱已经存在.") user = User(username=username,password=password,email=email) user.save() promo = Promo(user=user) promo.save() exist_ip_list = Ip.objects.all().filter(ipaddr=get_client_ip(request)) if len(exist_ip_list) !=0 and exist_ip_list[0].reged==False: theboss = exist_ip_list[0].p_user theboss.promo.invites += 1 theboss.promo.save() theboss.balance += 10 theboss.save() exist_ip_list[0].reged=True exist_ip_list[0].save() request.session['username'] = user.username request.session['password'] = user.password return msg(request,'mysite:account', "注册成功!")
def checkUser(self, username, realname, email): user = self.findUser(username) if user: return user parts = realname.split() if len(parts) == 1: first_name = parts[0] last_name = "" else: first_name = parts[0] last_name = parts[1] user = User(username=username, email=email, first_name=first_name, last_name=last_name) user.save()
class NoticeAdminOnly(TestCase): def setUp(self): self.my_admin_data = {'username': '******', 'password': '******'} self.my_admin = User(username=self.my_admin_data['username'], is_staff=True, is_superuser=True) self.my_admin.set_password(self.my_admin_data['password']) self.my_admin.save() def test_get_as_anonymous_user(self): response = self.client.get(reverse(f'{APP_NAME}:notice_admin_only')) self.assertEqual(response.status_code, 200) def test_get_as_superuser(self): self.client.login(username=self.my_admin_data['username'], password=self.my_admin_data['password']) response = self.client.get(reverse(f'{APP_NAME}:notice_admin_only')) self.assertEqual(response.status_code, 200)
def reg(request): #注册用户 #对应的网页模版地址为 mysite/templates/mysite/reg.html if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] form = RegForm(request.POST) if form.is_valid(): qry_usrs = User.objects.all().filter(username=username) if len(qry_usrs) != 0: return HttpResponse('用户已存在!') newusr = User( username=request.POST['username'], password=request.POST['password']) newusr.save() return HttpResponseRedirect(reverse('mysite:reg')) form = RegForm() usrs = User.objects.all() c = { 'form': form, 'usrs': usrs, } return render_to_response('mysite/reg.html', c, context_instance=RequestContext(request))
def userReg(request): if request.method == 'POST': verify_code = request.session['verify_code'] input_verify = request.POST['verify'] returndata = {} # 检查验证码 if verify_code.lower() != input_verify.lower(): returndata = {'code': 100, 'msg': '验证码错误'} # 检查用户名是否存在 userinfo = User.objects.filter( user_name=request.POST['username']).exists() if userinfo: returndata = {'code': 101, 'msg': '用户名已存在'} if returndata: return HttpResponse(json.dumps(returndata), 'application/json') user = User() user.user_name = request.POST['username'] user.password = md5(request.POST['password']) user.save() returndata = {'code': 200, 'msg': '注册成功'} return HttpResponse(json.dumps(returndata), 'application/json') else: return TemplateResponse(request, 'reg.html')