class Word(db.Model): id = db.Column(db.Integer, primary_key=True) string = db.Column(db.String, nullable=False) # categories relationship categories = db.relationship('Category', secondary=categories, lazy='subquery', backref=db.backref('words', lazy=True)) # concepts relationship concepts = db.relationship('Concept', secondary=concepts, lazy='subquery', backref=db.backref('related_words', lazy=True)) # gets the first letter def get_first_letter(self): return self.string[:2] # gets the second letter def get_second_letter(self): return self.string[2:3] # gets the non-string word def get_non_string_word(self): string = self.string.strip('"') string = string.strip("'") return string # repr def __repr__(self): return f"<w-{self.string.upper()}>"
class Company(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) location = db.Column(db.String, nullable=False) email = db.Column(db.String, nullable=False) logo = db.Column(db.String, nullable=False) created_at = db.Column(db.Float, nullable=False) modified_at = db.Column(db.Float, nullable=False) # stripe stripe_id = db.Column(db.String) # projects relationship projects = db.relationship('Project', backref='company', lazy=True) # serializes @property def serialize(self): json_company = { "id": self.id, "name": self.name, "location": self.location, "email": self.email, "logo": self.logo, "stripe_id": self.stripe_id, "created_at": self.created_at, "modified_at": self.modified_at, } return json_company # repr def __repr__(self): return "<Company - {}>".format(self.name)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80)) password = db.Column(db.String(160)) category = db.Column(db.String(80)) # posts relationship posts = db.relationship('Post', back_populates='user') # represents the table def __repr__(self): return '<User {}>'.format(self.username)
class ConceptRelationship(db.Model): id = db.Column(db.Integer, primary_key=True) # concept noun relationship concept_noun_id = db.Column(db.Integer, db.ForeignKey('concept.id')) concept_noun = db.relationship("Concept", backref="noun_relationships", foreign_keys=[concept_noun_id]) # concept verb relationship concept_verb_id = db.Column(db.Integer, db.ForeignKey('concept.id')) concept_verb = db.relationship("Concept", backref="verb_relationships", foreign_keys=[concept_verb_id]) # concept adjective relationship concept_adjective_id = db.Column(db.Integer, db.ForeignKey('concept.id')) concept_adjective = db.relationship("Concept", backref="adjective_relationships", foreign_keys=[concept_adjective_id]) # repr def __repr__(self): return f"<cor-{self.concept_noun} & {self.concept_verb} & {self.concept_adjective}>"
class Project(db.Model): id = db.Column(db.Integer, primary_key=True) published = db.Column(db.Boolean, nullable=False) demo = db.Column(db.Boolean, nullable=False) title = db.Column(db.String, nullable=False) description = db.Column(db.String, nullable=False) restrictions = db.Column(db.String) link = db.Column(db.String, nullable=False) created_at = db.Column(db.Float, nullable=False) modified_at = db.Column(db.Float, nullable=False) # company relationship company_id = db.Column(db.Integer, db.ForeignKey('company.id')) # tags relationship tags = db.relationship('Tag', secondary=tags, lazy='subquery', backref=db.backref('projects', lazy=True)) # serializes @property def serialize(self): json_project = { "id": self.id, "published": self.published, "demo": self.demo, "title": self.title, "description": self.description, "restrictions": self.restrictions, "link": self.link, "created_at": self.created_at, "modified_at": self.modified_at, } return json_project # repr def __repr__(self): return "<Project - {}>".format(self.title)
class User(db.Model): __tablename__ = 'users' id = db.Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True) name = db.Column(db.String(255), nullable=False) nfc_id = db.Column(db.String(16), nullable=False, unique=True) created_at = db.Column(db.DateTime, nullable=False, default=datetime.now) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now) created_by = db.Column(db.String(255), nullable=False) updated_by = db.Column(db.String(255), nullable=False) records = db.relationship("Record", backref="user") def __init__(self, name, nfc_id, created_by): self.name = name self.nfc_id = nfc_id self.created_by = created_by self.updated_by = created_by
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(160)) slug = db.Column(db.String(160)) body = db.Column(db.String(3000)) dateCreated = db.Column(db.String(80)) category = db.Column(db.String(80)) image = db.Column(db.String(160)) # user relationship user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', back_populates='posts') # defines the init function def __init__(self, *args, **kwargs): # creates the slug if not 'slug' in kwargs: kwargs['slug'] = slugify(kwargs.get('title', '')) super().__init__(*args, **kwargs) # represents the table def __repr__(self): return '<Post {}>'.format(self.title)
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column( db.Integer, primary_key=True, autoincrement=True, nullable=False ) username = db.Column( db.String(), index=True, unique=True, nullable=False ) email = db.Column( db.String(), index=True, unique=True, nullable=False ) password_hash = db.Column( db.String(), nullable=False, ) tasks = db.relationship( 'Task', backref='user', lazy='dynamic' ) images = db.relationship( 'Image', backref='user', lazy='dynamic' ) @staticmethod def verify_reset_password_token(token): try: decoded = jwt.decode( token, current_app.config['SECRET_KEY'], algorithms=['HS256'] ) id = decoded['reset_password'] except: # noqa: E722 TODO: figure out excepction thrown and catch it return return User.query.get(id) def __repr__(self): return '<User {}:{}>'.format( self.id, self.username ) def get_reset_password_token(self, expires_in=3000): return jwt.encode( {'reset_password': self.id, 'exp': time() + expires_in}, current_app.config['SECRET_KEY'], algorithm='HS256' ).decode('utf-8') def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def add_image(self, file_name): im = Image(file_name=file_name, user_id=self.id) db.session.add(im) return im def get_images(self): return Image.query.filter_by(user_id=self.id) def launch_task(self, task, params, name, description, priority, *args, **kwargs): rq_job = current_app.task_queues[priority].enqueue_call(func=task, args=params, *args, **kwargs) task = Task(id=rq_job.get_id(), name=name, description=description, user_id=self.id) db.session.add(task) return task def get_tasks_in_progress(self, name): return Task.query.filter_by(name=name, user_id=self.id, complete=False).all()