コード例 #1
0
class Activity(SurrogatePK, Model):

    __tablename__ = 'activity'
    activity_name = Column(db.String(200))
    is_reaction = Column(db.Boolean, nullable=False, default=False)
    activity_type_id = reference_col("activity_type", nullable=False)
    activity_type = relationship("ActivityType", backref="activities")
    classroom_id = reference_col("classroom", nullable=False)
    classroom = relationship("Classroom", backref="activities")
    code = Column(db.String(100), nullable=False, unique=True)
    reaction_to_id = reference_col("activity", nullable=True)
    reactions = relationship("Activity",
                             backref=db.backref('reaction_to',
                                                remote_side='Activity.id'))

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

    def __init__(self, activity_type_id, classroom, code, **kwargs):
        db.Model.__init__(self,
                          activity_type_id=activity_type_id,
                          classroom=classroom,
                          code=code,
                          **kwargs)

    @property
    def submissions_count(self):
        return len(self.submissions)
コード例 #2
0
class Field(SurrogatePK, Model):

    __tablename__ = 'field'

    # id is needed for primary join, it does work with SurrogatePK class
    id = db.Column(db.Integer, primary_key=True)
    label = Column(db.String(100))
    type = Column(db.String(50))
    space_id = reference_col('space', nullable=True)
    space = relationship('Space', back_populates='fields')
    author_id = reference_col('userprofile', nullable=False)
    author = relationship("UserProfile", backref="fields")

    # https://docs.sqlalchemy.org/en/latest/_modules/examples/adjacency_list/adjacency_list.html
    parent_id = reference_col('field', nullable=True)
    children = relationship(
        "Field",
        # cascade deletions
        cascade="all, delete-orphan",
        # many to one + adjacency list - remote_side
        # is required to reference the 'remote'
        # column in the join condition.
        backref=db.backref("parent", remote_side=id),
        # children will be represented as a dictionary
        # on the "id" attribute.
        # collection_class=attribute_mapped_collection("id"),
    )

    # https://docs.sqlalchemy.org/en/latest/orm/inheritance.html#joined-table-inheritance
    # https://docs.sqlalchemy.org/en/13/_modules/examples/inheritance/joined.html
    __mapper_args__ = {
        "polymorphic_identity": "field",
        "polymorphic_on": type,
    }
コード例 #3
0
ファイル: models.py プロジェクト: teliportme/remixVR-moved
class UserProfile(Model, SurrogatePK):
    __tablename__ = 'userprofile'

    # id is needed for primary join, it does work with SurrogatePK class
    id = db.Column(db.Integer, primary_key=True)

    user_id = reference_col('users', nullable=False, unique=True)
    user = relationship('User', backref=db.backref('profile', uselist=False))
    follows = relationship('UserProfile',
                           secondary=followers_assoc,
                           primaryjoin=id == followers_assoc.c.follower,
                           secondaryjoin=id == followers_assoc.c.followed_by,
                           backref='followed_by',
                           lazy='dynamic')

    def __init__(self, user, **kwargs):
        db.Model.__init__(self, user=user, **kwargs)

    def is_following(self, profile):
        return bool(
            self.follows.filter(
                followers_assoc.c.followed_by == profile.id).count())

    def follow(self, profile):
        if self is not profile and not self.is_following(profile):
            self.follows.append(profile)
            return True
        return False

    def unfollow(self, profile):
        if self is not profile and self.is_following(profile):
            self.follows.remove(profile)
            return True
        return False

    @property
    def following(self):
        if current_user:
            return current_user.profile.is_following(self)
        return False

    @property
    def username(self):
        return self.user.username

    @property
    def bio(self):
        return self.user.bio

    @property
    def image(self):
        return self.user.image

    @property
    def email(self):
        return self.user.email
コード例 #4
0
ファイル: models.py プロジェクト: teliportme/remixvr
class Submission(SurrogatePK, Model):

    __tablename__ = 'submission'
    author = Column(db.String(200), nullable=False)
    approved = Column(db.Boolean(), default=False, nullable=False)
    file_type = Column(db.String(100), nullable=False)
    file_id = reference_col("file")
    file = relationship("File",
                        backref=db.backref("submission", uselist=False))
    activity_id = reference_col("activity", nullable=False)
    activity = relationship("Activity", backref="submissions")
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
コード例 #5
0
class Theme(SurrogatePK, Model):

    __tablename__ = 'theme'
    title = Column(db.String(100), unique=True, nullable=False)
    description = Column(db.Text, nullable=False)
    slug = Column(db.String(100), unique=True, nullable=False)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    updated_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    author_id = reference_col('userprofile', nullable=False)
    author = relationship("UserProfile", backref="themes")
    # can be draft, published, deleted
    status = Column(db.String(15), nullable=False, default="draft")
    config = Column(JSONB)

    def __init__(self, author, title, description, slug=None, **kwargs):
        db.Model.__init__(self,
                          author=author,
                          title=title,
                          description=description,
                          slug=slug or slugify(title),
                          **kwargs)

    def update_status(self, status):
        """Update Status"""
        self.status = status
コード例 #6
0
class Link(Field):

    __tablename__ = 'link'
    id = Column(db.ForeignKey('field.id'), primary_key=True)
    linked_space_id = reference_col('space', nullable=False)
    linked_space = relationship(
        'Space', backref=db.backref('link', uselist=False))

    __mapper_args__ = {"polymorphic_identity": "link"}
コード例 #7
0
class Audio(Field):

    __tablename__ = 'audio'
    id = Column(db.ForeignKey("field.id"), primary_key=True)
    file_id = reference_col('file', nullable=True)
    file = relationship("File", backref=db.backref("audio", uselist=False))
    duration = Column(db.Integer)
    audio_format = Column(db.String(50))

    __mapper_args__ = {"polymorphic_identity": "audio"}
コード例 #8
0
class Image(Field):

    __tablename__ = 'image'
    id = Column(db.ForeignKey("field.id"), primary_key=True)
    file_id = reference_col('file', nullable=True)
    file = relationship("File", backref=db.backref("image", uselist=False))
    width = Column(db.Integer)
    height = Column(db.Integer)

    __mapper_args__ = {"polymorphic_identity": "image"}
コード例 #9
0
ファイル: models.py プロジェクト: teliportme/remixvr
class Classroom(SurrogatePK, Model):

    __tablename__ = 'classroom'
    classname = Column(db.String(200), nullable=False)
    slug = Column(db.String(200), nullable=False, unique=True)
    subject = Column(db.String(100))
    age_students = Column(db.String(100))
    school_id = reference_col("school", nullable=False)
    school = relationship("School", backref="classrooms")
    teacher_id = reference_col("userprofile", nullable=False)
    teacher = relationship("UserProfile", backref="classrooms")
    created_at = Column(db.DateTime, nullable=False,
                        default=dt.datetime.utcnow)

    def __init__(self, school, classname, teacher, slug=None, **kwargs):
        dtn = dt.datetime.now()
        date_slug = str(dtn.year % 100) + str(dtn.month) + \
            str(dtn.day) + str(dtn.hour) + str(dtn.minute)
        db.Model.__init__(self, school=school, classname=classname, teacher=teacher,
                          slug=slugify(classname + date_slug + str(teacher.id) + str(school.id)), **kwargs)
コード例 #10
0
class Project(SurrogatePK, Model):

    __tablename__ = 'project'
    title = Column(db.String(100), nullable=False)
    description = Column(db.Text, nullable=False)
    slug = Column(db.String(100), nullable=False, unique=True)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    updated_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    author_id = reference_col('userprofile', nullable=False)
    author = relationship('UserProfile', backref=db.backref('projects'))
    # https://docs.sqlalchemy.org/en/latest/_modules/examples/inheritance/joined.html
    fields = relationship('Field',
                          back_populates="project",
                          cascade="all, delete-orphan")
    theme_id = reference_col("theme", nullable=False)
    theme = relationship("Theme", backref="projects")
    # can be draft, published, deleted
    status = Column(db.String(15), nullable=False, default="draft")

    favoriters = relationship('UserProfile',
                              secondary=favoriter_assoc,
                              backref='favorites',
                              lazy='dynamic')

    tagList = relationship('Tags', secondary=tag_assoc, backref='projects')

    def __init__(self, author, title, description, slug=None, **kwargs):
        db.Model.__init__(
            self,
            author=author,
            title=title,
            description=description,
            slug=slug
            or slugify(title + "-" + str(uuid.uuid4().hex[:6].upper())),
            **kwargs)

    def favourite(self, profile):
        if not self.is_favourite(profile):
            self.favoriters.append(profile)
            return True
        return False

    def unfavourite(self, profile):
        if self.is_favourite(profile):
            self.favoriters.remove(profile)
            return True
        return False

    def is_favourite(self, profile):
        return bool(
            self.query.filter(
                favoriter_assoc.c.favoriter == profile.id).count())

    def add_tag(self, tag):
        if tag not in self.tagList:
            self.tagList.append(tag)
            return True
        return False

    def remove_tag(self, tag):
        if tag in self.tagList:
            self.tagList.remove(tag)
            return True
        return False

    @property
    def favoritesCount(self):
        return len(self.favoriters.all())

    @property
    def favorited(self):
        if current_user:
            profile = current_user.profile
            return self.query.join(Project.favoriters).filter(
                UserProfile.id == profile.id).count() == 1
        return False