def get_oauth_userinfo(self): openid = self.get_open_id() if openid: params = { 'access_token': self.access_token, 'oauth_consumer_key': self.client_id, 'openid': self.openid } rsp = self.do_get(self.API_URL, params) # logger.info(rsp) datas = json.loads(rsp) oauth_user = OAuthUser() oauth_user.openid = openid oauth_user.type = 'qq' oauth_user.token = self.access_token oauth_user.userdata = rsp user_pro = UsersProfile() # temp = save_avatar(datas.get('avatar_url'), 'qq'+str(oauth_user.openid)) # if temp: # user_pro.avatar = temp # else: # user_pro.avatar = None user_pro.nikename = datas.get('nickname', None) user_pro.gender = datas.get('gender', '男') return oauth_user, user_pro
def login(self, request, *args, **kwargs): if 'phone' not in request.data or not request.data['phone']: print(request.data) return JSONResponse(get_data_format(msg='手机号码不能为空')) if 'password' not in request.data or not request.data['password']: return JSONResponse(get_data_format(msg='密码不能为空')) try: phone = request.data['phone'] password = request.data['password'] user = authenticate(username=phone, password=password) except User.DoesNotExist: return JSONResponse(get_data_format(msg='找不到用户')) if user: login(request, user) try: user = UsersProfile.objects.get(user=user) except UsersProfile.DoesNotExist: user = UsersProfile() user.user = user user.save() serializer = UsersProfileSerializer(user, context={'request': request}) print(serializer.data) return JSONResponse(get_data_format(True, None, serializer.data)) else: return JSONResponse(get_data_format(msg='输入的账号密码不正确'))
def create_user(request): if request.method== "POST": form = UserForm(request.POST) if form.is_valid(): password = get_random_string(length=7) data = { 'user_name': form.data['user_name'], 'email' : form.data['email'], 'password': password } auth = User.objects.create_user(data) user_profile = UsersProfile() user_profile.email = form.data['email'] user_profile.user_type = form.data['user_type'] user_profile.user_name = form.data['user_name'] user_profile.user = auth user_profile.save() # send_mail( # 'Customer creation', # 'Please find the credentials.', # 'from_email', # [form.data['email']], # html_message= 'username :% s' % form.data['user_name'], # fail_silently=False, # ) return redirect('/customer_list') else: form = UserForm() return render(request, 'create_user.html', {'form': form})
def get_oauth_userinfo(self): rsp = self.do_get( self.API_URL, params={}, headers={"Authorization": "token " + self.access_token}) datas = json.loads(rsp) # {"login": "******", "id": 54814510, "node_id": "MDQ6VXNlcjU0ODE0NTEw", # "avatar_url": "https://avatars1.githubusercontent.com/u/54814510?v=4", "gravatar_id": "", # "url": "https://api.github.com/users/LB-zhang521", "html_url": "https://github.com/LB-zhang521", # "followers_url": "https://api.github.com/users/LB-zhang521/followers", # "following_url": "https://api.github.com/users/LB-zhang521/following{/other_user}", # "gists_url": "https://api.github.com/users/LB-zhang521/gists{/gist_id}", # "starred_url": "https://api.github.com/users/LB-zhang521/starred{/owner}{/repo}", # "subscriptions_url": "https://api.github.com/users/LB-zhang521/subscriptions", # "organizations_url": "https://api.github.com/users/LB-zhang521/orgs", try: oauth_user = OAuthUser() oauth_user.openid = datas.get('id', None) oauth_user.type = 'github' oauth_user.token = self.access_token oauth_user.userdata = rsp user_pro = UsersProfile() #temp = save_avatar(datas.get('avatar_url','github'+str(oauth_user.openid))) #if temp: # user_pro.avatar = temp # else: # user_pro.avatar = None user_pro.nikename = datas.get('name', None) user_pro.github = datas.get('html_url', None) user_pro.site = datas.get('blog', '') return oauth_user, user_pro except Exception as e: print('获取用户信息错误:', e) return None
def form_valid(self, form): # 存入数据库方式一 # from django.contrib.auth.hashers import make_password # pwd_md5=make_password(form.cleaned_data['password']) # form.cleaned_data['password']=pwd_md5 # UsersProfile.objects.create(**form.cleaned_data) # 存入数据库方式二 user = UsersProfile(**form.cleaned_data) user.set_password(form.cleaned_data['password']) user.save() return super().form_valid(form)
def form_valid(self, form): user = UsersProfile(**form.cleaned_data) user.set_password(form.cleaned_data['password']) user.save() return super().form_valid(form)