From d3078f7cf34d7a2e77aadb85329f03e7d2987708 Mon Sep 17 00:00:00 2001 From: PotatoDumplings Date: Mon, 21 Apr 2014 17:25:30 -0700 Subject: [PATCH 1/6] New feature - real names. Add a real name in preferences and user facing names changed to RealName [username]. --- r2/r2/controllers/api.py | 20 +++++++++++++- r2/r2/controllers/validator/validator.py | 30 +++++++++++++++++++++ r2/r2/lib/errors.py | 5 ++++ r2/r2/models/account.py | 10 ++++++- r2/r2/templates/articlenavigation.html | 2 +- r2/r2/templates/award.html | 4 +-- r2/r2/templates/award.xml | 2 +- r2/r2/templates/comment.xml | 2 +- r2/r2/templates/edit.xml | 4 +-- r2/r2/templates/inlinearticle.html | 2 +- r2/r2/templates/inlinearticle.xml | 2 +- r2/r2/templates/inlinecomment.html | 2 +- r2/r2/templates/link.xml | 2 +- r2/r2/templates/prefupdate.html | 9 +++++++ r2/r2/templates/printable.html | 2 +- r2/r2/templates/profilebar.html | 2 +- r2/r2/templates/showmeetup.html | 2 +- r2/r2/templates/topcontributors.html | 2 +- r2/r2/templates/topmonthlycontributors.html | 2 +- r2/r2/templates/usertableitem.html | 2 +- tasks/manual_test_script | 2 +- 21 files changed, 90 insertions(+), 20 deletions(-) diff --git a/r2/r2/controllers/api.py b/r2/r2/controllers/api.py index a14a911c..4ae843ee 100644 --- a/r2/r2/controllers/api.py +++ b/r2/r2/controllers/api.py @@ -577,15 +577,32 @@ def POST_resendconfirmation(self, res): VModhash(), curpass = nop('curpass'), email = ValidEmail("email"), + realname = VRname("real_name"), newpass = nop("newpass"), verpass = nop("verpass"), password = VPassword(['newpass', 'verpass'])) - def POST_update(self, res, email, curpass, password, newpass, verpass): + def POST_update(self, res, email, curpass, realname, password, newpass, verpass): res._update('status', innerHTML='') if res._chk_error(errors.WRONG_PASSWORD): res._focus('curpass') res._update('curpass', value='') return + + if res._chk_error(errors.BAD_REALNAME_CHARS): + res._focus('real_name') + elif res._chk_error(errors.BAD_REALNAME_SHORT): + res._focus('real_name') + elif res._chk_error(errors.BAD_REALNAME_LONG): + res._focus('real_name') + if realname and realname == c.user.real_name: + c.user.real_name = None + c.user._commit() + res._update('status', innerHTML=_('Your real name has been removed')) + elif realname: + c.user.real_name = realname + c.user._commit() + res._update('status', innerHTML=_('Your real name has been updated')) + updated = False if res._chk_error(errors.BAD_EMAIL): res._focus('email') @@ -594,6 +611,7 @@ def POST_update(self, res, email, curpass, password, newpass, verpass): elif email and (not hasattr(c.user,'email') or c.user.email != email): c.user.email = email + c.user._commit() self.send_confirmation() res._update('status', innerHTML=_('Your email has been updated. You will have to confirm before commenting or posting.')) diff --git a/r2/r2/controllers/validator/validator.py b/r2/r2/controllers/validator/validator.py index 0d4de246..9f6c8f2c 100644 --- a/r2/r2/controllers/validator/validator.py +++ b/r2/r2/controllers/validator/validator.py @@ -600,6 +600,36 @@ def run(self, user_name): except NotFound: return user_name +realname_rx = re.compile(r"^[a-zA-Z\s\-]{3,40}$", re.UNICODE) + +def chkrealname(x): + try: + return str(x) if realname_rx.match(x) else None + except TypeError: + return None + except UnicodeEncodeError: + return None + +def whyrealnamebad(x): + if not x: + return errors.BAD_REALNAME_CHARS + if len(x)<3: + return errors.BAD_REALNAME_SHORT + if len(x)>40: + return errors.BAD_REALNAME_LONG + return errors.BAD_REALNAME_CHARS + +class VRname(VRequired): + def __init__(self, item, *a, **kw): + VRequired.__init__(self, item, errors.BAD_REALNAME, *a, **kw) + def run(self, real_name): + original_real_name = real_name; + real_name = chkrealname(real_name) + if not real_name: + return self.error(whyrealnamebad(original_real_name)) + else: + return real_name + class VLogin(VRequired): def __init__(self, item, *a, **kw): VRequired.__init__(self, item, errors.WRONG_PASSWORD, *a, **kw) diff --git a/r2/r2/lib/errors.py b/r2/r2/lib/errors.py index 681f2577..90b5cbf3 100644 --- a/r2/r2/lib/errors.py +++ b/r2/r2/lib/errors.py @@ -35,6 +35,11 @@ ('BAD_USERNAME_SHORT', _('Username is too short')), ('BAD_USERNAME_LONG', _('Username is too long')), ('BAD_USERNAME_CHARS', _('Username may not contain special characters')), + ('BAD_REALNAME', _('Invalid name')), + ('BAD_REALNAME_SHORT', _('Name is too short')), + ('BAD_REALNAME_LONG', _('Name is too long')), + ('BAD_REALNAME_CHARS', _('Name may not contain special characters')), + ('REALNAME_REMOVE', _('Your real name has been removed')), ('USERNAME_TAKEN', _('That username is already taken')), ('NO_THING_ID', _('Id not specified')), ('NOT_AUTHOR', _("Only the author can do that")), diff --git a/r2/r2/models/account.py b/r2/r2/models/account.py index f4912514..0fb4170e 100644 --- a/r2/r2/models/account.py +++ b/r2/r2/models/account.py @@ -87,10 +87,18 @@ class Account(Thing): messagebanned = False, dashboard_visit = datetime(2006,10,1, tzinfo = g.tz), wiki_association_attempted_at = None, # None or datetime - wiki_account = None # None, str(account name) or the special string '__taken__', if a new + wiki_account = None, # None, str(account name) or the special string '__taken__', if a new # user didn't get an account because someone else already had the name. + real_name = None ) + @property + def printable_name(self): + if self.real_name: + return self.real_name + " [" + self.name + "]" + else: + return self.name + def karma_ups_downs(self, kind, sr = None): # NOTE: There is a legacy inconsistency in this method. If no subreddit # is specified, karma from all subreddits will be totaled, with each diff --git a/r2/r2/templates/articlenavigation.html b/r2/r2/templates/articlenavigation.html index 9cb23104..5e3b5c3e 100644 --- a/r2/r2/templates/articlenavigation.html +++ b/r2/r2/templates/articlenavigation.html @@ -60,7 +60,7 @@ ${_('Unknown')} %else: <% author_path = unsafe(add_sr("/user/%s/" % websafe(author.name), sr_path = False)) %> - ${author.name} + ${author.printable_name} %endif diff --git a/r2/r2/templates/award.html b/r2/r2/templates/award.html index 89023f82..51520741 100644 --- a/r2/r2/templates/award.html +++ b/r2/r2/templates/award.html @@ -11,11 +11,11 @@

- ${thing.author.name} + ${thing.author.printable_name}

gave - ${thing.recipient().name} ${thing.amount} + ${thing.recipient().printable_name} ${thing.amount} karma for ${thing.reason} (${prettytime(thing._date)})
diff --git a/r2/r2/templates/award.xml b/r2/r2/templates/award.xml index 032ce990..44c2d420 100644 --- a/r2/r2/templates/award.xml +++ b/r2/r2/templates/award.xml @@ -34,7 +34,7 @@ <% domain = get_domain(cname = c.cname, subreddit = False) %> - Awarded by <a href="http://${domain}/user/${thing.author.name}">${thing.author.name}</a> to <a href="http://${domain}/user/${thing.recipient().name}">${thing.recipient().name}</a> for ${thing.reason} + Awarded by <a href="http://${domain}/user/${thing.author.name}">${thing.author.printable_name}</a> to <a href="http://${domain}/user/${thing.recipient().name}">${thing.recipient().printable_name}</a> for ${thing.reason} diff --git a/r2/r2/templates/comment.xml b/r2/r2/templates/comment.xml index fa71d6a2..6af6e974 100644 --- a/r2/r2/templates/comment.xml +++ b/r2/r2/templates/comment.xml @@ -30,7 +30,7 @@ %> - ${thing.author.name} ${_("on")} ${thing.link.title} + ${thing.author.printable_name} ${_("on")} ${thing.link.title} ${url} ${url} ${thing._date.isoformat()} diff --git a/r2/r2/templates/edit.xml b/r2/r2/templates/edit.xml index d29e5235..f0b56fc9 100644 --- a/r2/r2/templates/edit.xml +++ b/r2/r2/templates/edit.xml @@ -11,8 +11,8 @@ ${rfc822format(thing._date)} <% domain = get_domain(cname = c.cname, subreddit = False) %> - Edited by <a href="http://${domain}/user/${thing.link_author.name}">${thing.author.name}</a> - Original by <a href="http://${domain}/user/${thing.link_author.name}">${thing.link_author.name}</a> + Edited by <a href="http://${domain}/user/${thing.link_author.name}">${thing.author.printable_name}</a> + Original by <a href="http://${domain}/user/${thing.link_author.name}">${thing.link_author.printable_name}</a> <% entities = {"\n": " "} %> <pre> ${saxutils.escape("\n".join(thing.diff[2:]), entities)} diff --git a/r2/r2/templates/inlinearticle.html b/r2/r2/templates/inlinearticle.html index 08ac9765..6e5e824e 100644 --- a/r2/r2/templates/inlinearticle.html +++ b/r2/r2/templates/inlinearticle.html @@ -48,7 +48,7 @@

by - ${thing.author.name} | + ${thing.author.printable_name} | ${thing.score_fmt(thing.score)['label']}v (${thing.num_comments}c) diff --git a/r2/r2/templates/inlinearticle.xml b/r2/r2/templates/inlinearticle.xml index 7f5e0c3a..43485fc7 100644 --- a/r2/r2/templates/inlinearticle.xml +++ b/r2/r2/templates/inlinearticle.xml @@ -38,7 +38,7 @@ ${rfc822format(thing._date)} <% domain = get_domain(cname = c.cname, subreddit = False) %> - Submitted by <a href="http://${domain}/user/${thing.author.name}">${thing.author.name}</a> + Submitted by <a href="http://${domain}/user/${thing.author.name}">${thing.author.printable_name}</a> <a href="${url}#comments">${thing.num_comments} ${com_label}</a> %if use_thumbs: diff --git a/r2/r2/templates/inlinecomment.html b/r2/r2/templates/inlinecomment.html index 07f7be3b..b59053da 100644 --- a/r2/r2/templates/inlinecomment.html +++ b/r2/r2/templates/inlinecomment.html @@ -7,7 +7,7 @@

${killhtml(thing.body)}

by - ${thing.author.name} + ${thing.author.printable_name} on ${thing.link.title} | ${thing.score_fmt(thing.score)['label']} diff --git a/r2/r2/templates/link.xml b/r2/r2/templates/link.xml index 729b186b..658f987b 100644 --- a/r2/r2/templates/link.xml +++ b/r2/r2/templates/link.xml @@ -43,7 +43,7 @@ votes = thing.score_fmt(thing.score)['label'] votes_lbl = ungettext("vote", "votes", votes) %> - Submitted by <a href="http://${domain}/user/${thing.author.name}">${thing.author.name}</a> + Submitted by <a href="http://${domain}/user/${thing.author.name}">${thing.author.printable_name}</a> &bull; ${votes} ${votes_lbl} &bull; diff --git a/r2/r2/templates/prefupdate.html b/r2/r2/templates/prefupdate.html index 260aa8e6..ce6cb2d5 100644 --- a/r2/r2/templates/prefupdate.html +++ b/r2/r2/templates/prefupdate.html @@ -38,6 +38,15 @@ ${error_field("NO_EMAIL")} + + ${_("Real Name")}: + + + ${error_field("BAD_REALNAME_CHARS")} + ${error_field("BAD_REALNAME_SHORT")} + ${error_field("BAD_REALNAME_LONG")} + + ${_("New password")}: diff --git a/r2/r2/templates/printable.html b/r2/r2/templates/printable.html index 1d7aba05..5407ca63 100644 --- a/r2/r2/templates/printable.html +++ b/r2/r2/templates/printable.html @@ -162,7 +162,7 @@ author_cls += " friend" elif gray: author_cls += " gray" - name = websafe(author.name) + name = websafe(author.printable_name) href = unsafe('href="%s"' % add_sr("/user/%s/" % name, sr_path = False)) if c.user_is_admin: name += " (%d)" % (author.link_karma) %> diff --git a/r2/r2/templates/profilebar.html b/r2/r2/templates/profilebar.html index dba7d561..fd6c3ae1 100644 --- a/r2/r2/templates/profilebar.html +++ b/r2/r2/templates/profilebar.html @@ -27,7 +27,7 @@ %if thing.user: