Ejemplo n.º 1
0
class FoodKind(BaseMixin, UserDefinedNameMixin, db.Model):
    user_id = db.Column(UUID(as_uuid=True), db.ForeignKey('app_user.id'))
    unit_of_measurement_id = db.Column(UUID(as_uuid=True),
                                       db.ForeignKey('unit_of_measurement.id'),
                                       default=UNIT_OF_MEASURE_IS_SELF_ID)
    serving_size = db.Column(db.Float, default=0.0)

    unit_of_measurement = db.relationship('UnitOfMeasurement', lazy='joined')
    nutrition_info = db.relationship('FoodKindNutritionInfo',
                                     lazy='joined',
                                     uselist=False)
    categories = db.relationship('FoodCategory',
                                 secondary='food_kind_category',
                                 lazy='subquery',
                                 backref=db.backref('food_kinds', lazy=True))
    stock_items = db.relationship('StockItem', lazy=True)

    def full_dict(self):
        remove = ['unit_of_measurement_id']
        return {
            **{
                key: getattr(self, key)
                for key in self.cols_dict().keys() if key not in remove
            }, 'unit_of_measurement': self.unit_of_measurement,
            'categories': self.categories,
            'nutrition_info': self.nutrition_info or {}
        }
Ejemplo n.º 2
0
class Organizations(db.Model):
    __tablename__ = "organizations"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    description = db.Column(db.String(80))

    def __init__(self, name, description):
        self.name = name
        self.description = description

    def json(self):
        return {
            "id": self.id,
            "name": self.name,
            "description": self.description
        }, 200

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def find_org_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

    @classmethod
    def get_all_organizations(cls):
        return cls.query.all()

    @classmethod
    def test(cls):
        return True
Ejemplo n.º 3
0
class AppUser(TimestampMixin, db.Model):
  email = db.Column(db.String, nullable=False, unique=True)
  pw_hash = db.Column(db.String, nullable=False)

  @classmethod 
  def get_by_email(cls, email):
    return cls.query.filter(cls.email == email).first()
Ejemplo n.º 4
0
class StockItem(UuidPrimaryKeyMixin, CreatedUpdatedTimestampMixin, db.Model):
    stock_id = db.Column(UUID(as_uuid=True),
                         db.ForeignKey('stock.id'),
                         nullable=False)
    item_id = db.Column(UUID(as_uuid=True),
                        db.ForeignKey('item.id'),
                        nullable=False)

    snapshots = db.relationship('StockItemSnapshot', lazy=True)
Ejemplo n.º 5
0
class Requirement(db.Model, BaseModelMixin):
    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String)
    value = db.Column(db.String)
    job_id = db.Column(db.Integer, db.ForeignKey('job.id'), nullable=False)

    def __init__(self, key, value):
        self.key = key
        self.value = value
class UserPermission(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)

    def __repr__(self):
        return f"UserPermission {self.id} - {self.name}"

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 7
0
class Language(db.Model, BaseModelMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))

    curriculum_id = db.Column(db.Integer,
                              db.ForeignKey('curriculum.id'),
                              nullable=False)

    def __init__(self, name):
        self.name = name
Ejemplo n.º 8
0
class Search(db.Model, BaseModelMixin):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)

    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def __init__(self, text, user_id):
        self.text = text
        self.user_id = user_id
class UserProfile(TimestampMixin, db.Model):
    user_id = db.Column(UUID(as_uuid=True),
                        db.ForeignKey('registered_user.id'),
                        primary_key=True)
    role_id = db.Column(db.Integer, db.ForeignKey('user_role.id'))
    username = db.Column(db.String, unique=True, nullable=False)
    role = db.relationship('UserRole', lazy='joined')

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()
class UserDefinedNameMixin:
  name = db.Column(db.String, nullable=False)
  uniform_name = db.Column(db.String, nullable=False)

  def __init__(self, **kwargs):
    self.name = kwargs.get('name')
    self.uniform_name = uniform_name(kwargs.get('name'))

  def update_name(self, name):
    self.name = name
    self.uniform_name = uniform_name(name)
Ejemplo n.º 11
0
class RegisteredUser(TimestampMixin, db.Model):
    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
    username = db.Column(db.String, nullable=False, unique=True)
    pw_hash = db.Column(db.String, nullable=False)

    @classmethod
    def find_by_username(cls, username):
        return cls.query.filter_by(username=username).first()

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 12
0
class Item(UuidPrimaryKeyMixin, CreatedUpdatedTimestampMixin, db.Model):
    name = db.Column(db.String, nullable=False)
    description = db.Column(db.String)

    tags = db.relationship('Tag',
                           lazy=False,
                           secondary='item_tag',
                           backref=db.backref('items', lazy=True))

    instances = db.relationship('StockItem',
                                lazy=True,
                                backref=db.backref('item', lazy=False))
Ejemplo n.º 13
0
class UserRole(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    permission = db.relationship('UserPermission',
                                 secondary='user_role_permission',
                                 lazy='joined')

    def __repr__(self):
        return f"UserRole {self.id} - {self.name} - {self.permission}"

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 14
0
class StockItemSnapshot(UuidPrimaryKeyMixin, CreatedUpdatedTimestampMixin,
                        db.Model):
    '''Represents the state of an item at any given time'''
    stock_item_id = db.Column(UUID(as_uuid=True),
                              db.ForeignKey('stock_item.id'),
                              nullable=False)
    maintainer_id = db.Column(UUID(as_uuid=True),
                              db.ForeignKey('maintainer.id'),
                              nullable=False)
    unit_of_measure_id = db.Column(db.Integer,
                                   db.ForeignKey('unit_of_measure.id'),
                                   nullable=False)
    quantity = db.Column(db.Float, nullable=False, default=0.0)
Ejemplo n.º 15
0
class UserLogin(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date_created = db.Column(TIMESTAMP(timezone=True), default=datetime.utcnow)
    user_id = db.Column(UUID(as_uuid=True),
                        db.ForeignKey('registered_user.id'),
                        nullable=False)

    def __repr__(self):
        return f"UserLogin {self.user_id} - {self.date_created}"

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 16
0
class Curriculum_Category(db.Model, BaseModelMixin):
    category_id = db.Column(db.Integer,
                            db.ForeignKey('category.id'),
                            primary_key=True)
    curriculum_id = db.Column(db.Integer,
                              db.ForeignKey('curriculum.id'),
                              primary_key=True)
    category = db.relationship("Category")

    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def __init__(self):
        pass
Ejemplo n.º 17
0
class Subcategory(db.Model, BaseModelMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    jobs = db.relationship('Job', backref='subcategory', lazy=False, cascade='all, delete-orphan')

    category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False)

    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def __init__(self, name):
        self.name = name

    @classmethod
    def getByName(cls, name):
        return cls.query.filter(cls.name == name).first()
Ejemplo n.º 18
0
class RegisteredUser(db.Model):
    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    oauth_openid = db.Column(
        db.String, unique=True)  # if the user registered through OAuth2
    username = db.Column(db.String)
    email = db.Column(db.String, unique=True, nullable=False)
    role_id = db.Column(db.Integer, db.ForeignKey('user_role.id'))
    role = db.relationship('UserRole', lazy='joined')
    logins = db.relationship('UserLogin', lazy='dynamic')
    posts = db.relationship('Post', lazy='dynamic')
    prompts = db.relationship('Prompt', lazy='dynamic')

    def __repr__(self):
        return f"RegisteredUser {self.id} - {self.email} - {self.username} - {self.role}"

    @classmethod
    def get_profile_user(cls, user_id):
        '''Use this method to fetch data to display when any given logged on user views 
    their profile information'''
        user = cls.query.get(user_id)
        if user:
            return dict(
                id=user.id,
                username=user.username,
                email=user.email,
                role=user.role,
                posts=[p for p in user.posts.all()],
                prompts=[p for p in user.prompts.all()],
            )

    @classmethod
    def get_profile_admin(cls, user_id):
        '''Use this method to fetch data about users for admins to review/use in their tasks'''
        user = cls.query.get(user_id)
        if user:
            return dict(
                id=user.id,
                username=user.username,
                email=user.email,
                role=user.role,
                posts=[p.with_reviews() for p in user.posts.all()],
                prompts=[p.with_reviews() for p in user.prompts.all()],
                logins=[l for l in user.logins.all()],
            )

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 19
0
class Notification(db.Model, BaseModelMixin):
    id = db.Column(db.Integer, primary_key=True)
    status = db.Column(db.String, nullable=False, default='unread')
    title = db.Column(db.String)
    description = db.Column(db.String)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    interview_id = db.Column(db.Integer,
                             db.ForeignKey('job.id'),
                             nullable=False)

    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def __init__(self, user_id, title, description, interview_id):
        self.user_id = user_id
        self.title = title
        self.description = description
        self.interview_id = interview_id

    @classmethod
    def get_pag(cls, data, id):
        return cls.query.filter(cls.user_id == id).order_by(
            cls.created_at.desc()).paginate(data['page'], 10, False)

    @classmethod
    def unreadNotificationsCount(cls, id):
        return cls.query.filter(cls.user_id == id,
                                cls.status == 'unread').count()
Ejemplo n.º 20
0
class RevokedToken(db.Model):
    jti = db.Column(db.String, primary_key=True)

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    def is_revoked(cls, jti):
        return bool(cls.query.get(jti))
class StockItemState(TimestampMixin, db.Model):
  stock_item_id = db.Column(UUID(as_uuid=True), db.ForeignKey('stock_item.id'))
  packaging_kind_id = db.Column(UUID(as_uuid=True), db.ForeignKey('packaging_kind.id'))
  packaging_state_id = db.Column(UUID(as_uuid=True), db.ForeignKey('packaging_state.id'))
  quantity = db.Column(db.Integer, default=0)
  
  # has lazy loaded 'backref'ed relationship field 'food_item'

  packaging_kind = db.relationship('PackagingKind', lazy='joined')
  packaging_state = db.relationship('PackagingState', lazy='joined')

  def full_dict(self):
    remove = ['packaging_kind_id', 'packaging_state_id']
    return {
      **{ key: getattr(self, key) for key in self.cols_dict().keys() if key not in remove },
      'packaging_kind': self.packaging_kind,
      'packaging_state': self.packaging_state
    }
Ejemplo n.º 22
0
class PromptReview(db.Model):
  id = db.Column(UUID(as_uuid=True), primary_key=True)
  prompt_id = db.Column(UUID(as_uuid=True), db.ForeignKey('prompt.id'), nullable=False)
  date_created = db.Column(TIMESTAMP(timezone=True), default=datetime.utcnow)
  date_modified = db.Column(TIMESTAMP(timezone=True))
  created_by = db.Column(UUID(as_uuid=True), db.ForeignKey('registered_user.id'), nullable=False)
  is_approved = db.Column(db.Boolean, nullable=False)
  comments = db.Column(db.String(400))
Ejemplo n.º 23
0
class StockItem(TimestampMixin, db.Model):
  stock_id = db.Column(UUID(as_uuid=True), db.ForeignKey('stock.id'))
  food_kind_id = db.Column(UUID(as_uuid=True), db.ForeignKey('food_kind.id'))
  date_item_was_new = db.Column(DATE(), default=datetime.utcnow)
  expiration_date = db.Column(DATE(), nullable=False)

  # 'states' field in StockItem instance is sqlalchemy query object which can
  # be used to specify whether or not to grab all previous 'StockItemState's (a lot)
  # or just the most recent one...or something else
  states = db.relationship('StockItemState', lazy='dynamic', backref=db.backref('food_item', lazy=True))
  food_kind = db.relationship('FoodKind', lazy='joined')

  def full_dict(self):
    current_state = self.states.order_by(text('date_created desc')).first()
    return {
      **self.cols_dict(),
      'food_kind': self.food_kind.full_dict(),
      'current_state': current_state.full_dict() if current_state else None,
    }
Ejemplo n.º 24
0
class RevokedToken(db.Model):
    jti = db.Column(db.String(120), primary_key=True)

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def is_revoked(cls, jti):
        return bool(cls.query.filter_by(jti=jti).first())
Ejemplo n.º 25
0
class BaseMixin:
  id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid4)

  def save(self):
    db.session.add(self)
    db.session.commit()

  def delete(self):
    db.session.delete(self)
    db.session.commit()
Ejemplo n.º 26
0
class Company(db.Model, BaseModelMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    info = db.Column(db.String)
    logo = db.Column(db.String)
    jobs = db.relationship('Job',
                           backref='company',
                           lazy=False,
                           cascade='all, delete-orphan')
    ratings = db.relationship('Rating', backref='company', lazy=False)

    def __init__(self, name):
        self.name = name

    @classmethod
    def get_by_name(cls, name):
        return cls.query.filter(Company.name == name).first()

    @classmethod
    def get_by_id(cls, id):
        return cls.query.filter(Company.id == id).first()

    @classmethod
    def get_pag(cls, data):
        filtro = cls.query

        filtro = filtro.filter(db.or_(~Company.name.contains('Workana')))

        if 'search' in data and data['search'] is not None:
            filtro = filtro.filter(
                db.or_(Company.name.contains(data['search'].strip())))

        if 'page' in data and 'per_page' in data:
            return filtro.paginate(int(data['page']), int(data['per_page']),
                                   False, 40)
        else:
            return filtro.paginate(1, 10, False)

    @classmethod
    def get_id(cls):
        filtro = db.engine.execute(
            "select setval('company_id_seq', (select max(id) from company));")
        return [row for row in filtro]
class RevokedToken(db.Model):
    jti = db.Column(UUID(as_uuid=True), primary_key=True)

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()
Ejemplo n.º 28
0
class Stock(UuidPrimaryKeyMixin, CreatedUpdatedTimestampMixin, db.Model):
    name = db.Column(db.String, nullable=False)

    items = db.relationship('StockItem',
                            lazy=True,
                            backref=db.backref('stock', lazy=False))

    maintainers = db.relationship('Maintainer',
                                  lazy=True,
                                  secondary='stock_maintainer',
                                  backref=db.backref('stocks', lazy=False))
Ejemplo n.º 29
0
class ItemModel(db.Model):
    __tablename__ = "items"

    id = db.Column(db.Integer, primary_key=True)
    item = db.Column(db.String(80))
    price = db.Column(db.Float(precision=2))
    store_id = db.Column(db.Integer, db.ForeignKey("stores.id"))
    store = db.relationship('StoreModel')

    def __init__(self, item, price, store_id):
        self.item = item
        self.price = price
        self.store_id = store_id

    def json(self):
        return {
            "name": self.item,
            "price": self.price,
            "store_id": self.store_id
        }

    @classmethod
    def find_by_name(cls, name):
        return cls.query.filter_by(item=name).first()

    # @classmethod
    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()

    @classmethod
    def get_all(cls):
        return cls.query.all()

    @classmethod
    def delete_all(cls):
        return cls.query.delete()
Ejemplo n.º 30
0
class Role(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(50), unique=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return '<Role {}>'.format(self.name)

    def json(self):
        return {"id": self.id, "name": self.name}, 200

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def find_role_by_name(cls, name):
        return cls.query.filter_by(name=name).first()