def user_reg(request): # 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 request.method == 'POST': 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() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'Gigstop/user_registration.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def profile(): userProfileForm = UserProfileForm() if request.method == "POST" and userProfileForm.validate_on_submit(): firstname = userProfileForm.fname.data lastname = userProfileForm.lname.data gender = userProfileForm.gender.data email = userProfileForm.email.data location = userProfileForm.location.data bio = userProfileForm.bio.data made_on = format_date_joined() photo = userProfileForm.photo.data image = secure_filename(photo.filename) photo.save(os.path.join(app.config['UPLOAD_FOLDER'], image)) user = UserProfile(first_name=firstname, last_name=lastname, gender=gender, email=email, location=location, bio=bio, image=image, created_on=made_on) db.session.add(user) db.session.commit() flash("Profile was successfully added", "success") return redirect(url_for("profiles")) flash_errors(userProfileForm) return render_template("profile.html", userProfileForm=userProfileForm)
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) 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 if profile_form.is_multipart(): picture = save_files(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('app/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def add_profile(): """Add a profile""" userid = g.user.get_id() form = UserProfileForm() if request.method == 'POST': if form.validate_on_submit(): username = request.form['username'].strip() firstname = request.form['firstname'].strip() lastname = request.form['lastname'].strip() sex = request.form['sex'] age = request.form['age'] image = request.files['img'] filename = "{}-{}".format(userid,secure_filename(image.filename)) filepath = "app/static/uploads/{}".format(filename) image.save(filepath) profile = Profile(username=username,userid=userid,firstname=firstname,lastname=lastname,image=filename,sex=sex,age=age,profile_added_on=datetime.now()) db.session.add(profile) db.session.commit() return redirect(url_for('view_profile',userid=userid)) user = db.session.query(Profile).filter(Profile.userid == userid).first() if user: return redirect(url_for('view_profile',userid=userid)) else: return render_template('addProfile.html', form=form)
def profile(): """Render the website's profile page.""" profilePage = UserProfileForm() if request.method == 'POST': if profilePage.validate_on_submit(): firstName = profilePage.firstName.data lastName = profilePage.lastName.data gender = profilePage.gender.data email = profilePage.email.data location = profilePage.location.data biography = profilePage.biography.data photo = profilePage.photo.data filename = secure_filename(photo.filename) photo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) profile = Profile(first_name=firstName, last_name=lastName, gender=gender, email=email, location=location, biography=biography, profile_picture="uploads/" + filename) db.session.add(profile) db.session.commit() flash('New Profile Created!', 'success') return redirect(url_for('profiles')) else: flash_errors(profilePage) return render_template('profile.html', form=profilePage)
def register_user(request, role): if request.method == 'GET': context = { 'form': RegisterForm(), 'user_register_form': UserProfileForm(), 'role': role } return render(request, 'auth/register.html', context) else: register_form = RegisterForm(request.POST) if register_form.is_valid(): user = register_form.save() user_group = Group.objects.get(name=role) user_group.user_set.add(user) form = UserProfileForm(request.POST, request.FILES) if form.is_valid(): up = form.save(commit=False) up.user_id = user.id up.save() else: context = { 'form': register_form, 'role': role } return render(request, 'auth/register.html', context) login(request, user) return redirect('home') context = { 'form': register_form, 'role': role } return render(request, 'auth/register.html', context)
def profile(): form = UserProfileForm() if request.method == 'POST' and form.validate_on_submit(): firstname = form.firstname.data lastname = form.lastname.data gender = form.gender.data #email = form.email.data location = form.location.data biography = form.biography.data display_pic = form.display_pic.data filename = secure_filename(display_pic.filename) display_pic.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) date_joined = format_date_join() newprofile = UserProfile(first_name=firstname, last_name=lastname, gender=gender, location=location, biography=biography, display_pic=filename) db.session.add(newprofile) db.session.commit() flash('Thank You For Joining Us!. Profile created..', 'success') return redirect(url_for('profiles')) else: flash_errors(form) return render_template('profile.html', form=form)
def register(request): # Like before, get the request's context. context = RequestContext(request) # 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 # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # 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, 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_to_response( 'app/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def profile_post(request): request.session['tab'] = 'tab1' profile = request.user.profile form = UserProfileForm(request.POST, instance=profile) if form.is_valid(): user_profile = form.save() user_profile.save() return redirect(to='profile')
def render_profile(request, context): # define the role of current user user = request.user if user.has_perm('app.employee_role'): if 'role' not in context: context.update({'role': get_top_employee_role(user)}) if 'cur_user' not in context or 'user_form' not in context: form = UserProfileForm(instance=user) context.update({ 'cur_user': user, 'user_form': form, }) if 'employee' not in context or 'employee_form' not in context: employee = Employee.objects.get(user_id=user.id) form = EmployeeProfileForm(instance=employee) context.update({ 'employee': employee, 'title': employee.get_short_instance_name(), 'employee_form': form, }) return render(request, 'app/profile.html', context) elif user.has_perm('app.client_role'): # current user is a client try: marks = Mark.objects.all() context.update({ 'marks': marks, }) if 'cur_user' not in context or 'user_form' not in context: form = UserProfileForm(instance=user) context.update({ 'cur_user': user, 'user_form': form, }) if 'client' not in context or 'client_form' not in context: client = Client.objects.get(user_id=request.user.id) form = ClientProfileForm(instance=client) context.update({ 'title': client.get_title_name(), 'client': client, 'client_form': form, }) return render(request, 'app/profile_client.html', context) except ObjectDoesNotExist as ex: context.update({ 'error': ex.message, }) return render(request, 'app/error.html', context) else: # user doesn't have employee or client role return render(request, 'app/access_denied.html', context)
def profile(request): user = request.user user_profile = UserProfile.objects.get(user=user) dict = { 'picture': user_profile.picture, 'name': user_profile.name, 'classes': user_profile.classes } data_json = QueryDict('', mutable=True) data_json.update(dict) updated = False if user.is_authenticated(): print("Authenticition complete") if request.method == 'POST': data = request.POST print(data) #profile_form = UserProfileForm(data=data) if True: #profile_form.is_valid(): print(data) #profile = profile_form.save(commit=False) user_profile.name = data.get("name") #user_profile.classes = profile.classes print(request.FILES) if 'file' in request.FILES: print(1000000000000) #user_profile.picture = request.FILES['picture'] if 'picture' in request.FILES: picture = str(request.FILES.get('picture')) print(str(request.FILES.get('picture', False)), 1000000) handle_uploaded_file(picture, request.FILES['picture']) user_profile.picture = "/profile_images/" + picture classes = data.getlist("classes", []) added_classes = [] for class_name in classes: added_classes.append(Class.objects.filter(name=class_name)) merged = Class.objects.filter( name="adasdasdaasdasdasdasdfasfsdagdrfgs") for query in added_classes: merged = itertools.chain(merged, query) user_profile.classes = merged user_profile.save() updated = True else: # Invalid form or forms - mistakes or something else? # Print problems to the terminal. print(user_profile.errors) user.email = data.__getitem__("email") user.save() else: profile_form = UserProfileForm(data=data_json) return render( request, 'ClassMateZ/profile.html', { #'profile_form': profile_form, 'updated': updated, 'user_profile': user_profile, 'all_classes': all_classes() })
def update_user_picture(request): if request.method != 'POST': return HttpResponseBadRequest({'error': 'Only POST is accepted'}) user = get_object_or_404(UserWithProfile, user=request.user.id) profile_form = UserProfileForm(data=request.POST) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = user.user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() return JsonResponse({'message': 'Profile picture updated'}) else: return HttpResponseBadRequest({'error': 'No picture received from POST files'})
def userprofile(request): if request.method == "POST": form = UserProfileForm(request.POST) if form.is_valid(): form.save() return redirect('/image') form = UserProfileForm() return render(request, 'form_1.html', {'form': form})
def profile(request): form_errors = None try: user_profile = request.user.userprofile except ObjectDoesNotExist: # While not strictly necessary, if there was a user created before # introducing a user profile, we will need to create the object. user_profile = UserProfile.objects.create(user=request.user) user_profile.save() """ Process the post request for form """ if request.method == 'POST': form = UserProfileForm(request.POST,instance=user_profile) if form.is_valid(): # All validation rules pass form.save() else: # Transform field name to label name and put in tuple with errors form_errors = [] for i in form.errors: try: label = form.fields[i].label if form.fields[i].label is not None else i form_errors.append((label, form.errors[i])) except: form_errors.append(('Error', form.errors[i])) """Renders the profile page.""" assert isinstance(request, HttpRequest) user_profile_form = UserProfileForm(instance=user_profile) return render( request, 'registration/profile.html', context_instance = RequestContext(request, { 'title':'Profile', 'message':'User Profile Information', 'year':datetime.now().year, 'user_profile': user_profile, 'form':user_profile_form, 'form_errors':form_errors, }) )
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 new_user = authenticate(username=request.POST['username'], password=request.POST['password']) login(request, new_user) return HttpResponseRedirect('/user/') else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() return JsonResponse({'errors': {'profile': profile_form.errors, 'user': user_form.errors}}, status=422);
def register(request): """ View for register returns register.html template """ if not request.user.is_authenticated(): context_dict={} if request.method == 'POST': user_form = UserRegisterForm(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_staff=True profile = profile_form.save(commit=False) user.save() profile.user = user profile.save() return HttpResponseRedirect(settings.REGISTER_REDIRECT_UTL) else: print user_form.errors print profile_form.errors context_dict["error1"] = user_form.errors context_dict["error2"] = user_form.errors else: user_form = UserRegisterForm() profile_form = UserProfileForm() context_dict["user_form"]=user_form context_dict["profile_form"]=profile_form return render(request, 'app/register.html',context_dict ) else : return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
def add_user(request): 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() try: user.set_password(user.password) user.save() print(user) print(type(user.id)) user_id = user.id profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True try: print('Sending') request_user = AddUser(str(user_id)) client.send(request_user) print('Sent') except: print('Not sent') return redirect(reverse('login')) except: return redirect(reverse('login')) else: print(user_form.errors, profile_form.errors) return redirect(reverse('register')) else: return redirect(reverse('login'))
def register(request): registered=False user_form=UserForm(data=request.POST) profile_form=UserProfileForm(data=request.POST) if request.method=='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() profile.user = user profile.save() registered=True else: print (user_form.errors, profile_form.errors) return render(request, 'app/register.html', context_instance=RequestContext(request,{'user_form': user_form, 'profile_form': profile_form, 'registered': registered} ))
def register(request): registered = False if request.method == 'POST': user_form = UserForm(request.POST) profile_form = UserProfileForm(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 'photo' in request.FILES: profile.photo = request.FILES['photo'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() dict = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} return render(request, 'blog/register.htm', dict)
def index(request): ''' If logged in, direct to logged in user's profile page. Otherwise this is a login and registration form. ''' # Redirect to user page if already logged in if request.user.is_authenticated(): user = UserProfile.objects.get(user_id=request.user.id) return HttpResponseRedirect('/user/%d' % user.id) # Otherwise this is a registration page 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 profile.save() user = authenticate(username=request.POST['username'], password=request.POST['password']) login(request, user) user = UserProfile.objects.get(user_id=user.id) return HttpResponseRedirect('/user/%d' % user.id) else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context = {'user_form': user_form, 'profile_form': profile_form} return render(request, 'app/index.html', context)
def register(request): context = RequestContext(request) # 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: pass # 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( 'app/user/register_form.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, context)
def register(request): registered = False user_form = UserForm() profile_form = UserProfileForm() # Prevent logged in users from registering again if request.user.is_authenticated(): return redirect(reverse('index')) else: return render( request, 'signup.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def get(self, request, **kwargs): print(request.session.get('_auth_user_id')) user = get_object_or_404(User, pk=request.session.get('_auth_user_id')) userProfile = get_object_or_404( UserProfileInfo, user_id=request.session.get('_auth_user_id')) # user = User.objects.filter(pk=request.session.get('_auth_user_id')) # print(self.user_form, profile_image_form) response_data = {"users": request.session} # if users.count() == 0: # user_message = "No record Found" # response_data['message'] = user_message print(request.session.get('_auth_user_id'), user) user_form = UserProfileForm(instance=user) profile_image_form = UserProfileInfoForm(instance=userProfile) # return self.render_to_response(self.get_context_data( # object=self.object, user_form=user_form, profile_image_form=profile_image_form)) return render(request, self.template_name, { 'user_form': user_form, 'profile_image_form': profile_image_form })
def register(request): """ Handles user registration logic Arguments: request -- [standard Django request arg] Returns: Render - render registration page with appropriate info """ registered = False if request.method == "POST": user_form = UserForm(request.POST) 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 # check for picture if ("picture" in request.FILES): profile.picture = request.FILES["picture"] profile.save() # create wish list for new user wish_list = ProductList.objects.create(name=user.username, user=profile) logger.info("User: %s is registered", user) registered = True else: print(user_form.errors, profile_form.errors) logger.info( "User: failed to register with user form errors: %s \n \ and profile form errors: %s", user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() logger.info("Rendered registration page") return render(request, "app/register.html", context={ "user_form": user_form, "profile_form": profile_form, "registered": registered })
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfile(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'] if 'rating' in request.POST: profile.rating = request.POST['rating'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response( 'app/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, context)
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() 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() context = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } return render(request, 'app/register.html', context)
class ProfileView(UpdateView): model = User template_name = 'app/my_profile.html' object = None user_form = UserProfileForm() profile_image_form = UserProfileInfoForm() def get(self, request, **kwargs): print(request.session.get('_auth_user_id')) user = get_object_or_404(User, pk=request.session.get('_auth_user_id')) userProfile = get_object_or_404( UserProfileInfo, user_id=request.session.get('_auth_user_id')) # user = User.objects.filter(pk=request.session.get('_auth_user_id')) # print(self.user_form, profile_image_form) response_data = {"users": request.session} # if users.count() == 0: # user_message = "No record Found" # response_data['message'] = user_message print(request.session.get('_auth_user_id'), user) user_form = UserProfileForm(instance=user) profile_image_form = UserProfileInfoForm(instance=userProfile) # return self.render_to_response(self.get_context_data( # object=self.object, user_form=user_form, profile_image_form=profile_image_form)) return render(request, self.template_name, { 'user_form': user_form, 'profile_image_form': profile_image_form }) def post(self, request): # user_form = UserProfileForm(data=request.POST) profile_form = UserProfileInfoForm(request.POST) print(profile_form.is_valid(), profile_form) if profile_form.is_valid(): # user = user_form.save(commit=False) # user['id'] = request.session.get('_auth_user_id') # user.set_password(user.password) # print(user, "------------------------------") # user.update() # profile.user = request.user#.get('_auth_user_id') # user = get_object_or_404(User, pk=request.session.get('_auth_user_id')) # print(profile_form) profile = profile_form.save(commit=False) # profile.user_id = request.user profileObj = get_object_or_404( UserProfileInfo, user_id=request.session.get('_auth_user_id')) profile.user_id = request.session.get('_auth_user_id') profile.id = profileObj.id print(profile, "+++++++++++++++++++++++++++++++") if 'profile_pic' in request.FILES: print('found it') profile.profile_pic = request.FILES['profile_pic'] profile.save() return render(request, self.template_name, { 'messages': "Update Successfully", "isUpdate": True }) else: return render( request, self.template_name, { 'user_form': self.user_form, 'profile_image_form': self.profile_image_form, 'messages': "Please fill all fields", "isUpdate": False })
def profile(request): """User profile page""" context = get_global_context(request) if request.method == 'POST': user = request.user if user.has_perm('app.employee_role'): employee = Employee.objects.get(user_id=user.id) employee_form = EmployeeProfileForm(request.POST, request.FILES, instance=employee) user_form = UserProfileForm(request.POST, instance=request.user) if user_form.is_valid() and employee_form.is_valid(): if user_form.has_changed(): user.username = user_form.cleaned_data['username'] user.email = user_form.cleaned_data['email'] user.first_name = user_form.cleaned_data['first_name'] user.last_name = user_form.cleaned_data['last_name'] user.save() if employee_form.has_changed(): # saving data if employee_form.cleaned_data['avatar'] and\ employee.avatar !=\ employee_form.cleaned_data['avatar']: cur_avatar = employee.avatar employee.avatar = employee_form.cleaned_data['avatar'] delete_current_avatar_file(cur_avatar.path) employee.save() else: user_data = dict((key, user_form.data[key]) for key in list( set(user_form.data) & set(user_form.fields))) current_data_user = User(**user_data) employee_data = dict( (key, employee_form.data[key]) for key in list( set(employee_form.data) & set(employee_form.fields)) if key != 'avatar') current_data_employee = Employee(**employee_data) current_data_employee.avatar = employee.avatar context.update({ 'title': employee.get_short_instance_name(), 'cur_user': current_data_user, 'employee': current_data_employee, 'employee_form': employee_form, 'user_form': user_form, }) return render_profile(request, context) elif user.has_perm('app.client_role'): client = Client.objects.get(user_id=request.user.id) client_form = ClientProfileForm(request.POST, request.FILES, instance=client) user_form = UserProfileForm(request.POST, instance=request.user) if user_form.is_valid() and client_form.is_valid(): if user_form.has_changed(): user.username = user_form.cleaned_data['username'] user.email = user_form.cleaned_data['email'] user.first_name = user_form.cleaned_data['first_name'] user.last_name = user_form.cleaned_data['last_name'] user.save() if client_form.has_changed(): # saving data client = Client.objects.get(user_id=user.id) client.name = client_form.cleaned_data['name'] client.full_name = client_form.cleaned_data['full_name'] client.code_ipn = client_form.cleaned_data['code_ipn'] client.address = client_form.cleaned_data['address'] client.phone = client_form.cleaned_data['phone'] client.overall_mark_id =\ client_form.data['overall_mark'] if client_form.cleaned_data['avatar'] and\ client.avatar != client_form.cleaned_data['avatar']: cur_avatar = client.avatar client.avatar = client_form.cleaned_data['avatar'] delete_current_avatar_file(cur_avatar.path) client.save() else: user_data = dict((key, user_form.data[key]) for key in list( set(user_form.data) & set(user_form.fields))) current_data_user = User(**user_data) client_data = dict( (key, client_form.data[key]) for key in list( set(client_form.data) & set(client_form.fields)) if key != 'avatar') client_data.update({ 'overall_mark': Mark.objects.get(id=client_form.data['overall_mark']), }) current_data_client = Client(**client_data) current_data_client.balance = client.balance current_data_client.avatar = client.avatar context.update({ 'title': client.get_title_name(), 'cur_user': current_data_user, 'client': current_data_client, 'client_form': client_form, 'user_form': user_form, }) return render_profile(request, context) else: return render_profile(request, context)
def register(request): # 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. print("registration request received") 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... print(user_form) 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 profile.name = user.username # Did the user provide a profile picture? # If so, we need to get it from the input form and #put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() password = request.POST.get('password', None) authenticated = authenticate(username=user.username, password=password) if authenticated: login(request, authenticated) # Update our variable to indicate that the template # registration was successful. registered = True else: # Invalid form or forms - mistakes or something else? # Print problems to the terminal. print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render( request, 'ClassMateZ/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })