class Farm(db.Model): """Farms Models for Users to create. One User can created as many farms as he wants""" __tablename__ = 'farm' id = db.Column(db.Integer, primary_key=True) farm_name = db.Column(db.String(25), unique=True, nullable=False) farm_location = db.Column(db.String(20)) farm_coordinates = db.Column(db.String(3000)) farm_area = db.Column(db.Float(precision=2)) farm_cultivation_process = db.Column(db.String(20)) # RELATIONSHIP # USER[1]-FARM[M] user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # FARM[1]-FIELD[M] fields = db.relationship('Field', backref='farm', lazy='dynamic') soil_tests = db.relationship('SoilTest', backref='farm', lazy='dynamic') water_tests = db.relationship('WaterTest', backref='farm', lazy='dynamic') _default = db.Column(db.Boolean) _time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) _time_updated = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) def __repr__(self): return '<farm {}>'.format(self.farm_name)
class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) last_name = db.Column(db.String(50)) email = db.Column(db.String(50), unique=True) password = db.Column(db.String(200)) birthday = db.Column(db.DateTime(timezone=True), nullable=True) mobile = db.Column(db.String(50), unique=True) username = db.Column(db.String(50), unique=True) address = db.Column(db.String(50)) zipcode = db.Column(db.Integer) city = db.Column(db.String(50)) state = db.Column(db.String(50)) country = db.Column(db.String(50)) email_rec = db.Column(db.String(50)) image = db.Column(db.String(200)) last_login_at = db.Column(db.DateTime(timezone=True)) current_login_at = db.Column(db.DateTime(timezone=True)) last_login_ip = db.Column(db.String(100)) current_login_ip = db.Column(db.String(100)) login_count = db.Column(db.Integer) active = db.Column(db.Boolean(), nullable=True) confirmed_at = db.Column(db.DateTime(timezone=True), nullable=True) roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic')) default_farm_id = db.Column(db.Integer, unique=True) completed_welcome = db.Column(db.Boolean) # RELATIONSHIP # USER[1]-FARM[M] farms = db.relationship('Farm', backref='user', lazy='dynamic') # USER[1]-AGRIMODULE[M] agrimodules = db.relationship('Agrimodule', backref='user', lazy='dynamic') # USER[1]-PUMP[M] pumps = db.relationship('Pump', backref='user', lazy='dynamic') # USER[1]-WELCOME[1] welcome = db.relationship('WelcomeLog', uselist=False, backref='user') _time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) _time_updated = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) def __repr__(self): return '<user {}>'.format(self.email)
class Agrisensor(db.Model): """each agrimodule has a different table where all data that is measured by agrimodule is saved in this model""" __tablename__ = 'agrisensors' id = db.Column(db.Integer, primary_key=True) identifier = db.Column(db.String(50), unique=True) lat = db.Column(db.Float(precision=8)) lon = db.Column(db.Float(precision=8)) batt_status = db.Column(db.Integer) # RELATIONSHIP # agrimodule[1]-AGRIMODULE[M] agrimodule_id = db.Column(db.Integer, db.ForeignKey('agrimodules.id')) # AGRIMODULE[1]-MEASUREMENT[M] measurements = db.relationship('Measurement', backref='agrisensor', lazy='dynamic') # SETTINGS _time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) _time_updated = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) def __repr__(self): return '<agrisensor {}>'.format(self.identifier)
class Agrimodule(db.Model): """Each agrimodule smart system is unique and has am agrimodule an agripump and maybe agresiensor and other agripumps depending on the complaexity of the farm and can be added to any user any farm with a unique identuifier which can connect the data being sent to server to an specific User.Model/Field.Model Each agrimodule""" __tablename__ = 'agrimodules' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) identifier = db.Column(db.String(100), unique=True, nullable=False) lat = db.Column(db.Float(precision=8)) lon = db.Column(db.Float(precision=8)) batt_status = db.Column(db.Integer) public_id = db.Column(db.String(200)) mac = db.Column(db.String(400)) # RELATIONSHIP # USER[1]-agrimodule[M] user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # FIELD[1]-agrimodule[1] field_id = db.Column(db.Integer, db.ForeignKey('field.id')) # AGRIMODULE[1]-MEASUREMENT[M] measurements = db.relationship('Measurement', backref='agrimodule', lazy='dynamic') # AGRIMODULE[1]-AGRISENSOR[M] agrisensors = db.relationship('Agrisensor', backref='agrimodule', lazy='dynamic') # AGRIMODULE[1]-AGRIPUMP[M] agripumps = db.relationship('Agripump', backref='agrimodule', lazy='dynamic') _time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) _time_updated = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) def __repr__(self): return '<agrimodule {}>'.format(self.identifier)
class Field(db.Model): """Fields that can exist inside the Farm.Model. One Farm can have as many Fields within for different crops to be cultivated, being limited by the size of the Farm""" __tablename__ = 'field' id = db.Column(db.Integer, primary_key=True) field_name = db.Column(db.String(25), nullable=False) field_cultivation_area = db.Column(db.Float(precision=2)) field_cultivation_start_date = db.Column(db.DateTime(timezone=True)) field_cultivation_finish_date = db.Column(db.DateTime(timezone=True)) field_current_yield = db.Column(db.Float(precision=2)) field_projected_yield = db.Column(db.Float(precision=2)) field_cultivation_state = db.Column(db.String(20)) field_cultivation_type = db.Column(db.String(5)) field_num_plants = db.Column(db.Integer) field_spacing_topology = db.Column(db.String(20)) field_water_required_day = db.Column(db.Integer) # field_ = db.Column(db.Float) # RELATIONSHIP TO BE ADDED agrimodule = db.relationship('Agrimodule', uselist=False, backref='field') # FIELD[1]-AGRIMODULE[1] # RELATIONSHIP # FIELD[M]-CROP[M] crops = db.relationship('Crop', secondary='crops_field', backref='field', lazy='dynamic') # FARM[1]-FIELD[M] farm_id = db.Column(db.Integer, db.ForeignKey('farm.id')) _time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) _time_updated = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) def __repr__(self): return '<field {}>'.format(self.field_name)
class Pump(db.Model): """pump database used for each field or each agripump whichi is installed in the farm. one farm can ahve as many pump the want as long as the have an agripump for it""" __tablename__ = 'pump' id = db.Column(db.Integer, primary_key=True) pump_name = db.Column(db.String(30)) pump_brand = db.Column(db.String(25)) pump_flow_rate = db.Column(db.Float(precision=2), nullable=False) pump_head = db.Column(db.Float(presicion=2), nullable=False) pump_watts = db.Column(db.Float(precision=2), nullable=False) # RELATIONSHIP # USER[1]-PUMP[M] user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # PUMP[1]-AGRIPUMP agripumps = db.relationship('Agripump', backref='pump', lazy='dynamic') _time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) _time_updated = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) def __repr__(self): return '<pump {}>'.format(self.pump_brand)