Esempio n. 1
0
class User(db.Model, UserMixin):
    # lower case table name
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    role = db.Column(db.Enum(RoleEnum), nullable=False)
    registrations = db.relationship('Registration', backref='user', lazy=True)

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.role}')"
Esempio n. 2
0
class Registration(db.Model):

    __tablename__ = 'registrations'
    # id = db.Column(db.Integer, unique=True)
    registration_time = db.Column(db.DateTime,
                                  nullable=False,
                                  default=datetime.utcnow)
    # todo should user_id and event_id be primary key?
    user_id = db.Column(db.Integer,
                        db.ForeignKey('users.id'),
                        nullable=False,
                        primary_key=True)
    event_id = db.Column(db.Integer,
                         db.ForeignKey('events.id'),
                         nullable=False,
                         primary_key=True)
    # user can write review only when registered the event.
    # added an extra column here to record review.
    # review can be null if no reviews added.
    # An extra review db table is not needed, but review collections still exist in API representation.

    # review includes: content, rating and creation time.
    review_time: Column = db.Column(db.DateTime, default=None)
    review_content = db.Column(db.String(100), default=None)
    review_rating = db.Column(db.Enum(Rating), default=None)

    def __repr__(self):
        return f"Registration('{self.event_id}', '{self.user_id}', '{self.registration_time}', " \
            f" review: '{self.review_time}', '{self.review_content}', '{self.review_rating}')"
Esempio n. 3
0
class Event(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    date = db.Column(db.Date, nullable=False)
    time = db.Column(db.Time, nullable=False)
    description = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Event('{self.title}', '{self.date} {self.time}')"
Esempio n. 4
0
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(120))
    last_name = db.Column(db.String(120))
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.Text(), nullable=False)
    roles = db.relationship('Role',
                            secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

    def hash_password(self):
        self.password = generate_password_hash(self.password)

    def is_admin(self):
        return 'admin' in self.roles

    # def __repr__(self):
    #     return '<User {first_name} {last_name}>'\
    #     .format(self.first_name, self.last_name)
Esempio n. 5
0
class User(db.Model, UserMixin):
    id = db.Column(db.INTEGER, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(90), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='user.png')
    password = db.Column(db.String(60), nullable=False)
    events = db.relationship('Event', backref='user', lazy=True)

    def get_reset_token(self, expire_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'], expire_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}')"
Esempio n. 6
0
class Event(db.Model):
    __tablename__ = 'events'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    time_posted = db.Column(db.DateTime,
                            nullable=False,
                            default=datetime.utcnow())
    time_start = db.Column(db.DateTime, nullable=True)
    time_end = db.Column(db.DateTime, nullable=True)
    event_address = db.Column(db.String(200), nullable=False)
    description = db.Column(db.String, nullable=False)
    sponsor_id = db.Column(db.Integer,
                           db.ForeignKey('users.id'),
                           nullable=False)
    category = db.Column(db.Enum(EventEnum), nullable=False)

    status = db.Column(db.Enum(StatusEnum), nullable=True)
    registrations = db.relationship('Registration',
                                    backref='event',
                                    lazy=True,
                                    cascade="all, delete-orphan")

    def __repr__(self):
        return f"Event('{self.id}', '{self.title}', '{self.description}', '{self.category}', \
Esempio n. 7
0
class Role(db.Model, BaseModel):
    description = db.Column(db.String(80), nullable=False)
Esempio n. 8
0
class BaseModel():
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120), unique=True, nullable=False)

    def __repr__(self):
        return '<Center %r>' % self.name
Esempio n. 9
0
class Center(db.Model, BaseModel):
    user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
    user = db.relationship('User',
                           backref=db.backref('centers', lazy='dynamic'))
    location = db.Column(db.String(80), nullable=False)