def user_profile_update(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/keenhome/accounts/login/') UserProfile = request.user.get_profile() if request.method == 'POST': form = UserProfileForm(request.POST) if form.is_valid(): UserProfile.name = form.cleaned_data["name"] UserProfile.productNo = form.cleaned_data["productNo"] UserProfile.location = form.cleaned_data["location"] UserProfile.average_temp = form.cleaned_data["average_temp"] #problem happens here: UserProfile.home_type = form.cleaned_data["home_type"] UserProfile.save() return HttpResponseRedirect('/keenhome/accounts/user_profile/') else: form = UserProfileForm() return render_to_response("polls/user_profile_update.html", {'form': form}, context_instance=RequestContext(request)) else: form = UserProfileForm() return render_to_response("polls/user_profile_update.html", {'form': form}, context_instance=RequestContext(request))
def user_registration_handler(sender, **kwargs): user = kwargs['user'] request = kwargs['request'] logger.info("new user %s registered." % user.get_username()) form = MyRegistrationForm(data=request.POST) profile = UserProfile() profile.age = form.age profile.birthday = form.bithday profile.save()
def user_profile_update(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/keenhome/accounts/login/') UserProfile = request.user.get_profile() if request.method == 'POST': form=UserProfileForm(request.POST) if form.is_valid(): UserProfile.name = form.cleaned_data["name"] UserProfile.productNo = form.cleaned_data["productNo"] UserProfile.location = form.cleaned_data["location"] UserProfile.average_temp = form.cleaned_data["average_temp"] #problem happens here: UserProfile.home_type = form.cleaned_data["home_type"] UserProfile.save() return HttpResponseRedirect('/keenhome/accounts/user_profile/') else: form = UserProfileForm() return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request)) else: form = UserProfileForm() return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
def register(request): if request.user.is_authenticated(): return HttpResponseRedirect('/main/') categories = Category.objects.all() subcats = SubCategory.objects.all() user = UserProfile() if request.method== 'POST': form=Register(request.POST) context = {'form': form,'my_template': 'NotLoggedIn.html','categories': categories, 'subcats': subcats} if form.is_valid(): if form.cleaned_data['password'] != form.cleaned_data['passwordRetype']: form._errors["password"] = ErrorList(["Passwords do not match"]) return render(request, 'register.html', context) if User.objects.filter(username = form.cleaned_data['userName']).exists(): form._errors["userName"] = ErrorList(["User already exists"]) return render(request, 'register.html', context) form._errors["userName"] = ErrorList(["this user already exists"]) user.user=User.objects._create_user(form.cleaned_data['userName'],form.cleaned_data['email'],form.cleaned_data['password'],False,False,first_name=form.cleaned_data['name'],last_name=form.cleaned_data['lastName']) user.gender=form.cleaned_data['gender'] user.save() return HttpResponseRedirect('/login/') else: form=Register() context = {'form': form,'my_template': 'NotLoggedIn.html','categories': categories, 'subcats': subcats} return render(request, 'register.html', context)
def register(request): if request.user.is_authenticated(): return HttpResponseRedirect('/main/') categories = Category.objects.all() subcats = SubCategory.objects.all() user = UserProfile() if request.method == 'POST': form = Register(request.POST) context = { 'form': form, 'my_template': 'NotLoggedIn.html', 'categories': categories, 'subcats': subcats } if form.is_valid(): if form.cleaned_data['password'] != form.cleaned_data[ 'passwordRetype']: form._errors["password"] = ErrorList( ["Passwords do not match"]) return render(request, 'register.html', context) if User.objects.filter( username=form.cleaned_data['userName']).exists(): form._errors["userName"] = ErrorList(["User already exists"]) return render(request, 'register.html', context) form._errors["userName"] = ErrorList(["this user already exists"]) user.user = User.objects._create_user( form.cleaned_data['userName'], form.cleaned_data['email'], form.cleaned_data['password'], False, False, first_name=form.cleaned_data['name'], last_name=form.cleaned_data['lastName']) user.gender = form.cleaned_data['gender'] user.save() return HttpResponseRedirect('/login/') else: form = Register() context = { 'form': form, 'my_template': 'NotLoggedIn.html', 'categories': categories, 'subcats': subcats } return render(request, 'register.html', context)
def register(request): """ Register a new user and create their UserProfile """ data = request.data try: # Email input check email = str(data['email_id']) if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email): return Response({'message': 'Please enter a valid email address.'}, status=403) # Unique username check username = str(data["username"]) try: user = User.objects.get(username=username) return Response({"message": "Username taken"}, status=412) except User.DoesNotExist: pass # Already registered email check if UserProfile.objects.filter(auth_user__email=email).exists(): return Response({"message": "Email already registered"}, status=412) else: first_name = str(data['first_name']) last_name = data['last_name'] password = str(data["password"]) confirm_pass = str(data["confirm_pass"]) if password != confirm_pass: return Response({"message": "Passwords don't match"}, status=412) if last_name == None: last_name = '' if (hasNumbers(first_name) or hasNumbers(last_name)): return Response({"message": "Invalid first/last name format"}, status=412) gender = str(data['gender']) # M/F/O valid_values = ["M", "F", "O"] if gender not in valid_values: return Response({"message": "Invalid gender value"}, status=412) age = int(data["age"]) if not age > 0: return Response({"message": "Invalid age value"}, status=412) location = str(data["location"]) if location == "": return Response({"message": "Please enter a location"}, status=412) income = int(data['income']) if income < 0: return Response({"message": "Invalid income value"}, status=412) try: # Create django User user = User() user.username = username user.email = email user.set_password(password) user.first_name = first_name user.last_name = last_name user.save() try: # Create UserProfile profile = UserProfile() profile.auth_user = user profile.gender = gender profile.age = age profile.income = income profile.location = location profile.save() return Response({"message": "Registered!"}, status=201) except: user.delete() return Response( {"message": "An error occurred. Please try again!"}, status=500) except: return Response( {"message": "An error occurred. Please try again!"}, status=500) except KeyError as missing_data: return Response( {'message': 'Data is Missing: {}'.format(missing_data)}, status=400)