Skip to content

Commit

Permalink
let's put the badge_name_to_id function in util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dtgay committed Sep 23, 2014
1 parent f4c2e7b commit a231489
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
20 changes: 2 additions & 18 deletions tahrir_api/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Remy D <remyd@civx.us>
# Description: API For interacting with the Tahrir database

from utils import autocommit
from utils import autocommit, badge_name_to_id
from model import Badge, Invitation, Issuer, Assertion, Person, Authorization
from sqlalchemy import create_engine, func, and_, not_
from sqlalchemy.orm import sessionmaker, scoped_session
Expand Down Expand Up @@ -114,22 +114,6 @@ def get_all_badges(self):

return self.session.query(Badge)

def badge_name_to_id(self, name):
"""
Convert a badge name into a valid badge ID.
:type name: string
:param name: The badge name to convert to an ID
"""

badge_id = name.lower().replace(" ", "-")
bad = ['"', "'", '(', ')', '*', '&', '?']
replacements = dict(zip(bad, [''] * len(bad)))
for a, b in replacements.items():
badge_id = badge_id.replace(a, b)

return badge_id

@autocommit
def delete_badge(self, badge_id):
"""
Expand Down Expand Up @@ -169,7 +153,7 @@ def add_badge(self, name, image, desc, criteria, issuer_id,
"""

if not badge_id:
badge_id = self.badge_name_to_id(name)
badge_id = badge_name_to_id(name)

if not self.badge_exists(badge_id):
# Make sure the tags string has a trailing
Expand Down
17 changes: 17 additions & 0 deletions tahrir_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ def _wrapper(self, *args, **kwargs):
_wrapper.__doc__ = func.__doc__

return _wrapper


def badge_name_to_id(name):
"""
Convert a badge name into a valid badge ID.
:type name: string
:param name: The badge name to convert to an ID
"""

badge_id = name.lower().replace(" ", "-")
bad = ['"', "'", '(', ')', '*', '&', '?']
replacements = dict(zip(bad, [''] * len(bad)))
for a, b in replacements.items():
badge_id = badge_id.replace(a, b)

return badge_id

0 comments on commit a231489

Please sign in to comment.