def register(request): context_dict = {} 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_picture' in request.FILES: profile.profile_picture = request.FILES['profile_picture'] profile.save() registered = True else: user_form = UserForm() profile_form = UserProfileForm() context_dict['user_form'] = user_form context_dict['profile_form'] = profile_form context_dict['registered'] = registered return render(request, 'main/register.html', context_dict)
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 profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() context_dict={} context_dict['user_form'] = user_form context_dict['profile_form'] = profile_form context_dict['registered'] = registered response = render(request, 'main/signup.html', context_dict) return response
def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) pform = UserProfileForm(request.POST) if form.is_valid() and pform.is_valid(): import logging logger = logging.getLogger('django.request') logger.info('New registered user\n' + str(request).replace('\n', '\n\t')) new_user = form.save() pform.instance.user = new_user pform.save() if pform.instance.fladRequest: from django.core.mail import send_mail send_mail('FLAD request', '{} requested to use FLAD.'.format( new_user.username), '*****@*****.**', ['*****@*****.**'], fail_silently=True) #Set FLAD test configuration for all new users from myflq.models import FLADconfig fc = FLADconfig(user=new_user, FLAD="forensic.ugent.be/flax", FLADname="testname", FLADkey="testkey") fc.save() return HttpResponseRedirect("/") else: form = UserCreationForm() pform = UserProfileForm() return render(request, "registration/register.html", { 'form': form, 'pform': pform, })
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) # don't save it in the database yet user.set_password( user.password ) # set_password function ensures hashing before saving user.is_active = False user.save() profile = profile_form.save( commit=False) # don't save it in the database yet profile.user = user # sets the one to one relationship profile.activationCode = randint(1000, 9999) if 'profilePicture' in request.FILES: profile.profilePicture = request.FILES['profilePicture'] registered = True profile.save() # emailing the user using a html template. if the template doesn't work, a txt file gets used as an alternative subject = 'Welcome! - Intelligent Q&A Forums' email_to = [user.email] with open(settings.BASE_DIR + "/main/templates/main/authentication/sign_up_email.txt" ) as temp: sign_up_email = temp.read() email = EmailMultiAlternatives(subject=subject, body=sign_up_email, from_email=settings.EMAIL_HOST_USER, to=email_to) html = get_template( "main/authentication/sign_up_email.html").render({ 'user': user, 'activationCode': profile.activationCode }) email.attach_alternative(html, "text/html") email.send() else: user_form = UserForm() profile_form = UserProfileForm() return render( request, 'main/authentication/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def profile_settings(request, username): context = RequestContext(request) content_user = check_and_set_user(request, username) context.content_user = content_user profile, created = UserProfile.objects.get_or_create(user=content_user) if request.method == 'POST': form = UserProfileForm(request.POST, instance=profile) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(public_profile, kwargs={'username': request.user.username})) else: form = UserProfileForm(instance=profile) return render_to_response("settings.html", { 'form': form }, context_instance=context)
def restore_object(self, attrs, instance=None): def _get_first_last_names(name): name_split = name.split() first_name = name_split[0] last_name = u'' if len(name_split) > 1: last_name = u' '.join(name_split[1:]) return first_name, last_name params = copy.deepcopy(attrs) username = attrs.get('user.username', None) password = attrs.get('user.password', None) name = attrs.get('name', None) email = attrs.get('user.email', None) if username: params['username'] = username if email: params['email'] = email if password: params.update({'password1': password, 'password2': password}) if instance: form = UserProfileForm(params, instance=instance) # form.is_valid affects instance object for partial updates [PATCH] # so only use it for full updates [PUT], i.e shallow copy effect if not self.partial and form.is_valid(): instance = form.save() # get user if email: instance.user.email = form.cleaned_data['email'] if name: first_name, last_name = _get_first_last_names(name) instance.user.first_name = first_name instance.user.last_name = last_name if email or name: instance.user.save() return super(UserProfileSerializer, self).restore_object(attrs, instance) #return instance # TODO: updates form = RegistrationFormUserProfile(params) if form.is_valid(): first_name, last_name = _get_first_last_names(name) new_user = User(username=username, first_name=first_name, last_name=last_name, email=email) new_user.set_password(password) new_user.save() created_by = self.context['request'].user profile = UserProfile(user=new_user, name=attrs.get('name', u''), created_by=created_by, city=attrs.get('city', u''), country=attrs.get('country', u''), organization=attrs.get('organization', u''), home_page=attrs.get('home_page', u''), twitter=attrs.get('twitter', u'')) return profile else: self.errors.update(form.errors) return attrs
def test_home_page_returns_correct_html(self): from django.contrib.auth.forms import UserCreationForm from main.forms import UserProfileForm request = HttpRequest() response = homepage(request) expected_html = render_to_string('main/home.html', { 'userform': UserCreationForm(), 'profileform': UserProfileForm() }) self.assertEqual(response.content.decode(), expected_html)
def account_settings(request): user = request.user userprofile = UserProfile.objects.get(user=user) userprofileform = UserProfileForm(instance=userprofile) userform = UserForm(instance=user) if request.method == 'POST': userprofileform = UserProfileForm(data=request.POST, files=request.FILES, instance=userprofile) userform = UserForm(request.POST, instance=user) if userform.is_valid() and userprofileform.is_valid(): userprofileform.save() userform.save() return HttpResponseRedirect(reverse('settings')) context = {'userprofileform': userprofileform, 'userform': userform} return render(request, 'accounts/account_settings.html', context)
def user_update(request): # Like before, get the request's context. context = RequestContext(request) user = request.user user_profile = UserProfile.objects.get(user=user.id) if request.method == 'POST': user_form = UserUpdateForm(data=request.POST, instance=user) profile_form = UserProfileForm(data=request.POST, instance=user_profile) if (user_form.is_valid() and profile_form.is_valid()): user_form.save() profile_form.save() return HttpResponseRedirect(reverse('index')) else: user_form = UserUpdateForm(instance=user) profile_form = UserProfileForm(instance=user_profile) return render_to_response('main/edit_profile.html', { 'user_form': user_form, 'profile_form': profile_form }, context)
def edit_profile(request): context = RequestContext(request) context_dict = {} old_profile = get_object_or_404(Researcher, user=request.user) if request.method == 'POST': profile_form = UserProfileForm(data=request.POST) if profile_form.is_valid(): if 'profile_picture' in request.FILES: old_profile.profile_picture = request.FILES['profile_picture'] old_profile.website = profile_form.cleaned_data["website"] old_profile.display_name = profile_form.cleaned_data[ "display_name"] old_profile.organization = profile_form.cleaned_data[ "organization"] old_profile.save() return HttpResponseRedirect( reverse("view_profile", kwargs={"userId": request.user.id})) else: profile_form = UserProfileForm(instance=old_profile) context_dict['profile_form'] = profile_form return render_to_response('main/edit_profile.html', context_dict, context)
def doctor_profile(request): """ RENDERS DOCTOR PROFILE PAGE TO BE UPDATED """ user_form = UserProfileForm(request.POST or None, instance=request.user) doctor_form = DoctorForm(request.POST or None, instance=request.user.user_category.doctor) if doctor_form.is_valid() and user_form.is_valid(): user_form.save() doctor_form.save() messages.success(request, "successfully updated") sleep(1) return HttpResponseRedirect(reverse("main:home")) else: pass return render(request, "doctor_form.html", {"user_form":user_form, "doctor_form":doctor_form})
def patient_profile(request): """ RENDERS PATIENT PROFILE TO BE UPDATED """ user_form = UserProfileForm(request.POST or None, instance=request.user) patient_form = PatientForm(request.POST or None, instance=request.user.user_category.patient) if patient_form.is_valid() and user_form.is_valid(): user_form.save() patient_form.save() messages.success(request, "successfully updated") sleep(1) return HttpResponseRedirect(reverse("main:home")) else: pass return render(request, "patient_form.html", {"user_form":user_form, "patient_form":patient_form})
def homepage(request): from django.contrib.auth.forms import UserCreationForm from main.forms import UserProfileForm registered = False uform = UserCreationForm() pform = UserProfileForm() if request.method == 'POST': uform = UserCreationForm(request.POST) pform = UserProfileForm(request.POST) if uform.is_valid() and pform.is_valid(): new_user = uform.save() pform.instance.user = new_user pform.save() return redirect('/?registration=success') if request.method == 'GET' and 'registration' in request.GET: registered = True return render(request, 'main/home.html', { 'userform': uform, 'profileform': pform, 'registered': registered })
def test_userProfile_create_form_valid(self): form = UserProfileForm(data={ 'profilePicture': None # a display photo is not compulsory }) self.assertTrue(form.is_valid())
def registration(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) user_type_form = UserTypeForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid( ) and user_type_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() nickname = profile_form.cleaned_data['nickname'] # Now sort out the Student 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. user_type = user_type_form.cleaned_data['user_type'] user_type = int(user_type) #user_type = 1 if user_type: profile = Student(nickname=nickname) else: profile = Instructor(nickname=nickname) profile.user = user # Add the personal calendar for the user calendar = Calendar(name=user.username + "'s personal calendar") calendar.save() profile.cal = calendar profile.school = None # Now we save the Student 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, user_type_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() user_type_form = UserTypeForm() # Render the template depending on the context. return render_to_response( 'main/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'user_type_form': user_type_form, 'registered': registered }, context)