Skip to content

Commit

Permalink
Change Perk to Milestone
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanchowdhury committed Aug 31, 2016
1 parent 00418af commit b9ddf3d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 55 deletions.
92 changes: 46 additions & 46 deletions tahrir_api/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from utils import autocommit, convert_name_to_id
from model import Badge, Invitation, Issuer, Assertion, Person, Authorization
from model import Team, Series, Perk
from model import Team, Series, Milestone
from sqlalchemy import create_engine, func, and_, not_
from sqlalchemy.orm import sessionmaker, scoped_session
from datetime import (
Expand Down Expand Up @@ -171,107 +171,107 @@ def get_all_series(self):

return self.session.query(Series)

def perk_exists(self, perk_id):
def milestone_exists(self, milestone_id):
"""
Check to see if this perk already exists in the database
Check to see if this milestone already exists in the database
:type perk_id: str
:param perk_id: The ID of a Perk
:type milestone_id: str
:param milestone_id: The ID of a Milestone
"""
return self.session.query(Perk).filter(Perk.id == perk_id).count() != 0
return self.session.query(Milestone).filter(Milestone.id == milestone_id).count() != 0

def perk_exists_for_badge_series(self, badge_id, series_id):
def milestone_exists_for_badge_series(self, badge_id, series_id):
"""
Check if the perk with the given series and badge id exists
Check if the milestone with the given series and badge id exists
:type badge_id: str
:param badge_id: The ID of the badge
:type series_id: str
:param series_id: The ID of the series
"""
return self.get_perk_from_badge_series(badge_id,
return self.get_milestone_from_badge_series(badge_id,
series_id).count() != 0

def get_perk_from_badge_series(self, badge_id, series_id):
def get_milestone_from_badge_series(self, badge_id, series_id):
"""
Return the perk with the given series and badge id
Return the milestone with the given series and badge id
:type badge_id: str
:param badge_id: The ID of the badge
:type series_id: str
:param series_id: The ID of the series
"""
return self.session.query(Perk).filter(
and_(Perk.series_id == func.lower(series_id),
Perk.badge_id == func.lower(badge_id)))
return self.session.query(Milestone).filter(
and_(Milestone.series_id == func.lower(series_id),
Milestone.badge_id == func.lower(badge_id)))

def get_perk(self, perk_id):
def get_milestone(self, milestone_id):
"""
Return the matching perk from the database
Return the matching milestone from the database
:type perk_id: str
:param perk_id: The ID of a Perk
:type milestone_id: str
:param milestone_id: The ID of a Milestone
"""
return self.session.query(Perk).filter(Perk.id == perk_id)
return self.session.query(Milestone).filter(Milestone.id == milestone_id)

def get_all_perks(self, series_id):
def get_all_milestones(self, series_id):
"""
Returns all the perks for the series
Returns all the milestones for the series
:type series_id: str
:param series_id: The id of the Series
"""
return self.session.query(Perk).filter(
Perk.series_id == series_id).all()
return self.session.query(Milestone).filter(
Milestone.series_id == series_id).all()

@autocommit
def create_perk(self, position, badge_id, series_id):
def create_milestone(self, position, badge_id, series_id):
"""
Adds a new perk to the database
Adds a new milestone to the database
:type name: int
:param name: position of the perk in the series
:param name: position of the milestone in the series
:type badge_id: str
:param badge_id: Badge ID for the Perk
:param badge_id: Badge ID for the Milestone
:type series_id: str
:param series_id: ID of the Series
"""
perk = self.get_perk_from_badge_series(badge_id, series_id).first()
if not perk:
perk = Perk(position=position,
milestone = self.get_milestone_from_badge_series(badge_id, series_id).first()
if not milestone:
milestone = Milestone(position=position,
badge_id=badge_id,
series_id=series_id)

self.session.add(perk)
self.session.add(milestone)
self.session.flush()
perk_id = perk.id
milestone_id = milestone.id

return perk_id
return milestone_id

def get_perk_from_series_ids(self, series_ids):
def get_milestone_from_series_ids(self, series_ids):
"""
Return list of perks for the list of series ids
Return list of milestones for the list of series ids
:type series: list
:param series: list of series ids
"""
perks = self.session.query(Perk).filter(
Perk.series_id.in_(series_ids)).all()
milestones = self.session.query(Milestone).filter(
Milestone.series_id.in_(series_ids)).all()

seen = set()
unique_perks = []
unique_milestones = []

for perk in perks:
perk_meta = (perk.series_id, perk.id)
if perk_meta not in seen:
seen.add(perk_meta)
unique_perks.append(perk)
for milestone in milestones:
milestone_meta = (milestone.series_id, milestone.id)
if milestone_meta not in seen:
seen.add(milestone_meta)
unique_milestones.append(milestone)

return unique_perks
return unique_milestones

def get_badges_from_team(self, team_id):
"""
Expand All @@ -284,8 +284,8 @@ def get_badges_from_team(self, team_id):
series = self.get_series_from_team(team_id)
series_ids = [elem.id for elem in series]

perks = self.get_perk_from_series_ids(series_ids)
badge_ids = list(set([perk.badge_id for perk in perks]))
milestones = self.get_milestone_from_series_ids(series_ids)
badge_ids = list(set([milestone.badge_id for milestone in milestones]))

badges = self.get_badges(badge_ids)
return badges
Expand Down
8 changes: 4 additions & 4 deletions tahrir_api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Badge(DeclarativeBase):
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")
milestone = relationship("Milestone", backref="badge")
authorizations = relationship("Authorization", backref="badge")
assertions = relationship("Assertion", backref="badge")
invitations = relationship("Invitation", backref="badge")
Expand Down Expand Up @@ -134,7 +134,7 @@ class Series(DeclarativeBase):
last_updated = Column(DateTime, nullable=False,
default=datetime.datetime.utcnow)
tags = Column(Unicode(128))
perk = relationship("Perk", backref="series")
milestone = relationship("Milestone", backref="series")
team_id = Column(Unicode(128), ForeignKey('team.id'), nullable=False)

def __json__(self):
Expand All @@ -147,8 +147,8 @@ def __json__(self):
)


class Perk(DeclarativeBase):
__tablename__ = 'perk'
class Milestone(DeclarativeBase):
__tablename__ = 'milestone'
__table_args__ = (
UniqueConstraint('position', 'badge_id', 'series_id'),
)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_dbai.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_add_series(self):

assert self.api.series_exists("testseries") is True

def test_add_perk(self):
def test_add_milestone(self):
team_id = self.api.create_team("TestTeam")

series_id = self.api.create_series("TestSeries",
Expand All @@ -91,16 +91,16 @@ def test_add_perk(self):
1337
)

perk_id_1 = self.api.create_perk(1,
milestone_id_1 = self.api.create_milestone(1,
badge_id_1,
series_id)

perk_id_2 = self.api.create_perk(2,
milestone_id_2 = self.api.create_milestone(2,
badge_id_2,
series_id)

assert self.api.perk_exists(perk_id_1) is True
assert self.api.perk_exists(perk_id_2) is True
assert self.api.milestone_exists(milestone_id_1) is True
assert self.api.milestone_exists(milestone_id_2) is True

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

0 comments on commit b9ddf3d

Please sign in to comment.