class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(150)) author = db.Column(db.String(75)) body = db.Column(db.String(800)) image_path = db.Column(db.String(100)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) def __repr__(self): return '<Post {}>'.format(self.body) def save_changes(self, form, file, userId, new=False): self.title = form.title.data self.author = form.author.data self.body = form.body.data self.user_id = userId if file: filename = secure_filename(file.filename) fileextension = filename.rsplit('.', 1)[1] Randomfilename = id_generator() filename = Randomfilename + '.' + fileextension try: blob_service.create_blob_from_stream(blob_container, filename, file) if self.image_path: blob_service.delete_blob(blob_container, self.image_path) except Exception: flash(Exception) self.image_path = filename if new: db.session.add(self) db.session.commit()
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) password_hash = db.Column(db.String(128)) def __repr__(self): return '<User {}>'.format(self.username) 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)
class UserUserfile(Base): """ Relation between a user and a file """ # create with: # UserUserfile(Userfile("folder","file"), user, permission=7) # check with: # uufile = session.query(UserUserfile).filter(UserUserfile.user_id==1,UserUserfile.userfile_id==2).first().permission __tablename__ = 'user_userfile' user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) userfile_id = db.Column(db.Integer, db.ForeignKey('userfile.id'), primary_key=True) permission = db.Column(db.String(50)) # bidirectional attribute/collection of "user"/"user_userfiles" user = db.relationship(User, backref=db.backref("user_userfiles", cascade="all, delete-orphan")) # reference to the "Keyword" object userfile = db.relationship("Userfile") def __init__(self, userfile=None, user=None, permission=None): """ Create a new releation between a user and a file :param Userfile userfile: The file for the relationship :param User user: the user for the relationship :param string permission: the permissions for the user """ self.user = user self.userfile = userfile self.permission = permission
class Userfile(Base): """ An uploaded file """ # create with: # Userfile("hanzfolder","hanzfile") # get with: # file = session.query(Userfile).filter(Userfile.id==1).first() __tablename__ = 'userfile' id = db.Column(db.Integer, primary_key=True) folder = db.Column('folder', db.String(64)) name = db.Column('name', db.String(120)) def __init__(self, folder, name): """ Create a new userfile :param string folder: The folder in which the file is located :param string name: The name of the file """ self.folder = folder self.name = name def get_id(self): """ Determines the id of the object :return string: The id of the file """ try: return unicode(self.id) # python 2 except NameError: return str(self.id) # python 3 def __repr__(self): """ Representation string of a file :return string: String representation of a file """ return '<Userfile %r>' % (self.name)
class Income_Expense(db.Model): __tablename__ = 'income_expense' id = db.Column(db.Integer, primary_key=True) category = db.Column(db.String(150)) date = db.Column(db.Date) amount = db.Column(db.Integer) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) def __repr__(self): return '<Amount {}>'.format(self.amount) def save_changes(self, form, userId, new=False): self.category = form.category.data self.amount = form.amount.data self.date = form.date.data self.user_id = userId if new: db.session.add(self) db.session.commit()
class User(Base): """ Model for a user. Contains their login data. """ # create with: # User("hanz","hanzmail","hanzpw","sso") # get with: # usr = session.query(User).filter(User.id==1).first() __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password = db.Column(db.String(120)) sso = db.Column(db.String(64)) # none, google, facebook # association proxy of "user_userfiles" collection # to "keyword" attribute userfiles = association_proxy('user_userfiles', 'userfile') def __init__(self, username, email, password, sso): """ Create a new user. :param string username: The username of the user to create :param string email: The email of the user to create :param string password: The password of the user to create :param string sso: The SSO login of the user to create """ self.username = username self.email = email self.password = password self.sso = sso def is_authenticated(self): """ Determines if the user is authenticated. Basically a dummy (for decorators) :return boolean: always returns True """ return True def is_active(self): """ Determines if the user is active. Dummy :return boolean: always returns True """ return True def is_anonymous(self): """ Determines if the user is anonymous. Dummy :return boolean: always returns True """ return False def get_id(self): """ Determines the id of the user :return string: The id of the user as a string """ try: return unicode(self.id) # python 2 except NameError: return str(self.id) # python 3 def __repr__(self): """ Representation string of a user object. :return string: String representation of a user """ return '<User %r>' % (self.username)