Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Added slugize to team model. Fixes #3426.
Browse files Browse the repository at this point in the history
  • Loading branch information
aandis committed Mar 9, 2016
1 parent 9272d0e commit 9290826
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions gratipay/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class NoSelfTipping(Exception): pass
class NoTippee(Exception): pass
class NoTeam(Exception): pass
class BadAmount(Exception): pass
class InvalidTeamName(Exception): pass

class FailedToReserveUsername(Exception): pass

Expand Down
18 changes: 18 additions & 0 deletions gratipay/models/team.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
"""Teams on Gratipay receive payments and distribute payroll.
"""
import re
import requests
from aspen import json, log
from gratipay.exceptions import InvalidTeamName
from gratipay.models import add_event
from postgres.orm import Model

# Should have at least one alphabet.
TEAM_NAME_PATTERN = re.compile(r'^(?=.*[A-Za-z])([A-Za-z0-9.,-_ ]+)$')

def slugize(name):
""" Create a slug from a team name.
"""
if TEAM_NAME_PATTERN.match(name) is None:
raise InvalidTeamName

slug = name.strip()
for c in (',', ' '):
slug = slug.replace(c, '-') # Avoid % encoded characters in slug url.
while '--' in slug:
slug = slug.replace('--', '-')
slug = slug.strip('-')
return slug

class Team(Model):
"""Represent a Gratipay team.
Expand Down
2 changes: 1 addition & 1 deletion www/teams/create.json.spt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ if request.method == 'POST':

try:
fields['slug'] = slugize(fields['name'])
except AssertionError:
except InvalidTeamName:
raise Response(400, _("Sorry, team name contains invalid characters."))

try:
Expand Down

0 comments on commit 9290826

Please sign in to comment.