def register_expert(request): if request.method == 'POST': form = RegistrationForm(request.POST, request.FILES) if form.is_valid(): form.save() username = form.cleaned_data['email'] password = form.cleaned_data['password1'] #Log user in user = authenticate(username = username, password = password) login(request, user) create_thumbnail(username) subject = "New Expert Registration on Local Experts" message = "New expert joined Local Experts. | Email: " + username + " | Category_id: " + str(user.category_id) + " | District: " + user.district from_email = settings.EMAIL_HOST_USER password = settings.EMAIL_HOST_PASSWORD to_list = ['*****@*****.**'] send_mail(subject, message, from_email, to_list, fail_silently=True) return redirect('/webapp/expert?id='+str(user.id)) else: #When form invalid form = RegistrationForm(request.POST) expertieses = Expertise.objects.all() context = { 'form' : form, 'expertieses' : expertieses } return render(request, 'registration/register_expert.html', context) else: form = RegistrationForm() expertieses = Expertise.objects.all() context = { 'form' : form, 'expertieses' : expertieses } return render(request, 'registration/register_expert.html', context)
def register_client(request): if request.method == 'POST': form = RegistrationForm(request.POST, request.FILES) if form.is_valid(): form.save() username = form.cleaned_data['email'] password = form.cleaned_data['password1'] #Log user in user = authenticate(username = username, password = password) login(request, user) subject = "New Client Registration on Local Experts" message = "New client joined Local Experts. | Email: " + username from_email = settings.EMAIL_HOST_USER password = settings.EMAIL_HOST_PASSWORD to_list = ['*****@*****.**'] send_mail(subject, message, from_email, to_list, fail_silently=True) return redirect('/?just_registered=true') else: #print form.errors #When form invalid form = RegistrationForm(request.POST) context = { 'form' : form } return render(request, 'registration/register_client.html', context) else: form = RegistrationForm() context = { 'form' : form } return render(request, 'registration/register_client.html', context)
def register(request): if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): """ email = form.cleaned_data['email'] password = request.POST.get('password', None) first_name = request.POST.get('first_name') last_name = request.POST.get('last_name') username = request.POST.get('username') institute = request.POST.get('institute') birth_date = request.POST.get('birth_date') """ #Checking email and username validation email = form.cleaned_data['email'] username = form.cleaned_data['username'] if User.objects.filter(username = username).exists(): #Username taken return render(request, 'register_page.html', {'form' : form, 'username_error' : 'Username already taken.'}) if User.objects.filter(email = email).exists(): #Email ID exists print('Email address already taken.') return render(request, 'register_page.html', {'form': form, 'email_error' : 'Email already taken.'}) #Save user to database user = form.save() user.refresh_from_db() user.profile.birth_date = form.cleaned_data.get('birth_date') user.profile.institute = form.cleaned_data.get('institute') raw_password = form.cleaned_data.get('password2') user.set_password(raw_password) user.profile.save() return render(request, 'homepage.html') else: print(form.errors) return render(request, 'register_page.html', {'form' : form}) args = {'form' : form, 'email' : email, 'username' : username, 'password' : password, 'first_name' : first_name, 'last_name': last_name} return render(request, 'subjects_user.html', args) else: form = RegistrationForm() return render(request, 'register_page.html', {'form' : form})
def signup(request): """ Registration of a user """ if request.method == "POST": userform = RegistrationForm(request.POST) if userform.is_valid(): user = userform.save() Users(username=user.username, password=user.password, email=user.email).save() return HttpResponseRedirect(reverse('signup_ok')) elif request.method == "GET": userform = RegistrationForm() return render(request, 'registration/signup.html', {"userform": userform})
def update_profile(request): expertieses = Expertise.objects.all() if request.method == 'POST': user_id = request.POST['user_id'] #Only profile owner can edit profile if int(request.user.id) == int(user_id): instance = User.objects.get(id=user_id) form = RegistrationForm(request.POST, request.FILES, instance=instance) if form.is_valid(): form.save() username = form.cleaned_data['email'] password = form.cleaned_data['password1'] #Log user in user = authenticate(username = username, password = password) login(request, user) create_thumbnail(username) return redirect('/webapp/expert?id='+str(user_id)) else: #invalid form #TODO: catch the form values and add condition to form to show them... form = RegistrationForm(request.POST) #print form.errors context = { 'form' : form, 'expertieses' : expertieses, 'instance' : instance } return render(request, 'registration/update_profile.html', context) else: # Not the profile owner return redirect('/webapp/expert?id='+str(user_id)) else: form = RegistrationForm(request.POST) user_id = request.GET.get('id') instance = User.objects.get(id=user_id) context = { 'form' : form, 'expertieses' : expertieses, 'instance' : instance } #Only profile owner can edit profile if int(request.user.id) == int(user_id): return render(request, 'registration/update_profile.html', context) else: # Not the profile owner return redirect('/webapp/expert?id='+str(user_id))