Skip to content

Commit

Permalink
#209 let users change their username
Browse files Browse the repository at this point in the history
  • Loading branch information
tonylampada committed Aug 28, 2014
1 parent ab75a45 commit 5773393
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
5 changes: 5 additions & 0 deletions djangoproject/core/services/user_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from emailmgr.models import EmailAddress
from core.models import *
from django.conf import settings
import re

__author__ = 'tony'

Expand Down Expand Up @@ -70,6 +71,10 @@ def deactivate_user(user):
mail_services.notify_admin(subject, body)


def is_valid_username(username):
return re.search(r'^\w*[a-zA-Z]\w*$', username) is not None


def is_username_available(username):
existing_user = get_or_none(User, username=username)
return False if existing_user else True
Expand Down
6 changes: 4 additions & 2 deletions djangoproject/core/views/json_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ def latest_activity(request):

@login_required
def check_username_availability(request, username):
is_available = user_services.is_username_available(username)
return HttpResponse(str(is_available).lower())
result = {'is_valid': user_services.is_valid_username(username)}
if result['is_valid']:
result['is_available'] = user_services.is_username_available(username)
return HttpResponse(json.dumps(result))


def _convert_offers_to_dict(offers):
Expand Down
19 changes: 11 additions & 8 deletions djangoproject/core/views/user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,18 @@ def change_username(request):
if can_change:
old_username = request.user.username
new_username = request.POST['new_username']
change_ok = user_services.change_username(request.user, new_username)
if change_ok:
messages.info(request, 'Your username has been changed')
can_change = False
subject = 'user %s changed username %s --> %s' % (request.user.id, old_username, new_username)
body = '<a href="http://freedomsponsors.org/user/%s">%s</a>' % (request.user.id, new_username)
mail_services.notify_admin(subject, body)
if not user_services.is_valid_username(new_username):
messages.error(request, 'Sorry, this username is invalid')
else:
messages.error(request, 'Sorry, that username is already taken')
change_ok = user_services.change_username(request.user, new_username)
if change_ok:
messages.info(request, 'Your username has been changed')
can_change = False
subject = 'user %s changed username %s --> %s' % (request.user.id, old_username, new_username)
body = '<a href="http://freedomsponsors.org/user/%s">%s</a>' % (request.user.id, new_username)
mail_services.notify_admin(subject, body)
else:
messages.error(request, 'Sorry, that username is already taken')
else:
messages.warning(request, 'You cannot change your username anymore')
return render_to_response(
Expand Down
23 changes: 15 additions & 8 deletions djangoproject/templates/core2/change_username.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
$scope.checking = true;
var new_username = $scope.new_username;
FSApi.check_username_availability(new_username).onResult(function(result){
result = ''+result;
$scope.$apply(function(){
$scope.available = result.toLowerCase() == 'true';
if($scope.available){
$scope.message = 'Great! "'+new_username+'" is available!'
} else {
if(!result.is_valid){
$scope.available = false;
$scope.message = 'Sorry! "'+new_username+'" is not a valid username (should be alphanumeric).'
} else if(!result.is_available){
$scope.available = false;
$scope.message = 'Sorry, "'+new_username+'" is already taken.'
} else {
$scope.available = true;
$scope.message = 'Great! "'+new_username+'" is available!'
}
$scope.checking = false;
});
Expand All @@ -56,13 +59,17 @@
<div class="content" align="center" ng-app="main" ng-controller="ChangeUsernameCtrl">
<div class="content-section" align="left">
{% if can_change %}
<h3>Change username</h3>
<h2>Change username</h2>
<h3 class="blue-text">Attention: you can only change your username once. Make it a good one :-)</h3>
<hr>
<form id="le_form" method="post" action="/user/change_username">
{% csrf_token %}
<div>
New username: <input type="text" name="new_username" ng-model="new_username"
ng-keypress="keypress($event)">
New username:
<input type="text" name="new_username"
ng-model="new_username"
ng-pattern="/\w*[a-zA-Z]\w*/"
ng-keypress="keypress($event)">
<a href ng-click="check()" class="fs-button blue medium">Available?</a>
<a href ng-show="available" class="fs-button green medium" ng-click="submit()">Change!</a>
</div>
Expand Down

0 comments on commit 5773393

Please sign in to comment.