Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
[fix bug 784854] Display form errors on top of form.
Browse files Browse the repository at this point in the history
  • Loading branch information
glogiotatidis committed Oct 15, 2012
1 parent 93fbb01 commit af216ed
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 38 deletions.
9 changes: 0 additions & 9 deletions apps/common/backends.py
@@ -1,6 +1,3 @@
import base64
import hashlib

from django.contrib.auth.models import User

import commonware.log
Expand All @@ -9,12 +6,6 @@
log = commonware.log.getLogger('b.common')


def get_username(email):
"""Return username."""
return 'u/{0}'.format(base64.urlsafe_b64encode(
hashlib.sha1(email).digest()).rstrip('='))


class MozilliansBrowserID(BrowserIDBackend):
"""
Special auth backend to allow registration to work without a
Expand Down
23 changes: 0 additions & 23 deletions apps/common/browserid.py
@@ -1,23 +0,0 @@
import hashlib
import re

USERNAME_MAX_LENGTH = 30


def get_username(email):
"""Calculate username from email address."""
from django.contrib.auth.models import User

email = email.split('@')[0]
username = re.sub(r'[^\w.@+-]', '-', email)
username = username[:USERNAME_MAX_LENGTH]

while User.objects.filter(username=username).exists():
username += '_'

if username > USERNAME_MAX_LENGTH:
# We failed to calculate a name for you, default to a
# email digest.
username = hashlib.sha1(email).hexdigest()

return username
1 change: 0 additions & 1 deletion apps/common/middleware.py
Expand Up @@ -12,7 +12,6 @@
from funfactory.urlresolvers import reverse



# TODO: this is hackish. Once we update mozillians to the newest playdoh layout
error_page = __import__('%s.urls' % os.path.basename(ROOT)).urls.error_page
log = commonware.log.getLogger('m.phonebook')
Expand Down
13 changes: 12 additions & 1 deletion apps/phonebook/templates/phonebook/edit_profile.html
Expand Up @@ -15,12 +15,23 @@
{% block body_classes %}box-content{% endblock %}

{% block main_content %}
<h1>{{ _('Edit Your Profile') }}</h1>

{% if form.errors %}
<div class="alert alert-error">
Please correct the following errors:
<ol>
{% for error in form.errors %}
<li>{{ error|escape }}</li>
{% endfor %}
</ol>
</div>
{% endif %}
<form action="{{ url('profile.edit') }}"
method="POST"
enctype="multipart/form-data"
class="form-horizontal edit-profile">
{{ csrf() }}
<h1>{{ _('Edit Your Profile') }}</h1>
<div class="tabbable">
<ul class="nav nav-pills">
<li class="active"><a href="#1" data-toggle="tab">{{ _('Profile') }}</a></li>
Expand Down
2 changes: 2 additions & 0 deletions apps/users/admin.py
Expand Up @@ -6,6 +6,7 @@


class UserProfileAdmin(AdminImageMixin, admin.ModelAdmin):
"""UserProfile Admin."""
fields = ['user', 'user_email', 'display_name', 'photo', 'ircname',
'is_vouched', 'vouched_by', 'bio', 'website', 'groups', 'skills',
'languages', 'country', 'region', 'city']
Expand All @@ -30,6 +31,7 @@ def has_delete_permission(self, *a, **kw):


class UsernameBlacklistAdmin(admin.ModelAdmin):
"""UsernameBlacklist Admin."""
save_on_top = True
search_fields = ['value']
list_filter = ['is_regex']
Expand Down
33 changes: 31 additions & 2 deletions apps/users/helpers.py
@@ -1,9 +1,14 @@
import hashlib
import re

from models import UsernameBlacklist


def validate_username(username):
"""Validate username.
Import modules here to prevent dependency breaking.
"""
from models import UsernameBlacklist
username = username.lower()

if (UsernameBlacklist.
Expand All @@ -15,3 +20,27 @@ def validate_username(username):
return False

return True


def calculate_username(email):
"""Calculate username from email address.
Import modules here to prevent dependency breaking.
"""
from models import USERNAME_MAX_LENGTH
from django.contrib.auth.models import User

email = email.split('@')[0]
username = re.sub(r'[^\w.@+-]', '-', email)
username = username[:USERNAME_MAX_LENGTH]

while User.objects.filter(username=username).exists():
username += '_'

if username > USERNAME_MAX_LENGTH:
# We failed to calculate a name for you, default to a
# email digest.
username = hashlib.sha1(email).hexdigest()

return username
2 changes: 2 additions & 0 deletions apps/users/models.py
Expand Up @@ -33,6 +33,8 @@
product_details.get_regions('en-US').values())
COUNTRIES = sorted(COUNTRIES, key=lambda country: country[1])

USERNAME_MAX_LENGTH = 30


class UserProfile(SearchMixin, models.Model):
# This field is required.
Expand Down
4 changes: 2 additions & 2 deletions settings/default.py
Expand Up @@ -7,7 +7,7 @@
from funfactory import settings_base as base
from settings import initial as pre

from common.browserid import get_username
from apps.users.helpers import calculate_username

## Log settings
SYSLOG_TAG = "http_app_mozillians"
Expand Down Expand Up @@ -155,7 +155,7 @@

# BrowserID creates a user if one doesn't exist.
BROWSERID_CREATE_USER = True
BROWSERID_USERNAME_ALGO = get_username
BROWSERID_USERNAME_ALGO = calculate_username

# On Login, we redirect through register.
LOGIN_REDIRECT_URL = '/register'
Expand Down

0 comments on commit af216ed

Please sign in to comment.