Skip to content

Commit

Permalink
Improved DB naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruskov Martin Consulente authored and Ruskov Martin Consulente committed Jan 15, 2019
1 parent 292fca9 commit 7bad91a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
16 changes: 8 additions & 8 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
engine = create_engine(db_url, echo=False)


class Locations(Base):
__tablename__ = 'Locations'
class Location(Base):
__tablename__ = 'location'

id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
Expand All @@ -47,12 +47,12 @@ def reset_db(blank=False, backup=True):

print("Populate with dummy data: %s" % ('True' if not blank else 'False'))
if not blank:
mecca = Locations(name="MECCA", latitude=21.389082, longitude=39.857912)
berlin = Locations(name="BERLIN", latitude=52.520007, longitude=13.404954)
london = Locations(name="LONDON", latitude=51.507351, longitude=-0.127758)
milano = Locations(name="MILANO", latitude=45.465422, longitude=9.185924)
sofia = Locations(name="SOFIA", latitude=42.697708, longitude=23.321868)
brasilia = Locations(name="BRASILIA", latitude=-14.235004, longitude=-51.92528)
mecca = Location(name="MECCA", latitude=21.389082, longitude=39.857912)
berlin = Location(name="BERLIN", latitude=52.520007, longitude=13.404954)
london = Location(name="LONDON", latitude=51.507351, longitude=-0.127758)
milano = Location(name="MILANO", latitude=45.465422, longitude=9.185924)
sofia = Location(name="SOFIA", latitude=42.697708, longitude=23.321868)
brasilia = Location(name="BRASILIA", latitude=-14.235004, longitude=-51.92528)

session.add_all([mecca, berlin, london, milano, sofia, brasilia])
session.commit()
Expand Down
10 changes: 5 additions & 5 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from settings import debug

from db import Base, engine, Session
from db import Locations
from db import Location

def loc2csv(loc):
return "%s,%s"%(getattr(loc, "latitude"),getattr(loc, "longitude"))
Expand All @@ -29,11 +29,11 @@ def query_location(name = None, latitude = None, longitude = None):
session = Session()
if name is not None and len(name) > 0:
name = unquote(name)
location = session.query(Locations).filter(Locations.name.like(name)).first()
location = session.query(Location).filter(Location.name.like(name)).first()
if latitude is not None and len(latitude) > 0 and longitude is not None and len(longitude) > 0:
if location is None:
# print('create')
location = Locations(name=name.upper(), latitude=latitude, longitude=longitude)
location = Location(name=name.upper(), latitude=latitude, longitude=longitude)
session.add(location)
session.commit()
else:
Expand All @@ -46,11 +46,11 @@ def query_location(name = None, latitude = None, longitude = None):
if location is None:
# print('import')
rloc = locator.geocode(name)
location = Locations(name=name.upper(), latitude=rloc.latitude, longitude=rloc.longitude)
location = Location(name=name.upper(), latitude=rloc.latitude, longitude=rloc.longitude)
session.add(location)
session.commit()
else:
location = session.query(Locations).order_by(Locations.name.asc()).all()
location = session.query(Location).order_by(Location.name.asc()).all()

return to_csv(location)

Expand Down

0 comments on commit 7bad91a

Please sign in to comment.