def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response('HTML/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def profile(request): if request.method == 'POST': if request.user.profile.hasBio(): userForm = StaffUserForm(request.POST, instance=request.user) profileForm = StaffProfileForm(request.POST, files=request.FILES, instance=request.user.profile) else: userForm = UserForm(request.POST, instance=request.user) profileForm = ProfileForm(request.POST, files=request.FILES, instance=request.user.profile) if userForm.is_valid() and profileForm.is_valid(): userForm.save() profileForm.save() if 'email' in userForm.changed_data: request.user.profile.active_email = False request.user.profile.save() send_email_verify(request, request.user, 'Verify your email with PAClab') if profileForm.cleaned_data['photo']: image = Image.open(request.user.profile.photo) try: x = float(request.POST.get('crop_x', 0)) y = float(request.POST.get('crop_y', 0)) w = float(request.POST.get('crop_w', 0)) h = float(request.POST.get('crop_h', 0)) if x and y and w and h: image = image.crop((x, y, w + x, h + y)) except: pass image = image.resize(settings.THUMBNAIL_SIZE, Image.LANCZOS) image.save(request.user.profile.photo.path) messages.success(request, 'Profile successfully updated') return redirect('website:edit_profile') messages.error(request, 'Invalid form entry') else: if request.user.profile.hasBio(): userForm = StaffUserForm(instance=request.user) profileForm = StaffProfileForm(instance=request.user.profile) else: userForm = UserForm(instance=request.user) profileForm = ProfileForm(instance=request.user.profile) return render( request, 'website/user/editprofile.html', { 'userForm': userForm, 'profileForm': profileForm, 'min_width': settings.THUMBNAIL_SIZE, 'min_height': settings.THUMBNAIL_SIZE })
def user_login(request): # If the request is a HTTP POST, try to pull out the relevant information. if request.method == 'POST': # Gather the username and password provided by the user. # This information is obtained from the login form. # We use request.POST.get('<variable>') as opposed to request.POST['<variable>'], # because the request.POST.get('<variable>') returns None, if the value does not exist, # while the request.POST['<variable>'] will raise key error exception username = request.POST.get('username_login') password = request.POST.get('pass_login') # Use Django's machinery to attempt to see if the username/password # combination is valid - a User object is returned if it is. user = authenticate(username=username, password=password) user_form = UserForm() profile_form = UserProfileForm() # If we have a User object, the details are correct. # If None (Python's way of representing the absence of a value), no user # with matching credentials was found. if user: # Is the account active? It could have been disabled. if user.is_active: # If the account is valid and active, we can log the user in. # We'll send the user back to the homepage. login(request, user) return redirect('/dashboard/') else: # An inactive account was used - no logging in! return render( request, 'website/index.html', { 'login_error': 'Your account is disabled!', 'user_reg_form': user_form, 'user_profile_form': profile_form }) else: # Bad login details were provided. So we can't log the user in. # print "Invalid login details: {0}, {1}".format(username, password) return render( request, 'website/index.html', { 'login_error': 'Invalid Credentials', 'user_reg_form': user_form, 'user_profile_form': profile_form }) # The request is not a HTTP POST, so display the login form. # This scenario would most likely be a HTTP GET. else: user_form = UserForm() profile_form = UserProfileForm() # No context variables to pass to the template system, hence the # blank dictionary object... return render(request, 'website/index.html', { 'user_reg_form': user_form, 'user_profile_form': profile_form })
def register(request): if not request.user.is_authenticated(): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print("User Form Errors :" + str(user_form.errors) + "Profile Form Errors" + str(profile_form.errors)) # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'website/index.html', {'user_reg_form': user_form, 'user_profile_form': profile_form, 'registered': registered}) else: return redirect('/dashboard/')
def account_edit(request): #import ipdb; ipdb.set_trace() form = UserForm(instance=request.user) if request.method == 'POST': form = UserForm(request.POST, instance=request.user) #import ipdb; ipdb.set_trace() if form.is_valid(): form.save() return render(request, "website/account_edit.html",{ 'user': request.user, 'form': form })
def register(request): if request.user.is_anonymous(): user_form = UserForm(request.POST or None) reporter_form = ReporterForm(request.POST or None) if request.method == "POST": if user_form.is_valid() and reporter_form.is_valid(): user = user_form.save() user.save() reporter = reporter_form.save(commit=False) reporter.user = user reporter.save() return HttpResponseRedirect('/login') return render(request, 'registration/register.html', {'userForm': user_form, 'reporterForm': reporter_form}) else: return HttpResponseRedirect('/')
def edit_profile(request): try: Profile.objects.get(user=request.user) except: raise Http404("Profile does not exist. Contact Admin") js_script = """<script>var simplemde = new SimpleMDE({ element: $("#id_profile_page_markdown")[0], forceSync:true }); </script>""" context = {'js_script': js_script} if request.method == 'POST': submitted_user_form = UserForm(request.POST, instance=request.user) submitted_profile_form = EditProfileForm(request.POST, request.FILES, instance=request.user.profile) if submitted_profile_form.is_valid() and submitted_user_form.is_valid( ): submitted_user_form.save() submitted_profile_form.save() return redirect(reverse('dashboard')) else: context['user_form'] = submitted_user_form context['profile_form'] = submitted_profile_form return render(request, 'website/editprofile.html', context) user_form = UserForm(instance=request.user) profile_form = EditProfileForm(instance=request.user.profile) context['user_form'] = user_form context['profile_form'] = profile_form return render(request, 'website/editprofile.html', context)
def register(request): """ purpose: allow a user to register an account author: Helana Nosrat, Kayla Brewer args: pulls information from two forms(user_form and profile_form) and sends that information to the database as a POST to the user and userprofile models """ registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() user_profile = profile_form.save(commit=False) user_profile.user = user user_profile.save() registered = True return login_user(request) elif request.method == 'GET': user_form = UserForm() profile_form = UserProfileForm() template_name = 'register.html' return render(request, template_name, {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): '''Handles the creation of a new user for authentication Method arguments: request -- The full HTTP request object ''' # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # Create a new user by invoking the `create_user` helper method # on Django's built-in User model if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Update our variable to tell the template registration was successful. registered = True return login_user(request) elif request.method == 'GET': user_form = UserForm() template_name = 'register.html' return render(request, template_name, {'user_form': user_form})
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response( 'HTML/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, context)
def register(request): '''Handles the creation of a new user for authentication Method arguments: request -- The full HTTP request object ''' registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = ProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): print("valid") form_data = request.POST state = form_data["state"] user = user_form.save() user.set_password(user.password) user.save() profile = Profile.objects.create(state=state, user=user) profile.save() registered = True return login_user(request) elif request.method == 'GET': user_form = UserForm() profile_form = ProfileForm() template_name = 'register.html' context = {'user_form': user_form, 'next': request.GET.get('next', '/'), 'profile_form': profile_form} return render(request, template_name, context)
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: print('found it') profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render( request, 'website/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): if not request.user.is_authenticated(): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print("User Form Errors :" + str(user_form.errors) + "Profile Form Errors" + str(profile_form.errors)) # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render( request, 'website/index.html', { 'user_reg_form': user_form, 'user_profile_form': profile_form, 'registered': registered }) else: return redirect('/dashboard/')
def sign_up(request): # A boolean value for telling the template # whether the registration was successful. registered = False if request.method == "POST": user_form = UserForm(request.POST, request.FILES) profile_form = UserProfileForm(request.POST, request.FILES) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user # profile picture if available if "picture" in request.FILES: profile.picture = request.FILES["picture"] profile.save() # Update our variable to indicate that the template # registration was successful. registered = True else: render( request, "SkyView/signUp.html", context={ "user_form": user_form, "profile_form": profile_form, "registered": registered, }, ) else: user_form = UserForm() profile_form = UserProfileForm() return render( request, "SkyView/signUp.html", context={ "user_form": user_form, "profile_form": profile_form, "registered": registered, }, )
def register(request): '''Handles the creation of a new user for authentication Method arguments: request -- The full HTTP request object ''' # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # Create a new user by invoking the `create_user` helper method # on Django's built-in User model if request.method == 'POST': user_form = UserForm(data=request.POST) customer_form = CustomerForm(data=request.POST) if user_form.is_valid() and customer_form.is_valid(): # Save the user's form data to the database. user = user_form.save() customer = customer_form.save(commit=False) customer.user = user customer.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Update our variable to tell the template registration was successful. registered = True payment = PaymentType.objects.create(name="southwest", accountNumber="12355346", customer=customer) user_id = user.id Order.objects.create(isCompleted=0, customer=customer, paymentType=payment) return login_user(request) elif request.method == 'GET': user_form = UserForm() customer_form = CustomerForm() template_name = 'register.html' return render(request, template_name, { 'user_form': user_form, 'customer_form': customer_form })
def register(request): """ Handles the creation of a new user for authentication ---Arguments--- None ---GET--- Renders register.html ---Context--- 'user_form': the form from user_form.py ---POST--- runs the login_user function Author: Steve Browlee """ # A boolean value for telling the template # whether the registration was successful. # Set to False initially. Code changes value to True when registration # succeeds. registered = False # Create a new user by invoking the `create_user` helper method # on Django's built-in User model if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Update our variable to tell the template # registration was successful. registered = True return login_user(request) elif request.method == 'GET': user_form = UserForm() template_name = 'register.html' return render(request, template_name, {'user_form': user_form})
def register(request): if request.method == "POST": form = UserForm(request.POST) if form.is_valid(): user = form.save() messages.success(request, "Your Account Was Created Successfully") login(request, user) return redirect("website:homepage") else: form = UserForm() form = UserForm return render(request=request, template_name="website/register.html", context={"form": form})
def register(request): """ purpose: Handles the creation of a new user for authentication author: steve brownlee args: request -- The full HTTP request object returns: render of a registration from or invocation of django's login() method """ # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # Create a new user by invoking the `create_user` helper method # on Django's built-in User model if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = ProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() user.profile.phone_number = profile_form.cleaned_data['phone_number'] user.profile.address = profile_form.cleaned_data['address'] user.save() # Update our variable to tell the template registration was successful. registered = True return login_user(request) elif request.method == 'GET': user_form = UserForm() profile_form = ProfileForm() template_name = 'register.html' return render(request, template_name, { 'user_form': user_form, 'profile_form': profile_form})
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() registered = True return login_user(request) elif request.method == 'GET': user_form = UserForm() template_name = 'register.html' return render(request, template_name, {'user_form': user_form})
def register(request): '''Handles the creation of a new user for authentication Method arguments: request -- The full HTTP request object ''' # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # Create a new user as well as a new customer at the same time if request.method == 'POST': user_form = UserForm(data=request.POST) customer_form = CustomerForm(data=request.POST) confirm_password = request.POST['confirm_password'] newPassword = request.POST.get('password') if user_form.is_valid() and customer_form.is_valid(): if newPassword == confirm_password: user = user_form.save() customer = customer_form.save() customer.user = user user.set_password(user.password) user.save() customer.save() return login_user(request) else: # print("user form", newPassword) # print("confirm", confirm_password) print("passwords don't match") template_name = 'error.html' return render(request, template_name) else: print("not valid user form") elif request.method == 'GET': user_form = UserForm() customer_form = CustomerForm() template_name = 'register.html' return render(request, template_name, { 'user_form': user_form, 'customer_form': customer_form })
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() registered = True else: pass else: user_form = UserForm() return render( request, 'register.html', {'user_form': user_form, 'registered': registered})
def register(request): #registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save(commit=False) user.set_password(user.password) user.is_active = False user.save() profile = profile_form.save() profile.user = user profile.save() current_site = get_current_site(request) subject = 'activate your account' message = render_to_string( 'registration/account_activation_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) user.email_user(subject, message) return redirect('account_activation_sent') # registered = True # user_form = UserForm() # profile_form = UserProfileForm() else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'registration/registration_form.html', { 'user_form': user_form, 'profile_form': profile_form })
def register(request): '''Handles the creation of a new user for authentication Method arguments: request -- The full HTTP request object ''' # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # Create a new user by invoking the `create_user` helper method # on Django's built-in User model if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = ProfileForm(data=request.POST) if user_form.is_valid(): # Save the user's form data to the database. user = user_form.save() profile = profile_form.save(commit=False) profile.user = user profile.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Update our variable to tell the template registration was successful. registered = True return login_user(request) elif request.method == 'GET': # If we go to /register, this will load. profile_form = ProfileForm() user_form = UserForm() # UserForm is from Django, return render(request, 'register.html', {'user_form': user_form, 'profile_form': profile_form}) # renders form # {'user_form': user_form} <-- this contains the form objects
def user_profile(request): if request.user.is_authenticated(): user = get_object_or_404(User, username=request.user.username) userprofile = get_object_or_404(UserProfile, user=user) user_form = UserForm(instance=user) basic_profile_form = BasicProfileForm(instance=userprofile) current_profile_form = CurrentAProfileForm(instance=userprofile) previous_profile_form = PreviousAProfileForm(instance=userprofile) additional_profile_form = AdditionalProfileForm(instance=userprofile) message = "" upload_error = "" if request.POST.get('update') == 'update_basic': if request.method == 'POST': basic_profile_form = BasicProfileForm(request.POST, request.FILES, instance=userprofile) if basic_profile_form.is_valid(): if 'passphoto' in request.FILES: if request.FILES['passphoto']: if request.FILES['passphoto'].size <= 300000: p_up_count = userprofile.p_up_count p_up_count += 1 if userprofile.passphoto is not None and userprofile.passphoto != "": cloudinary.uploader.destroy( userprofile.passphoto, invalidate=True, type='authenticated') cloudinary.uploader.upload( request.FILES['passphoto'], public_id=user.username + "/passportV" + str(p_up_count), type='authenticated') userprofile.passphoto = user.username + "/passportV" + str( p_up_count) userprofile.p_up_count = p_up_count else: upload_error = "File size too large!" if 'sign' in request.FILES: if request.FILES['sign']: if request.FILES['sign'].size <= 300000: s_up_count = userprofile.s_up_count s_up_count += 1 if userprofile.sign is not None and userprofile.sign != "": cloudinary.uploader.destroy( userprofile.sign, invalidate=True, type='authenticated') cloudinary.uploader.upload( request.FILES['sign'], public_id=user.username + "/signV" + str(s_up_count), type='authenticated') userprofile.sign = user.username + "/signV" + str( s_up_count) userprofile.s_up_count = s_up_count else: upload_error = "File size too large!" userprofile = basic_profile_form.save() userprofile.age = int( (datetime.date.today() - userprofile.dob).days / 365.25) userprofile.save() message = 'Successfully Updated!' else: print("Profile Form Errors" + str(basic_profile_form.errors)) else: if request.POST.get('update') == 'update_current': if request.method == 'POST': current_profile_form = CurrentAProfileForm( data=request.POST, instance=userprofile) if current_profile_form.is_valid(): userprofile = current_profile_form.save() userprofile.save() message = 'Successfully Updated!' else: print("Profile Form Errors" + str(current_profile_form.errors)) else: if request.POST.get('update') == 'update_previous': if request.method == 'POST': previous_profile_form = PreviousAProfileForm( data=request.POST, instance=userprofile) if previous_profile_form.is_valid(): userprofile = previous_profile_form.save() userprofile.save() message = 'Successfully Updated!' else: print("Profile Form Errors" + str(previous_profile_form.errors)) else: if request.POST.get('update') == 'update_additional': if request.method == 'POST': additional_profile_form = AdditionalProfileForm( data=request.POST, instance=userprofile) if additional_profile_form.is_valid(): userprofile = additional_profile_form.save() userprofile.save() message = 'Successfully Updated!' else: print("Profile Form Errors" + str(additional_profile_form.errors)) else: message = "" currpassphoto = cloudinary.CloudinaryImage( userprofile.passphoto).image(sign_url=True, width=0.5, type='authenticated') sign = cloudinary.CloudinaryImage(userprofile.sign).image( sign_url=True, width=0.3, type='authenticated') if userprofile.type: return render( request, 'website/profile.html', { 'message:': message, 'user_form': user_form, 'basic_profile_form': basic_profile_form, 'additional_profile_form': additional_profile_form, 'currpassphoto': currpassphoto, 'sign': sign, 'upload_error': upload_error, 'type': 'faculty' }) else: return render( request, 'website/profile.html', { 'message:': message, 'user_form': user_form, 'basic_profile_form': basic_profile_form, 'current_profile_form': current_profile_form, 'previous_profile_form': previous_profile_form, 'additional_profile_form': additional_profile_form, 'currpassphoto': currpassphoto, 'sign': sign, 'upload_error': upload_error, 'type': 'student' }) else: return redirect('/')