class Storage(database.Model): """ Model representation of the storage table. """ id = database.Column(database.Integer, primary_key=True) name = database.Column(database.String(50), unique=True, nullable=False) location = database.Column(database.String(120), nullable=False) user_id = database.Column(database.Integer, database.ForeignKey('user.id'), nullable=False) @classmethod def get_user_storages(cls, user_id): """ Args: user_id: Returns: """ storages = [] for storage in Storage.query.filter_by(user_id=user_id).all(): storages.append((storage.id, storage.name)) return storages def __repr__(self): return '<Storage %r>' % self.name
class Tag(database.Model): """ Model representation of the tag table. """ id = database.Column(database.Integer, primary_key=True) name = database.Column(database.String(25), nullable=False, unique=True, index=True) @staticmethod def get_or_create(name): """ Method to get a tag by a given name or create it if does not exists. Args: name: Name for the tag Returns: Tag object """ try: return Tag.query.filter_by(name=name).one() except Exception: return Tag(name=name) @staticmethod def all(): """ Get all tags from database. Returns: List with all tags on database. """ return Tag.query.all() def __repr__(self): return self.name
class Category(database.Model): """ Model representation of the category table. """ id = database.Column( database.Integer, primary_key=True ) name = database.Column( database.String(50), unique=True, nullable=False ) user_id = database.Column( database.Integer, database.ForeignKey('user.id'), nullable=False ) def __repr__(self): return '<Category %r>' % self.name @classmethod def get_user_categories(cls, user_id): """ Args: user_id: Returns: """ categories = [] for category in Category.query.filter_by(user_id=user_id).all(): categories.append((category.id, category.name)) return categories
class User(database.Model, UserMixin): """ Model representation of the user table. """ id = database.Column(database.Integer, primary_key=True) username = database.Column(database.String(80), unique=True, nullable=False) email = database.Column(database.String(120), unique=True, nullable=False) things = database.relationship('Thing', backref='user', lazy='dynamic') password_hash = database.Column(database.String) @property def password(self): """ Property which is write-only so it will raise an exception if trying to access to it. Raises: AttributeError. """ raise AttributeError('password: write-only field') @password.setter def password(self, password): """ Setter for the password hash. Args: password: Password to be hashed. """ self.password_hash = generate_password_hash(password) def check_password(self, password): """ Validate hash of given password. Args: password: Password to be checked. Returns: True if it is correct else False. """ return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): """ Get the user from the username given as parameter. Args: username: string with the username. Returns: User object. """ return User.query.filter_by(username=username).first() def __repr__(self): return "<User %r>" % self.username def __unicode__(self): return self.username
class Thing(database.Model): """ Model representation of the thing table. """ id = database.Column(database.Integer, primary_key=True) name = database.Column(database.Text, nullable=False) description = database.Column(database.String(300)) unit = database.Column(database.String(100)) quantity = database.Column(database.Integer) user_id = database.Column(database.Integer, database.ForeignKey('user.id'), nullable=False) category_id = database.Column(database.Integer, database.ForeignKey('category.id'), nullable=False) storage_id = database.Column(database.Integer, database.ForeignKey('storage.id'), nullable=False) date = database.Column(database.DateTime, default=datetime.utcnow) _tags = database.relationship('Tag', secondary=tags, backref=database.backref('things', lazy='dynamic')) category = database.relationship('Category', backref=database.backref('things', lazy='dynamic')) storage = database.relationship('Storage', backref=database.backref('things', lazy='dynamic')) @staticmethod def newest(num): """ Get a given number of items based on the date created. Args: num: Integer for the number of things to be retrieved. Returns: List with things. """ return Thing.query.order_by(desc(Thing.date)).limit(num) @property def tags(self): """ Tags of the thing to be requested. Returns: All thing tags separated by coma. """ return ",".join([t.name for t in self._tags]) @tags.setter def tags(self, string): """ Setter for thing tags splitting the text given as argument to generate a new tag for each slice on the string. Args: string: string to be split. """ if string: self._tags = [ Tag.get_or_create(name) for name in string.split(',') ] def __repr__(self): return "<Thing %r>" % self.name
from things_organizer.extensions import database tags = database.Table( 'thing_tag', database.Column('tag_id', database.Integer, database.ForeignKey('tag.id')), database.Column('thing_id', database.Integer, database.ForeignKey('thing.id'))) class Tag(database.Model): """ Model representation of the tag table. """ id = database.Column(database.Integer, primary_key=True) name = database.Column(database.String(25), nullable=False, unique=True, index=True) @staticmethod def get_or_create(name): """ Method to get a tag by a given name or create it if does not exists. Args: name: Name for the tag Returns: Tag object