# Which database to use depends on whether we are testing or in production # To prevent circular dependencies we simply set and read environment variables if environ.get("TESTING", False) == "true": environ["SQLALCHEMY_DATABASE_URI"] = TESTING_URI else: environ["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI from models import Device, Reading, User, Role from api import MultiApi, DeviceResource, ReadingResource from admin import HomeView, AuthModelView, UploadView from database import make_db_utils # Database Connection _, db_session, _ = make_db_utils() # App configuration app = Flask(__name__, static_url_path='', static_folder='../static') app.config["SECRET_KEY"] = urandom(24) app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_DATABASE_URI"] = environ["SQLALCHEMY_DATABASE_URI"] app.add_url_rule("/", "root", lambda: app.send_static_file("index.html")) # API configuration api = MultiApi(app) api.add_resource(ReadingResource, "/api/reading/") api.add_resource(DeviceResource, "/api/dev/") # Admin and Security configuration admin = Admin(index_view=HomeView("Helmuth"))
""" Models contains the python representations for our database tables and contains various utility methods for serializing the data for transfer """ import os from flask.ext.security import UserMixin, RoleMixin from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Boolean, BigInteger from sqlalchemy.orm import relationship, backref from database import make_db_utils from json import dumps _, db_session, Base = make_db_utils() class Device(Base): """ Represents the metadata about a meter and its location""" __tablename__ = "devices" id = Column(String(50), unique=True, index=True) site = Column(String(50)) field_lat = Column(Float) field_lon = Column(Float) location = Column(String(50)) state_province = Column(String(50)) country = Column(String(50)) biomimic = Column(String(50)) zone = Column(String(50), nullable=True) sub_zone = Column(String(50), nullable=True) wave_exp = Column(String(50), nullable=True) tide_height = Column(Float, nullable=True) dev_id = Column(BigInteger, primary_key=True, autoincrement=True)