Merge branch 'registration-failed-restore' of https://gitea.42ctf.org/Yir/website into Yir-registration-failed-restore

This commit is contained in:
Starthur 2022-05-27 20:56:42 +02:00
commit f391a5a5cb
2 changed files with 83 additions and 46 deletions

View File

@ -2,42 +2,42 @@
{% block content %} {% block content %}
{% load i18n %} {% load i18n %}
<div class="row"> <div class="row">
<div class="col-sm-12 col-md-9"> <div class="col-sm-12 col-md-9">
<div class="ctf-block"> <div class="ctf-block">
<div class="ctf-head"> <div class="ctf-head">
<h3>Register</h3> <h3>Register</h3>
</div> </div>
<div class="ctf-body sign-body">
<div class="col-sm-8 col-md-6 mx-auto">
{% if registered %}
<h1>{% trans "Welcome !" %}</h1>
<span class="message success-msg">{% trans "Your account has been created." %}</span>
{% else %}
{% if registered_failed %}
<span class="message error-msg">{{ registered_failed }}</span>
{% endif %}
<form enctype="multipart/form-data" method="POST">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" name="username" placeholder="{% trans "Username" %} *" maxlength="150" required="" id="id_username"></br>
<input class="form-control" type="password" name="password" placeholder="{% trans "Password" %} *" required="" id="id_password"></br>
<input class="form-control" type="email" name="email" placeholder="pleasedontgivemy@private.infos*" required="" maxlength="254" id="id_email"></br>
<input class="form-control" type="url" name="portfolio_site" placeholder="{% trans "Personal website" %}"maxlength="200" id="id_portfolio_site"></br>
<input type="submit" name="" class="form-control" value="{% trans "Register" %}">
</div>
</form>
{% endif %}
</div>
</div>
</div>
<div class="ctf-body sign-body">
<div class="col-sm-8 col-md-6 mx-auto">
{% if registered %}
<h1>{% trans "Welcome !" %}</h1>
<span class="message success-msg">{% trans "Your account has been created." %}</span>
{% else %}
{% if registered_failed %}
<span class="message error-msg">{{ registered_failed }}</span>
{% endif %}
<form enctype="multipart/form-data" method="POST">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" name="username" placeholder="{% trans "Username" %} *" maxlength="150" required="" id="id_username" value="{{ old_username }}"></br>
<input class="form-control" type="password" name="password" placeholder="{% trans "Password" %} *" required="" id="id_password"></br>
<input class="form-control" type="email" name="email" placeholder="pleasedontgivemy@private.infos*" required="" maxlength="254" id="id_email" value="{{ old_email }}"></br>
<input class="form-control" type="url" name="portfolio_site" placeholder="{% trans "Personal website" %}"maxlength="200" id="id_portfolio_site" value="{{ old_website }}"></br>
<input type="submit" name="" class="form-control" value="{% trans "Register" %}">
</div>
</form>
{% endif %}
</div>
</div> </div>
<div class="col-sm-12 col-md-3 right-sidebar"> </div>
<ul class="list-group">
<a href="/accounts/signup" class="list-group-item">{% trans "Sign up" %}</a> <div class="col-sm-12 col-md-3 right-sidebar">
<a href="/accounts/signin" class="list-group-item">{% trans "Login" %}</a> <ul class="list-group">
</ul> <a href="/accounts/signup" class="list-group-item">{% trans "Sign up" %}</a>
</div> <a href="/accounts/signin" class="list-group-item">{% trans "Login" %}</a>
</ul>
</div>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -43,32 +43,69 @@ def signup(request):
user_form = UserForm() user_form = UserForm()
profile_form = UserProfileInfoForm() profile_form = UserProfileInfoForm()
registered = False registered = False
if request.method == 'POST': if request.method == 'POST':
pass1 = request.POST.get('password') username = request.POST.get('username')
if len(pass1) < 8: passwd = request.POST.get('password')
return render(request,'accounts/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered_failed':"The new password must be at least %d characters long." % 8}) email = request.POST.get('email')
first_isalpha = pass1[0].isalpha() website = request.POST.get('portfolio_site')
if not any(c.isdigit() for c in pass1) or not any(c.isalpha() for c in pass1):
return render(request,'accounts/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered_failed':_("The password must contain at least one letter and at least one digit or punctuation character.")}) if len(passwd) < 8:
return render(request, 'accounts/register.html', {
'user_form': user_form,
'profile_form': profile_form,
'registered_failed': _("The password must be at least 8 characters long."),
'old_username': username,
'old_email': email,
'old_website': website
})
if not any(c.isdigit() for c in passwd) or not any(c.isalpha() for c in passwd):
return render(request, 'accounts/register.html', {
'user_form': user_form,
'profile_form': profile_form,
'registered_failed': _("The password must contain at least one letter and at least one digit or punctuation character."),
'old_username': username,
'old_email': email,
'old_website': website
})
if User.objects.filter(email=request.POST.get('email')).exists(): if User.objects.filter(email=request.POST.get('email')).exists():
return render(request,'accounts/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered_failed':_("A user with that email already exists.")}) return render(request, 'accounts/register.html', {
'user_form': user_form,
'profile_form': profile_form,
'registered_failed': _("A user with that email already exists."),
'old_username': username,
'old_website': website
})
user_form = UserForm(data=request.POST) user_form = UserForm(data=request.POST)
profile_form = UserProfileInfoForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid(): if user_form.is_valid() and profile_form.is_valid():
user = user_form.save() user = user_form.save()
user.set_password(user.password) user.set_password(user.password)
user.save() user.save()
profile = profile_form.save(commit=False) profile = profile_form.save(commit=False)
profile.user = user profile.user = user
profile.token = token_hex(16) profile.token = token_hex(16)
profile.save() profile.save()
registered = True registered = True
else: else:
return render(request,'accounts/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered_failed':_("A user with that username already exists.")}) return render(request, 'accounts/register.html', {
return render(request,'accounts/register.html', 'user_form': user_form,
{'user_form':user_form, 'profile_form': profile_form,
'profile_form':profile_form, 'registered_failed': _("A user with that username already exists."),
'registered':registered}) 'old_email': email,
'old_website': website
})
return render(request, 'accounts/register.html', {
'user_form': user_form,
'profile_form': profile_form,
'registered': registered
})
else: else:
return HttpResponseRedirect(reverse('home')) return HttpResponseRedirect(reverse('home'))