Skip to content

Commit

Permalink
Add change username form
Browse files Browse the repository at this point in the history
  • Loading branch information
madprime committed Feb 11, 2020
1 parent 326ca2d commit baac3ec
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
21 changes: 21 additions & 0 deletions open_humans/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.urls import reverse

from allauth.account import app_settings as account_settings
from allauth.account.forms import (
ChangePasswordForm as AllauthChangePasswordForm,
LoginForm as AllauthLoginForm,
Expand All @@ -18,6 +20,8 @@

from .models import Member

User = get_user_model()


def _clean_password(child_class, self_instance, password_field_name):
"""
Expand Down Expand Up @@ -149,6 +153,23 @@ class Meta: # noqa: D101
fields = ("name",)


class MemberChangeUsernameForm(forms.ModelForm):
"""
A form for editing a member's username.
"""

def clean_username(self):
username = self.cleaned_data["username"]
for validator in account_settings.USERNAME_VALIDATORS:
validator(username)
return username

class Meta: # noqa: D101
model = User
fields = ("username",)
help_texts = {"username": "Letters, number, or underscore only."}


class ActivityMessageForm(forms.Form):
"""
A form that allows a user to send a message to a project.
Expand Down
18 changes: 18 additions & 0 deletions open_humans/member_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.apps import apps
from django.contrib import messages as django_messages
from django.contrib.auth import get_user_model
from django.db.models import Count, Q
from django.http import Http404, HttpResponseRedirect
from django.urls import reverse, reverse_lazy
Expand All @@ -30,11 +31,14 @@
from .forms import (
EmailUserForm,
MemberChangeNameForm,
MemberChangeUsernameForm,
MemberContactSettingsEditForm,
MemberProfileEditForm,
)
from .models import Member, EmailMetadata

User = get_user_model()


class MemberDetailView(DetailView):
"""
Expand Down Expand Up @@ -178,6 +182,20 @@ def get_object(self, queryset=None):
return self.request.user.member


class MemberChangeUsernameView(PrivateMixin, UpdateView):
"""
Creates an edit view of the current member's name.
"""

form_class = MemberChangeUsernameForm
model = User
template_name = "member/my-member-change-username.html"
success_url = reverse_lazy("my-member-settings")

def get_object(self, queryset=None):
return self.request.user


class MemberSendConfirmationEmailView(PrivateMixin, RedirectView):
"""
Send a confirmation email and redirect back to the settings page.
Expand Down
2 changes: 1 addition & 1 deletion open_humans/templates/member/my-member-change-name.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{{ form|as_bootstrap_horizontal:"col-md-4" }}

<span id="helpBlock" class="help-block col-md-offset-4">
You may use any name you wish, provided you follow our
Your name is publicly visible. You may use any name you wish, provided you follow our
<a href="{% url 'community_guidelines' %}#naming">Naming Guidelines</a>.
</span>

Expand Down
15 changes: 4 additions & 11 deletions open_humans/templates/member/my-member-settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,11 @@ <h5>Allow other members to contact you?</h5>

<div class="col-md-6">
<p>{{ user.username }}</p>

<p class="text-muted">
Please email <a href="mailto:[email protected]">[email protected]</a>
to request a username change.
</p>

{% comment %}
<a href="#?next={% url 'my-member-settings' %}"
class="btn btn-default btn-sm btn-primary">
Change username
<a href="{% url 'my-member-change-username' %}?next={% url 'my-member-settings' %}"
class="btn btn-sm btn-primary">
Change username
</a>
{% endcomment %}

</div>
</div>

Expand Down
5 changes: 5 additions & 0 deletions open_humans/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@
member_views.MemberChangeNameView.as_view(),
name="my-member-change-name",
),
path(
"member/me/change-username/",
member_views.MemberChangeUsernameView.as_view(),
name="my-member-change-username",
),
path(
"member/me/send-confirmation-email/",
member_views.MemberSendConfirmationEmailView.as_view(),
Expand Down

0 comments on commit baac3ec

Please sign in to comment.