예제 #1
0
class Participant(db.Model):
    __tablename__ = 'participants'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    email = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(80), nullable=False)
    picture = db.Column(db.String(80))
    location = db.Column(db.String(80), nullable=False)
    about = db.Column(db.String(180))
    enrollments = db.relationship("Enrollment",
                                  back_populates="participant",
                                  lazy='joined')

    events = db.relationship('Event',
                             secondary='enrollments',
                             back_populates='participants',
                             lazy='joined')

    def set_password(self, password):
        self.password = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password, str(password))

    def __str__(self):
        return f'{self.name}'
예제 #2
0
class TargetMember(db.Model):
    __tablename__ = 'target_member'

    target_id = db.Column(
        UUID(as_uuid=True),
        db.ForeignKey('target.id'),
        primary_key=True,
    )
    member_id = db.Column(
        UUID(as_uuid=True),
        db.ForeignKey('member.id'),
        primary_key=True,
    )

    member = db.relationship(
        'Member',
        foreign_keys=[member_id],
    )
    target = db.relationship(
        'Target',
        foreign_keys=[target_id],
    )

    def init(self, member_id, target_id):
        self.member_id = member_id
        self.target_id = target_id

    def to_dict(self):
        return dict(
            member_id=self.member_id,
            target_id=self.target_id,
        )
예제 #3
0
class Event(db.Model):
    __tablename__ = 'events'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), nullable=False)
    description = db.Column(db.String(80), nullable=False)
    date = db.Column(db.Date, nullable=False)
    time = db.Column(db.Time, nullable=False)
    type = db.Column(ChoiceType(EventType), nullable=False)
    category = db.Column(ChoiceType(CategoryType), nullable=False)
    location_id = db.Column(db.Integer, db.ForeignKey("locations.id"))
    location = db.relationship("Location",
                               back_populates="events",
                               lazy='joined')
    address = db.Column(db.String(80), nullable=False)
    seats = db.Column(db.Integer)

    enrollments = db.relationship("Enrollment",
                                  back_populates="event",
                                  lazy='joined')

    participants = db.relationship('Participant',
                                   secondary='enrollments',
                                   back_populates='events',
                                   lazy='joined')

    def __str__(self):
        return f'{self.title}'
예제 #4
0
class Category(db.Model):
    """
    actually our wiki, containing concepts and tags, linked to by markdown
    """
    category = db.Column(db.String(64), primary_key=True)        # Local Authority, Planning Authority, etc
    text = db.Column(db.Text)

    publications = db.relationship('Publication', backref='category', lazy=True)
    organisations  = db.relationship('Organisation', backref='category', lazy=True)
예제 #5
0
class User(UserMixin, db.Model):
    """User Model """

    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(100), unique=True, nullable=False)
    firstname = db.Column(db.String(100), nullable=False)
    lastname = db.Column(db.String(100), nullable=False)
    password = db.Column(db.String(250), unique=True, nullable=False)
    posts = db.relationship('Post', backref=db.backref('posts', lazy=True))
    followed = db.relationship('Follow',
                               foreign_keys=[Follow.follower_id],
                               backref=db.backref('follower', lazy='joined'),
                               lazy='dynamic',
                               cascade='all, delete-orphan')
    followers = db.relationship('Follow',
                                foreign_keys=[Follow.followed_id],
                                backref=db.backref('followed', lazy='joined'),
                                lazy='dynamic',
                                cascade='all, delete-orphan')
    created_on = db.Column(db.DateTime, default=datetime.utcnow)

    def set_password(self, password):
        """Create User Password """
        self.password = generate_password_hash(password, method='sha256')

    def check_password(self, password):
        """Check Hashed Password"""
        return check_password_hash(self.password, password)

    def is_following(self, user):
        if user.id is None:
            return False
        return self.followed.filter_by(followed_id=user.id).first() is not None

    def is_followed_by(self, user):
        if user.id is None:
            return False
        return self.followers.filter_by(
            follower_id=user.id).first() is not None

    def follow(self, user):
        if not self.is_following(user):
            f = Follow(follower=self, followed=user)
            db.session.add(f)

    def unfollow(self, user):
        f = self.followed.filter_by(followed_id=user.id).first()
        if f:
            f = self.followed.filter_by(followed_id=user.id).first()
            db.session.delete(f)
            return True

    def __repr__(self):
        return '<User {}>'.format(self.username)
예제 #6
0
class Category(db.Model):
    __tablename__ = 'category'

    id = db.Column(db.Integer, primary_key=True)
    category_media = db.Column(db.String(80), unique=True, nullable=False)
    videos = db.relationship('Video',
                             backref='category',
                             cascade="all, delete-orphan",
                             lazy=True)
    sounds = db.relationship('Sound',
                             backref='category',
                             cascade="all, delete-orphan",
                             lazy=True)
예제 #7
0
class Target(db.Model, SoftDeleteMixin):
    __tablename__ = 'target'

    id = db.Column(
        UUID(as_uuid=True),
        default=uuid4,
        primary_key=True,
    )
    last_message_id = db.Column(
        UUID(as_uuid=True),
        nullable=True,
    )
    type = db.Column(
        db.String(25),
        nullable=False,
    )

    messages = db.relationship('Message', )

    __mapper_args__ = {
        'polymorphic_identity': __tablename__,
        'polymorphic_on': type,
    }

    def to_dict(self):
        return dict(
            id=self.id,
            last_message_id=self.last_message_id,
            members=[m.member_id for m in self.target_members],
            is_deleted=True if self.removed_at is not None else False,
            removed_at=self.removed_at,
            created_at=self.created_at,
        )
예제 #8
0
파일: user.py 프로젝트: zxjlm/Christin
class User(db.Model, fsqla.FsUserMixin):
    # __tablename__ = 'tb_sy_user'
    name = db.Column(db.String(100))
    sex = db.Column(db.String(1))
    age = db.Column(db.Integer)

    logs = db.relationship("AnalysesLog", backref=db.backref("user"))

    @staticmethod
    def data_table_fields() -> list:
        """
        数据表的字段列表
        Returns:

        """
        return ["id", "username", "s_name", "last_login_date"]

    def to_dict(self):
        return {
            "id": self.id,
            "s_name": self.name,
            "username": self.username,
            "last_login_date": self.last_login_at,
            "access": self.roles[0].name
        }
예제 #9
0
class Licence(db.Model):
    licence = db.Column(db.String(256), primary_key=True)
    name = db.Column(db.String(256))
    url = db.Column(db.Text)
    text = db.Column(db.Text)

    publications = db.relationship('Publication', backref='licence', lazy=True)
예제 #10
0
class CompulsoryPurchaseOrderInvestigation(db.Model, OrderedMixin):

    compulsory_purchase_order_investigation = db.Column(db.Integer(),
                                                        primary_key=True,
                                                        nullable=False)
    status = db.Column(db.String())
    inspector_report_url = db.Column(db.String())
    decision_url = db.Column(db.String())
    start_date = db.Column(db.Date())
    end_date = db.Column(db.Date())

    compulsory_purchase_order_id = db.Column(
        db.String,
        db.ForeignKey('compulsory_purchase_order.compulsory_purchase_order'))
    compulsory_purchase_order = db.relationship(
        'CompulsoryPurchaseOrder', back_populates='investigations')

    def is_final_state(self):
        return self.status in final_states

    def __hash__(self):
        return hash(self.compulsory_purchase_order_investigation)

    def __eq__(self, other):
        return self.compulsory_purchase_order_investigation == other.compulsory_purchase_order_investigation
예제 #11
0
class User(UserMixin, db.Model, TimestampMixin):
    """
    User model.
    `account_type`: `basic` and `premium`, also `admin`, but not sure about that yet. It"s `basic` by default
    `status`: by default inactive, becomes active after email confirmation
    """
    __tablename__ = "users"

    id = db.Column(UUID(as_uuid=True), primary_key=True, nullable=False, default=uuid4)
    email = db.Column(db.String(255), nullable=False)
    first_name = db.Column(db.String(255), nullable=False)
    last_name = db.Column(db.String(255), nullable=False)
    password = db.Column(PasswordType(schemes=["bcrypt"]), nullable=False)
    is_active = db.Column(db.Boolean(), nullable=False, server_default="1")
    confirmed = db.Column(db.Boolean(), nullable=False, server_default="0")
    email_confirmed_at = db.Column(db.DateTime())

    roles = db.relationship("Role", secondary="user_roles")

    __table_args__ = (
        UniqueConstraint("email", name="uq_users_email"),
    )

    def __init__(self, *args, **kwargs):
        super(User, self).__init__(*args, **kwargs)

    @staticmethod
    def auth(email, password):
        user = User.query.filter_by(email=email).first()
        if user and user.password == password:
            return user
        return None

    def __repr__(self):
        return f"User(id={self.id}, email={self.email}, account_type={self.account_type}, status={self.status})"
예제 #12
0
class Permission(db.Model):
    __tablename__ = 'permissions'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    roles = db.relationship(
        'Role',
        secondary=roles_permissions,
        backref=db.backref('permissions', lazy='dynamic'))
예제 #13
0
class Organisation(db.Model):
    organisation = db.Column(db.String(64), primary_key=True)    # government-organisation:D6
    name = db.Column(db.String(256))
    website = db.Column(db.String(256))
    text = db.Column(db.Text)
    feature_id = db.Column(db.String(256), ForeignKey('feature.feature', name='organisation_feature_fkey'))
    feature = db.relationship('Feature', uselist=False)

    category_id = db.Column(db.String(64), ForeignKey('category.category', name='organisation_category_fkey'))
    publications = db.relationship('Publication', backref='organisation', lazy=True)

    other_features = db.relationship('Feature',
                                     lazy='dynamic',
                                     secondary=organisation_feature,
                                     primaryjoin='Organisation.organisation == organisation_feature.columns.organisation',
                                     secondaryjoin='Feature.feature == organisation_feature.columns.feature',
                                     backref=db.backref('organisation', lazy=True))
예제 #14
0
class StudentClass(db.Model, CRUDMixin, TimeMixin):
    student_id = db.Column(db.String,
                           db.ForeignKey('undergraduate_student.id'),
                           primary_key=True)
    class_id = db.Column(db.UUID(as_uuid=True),
                         db.ForeignKey('class.id'),
                         primary_key=True)
    # grade_1 = db.Column(db.Integer, default=None)
    # grade_2 = db.Column(db.Integer, default=None)
    grade = db.Column(db.Float, default=None)
    point = db.Column(db.Float, default=None)
    _class = db.relationship("Class", back_populates="students")
    student = db.relationship("UndergraduateStudent", back_populates="classes")

    @property
    def is_grade_register(self):
        return self.grade_1 is not None and self.grade_2 is not None
예제 #15
0
class Category(db.Model):
    __tablename__ = 'categories'
    id = db.Column(db.Integer, primary_key=True)
    code = db.Column(db.String(15), unique=True, nullable=False)
    title = db.Column(db.String(80), unique=True, nullable=False)
    meals = db.relationship("Meal", back_populates="category", lazy='joined')

    def __str__(self):
        return f'{self.title}'
예제 #16
0
class Meal(db.Model):
    __tablename__ = 'meals'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), unique=True, nullable=False)
    price = db.Column(db.Integer, nullable=False)
    description = db.Column(db.String(200), nullable=False)
    picture = db.Column(db.String(80), nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey("categories.id"))
    category = db.relationship("Category",
                               back_populates="meals",
                               lazy='joined')
    orders = db.relationship('Order',
                             secondary='meals_orders',
                             back_populates='meals',
                             lazy='joined')

    def __str__(self):
        return f'{self.title}'
예제 #17
0
class Location(db.Model):
    __tablename__ = 'locations'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), unique=True, nullable=False)
    code = db.Column(db.String(8), unique=True, nullable=False)
    events = db.relationship("Event", back_populates="location", lazy='joined')

    def __str__(self):
        return f'{self.title}'
예제 #18
0
class Order(db.Model):
    __tablename__ = 'orders'
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.DateTime, nullable=False, server_default=db.func.now())
    total = db.Column(db.Integer, nullable=False)
    status = db.Column(ChoiceType(OrderStatusType),
                       nullable=False,
                       default='new')
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
    user = db.relationship("User", lazy='joined')
    address = db.Column(db.String(80), nullable=False)
    phone = db.Column(db.String(80), nullable=False)
    comment = db.Column(db.String(80), nullable=True)
    meals = db.relationship('Meal',
                            secondary='meals_orders',
                            back_populates='orders',
                            lazy='joined')

    def __str__(self):
        return f'{self.id}'
예제 #19
0
class UndergraduateStudent(User):
    id = db.Column(db.String, db.ForeignKey('user.id'), primary_key=True)
    # classes = db.relationship('Class', secondary=student_classes, lazy='subquery',
    #    backref=db.backref('dtudents', lazy=True))
    classes = db.relationship('StudentClass', back_populates="student")
    __mapper_args__ = {
        'polymorphic_identity': 'undergraduate_student',
    }

    def __unicode__(self):
        return self.name
예제 #20
0
class Enrollment(db.Model):
    __tablename__ = 'enrollments'
    __table_args__ = (db.UniqueConstraint(
        'event_id', 'participant_id', name='event-participant-constraint'), )

    id = db.Column(db.Integer, primary_key=True)

    event_id = db.Column(db.Integer, db.ForeignKey("events.id"))
    event = db.relationship("Event",
                            back_populates="enrollments",
                            lazy='joined')

    participant_id = db.Column(db.Integer, db.ForeignKey("participants.id"))
    participant = db.relationship("Participant",
                                  back_populates="enrollments",
                                  lazy='joined')

    datetime = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    def __str__(self):
        return f'{self.id}'
예제 #21
0
class Message(db.Model, SoftDeleteMixin):
    __tablename__ = 'message'

    id = db.Column(
        UUID(as_uuid=True),
        default=uuid4, primary_key=True
    )
    body = db.Column(
        db.String,
        nullable=False,
    )
    sender_id = db.Column(
        UUID(as_uuid=True),
        db.ForeignKey('member.id')
    )
    reciever_id = db.Column(
        UUID(as_uuid=True),
        db.ForeignKey('member.id')
    )
    target_id = db.Column(
        UUID(as_uuid=True),
        db.ForeignKey('target.id'),
    )

    sender = db.relationship(
        "Member",
        foreign_keys=[sender_id],
        uselist=False
    )
    reciever = db.relationship(
        "Member",
        foreign_keys=[reciever_id],
        uselist=False
    )

    def __init__(self, body, sender_id, reciever_id, target_id):
        self.body = body
        self.sender_id = sender_id
        self.reciever_id = reciever_id
        self.target_id = target_id
예제 #22
0
class PlanningApplication(db.Model):

    reference = db.Column(db.String(64), primary_key=True)
    url = db.Column(db.String)

    local_authority_id = db.Column(db.String(64),
                                   db.ForeignKey('local_authority.id'),
                                   nullable=False,
                                   primary_key=True)
    local_authority = db.relationship('LocalAuthority',
                                      back_populates='planning_applications')

    section106_contributions = db.relationship('Contribution', lazy=True)
    section106_signed_date = db.Column(db.Date)
    section106_url = db.Column(db.String)

    viability_assessments = db.relationship(
        'ViabilityAssessment',
        lazy=True,
        back_populates='planning_application')

    address = db.Column(db.String)
예제 #23
0
class Post(db.Model, CRUDMixin, TimeMixin):
    id = db.Column(db.UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
    author_id = db.Column(db.String, db.ForeignKey('user.id'))
    type = db.Column(db.String)  # lost or found
    tags = db.Column(db.ARRAY(db.String, dimensions=1))
    title = db.Column(db.String)
    category = db.Column(db.String)
    content = db.Column(db.String)
    img_URLs = db.Column(postgresql.ARRAY(db.String, dimensions=1))
    site = db.Column(db.String)
    hit = db.Column(db.Integer, default=0)
    author = db.relationship('User', backref=db.backref(
        'posts', lazy=True))
예제 #24
0
class Applicant(db.Model):
    __tablename__ = 'applicants'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    phone = db.Column(db.String(20), nullable=False)
    email = db.Column(db.String(80), nullable=False)
    status = db.Column(ChoiceType(ApplicantStatus), nullable=False)
    group_id = db.Column(db.Integer, db.ForeignKey("groups.id"))
    group = db.relationship("Group",
                            back_populates="applicants",
                            lazy='joined')

    def __str__(self):
        return f'{self.name}'
예제 #25
0
class PlanningAuthority(db.Model):

    id = db.Column(db.String(64), primary_key=True)
    ons_code = db.Column(db.String(9))
    name = db.Column(db.String(256))
    website = db.Column(db.String())
    local_plans = db.relationship(
        'LocalPlan',
        secondary=planning_authority_plan,
        lazy='dynamic',
        back_populates='planning_authorities',
    )

    def sorted_plans(self, reverse=False, deleted=False):
        plans = self.local_plans.filter(LocalPlan.deleted == deleted).all()
        return sorted(plans, reverse=reverse)

    def get_earliest_plan_start_year(self):
        try:
            first = next(p for p in self.sorted_plans()
                         if p.plan_start_year is not None)
            if first is not None:
                return first.plan_start_year.year
            else:
                return None
        except StopIteration:
            return None

    def get_latest_plan_end_year(self):
        try:
            first = next(p for p in self.sorted_plans(reverse=True)
                         if p.plan_end_year is not None)
            if first is not None:
                return first.plan_end_year.year
            else:
                return None
        except StopIteration:
            return None

    def code(self):
        return self.id.split(':')[-1]

    def to_dict(self):
        data = {
            'id': self.id,
            'name': self.name,
            'ons_code': self.ons_code,
            'plans': [plan.to_dict(self.id) for plan in self.local_plans]
        }
        return data
예제 #26
0
class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    hashed_password = db.Column(db.String(512), unique=True, nullable=False)
    videos = db.relationship('Video',
                             backref='user',
                             cascade="all, delete-orphan",
                             lazy=True)
    sounds = db.relationship('Sound',
                             backref='user',
                             cascade="all, delete-orphan",
                             lazy=True)
    books = db.relationship('Book',
                            backref='user',
                            cascade="all, delete-orphan",
                            lazy=True)

    def __repr__(self):
        return self.username

    def hash_password(self, password):
        return generate_password_hash(password)
예제 #27
0
class Room(db.Model, CRUDMixin):
    __tablename__ = 'room'
    id = db.Column(db.UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
    group = db.Column(db.String())
    campus = db.Column(db.String())
    building = db.Column(db.String)
    floor = db.Column(db.Integer)
    no = db.Column(db.Integer)
    capacity = db.Column(db.Integer)
    name = db.Column(db.String(80), nullable=False)
    detail = db.Column(db.String(80), nullable=False)
    available = db.Column(db.Boolean, default=True)
    orders = db.relationship('Order',
                             lazy='select',
                             backref=db.backref('room', lazy=True))
예제 #28
0
class ShopMember(db.Model):
    __tablename__ = 'shop_members'
    id = db.Column(db.Integer, primary_key=True)
    mobile_number = db.Column(db.String(100))
    is_manager = db.Column(db.Boolean, default=False)

    shop_id = db.Column(db.Integer, db.ForeignKey('shops.id'))
    shop = db.relationship("Shop", back_populates="members")

    @classmethod
    def create(cls, mobile_number, is_manager, shop_id):
        shop_member = ShopMember(mobile_number=mobile_number,
                                 is_manager=is_manager,
                                 shop_id=shop_id)
        db.session.add(shop_member)
        db.session.commit()
        return shop_member
예제 #29
0
class LocalAuthority(db.Model):

    id = db.Column(db.String(64), primary_key=True)
    name = db.Column(db.String(256))

    planning_applications = db.relationship('PlanningApplication',
                                            back_populates='local_authority',
                                            lazy=True)

    def has_viability_assessments(self):
        if self.planning_applications:
            for p in self.planning_applications:
                if p.viability_assessments:
                    return True
            else:
                return False
        else:
            return False
예제 #30
0
class Teacher(User):
    __tablename__ = 'teacher'
    id = db.Column(db.String, db.ForeignKey('user.id'), primary_key=True)
    degree = db.Column(db.String())
    sex = db.Column(db.String())
    title = db.Column(db.String())
    education = db.Column(db.String())
    dept = db.Column(db.String())
    cs = db.Column(db.String())
    intro = db.Column(db.String())
    classes = db.relationship(Class, backref='teacher', lazy=True)

    __mapper_args__ = {
        'polymorphic_identity': 'teacher',
    }

    def __unicode__(self):
        return self.name