Skip to content

Commit

Permalink
Add get_person_email and make person_exists take an email OR id.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtgay committed Jun 19, 2013
1 parent 42fe140 commit f056f26
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions tahrir_api/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def add_badge(self, name, image, desc, criteria, issuer_id):
return badge_id
return False

def person_exists(self, person_email):
def person_exists(self, email=None, id=None):
"""
Check if a Person with this email is stored in the database
Expand All @@ -106,7 +106,12 @@ def person_exists(self, person_email):
"""

session = scoped_session(self.session_maker)
return session.query(Person).filter_by(email=person_email).count() != 0
if email:
return session.query(Person).filter_by(email=email).count() != 0
elif id:
return session.query(Person).filter_by(id=id).count() != 0
else:
return False

def get_all_persons(self):
"""
Expand All @@ -116,6 +121,30 @@ def get_all_persons(self):
session = scoped_session(self.session_maker)
return session.query(Person)

def get_person_email(self, person_id):
"""
Convience function to retrieve a person email from an id.
I am so sorry that I had to write this.
It is easier than rewriting all of the get_person and
person_exists methods to take ids for now.
Eventaully, I will get around to fully refactoring
this API.
This was written after get_person etc., and at some point we really
should make all these methods uniform (either get_x and
get_x_by_email or get_x and get_x_by_id).
:type person_id: str
:param person_id: The email of a Person in the database.
"""

session = scoped_session(self.session_maker)
if self.person_exists(id=person_id):
return session.query(Person).filter_by(
id=person_id).one().email
return None

def get_person(self, person_email):
"""
Convience function to retrieve a person object from an email
Expand All @@ -125,7 +154,7 @@ def get_person(self, person_email):
"""

session = scoped_session(self.session_maker)
if self.person_exists(person_email):
if self.person_exists(email=person_email):
return session.query(Person).filter_by(email=person_email).one()
return None

Expand All @@ -138,7 +167,7 @@ def delete_person(self, person_email):
"""

session = scoped_session(self.session_maker)
if self.person_exists(person_email):
if self.person_exists(email=person_email):
session.delete(self.get_person(person_email))
session.commit()
return person_email
Expand All @@ -156,7 +185,7 @@ def add_person(self, email):
"""

session = scoped_session(self.session_maker)
if not self.person_exists(email):
if not self.person_exists(email=email):
new_person = Person(email=email)
session.add(new_person)
session.commit()
Expand Down Expand Up @@ -305,7 +334,7 @@ def get_assertions_by_email(self, person_email):
"""

session = scoped_session(self.session_maker)
if self.person_exists(person_email):
if self.person_exists(email=person_email):
person_id = session.query(Person).filter_by(
email=person_email).one().id
return session.query(Assertion).filter_by(
Expand Down Expand Up @@ -349,7 +378,7 @@ def add_assertion(self, badge_id, person_email, issued_on):
session = scoped_session(self.session_maker)
if issued_on is None:
issued_on = datetime.now()
if self.person_exists(person_email) and self.badge_exists(badge_id):
if self.person_exists(email=person_email) and self.badge_exists(badge_id):
new_assertion = Assertion(badge_id=badge_id,
person_id=self.get_person(
person_email).id,
Expand Down

0 comments on commit f056f26

Please sign in to comment.