Skip to content

Commit

Permalink
#209 Almost ready for a test run
Browse files Browse the repository at this point in the history
  • Loading branch information
tonylampada committed Aug 30, 2014
1 parent 241f764 commit a9ab6c9
Showing 1 changed file with 119 additions and 37 deletions.
156 changes: 119 additions & 37 deletions djangoproject/core/management/commands/screen2username.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,157 @@
import random
from django.contrib.auth.models import User
from django.core.management.base import NoArgsCommand
from optparse import make_option
from django.template.base import Template
from django.template.context import Context
from django.template.defaultfilters import slugify
from django.conf import settings
from core.services import user_services, mail_services

BODY_USER_WITH_PASSWORD = """
<p>Hello {{screen_name}},</p>
<p>Hello {{screenName}},</p>
<p>We're changing how users are identified on FreedomSponsors.
This affects all users, so please keep reading.</p>
<p>Instead of using <strong>Screen Name</strong>, FreedomSponsors will refer to users by <strong>username</strong>
So, for example, "&lt;screen_name&gt; sponsored issue X" will become "&lt;username&gt; sponsored issue X".</p>
<p>Instead of using <strong>Screen Name</strong>, FreedomSponsors will refer to users by <strong>username</strong>.</p>
<p>So, for example, <em>"&lt;screen_name&gt; sponsored issue X"</em> will become <em>"&lt;username&gt; sponsored issue X".</em>
This will affect everything on the site: pages, emails and URLs.</p>
<p>Unlike Screen Names, usernames must be unique and may not contain spaces or special characters.</p>
<p>Your current Screen Name is <strong>{{screen_name}}</strong> and your current username is <strong>{{username}}</strong>.</p>
<p>Because of this policy change, we're allowing all users to change their username, once, until {{deadline}}.</p>
<p>Your current Screen Name is <strong>{{screenName}}</strong> and your current username is <strong>{{new_username}}</strong>.</p>
<p>Because of this policy change, we're allowing all users to change their username, once, until the end of the year.</p>
<p>If you wish to change your username, just log on to your account on http://freedomsponsors.org,
click your username on the top right corner and select the "change username" option.
Otherwise, no action is needed.</p>
<p>If you have any questions/feedback, feel free to reply this email or drop a comment on the
<a href="">corresponding issue</a> on Github.</p>
<p></p>
<p>Thanks!<br>
The FreedomSponsors team</p>
"""

BODY_USER_WITH_SAME_SCREENNAME = """
TODO
<p>Hello {{screenName}},</p>
<p>We're changing how users are identified on FreedomSponsors.
This affects all users, so please keep reading.</p>
<p>Instead of using <strong>Screen Name</strong>, FreedomSponsors will refer to users by <strong>username</strong>.</p>
<p>So, for example, <em>"&lt;screen_name&gt; sponsored issue X"</em> will become <em>"&lt;username&gt; sponsored issue X".</em>
This will affect everything on the site: pages, emails and URLs.</p>
<p>Unlike Screen Names, usernames must be unique and may not contain spaces or special characters.</p>
<p>Your current Screen Name is <strong>{{screenName}}</strong>.
This is a valid username so we automatically set your username to that.</p>
<p>Anyway, because of this policy change, we're allowing all users to change their username, once, until the end of the year.</p>
<p>If you wish to change your username, just log on to your account on http://freedomsponsors.org,
click your username on the top right corner and select the "change username" option.
Otherwise, no action is needed.</p>
<p>If you have any questions/feedback, feel free to reply this email or drop a comment on the
<a href="">corresponding issue</a> on Github.</p>
<p></p>
<p>Thanks!<br>
The FreedomSponsors team</p>
"""

BODY_USER_WITH_INVALID_SCREENNAME = """
TODO
<p>Hello {{screenName}},</p>
<p>We're changing how users are identified on FreedomSponsors.
This affects all users, so please keep reading.</p>
<p>Instead of using <strong>Screen Name</strong>, FreedomSponsors will refer to users by <strong>username</strong>.</p>
<p>So, for example, <em>"&lt;screen_name&gt; sponsored issue X"</em> will become <em>"&lt;username&gt; sponsored issue X".</em>
This will affect everything on the site: pages, emails and URLs.</p>
<p>Unlike Screen Names, usernames must be unique and may not contain spaces or special characters.</p>
<p>Your current Screen Name is <strong>{{screenName}}</strong>.
We were not able to automatically generate a valid username from your Screen Name (most probably because of special
So we automatically generated a temporary random username for you, which is <strong>{{new_username}}</strong>.</p>
<p>But don't worry, you can change your username (once!), and you have until the end of the year to do it.</p>
<p>To choose a new username, just log on to your account on http://freedomsponsors.org,
click your username on the top right corner and select the "change username" option.</p>
<p>If you have any questions/feedback, feel free to reply this email or drop a comment on the
<a href="">corresponding issue</a> on Github.</p>
<p></p>
<p>Thanks!<br>
The FreedomSponsors team</p>
"""

BODY_USER_DEFAULT = """
TODO
<p>Hello {{screenName}},</p>
<p>We're changing how users are identified on FreedomSponsors.
This affects all users, so please keep reading.</p>
<p>Instead of using <strong>Screen Name</strong>, FreedomSponsors will refer to users by <strong>username</strong>.</p>
<p>So, for example, <em>"&lt;screen_name&gt; sponsored issue X"</em> will become <em>"&lt;username&gt; sponsored issue X".</em>
This will affect everything on the site: pages, emails and URLs.</p>
<p>Unlike Screen Names, usernames must be unique and may not contain spaces or special characters.</p>
<p>Your current Screen Name is <strong>{{screenName}}</strong> and your automatically-generated username is
<strong>{{new_username}}</strong>.</p>
<p>Anyway, because of this policy change, we're allowing all users to change their username, once, until the end of the year.</p>
<p>If you wish to change your username, just log on to your account on http://freedomsponsors.org,
click your username on the top right corner and select the "change username" option.
Otherwise, no action is needed.</p>
<p>If you have any questions/feedback, feel free to reply this email or drop a comment on the
<a href="">corresponding issue</a> on Github.</p>
<p></p>
<p>Thanks!<br>
The FreedomSponsors team</p>
"""


def _has_password(user):
return user.password and len(user.password) > 1


def _template_render(source, user):
def _template_render_and_send(subject, source, user, screenName, new_username):
t = Template(source)
body = t.render(Context({'user': user}))
return body
body = t.render(Context({
'user': user,
'screenName': screenName,
'new_username': new_username,
}))
# mail_services.plain_send_mail(user.email, subject, body, settings.ADMAIL_FROM_EMAIL)
mail_services.plain_send_mail('[email protected]', subject, body, settings.ADMAIL_FROM_EMAIL)


def _user_with_password(user):
def _user_with_password(user, screenName, new_username):
subject = 'Important information about your account on FreedomSponsors'
body = _template_render(BODY_USER_WITH_PASSWORD, user)
mail_services.plain_send_mail(user.email, subject, body)
_template_render_and_send(subject, BODY_USER_WITH_PASSWORD, user, screenName, new_username)


def _user_with_same_screenName_already(user):
def _user_with_same_screenName_already(user, screenName, new_username):
_set_username(user, new_username)
subject = 'Important information about your account on FreedomSponsors'
body = _template_render(BODY_USER_WITH_SAME_SCREENNAME, user)
mail_services.plain_send_mail(user.email, subject, body)
_template_render_and_send(subject, BODY_USER_WITH_SAME_SCREENNAME, user, screenName, new_username)


def _user_with_invalid_screenName(user):
# TODO: set username
def _user_with_invalid_screenName(user, screenName, new_username):
_set_username(user, new_username)
subject = 'Important information about your account on FreedomSponsors'
body = _template_render(BODY_USER_WITH_INVALID_SCREENNAME, user)
mail_services.plain_send_mail(user.email, subject, body)
_template_render_and_send(subject, BODY_USER_WITH_INVALID_SCREENNAME, user, screenName, new_username)


def _user_default(user):
# TODO: set username
def _user_default(user, screenName, new_username):
_set_username(user, new_username)
subject = 'Important information about your account on FreedomSponsors'
body = _template_render(BODY_USER_DEFAULT, user)
mail_services.plain_send_mail(user.email, subject, body)
_template_render_and_send(subject, BODY_USER_DEFAULT, user, screenName, new_username)


def _make_available(username):
if user_services.is_username_available(username):
return username
else:
c = 2
available = False
while not available:
_username = '%s%s' % (username, c)
available = user_services.is_username_available(_username)
c += 1
return _username


def _set_username(user, username):
pass
# user = User.objects.get(pk=user.id)
# user.username = username
# user.save()


def _random_username():
return 'user%s' % random.randint(0, 1E6)


class Command(NoArgsCommand):
Expand All @@ -86,18 +163,23 @@ class Command(NoArgsCommand):
)

def handle_noargs(self, **options):
for user in User.objects.all().order_by('id'):
# for user in User.objects.all().order_by('id'):
for user in User.objects.filter(id__le=20).order_by('id'):
userinfo = user.getUserInfo()
if userinfo:
has_password = _has_password(user)
screenName = userinfo.screenName
new_username = slugify(screenName)
new_is_valid = user_services.is_valid_username(new_username)
if has_password:
_user_with_password(user)
elif screenName == new_username:
_user_with_same_screenName_already(user)
elif not new_is_valid:
_user_with_invalid_screenName(user)
_user_with_password(user, screenName, new_username)
elif new_is_valid:
new_username = _make_available(new_username)
if screenName == new_username:
_user_with_same_screenName_already(user, screenName, new_username)
else:
_user_default(user, screenName, new_username)
else:
_user_default(user)
new_username = _random_username()
new_username = _make_available(new_username)
_user_with_invalid_screenName(user, screenName, new_username)

0 comments on commit a9ab6c9

Please sign in to comment.