Skip to content

Commit

Permalink
Add new models and api for the path support along with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanchowdhury committed Jul 18, 2016
1 parent 3247757 commit 00295a0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
49 changes: 47 additions & 2 deletions tahrir_api/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Remy D <remyd@civx.us>
# Description: API For interacting with the Tahrir database

from utils import autocommit, badge_name_to_id
from utils import autocommit, convert_name_to_id
from model import Badge, Invitation, Issuer, Assertion, Person, Authorization
from model import Team
from sqlalchemy import create_engine, func, and_, not_
from sqlalchemy.orm import sessionmaker, scoped_session
from datetime import (
Expand Down Expand Up @@ -49,6 +50,50 @@ def __init__(self, dburi=None, session=None, autocommit=True,

self.notification_callback = notification_callback

def team_exists(self, team_id):
"""
Check to see if this badge already exists in the database
:type team_id: str
:param team_id: The ID of a Team
"""

return self.session.query(Team).filter(
func.lower(Team.id) == func.lower(team_id)).count() != 0

def get_team(self, team_id):
"""
Return the team with the given ID
:type team_id: str
:param team_id: The ID of the badge to return
"""

if self.team_exists(team_id):
return self.session.query(Team).filter(
func.lower(Team.id) == func.lower(team_id)).one()
return None

@autocommit
def create_team(self, name, team_id=None):
"""
Adds a new team to the database
:type name: str
:param name: Name of the team
"""

if not team_id:
team_id = convert_name_to_id(name)

if not self.team_exists(team_id):
new_team = Team(id=team_id,
name=name)

self.session.add(new_team)
self.session.flush()
return team_id

def badge_exists(self, badge_id):
"""
Check to see if this badge already exists in the database
Expand Down Expand Up @@ -153,7 +198,7 @@ def add_badge(self, name, image, desc, criteria, issuer_id,
"""

if not badge_id:
badge_id = badge_name_to_id(name)
badge_id = convert_name_to_id(name)

if not self.badge_exists(badge_id):
# Make sure the tags string has a trailing
Expand Down
38 changes: 35 additions & 3 deletions tahrir_api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,22 @@ def __json__(self):
)


def badge_id_default(context):
def generate_default_id(context):
return context.current_parameters['name'].lower().replace(' ', '-')


class Badge(DeclarativeBase):
__tablename__ = 'badges'
id = Column(Unicode(128), primary_key=True, default=badge_id_default)
id = Column(Unicode(128), primary_key=True, default=generate_default_id)
name = Column(Unicode(128), nullable=False, unique=True)
image = Column(Unicode(128), nullable=False)
stl = Column(Unicode(128))
description = Column(Unicode(128), nullable=False)
criteria = Column(Unicode(128), nullable=False)
issuer_id = Column(Integer, ForeignKey('issuers.id'), nullable=False)
perk = relationship("Perk", backref="badge")
authorizations = relationship("Authorization", backref="badge")
assertions = relationship("Assertion", backref="badge")
issuer_id = Column(Integer, ForeignKey('issuers.id'), nullable=False)
invitations = relationship("Invitation", backref="badge")
created_on = Column(DateTime, nullable=False,
default=datetime.datetime.utcnow)
Expand Down Expand Up @@ -106,6 +107,37 @@ def authorized(self, person):
return False


class Team(DeclarativeBase):
__tablename__ = "team"
id = Column(Unicode(128), primary_key=True, default=generate_default_id)
name = Column(Unicode(128), nullable=False, unique=True)
series = relationship("Series", backref="team")
created_on = Column(DateTime, nullable=False,
default=datetime.datetime.utcnow)


class Series(DeclarativeBase):
__tablename__ = 'series'
id = Column(Unicode(128), primary_key=True, default=generate_default_id)
name = Column(Unicode(128), nullable=False, unique=True)
description = Column(Unicode(128), nullable=False)
created_on = Column(DateTime, nullable=False,
default=datetime.datetime.utcnow)
last_updated = Column(DateTime, nullable=False,
default=datetime.datetime.utcnow)
tags = Column(Unicode(128))
perk = relationship("Perk", backref="series")
team_id = Column(Unicode(128), ForeignKey('team.id'), nullable=False)


class Perk(DeclarativeBase):
__tablename__ = 'badgeseries'
id = Column(Integer, unique=True, primary_key=True)
position = Column(Integer, default=None)
badge_id = Column(Unicode(128), ForeignKey('badges.id'), nullable=False)
series_id = Column(Unicode(128), ForeignKey('series.id'), nullable=False)


class Person(DeclarativeBase):
__tablename__ = 'persons'
id = Column(Integer, unique=True, primary_key=True)
Expand Down
2 changes: 1 addition & 1 deletion tahrir_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _wrapper(self, *args, **kwargs):
return _wrapper


def badge_name_to_id(name):
def convert_name_to_id(name):
"""
Convert a badge name into a valid badge ID.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_dbai.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def test_add_badges(self):

assert self.api.badge_exists("testbadge") is True

def test_add_team(self):
self.api.create_team("TestTeam")

assert self.api.team_exists("testteam") is True

def test_add_person(self):
self.api.add_person("test@tester.com")
assert self.api.person_exists("test@tester.com") is True
Expand Down

0 comments on commit 00295a0

Please sign in to comment.