Skip to content

Commit

Permalink
Restore usernames to user model (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsargent authored Jul 23, 2024
1 parent 3b22b21 commit 97209e0
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 73 deletions.
12 changes: 12 additions & 0 deletions approval_polls/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from allauth.account.forms import SignupForm
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -89,3 +90,14 @@ def clean(self):
else:
msg = "This field is required."
self.add_error("zipcode", msg)


class CustomSignupForm(SignupForm):
def __init__(self, *args, **kwargs):
super(CustomSignupForm, self).__init__(*args, **kwargs)
del self.fields["password2"]

def save(self, request):
# Ensure you don't need to use password2 in the save method
user = super(CustomSignupForm, self).save(request)
return user
7 changes: 4 additions & 3 deletions approval_polls/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,13 @@
}
}

ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_USER_MODEL_USERNAME_FIELD = "username"
ACCOUNT_USERNAME_REQUIRED = True
SOCIALACCOUNT_EMAIL_AUTHENTICATION_AUTO_CONNECT = True
SOCIALACCOUNT_EMAIL_AUTHENTICATION = True
ACCOUNT_EMAIL_VERIFICATION = "optional"
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
ACCOUNT_SESSION_REMEMBER = True
Expand All @@ -273,6 +273,7 @@
)
# ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5 # or any reasonable number
ACCOUNT_RATE_LIMITS = False
ACCOUNT_FORMS = {"signup": "approval_polls.forms.CustomSignupForm"}


sentry_sdk.init(
Expand Down
2 changes: 1 addition & 1 deletion approval_polls/templates/account/base_manage.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{% extends "allauth/layouts/manage.html" %}
{% extends "../base.html" %}
87 changes: 46 additions & 41 deletions approval_polls/templates/account/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,55 @@
{% trans "Sign In" %}
{% endblock head_title %}
{% block content %}
<h1>{% trans "Sign In" %}</h1>
<form method="post"
action="{% url 'account_login' %}"
class="form-horizontal">
{% csrf_token %}
<div class="form-body">
<div class="mb-3">
<label for="{{ form.login.id_for_label }}" class="form-label">Email</label>
{{ form.login|add_class:"form-control" }}
</div>
<div class="mb-3">
<label for="{{ form.password.id_for_label }}" class="form-label">Password</label>
{{ form.password|add_class:"form-control" }}
<div id="{{ form.password.auto_id }}-help" class="form-text">
<a href="{% url 'account_reset_password' %}">Forgot your password?</a>
</div>
</div>
{% if redirect_field_value %}
<input type="hidden"
name="{{ redirect_field_name }}"
value="{{ redirect_field_value }}" />
{% endif %}
</div>
<div class="form-actions">
<div class="row">
<div class="col-md-12">
<div class="text-center">
<button class="btn btn-primary" type="submit">{% trans "Sign In" %}</button>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card mt-5">
<div class="card-body">
<h2 class="card-title text-center mb-4">{% trans "Sign In" %}</h2>
<form method="post" action="{% url 'account_login' %}">
{% csrf_token %}
<div class="mb-3">
<label for="{{ form.login.id_for_label }}" class="form-label">Email</label>
{{ form.login|add_class:"form-control" }}
</div>
<div class="mb-3">
<label for="{{ form.password.id_for_label }}" class="form-label">Password</label>
{{ form.password|add_class:"form-control" }}
<div id="{{ form.password.auto_id }}-help" class="form-text">
<a href="{% url 'account_reset_password' %}">Forgot your password?</a>
</div>
</div>
{% if redirect_field_value %}
<input type="hidden"
name="{{ redirect_field_name }}"
value="{{ redirect_field_value }}" />
{% endif %}
<div class="d-grid gap-2 mt-4">
<button class="btn btn-primary btn-lg" type="submit">{% trans "Sign In" %}</button>
</div>
</form>
<div class="text-center mt-3">
<p>
Don't have an account? <a href="{% url 'account_signup' %}">Register</a>
</p>
</div>
{% if SOCIALACCOUNT_ENABLED %}
<hr class="my-4">
<div class="text-center">
<p class="mb-2">Or sign in with:</p>
<a href="{% provider_login_url 'google' %}"
class="btn btn-outline-secondary">
<img src="{% static 'images/google-signin-button.png' %}"
alt="Sign in with Google"
width="200px"
height="45px" />
</a>
</div>
{% endif %}
</div>
</div>
</div>
</div>
</form>
<!-- Register button outside the form -->
<div class="text-center mt-3">
<a href="{% url 'account_signup' %}" class="btn btn-secondary">Register</a>
</div>
<hr />
<div class="text-center">
<a href="{% provider_login_url 'google' %}">
<img src="{% static 'images/google-signin-button.png' %}"
alt="Sign in with Google"
width="200px"
height="45px" />
</a>
</div>
{% endblock content %}
68 changes: 40 additions & 28 deletions approval_polls/templates/account/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,49 @@
{% load allauth %}
{% load i18n %}
{% block head_title %}
{% trans "Signup" %}
{% trans "Sign Up" %}
{% endblock head_title %}
{% block content %}
{% element h1 %}
{% trans "Sign Up" %}
{% endelement %}
<p>
{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}
</p>
{% url 'account_signup' as action_url %}
{% element form form=form method="post" action=action_url tags="entrance,signup" %}
{% slot body %}
{% csrf_token %}
{% element fields form=form unlabeled=True %}
{% endelement %}
{% if redirect_field_value %}
<input type="hidden"
name="{{ redirect_field_name }}"
value="{{ redirect_field_value }}" />
{% endif %}
{% endslot %}
{% slot actions %}
{% element button tags="prominent,signup" type="submit" %}
{% trans "Sign Up" %}
{% endelement %}
{% endslot %}
{% endelement %}
<hr />
<div class="text-center">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card mt-5">
<div class="card-body">
{% element h1 %}
<h2 class="card-title text-center mb-4">{% trans "Sign Up" %}</h2>
{% endelement %}
<p class="text-center mb-4">
{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}
</p>
{% url 'account_signup' as action_url %}
{% element form form=form method="post" action=action_url tags="entrance,signup" %}
{% slot body %}
{% csrf_token %}
{% for field in form %}
<div class="mb-3">
<label for="{{ field.id_for_label }}" class="form-label">{{ field.label }}</label>
{{ field|add_class:"form-control" }}
{% if field.help_text %}<small class="form-text text-muted">{{ field.help_text }}</small>{% endif %}
{% for error in field.errors %}<div class="invalid-feedback d-block">{{ error }}</div>{% endfor %}
</div>
{% endfor %}
{% if redirect_field_value %}
<input type="hidden"
name="{{ redirect_field_name }}"
value="{{ redirect_field_value }}" />
{% endif %}
{% endslot %}
{% slot actions %}
<div class="d-grid gap-2 mt-4">
<button class="btn btn-primary btn-lg">{% trans "Sign Up" %}</button>
</div>
{% endslot %}
{% endelement %}
{% if SOCIALACCOUNT_ENABLED %}
{% include "socialaccount/snippets/login.html" with page_layout="entrance" %}
<hr class="my-4">
<div class="text-center">{% include "socialaccount/snippets/login.html" with page_layout="entrance" %}</div>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock content %}

0 comments on commit 97209e0

Please sign in to comment.