class Strain(db.Model): """ A model with a many-to-many relation set to the field terpenes. A strain can have many terpenes, and many terpenes can belong to a strain. """ id = db.Column(db.Integer, primary_key=True) image = db.Column(db.String(255), unique=False, nullable=True) genetics = db.Column(db.Text(), unique=False, nullable=True) lineage = db.Column(JSON, nullable=True) name = db.Column(db.String(96), unique=True, nullable=False) terpenes = db.relationship('Terpene', secondary=terpenes, lazy='subquery', backref=db.backref('strains', lazy=True)) @property def serialize(self): return { 'id': self.id, 'name': self.name, 'terpenes': self.serialize_many2many, } @property def serialize_many2many(self): return [i.serialize for i in self.terpenes] def get_strain_data(self): """ This method is a GET request to cannabisreports API passing strain name as a query param. The server returns JSON which is parsed and set to the strain instance attributes. """ strain_query_url = cannabis_reports_url + "%s" % (self.name) r = requests.get(strain_query_url, headers) if r.status_code == 200: data = r.json() # json strain object print(data) if len(data['data']) > 0: image_url = data['data'][0]['image'] # url property of object genetics = data['data'][0]['genetics']['names'] lineage = data['data'][0]['lineage'] self.image = image_url self.genetics = genetics self.lineage = lineage db.session.add(self) else: pass return db.session.commit() def __repr__(self): return f"<Strain {self.name}>"
class Role(db.Model, RoleMixin): ''' A way to associate permissions with a group of users. For instance: Editor, Admin, or Superuser. ''' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) def __repr__(self): return f"<Role {self.name}>"
class User(db.Model, UserMixin): ''' The user model which will undergo authentication. The UserMixin adds several methods from the Flask-Login package, which is included with Flask- Security. ''' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True) username = db.Column(db.String(255)) password = db.Column(db.String(255)) last_login_at = db.Column(db.DateTime()) current_login_at = db.Column(db.DateTime()) 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()) fs_uniquifier = db.Column(db.String(255)) confirmed_at = db.Column(db.DateTime()) roles = db.relationship('Role', secondary='roles_users', backref=db.backref('users', lazy='dynamic')) def __repr__(self): return f"<User {self.email}>"
class Compound(db.Model): """ A many-to-one relationship to Terpene. To account for variations in analytical testing, there can be many terpene instances for one compound. Test data can be stored on the terpene model. """ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(96), nullable=False) terpenes = db.relationship('Terpene', backref='compound', lazy=True) def __repr__(self): return f"<Compound {self.name}>"
class Terpene(db.Model): """ A model with a an fk relationship to Compound. This allows for future additions to the Terpene model such as %-composition (ie. testing COAs). """ id = db.Column(db.Integer, primary_key=True) aroma = db.Column(db.String(255), nullable=False) compound_id = db.Column(db.Integer, db.ForeignKey('compound.id'), nullable=False) @property def serialize(self): return {'id': self.id, 'compound_id': self.compound.name} def __repr__(self): return f"{self.compound.name}"