Esempio n. 1
0
class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column(UUID(as_uuid=True),
                   primary_key=True,
                   default=lambda: uuid.uuid4().hex)
    is_default = db.Column(db.Boolean, default=False, server_default='f')
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role', lazy='dynamic')
Esempio n. 2
0
class Account(db.Model):
    __tablename__ = 'account'
    id = db.Column(UUID(as_uuid=True),
                   primary_key=True,
                   default=lambda: uuid.uuid4().hex)
    user = db.relationship('User', back_populates='account')
    account_history = db.relationship("AccountHistory",
                                      backref='account',
                                      lazy='dynamic')

    # Payment information
    payment_vendor_code = db.Column(
        db.String(64), nullable=True)  # For example, 'stripe' or 'paypal'
    payment_data = db.Column(
        JSONB(), nullable=True)  # Vendor-specific information in Json format
Esempio n. 3
0
class AccountHistory(db.Model):
    __tablename__ = 'account_history'
    id = db.Column(UUID(as_uuid=True),
                   primary_key=True,
                   default=lambda: uuid.uuid4().hex)
    account_id = db.Column(UUID(as_uuid=True), db.ForeignKey('account.id'))
    date = db.Column(db.DateTime(), nullable=True)
    event = db.Column(db.String(32),
                      nullable=True)  # Some text representation of event
    comment = db.Column(db.String(128))  # Text comment

    def toDict(self):
        def read_field(field_value, field_name):
            if (field_name == 'event'):
                return get_text_event(EventType(field_value))
            res = str(field_value)
            return res

        return {
            c.name: read_field(getattr(self, c.name), c.name)
            for c in self.__table__.columns
        }  #{ c.key: getattr(self, c.key) for c in inspect(self).mapper.column_attrs }
Esempio n. 4
0
class User(UserMixin, db.Model):
    __tablename__ = 'user'

    id = db.Column(UUID(as_uuid=True),
                   primary_key=True,
                   default=lambda: uuid.uuid4().hex)
    username = db.Column(db.String(120), nullable=False)
    email = db.Column(db.String(64), unique=True)
    password_hash = db.Column(db.String(120), nullable=False)
    role_id = db.Column(UUID(as_uuid=True), db.ForeignKey('role.id'))
    confirmed = db.Column(db.Boolean, default=False, server_default='f')
    account_id = db.Column(UUID(as_uuid=True), db.ForeignKey('account.id'))
    account = db.relationship('Account', back_populates='user')
    created = db.Column(db.DateTime(), nullable=True)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        if self.role is None:
            admin_emails = get_config_var('ADMIN_EMAIL').split(
                ' ') if get_config_var('ADMIN_EMAIL') is not None else []
            if len(admin_emails) > 0 and self.email in admin_emails:
                self.role = Role.query.filter_by(name='Admin').first()
            else:
                default_role = Role.query.filter_by(is_default=True).first()
                self.role = default_role
        if self.account == None:
            self.account = Account()

    def set_password(self, password):
        '''
        Creates a hash from a password
        '''
        hash = self.generate_hash(password)
        self.password_hash = hash

    @login_manager.user_loader
    def load_user(user_id):
        return User.query.get(uuid.UUID(user_id))

    def generate_hash(self, password):
        return sha256.hash(password)

    def verify_hash(self, password):
        return sha256.verify(password, self.password_hash)

    def generate_confirmation_token(self, expiration=3600):
        s = Serializer(get_config_var('SECRET_KEY'), expiration)
        return s.dumps({'confirm': self.id.__str__()}).decode('utf-8')

    def confirm(self, token):
        s = Serializer(get_config_var('SECRET_KEY'))
        try:
            data = s.loads(token.encode('utf-8'))
        except:
            return False
        if data.get('confirm') != self.id.__str__():
            return False
        self.confirmed = True
        return True

    def ping(self):
        self.last_seen = datetime.utcnow()
        db.session.add(self)

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except Exception as ex:
            print('ERROR while saving user:')
            print(ex)  # to-do: log
            return False
        return True
Esempio n. 5
0
class Account(db.Model):
    __tablename__ = 'account'
    id = db.Column(UUID(as_uuid=True), primary_key=True, default=lambda: uuid.uuid4().hex)
    user = db.relationship('User', back_populates='account')
    account_history = db.relationship("AccountHistory", backref='account', lazy='dynamic')