def get_facebook_info(user): access_token = SocialToken.objects.filter( account__user=user, account__provider='facebook').first() graph = facebook.GraphAPI(access_token=access_token, version=2.10) fb_info = [ 'name', 'location', 'hometown', 'friends', 'education', 'birthday', 'religion', 'age_range' ] fb_user_info = graph.get_object(id='me', fields=', '.join(fb_info)) user_info = UserInfo() for k in fb_user_info: if k in fb_info: if k == 'education': temp = fb_user_info['education'] user_info.education = temp[-1]['school']['name'] if k == 'location': user_info.location = fb_user_info['location']['name'] if k == 'hometown': user_info.hometown = fb_user_info['hometown']['name'] if k == 'friends': user_info.num_friends = fb_user_info['friends']['summary'][ 'total_count'] if k == 'name': user_info.name = fb_user_info['name'] if k == 'age_range': user_info.age_range = fb_user_info['age_range']['min'] if k == 'religion': user_info.religion = fb_user_info['religion'] user_info.save()
def getUserInfo(user): """Returns UserInfo associated with User Creates one, if it doesn't already exist.""" try: return UserInfo.objects.get(user=user) except ObjectDoesNotExist: userInfo = UserInfo(user=user) userInfo.save() return userInfo
def signup(request): if request.method == 'GET': form = CustomizedUserCreationForm() context = {"form": form} return render(request, 'signup.html', context=context) else: form = CustomizedUserCreationForm(request.POST) context = {"form": form} if form.is_valid(): user = form.save() if user is not None: ui = UserInfo() ui.user = user ui.status = 'OFFLINE' ui.save() return redirect('login') else: return render(request, 'signup.html', context=context)
def register(request): user = request.user if user.is_authenticated: logout(request) return redirect(to='infomanage') if request.method == 'POST': form = UserInfoForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] user_exist = User.objects.filter(username=username) if user_exist: messages.add_message(request, messages.ERROR, "用户名已存在,请更换!") return redirect(to='register') else: password = form.cleaned_data['password'] User.objects.create_user(username=username, password=password) try: acc = User.objects.get(username=username) user = UserInfo() user.account = acc user.sex = form.cleaned_data['sex'] user.name = form.cleaned_data['username'] user.job = form.cleaned_data['job'] num = UserInfo.objects.order_by( 'job_num').reverse().all()[0].job_num user.job_num = num + 1 user.save() str = "注冊成功!" messages.add_message(request, messages.ERROR, str) except: print('error') pass return redirect(to='login') elif request.method == 'GET': form = UserInfoForm return render(request, 'register.html', {'form': form})
def register(request): if request.method == 'POST': form = UserInfoForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] user_exist = User.objects.filter(username=username) if user_exist: messages.add_message(request, messages.ERROR, "用户名已存在,请更换!") return redirect(to='register') else: user = UserInfo() user.username = form.cleaned_data['username'] user.password = form.cleaned_data['password'] user.sex = form.cleaned_data['sex'] user.job = form.cleaned_data['job'] user.job_num = form.cleaned_data['job_num'] user.save() login(request, user) return redirect(to='infomanage') elif request.method == 'GET': form = UserInfoForm return render(request, 'register.html', {'form': form})
def registration(request): logger.debug("REGISTRATION") if request.user.is_active: return HttpResponse("Already registered") else: username = request.POST.get('username', '') email = request.POST.get('email', '') password = request.POST.get('password', '') skill1 = request.POST.get('skill1', '') skill2 = request.POST.get('skill2', '') skill3 = request.POST.get('skill3', '') skill1info = request.POST.get('skill1info', '') skill2info = request.POST.get('skill2info', '') skill3info = request.POST.get('skill3info', '') logger.debug("reg user:"******"reg pw:" + password) if email: try: u = User.objects.create_user(username=str(username), email=email, password=str(password)) u.set_password(password) u.save() except Exception: return HttpResponse("This username is taken.") user = auth.authenticate(username=str(username), password=str(password)) ui = UserInfo(message="{}::{}::{}".format(skill1, skill2, skill3), authuser_id_id=u.id) ui.save() if skill1: try: s = SkillType.objects.get(name=skill1) except Exception: s = SkillType(level=1,name=skill1) s.save() if s: si = SkillInstance(user_id=ui, skill_type_id=s, instance_type="Offer",snippet=skill1info) si.save() if skill2: try: s = SkillType.objects.get(name=skill2) except Exception: s = SkillType(level=1,name=skill2) s.save() if s: si = SkillInstance(user_id=ui, skill_type_id=s, instance_type="Offer",snippet=skill2info) si.save() if skill3: try: s = SkillType.objects.get(name=skill3) except Exception: s = SkillType(level=1,name=skill3) s.save() if s: si = SkillInstance(user_id=ui, skill_type_id=s, instance_type="Offer",snippet=skill3info) si.save() auth.login(request, user) return redirect("/") template = loader.get_template('app/registration.html') context = RequestContext(request, { 'current_user': str(request.user), "page": "registration", }) logger.debug("Data: " + str(email) + " " + str(password) + " " + str(skill1)) return HttpResponse(template.render(context))