Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up model code #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions app/mod_endpoints/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ def as_dict(self):


@classmethod
def find_by_name_or_code(cls,state_name_or_code):
def find_by_name_or_code(cls, state_name_or_code):
try:
if len(state_name_or_code) == 2:
state = State.Query.get(state_code=state_name_or_code.upper())
elif len(state_name_or_code) > 2:
state = State.Query.get(name=state_name_or_code.title())
return state
except QueryResourceDoesNotExist, e:
raise InvalidAPIUsage("State with state name or code '{}' does not exist".format(state_name_or_code), status_code=404)
raise InvalidAPIUsage(
"State with state name or code '{}' does not exist".format(state_name_or_code),
status_code=404
)

@staticmethod
def get_all_states():
return [ state.as_dict() for state in State.Query.all() ]
return [state.as_dict() for state in State.Query.all()]

@staticmethod
def get_one_state(state_name_or_code):
Expand All @@ -43,24 +46,21 @@ def as_dict(self):
}

@classmethod
def find_state_cities(cls,state_name_or_code):
def find_state_cities(cls, state_name_or_code):
_state_ = State.find_by_name_or_code(state_name_or_code)
return [ lga.as_dict() for lga in LGA.Query.filter( state=_state_, city=True ) ]
return [lga.as_dict() for lga in LGA.Query.filter( state=_state_, city=True )]

@staticmethod
def get_all_lgas_with_state_name(state_name):
result_set = []
state_result = State.Query.get(name=state_name)
print state_result.objectId
lgas = LGA.Query.filter(state=state_result)
for lga in lgas:
result_set.append(lga.as_dict())
return result_set
return list(map(lambda lga: lga.as_dict(), LGA.Query.filter(state=state_result)))

@staticmethod
def get_all_lgas(state_name_or_code):
_state_ = State.find_by_name_or_code(state_name_or_code)
return [ lga.as_dict() for lga in LGA.Query.filter(state=_state_) ]
return [lga.as_dict() for lga in LGA.Query.filter(state=_state_)]


@staticmethod
def get_all_cities(state_name_or_code):
Expand Down