Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
refactor versioned mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
avilaton committed Oct 28, 2015
1 parent fe88bd9 commit bf0e09c
Show file tree
Hide file tree
Showing 23 changed files with 71 additions and 109 deletions.
10 changes: 5 additions & 5 deletions app/commands/dumpload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from flask.ext.script import Command, Option

from app.models import *
from app.models.entity import Entity
from app.models.base import Base
from app import db

logger = logging.getLogger(__name__)
Expand All @@ -17,7 +17,7 @@ class DumpData(Command):

def get_options(self):
return [
Option('-d', '--destination', dest='destination', default='./'),
Option('-d', '--destination', dest='destination', default='app/fixtures/'),
Option('-m', '--modelname', dest='modelname')
]

Expand All @@ -30,10 +30,11 @@ def run(self, destination, modelname=None):
else:
for item in globals().values():
if inspect.isclass(item):
if issubclass(item, Entity) and item is not Entity:
if issubclass(item, GTFSBase) and item is not GTFSBase:
self.export_model(item)

def export_model(self, Model):
logger.info("exporting Model: {0}".format(Model.__tablename__))
with open(self.destination + Model.__tablename__ + '.jsonl','wb') as output:
for row in db.session.query(Model).all():
output.write(json.dumps(row.to_json) + '\n')
Expand All @@ -59,7 +60,7 @@ def run(self, origin='app/fixtures/', modelname=None):

for table in db.metadata.sorted_tables:
Model = get_class_by_tablename(table.name)
if Model and issubclass(Model, Entity) and Model is not Entity:
if Model and issubclass(Model, GTFSBase) and Model is not GTFSBase:
Models.append(Model)

for Model in Models:
Expand Down Expand Up @@ -99,7 +100,6 @@ def import_model(self, Model):

db.session.commit()

from app.models.base import Base

def get_class_by_tablename(tablename):
"""Return class reference mapped to table.
Expand Down
2 changes: 1 addition & 1 deletion app/fixtures/stops.jsonl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
{"stop_lat": -31.4611728508, "stop_numero": "3900", "stop_code": "C4146", "stop_calle": "P\u00fablica", "stop_entre": null, "stop_lon": -64.1571498546, "stop_esquina": null, "stop_id": 4146, "stop_desc": null, "stop_name": "P\u00fablica 3900"}
{"stop_lat": -31.4587652906, "stop_numero": "3700", "stop_code": "C4147", "stop_calle": "P\u00fablica", "stop_entre": null, "stop_lon": -64.1572166992, "stop_esquina": "Celso Barrios", "stop_id": 4147, "stop_desc": null, "stop_name": "P\u00fablica 3700"}
{"stop_lat": -31.4585299305, "stop_numero": "2900", "stop_code": "C4148", "stop_calle": "Celso Barrios", "stop_entre": null, "stop_lon": -64.1584032611, "stop_esquina": "Luis Saavedra", "stop_id": 4148, "stop_desc": null, "stop_name": "Celso Barrios 2900"}
{"stop_lat": -31.4511892057, "stop_numero": "2198", "stop_code": "C4234", "stop_calle": "Copina", "stop_entre": null, "stop_lon": -64.1667787618, "stop_esquina": null, "stop_id": 4234, "stop_desc": null, "stop_name": "Copina 2198"}
{"stop_lat": -31.4585246687, "stop_numero": "2800", "stop_code": "C4149", "stop_calle": "Celso Barrios", "stop_entre": null, "stop_lon": -64.1602765517, "stop_esquina": "Soconcho", "stop_id": 4149, "stop_desc": null, "stop_name": "Celso Barrios 2800"}
{"stop_lat": -31.4575899989, "stop_numero": "3719", "stop_code": "C4150", "stop_calle": "Guasapampa", "stop_entre": null, "stop_lon": -64.1615470094, "stop_esquina": null, "stop_id": 4150, "stop_desc": null, "stop_name": "Guasapampa 3719"}
{"stop_lat": -31.4561040148, "stop_numero": "3671", "stop_code": "C4151", "stop_calle": "Guasapampa", "stop_entre": null, "stop_lon": -64.1616491846, "stop_esquina": null, "stop_id": 4151, "stop_desc": null, "stop_name": "Guasapampa 3671"}
Expand Down Expand Up @@ -220,7 +221,6 @@
{"stop_lat": -31.4485096601, "stop_numero": "3150", "stop_code": "C4231", "stop_calle": "Hernando de Lerma", "stop_entre": null, "stop_lon": -64.1737302296, "stop_esquina": null, "stop_id": 4231, "stop_desc": null, "stop_name": "Hernando de Lerma 3150"}
{"stop_lat": -31.450766661, "stop_numero": "3268", "stop_code": "C4232", "stop_calle": "Espinoza Amespil", "stop_entre": null, "stop_lon": -64.1719131779, "stop_esquina": null, "stop_id": 4232, "stop_desc": null, "stop_name": "Espinoza Amespil 3268"}
{"stop_lat": -31.4512000258, "stop_numero": "1848", "stop_code": "C4233", "stop_calle": "Copina", "stop_entre": null, "stop_lon": -64.1692209656, "stop_esquina": null, "stop_id": 4233, "stop_desc": null, "stop_name": "Copina 1848"}
{"stop_lat": -31.4511892057, "stop_numero": "2198", "stop_code": "C4234", "stop_calle": "Copina", "stop_entre": null, "stop_lon": -64.1667787618, "stop_esquina": null, "stop_id": 4234, "stop_desc": null, "stop_name": "Copina 2198"}
{"stop_lat": -31.4512116054, "stop_numero": "2286", "stop_code": "C4235", "stop_calle": "Copina", "stop_entre": null, "stop_lon": -64.1653497522, "stop_esquina": null, "stop_id": 4235, "stop_desc": null, "stop_name": "Copina 2286"}
{"stop_lat": -31.4512170553, "stop_numero": "2390", "stop_code": "C4236", "stop_calle": "Copina", "stop_entre": null, "stop_lon": -64.1640178627, "stop_esquina": null, "stop_id": 4236, "stop_desc": null, "stop_name": "Copina 2390"}
{"stop_lat": -31.4522762608, "stop_numero": "3378", "stop_code": "C4237", "stop_calle": "Carlos Paz", "stop_entre": null, "stop_lon": -64.1631223386, "stop_esquina": null, "stop_id": 4237, "stop_desc": null, "stop_name": "Carlos Paz 3378"}
Expand Down
1 change: 1 addition & 0 deletions app/models/gtfs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

from .gtfsbase import GTFSBase
from .agency import Agency
from .calendar import Calendar
from .calendardate import CalendarDate
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class Agency(Base, ToJSONMixin):

class Agency(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'agency'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

agency_id = Column(types.Integer, primary_key=True)
agency_name = Column(types.String(50))
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class Calendar(Base, ToJSONMixin):

class Calendar(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'calendar'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

service_id = Column(types.Integer, primary_key=True)
service_name = Column(types.String(50))
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/calendardate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types, ForeignKey

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class CalendarDate(Base, ToJSONMixin):

class CalendarDate(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'calendar_dates'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

service_id = Column(types.Integer,
ForeignKey("calendar.service_id",
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/fareattribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class FareAttribute(Base, ToJSONMixin):

class FareAttribute(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'fare_attributes'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

fare_id = Column(types.Integer, primary_key=True)
price = Column(types.String(50))
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/farerule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class FareRule(Base, ToJSONMixin):

class FareRule(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'fare_rules'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

fare_id = Column(types.Integer, primary_key=True)
route_id = Column(types.Integer)
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/feedinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class FeedInfo(Base, ToJSONMixin):

class FeedInfo(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'feed_info'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

feed_publisher_name = Column(types.String(50), primary_key=True)
feed_publisher_url = Column(types.String(50))
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types, ForeignKey

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class Frequency(Base, ToJSONMixin):

class Frequency(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'frequencies'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

trip_id = Column(types.Integer, ForeignKey("trips.trip_id", onupdate="CASCADE"),
primary_key=True)
Expand Down
3 changes: 3 additions & 0 deletions app/models/gtfs/gtfsbase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

class GTFSBase(object):
pass
9 changes: 3 additions & 6 deletions app/models/gtfs/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types, ForeignKey

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class Route(Base, ToJSONMixin):

class Route(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'routes'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

route_id = Column(types.Integer, primary_key=True)
agency_id = Column(types.Integer, ForeignKey("agency.agency_id",
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/routefrequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types, ForeignKey

from ..mixins import ToJSONMixin
from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin, Versioned


class RouteFrequency(Base, ToJSONMixin):

class RouteFrequency(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'route_frequencies'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

route_id = Column(types.Integer,
ForeignKey("routes.route_id", onupdate="CASCADE"),
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class Shape(Base, ToJSONMixin):

class Shape(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'shapes'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

shape_id = Column(types.Integer, primary_key=True)
shape_pt_lat = Column(types.Float(precision=53))
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/shapepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
import json
from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class ShapePath(Base, ToJSONMixin):

class ShapePath(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'shape_paths'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

shape_id = Column(types.Integer, primary_key=True)
shape_path = Column(types.UnicodeText) # Stores json Array of Lon, Lat pairs
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class Stop(Base, ToJSONMixin):

class Stop(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'stops'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

stop_id = Column(types.Integer, primary_key=True)
stop_code = Column(types.String(50))
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/stopseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types, ForeignKey

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class StopSeq(Base, ToJSONMixin):

class StopSeq(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'stop_seq'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

trip_id = Column(types.Integer,
ForeignKey("trips.trip_id", onupdate="CASCADE"),
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/stoptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types, ForeignKey

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class StopTime(Base, ToJSONMixin):

class StopTime(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'stop_times'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

trip_id = Column(types.Integer,
ForeignKey("trips.trip_id", onupdate="CASCADE"),
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class Transfer(Base, ToJSONMixin):

class Transfer(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'transfers'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

from_stop_id = Column(types.Integer, primary_key=True)
to_stop_id = Column(types.Integer)
Expand Down
9 changes: 3 additions & 6 deletions app/models/gtfs/trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

from sqlalchemy import Column, types, ForeignKey

from .gtfsbase import GTFSBase
from ..base import Base
from ..mixins import ToJSONMixin
from ..mixins import ToJSONMixin, Versioned


class Trip(Base, ToJSONMixin):

class Trip(Base, ToJSONMixin, Versioned, GTFSBase):
__tablename__ = 'trips'
__versioned__ = {
'base_classes': (Base, ToJSONMixin, )
}

trip_id = Column(types.Integer, primary_key=True)
route_id = Column(types.Integer, ForeignKey("routes.route_id"))
Expand Down
Loading

0 comments on commit bf0e09c

Please sign in to comment.