Created
October 16, 2014 16:57
-
-
Save lukecampbell/80a1140c4af598f541de to your computer and use it in GitHub Desktop.
OOI Stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from flask import Flask | |
from datetime import datetime | |
import json | |
app = Flask("ooi_services") | |
@app.route("/") | |
def index(): | |
return "Hello" | |
class BaseAdaptor: | |
pass | |
class FileAdaptor: | |
pass | |
class BaseModel: | |
__base_table__ = { | |
"id" : str, | |
"name" : str, | |
"ts_created" : datetime, | |
"ts_updated" : datetime, | |
"adaptor" : BaseAdaptor | |
} | |
doc = {} | |
def __init__(self, doc): | |
for colname, coltype in self.__base_table__.iteritems(): | |
if colname in doc: | |
self.doc[colname] = doc[colname] | |
def __repr__(self): | |
return "%s:%s" % (self.__class__.__name__, repr(self.doc)) | |
class PlatformModel(BaseModel): | |
__table__ = { | |
"lat" : int, | |
"lon" : int, | |
"owner" : str | |
} | |
# just for demo purposes | |
db = [{"id" : "01", "name" : "Bodhi", "lat" : 40, "lon" : -70}, {"id" : "02", "name" : "Utah", "lat" : 19, "lon" : 70}] | |
def __init__(self, doc): | |
BaseModel.__init__(self, doc) | |
for key, val in doc.iteritems(): | |
if key in self.__table__: | |
doc[key] = val | |
@classmethod | |
def where(cls, **kwargs): | |
''' | |
Finds a doc where the contents of the document equal what's specified in the kwargs | |
''' | |
for entry in cls.db: | |
for key in kwargs: | |
if key not in entry: | |
break | |
if entry[key] != kwargs[key]: | |
break | |
else: | |
continue | |
return cls(entry) | |
@classmethod | |
def list(cls): | |
return [cls(entry) for entry in cls.db] | |
class PlatformController: | |
def index(self): | |
return json.dumps([i.doc for i in PlatformModel.list()]) | |
def show(self, platform_id): | |
platform = PlatformModel.where(id=platform_id) | |
if platform: | |
return platform.doc | |
else: | |
return "{}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment