def setUp(self): permission = UserPermission.objects.create(userPermissionID=3, userType='user', modifyOtherAccount=0, modifyPost=0) permission1 = UserPermission.objects.create(userPermissionID=2, userType='editor', modifyOtherAccount=0, modifyPost=1) permission.save() permission1.save() user = User.objects.create_user(username='******', password='******', email='*****@*****.**') user_profile = MyUser(user=user) user_profile.userCreateTime = datetime.date.today() user_profile.userBirthday = datetime.date(2018, 1, 1) user_profile.userDescription = 'i am user test1' obj = UserPermission.objects.filter(userPermissionID=2) user_profile.userPermissionID = obj[0] user_profile.save() user = User.objects.create_user(username='******', password='******', email='*****@*****.**') user_profile = MyUser(user=user) user_profile.userCreateTime = datetime.date.today() user_profile.userBirthday = datetime.date(2018, 10, 10) user_profile.userDescription = 'i am user test2' obj = UserPermission.objects.filter(userPermissionID=3) print('obj:') print(obj) user_profile.userPermissionID = obj[0] user_profile.save()
def signup(request): if request.method == 'POST': form = signupForm(request.POST) if form.is_valid(): newUser = MyUser() try: _newUser = User.objects.create_user(username=request.POST['username'], password=request.POST['password'] , first_name=request.POST['name'], email=request.POST['email']) newUser.user = _newUser newUser.birthday = request.POST['birthday_year'] + '-' + request.POST['birthday_month'] + '-' + \ request.POST['birthday_day'] newUser.save() except: form.add_error(None, 'username "' + request.POST['username'] + '" already existed') return render(request, 'signup.html', {'signupForm': form}) return HttpResponseRedirect(reverse('login')) else: return render(request, 'signup.html', {'signupForm': form}) else: form = signupForm() return render(request, 'signup.html', {'signupForm': form})
def register(req): # print("django version: %s" % django.get_version()) if req.method == 'POST': message = 'please recheck your information' username = req.POST.get("userName", None) email = req.POST.get("inputEmail", None) passwd = req.POST.get("inputPassword", None) passwd2 = req.POST.get("repeatPassword", None) if passwd != passwd2: message = 'password are different' return render(req, 'register/register.html', locals()) else: if not User.objects.all().filter(email=email): if not User.objects.all().filter(username=username): # 使用内置User自带create_user方法创建用户,不需要使用save() user = User.objects.create_user(username=username, password=passwd, email=email) # 如果直接使用objects.create()方法后不需要使用save() user_profile = MyUser(user=user) user_profile.userCreateTime = datetime.date.today() user_profile.userBirthday = datetime.date(2017, 3, 22) user_profile.userDescription = 'i am what i am' # user_profile.userPermission = UserPermission.objects.create( # userPermissionID_id=3, # userType='user', # modifyOtherAccount=False, # modifyPost=False, # ) obj = UserPermission.objects.filter(userPermissionID=3) user_profile.userPermissionID = obj[0] # user_profile.userPermissionID_id = 3 user_profile.save() mailname = EmailBackend() user = mailname.authenticate(email=email, password=passwd) print(user) if user and user.is_active: auth.login(req, user, backend='users.views.EmailBackend') name = user.username print("user name: %s" % name) # return render_to_response('member.html', {'username': username}) return redirect('/') return render(req, 'register/register.html', {'message': 'username already exists'}) return render(req, 'register/register.html', {'message': 'user email already exists'}) return render(req, "register/register.html")
def oauth_callback(request): code = request.GET['code'] url = 'https://api.weibo.com/oauth2/access_token' values = { 'client_id': '2027959579', 'client_secret': 'f839c30fc7681ba271dd28d2356b37d4', 'grant_type': 'authorization_code', 'redirect_uri': 'themer.chinacloudapp.cn/callback', 'code': code } data = urllib.parse.urlencode(values) data = data.encode('ascii') req = urllib.request.Request(url, data) response = urllib.request.urlopen(req) r = response.read().decode('utf-8') result = json.loads(r) print(result) access_token = result['access_token'] uid = result['uid'] expire_time = result['expires_in'] d = get_info(access_token, uid) try: user = User.objects.get(username=uid) except: user = User.objects.create_user(uid, '[email protected]', '********') user.save() user = authenticate(username=uid, password='******') login(request, user) with urllib.request.urlopen(d['avatar']) as response: data = response.read() avatar = Avatar(user=user, primary=True) image_name = str(uuid.uuid4()) + ".jpg" avatar.avatar = ContentFile(data, image_name) avatar.save() try: ouser = MyUser.objects.get(user=user) except: ouser = MyUser(user=user) ouser.type = 1 ouser.access_token = access_token ouser.expire_time = int(expire_time) ouser.username = d['name'] ouser.save() return redirect('/timeline')
def save(self): account = MyUser( phone_number=self.validated_data['phone_number'], email=self.validated_data['email'], first_name=self.validated_data['first_name'], last_name=self.validated_data['last_name'], username=self.validated_data['username'], ) password = self.validated_data['password'] password2 = self.validated_data['password2'] if password and password2 and password != password2: raise serializers.ValidationError({'گذرواژه ها باید یکسان باشند'}) account.set_password(password) account.save() return account
def save(self): user = MyUser( email=self.validated_data['email'], username=self.validated_data['username'], ) password1 = self.validated_data['password'] password2 = self.validated_data['password2'] try: password_validation.validate_password(password2, self.instance) except forms.ValidationError as error: raise serializers.ValidationError( {'password': '******'}) if password1 and password2 and password1 != password2: raise serializers.ValidationError( {'password': '******'}) user.set_password(password1) user.save() return user
def signup(request): if request.method == 'POST': form = signupForm(request.POST) if form.is_valid(): newUser = MyUser() try: _newUser = User.objects.create_user( username=request.POST['username'], password=request.POST['password'], first_name=request.POST['name'], email=request.POST['email']) newUser.user = _newUser newUser.birthday = request.POST['birthday_year'] + '-' + request.POST['birthday_month'] + '-' + \ request.POST['birthday_day'] newUser.save() except: form.add_error( None, 'username "' + request.POST['username'] + '" already existed') return render(request, 'signup.html', {'signupForm': form}) return render( request, 'signin.html', { 'form': loginForm(), 'msg': 'You have successfully registered,' ' login now' }) else: return render(request, 'signup.html', {'signupForm': form}) else: form = signupForm() return render(request, 'signup.html', {'signupForm': form})