Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Company(db.Model):
- """ Company representation in the system """
- __tablename__ = 'companies'
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String(), nullable=False, index=True)
- description = db.Column(db.Text())
- start_date = db.Column(db.Date(), index=True)
- city = db.Column(db.String())
- created_at = db.Column(db.DateTime(), default=datetime.datetime.now)
- updated_at = db.Column(db.DateTime(), onupdate=datetime.datetime.now)
- employees = db.relationship('CompanyUser', backref=db.backref('companies', lazy=True))
- class User(db.Model):
- """ User representation in the system """
- __tablename__ = 'users'
- id = db.Column(db.Integer, primary_key=True)
- email = db.Column(db.String(), unique=True, nullable=False)
- first_name = db.Column(db.String())
- last_name = db.Column(db.String())
- password_digest = db.Column(db.String(), nullable=False)
- companies = db.relationship('CompanyUser', backref=db.backref('users', lazy=True))
- def __init__(self, **kwargs):
- password = kwargs.pop('password', None)
- super(User, self).__init__(**kwargs)
- self.password_digest = self.set_password(password)
- def set_password(self, password):
- """ Hash and store password """
- return bcrypt.hashpw(
- password.encode(), bcrypt.gensalt()
- ).decode('utf8')
- def authenticate(self, password):
- """ Authenticate user; Check if user's password match the given one """
- return bcrypt.checkpw(password.encode(), self.password_digest.encode())
- class CompanyUser(db.Model):
- """ Association object class for company and user"""
- __tablename__ = 'company_users'
- company_id = db.Column(
- db.Integer, db.ForeignKey('companies.id'), primary_key=True, nullable=False
- )
- user_id = db.Column(
- db.Integer, db.ForeignKey('users.id'), primary_key=True, nullable=False
- )
- role = db.Column(db.Integer, index=True)
- created_at = db.Column(db.DateTime(), default=datetime.datetime.now)
- updated_at = db.Column(db.DateTime(), onupdate=datetime.datetime.now)
- company = Company(**params)
- company_user = CompanyUser(
- role=self.DEFAULT_ROLE
- )
- company.employees.append(company_user)
- self.current_user.companies.append(company_user)
- db.session.add(company)
- db.session.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement