예제 #1
0
class Company(PkModel):
    __tablename__ = "company"

    name = Column(db.String(80), unique=True, nullable=False)
    description = Column(db.String(500), nullable=True)
    challenges = relationship("Challenge", backref="company")

    def __init__(self, name, description, **kwargs):
        """Create instance."""
        super().__init__(name=name, description=description, **kwargs)
예제 #2
0
class Challenge(PkModel):
    """A challenge of the app."""

    __tablename__ = "challenges"
    challengename = Column(db.String(80), unique=True, nullable=False)
    description = Column(db.String(3000), nullable=False)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    active = Column(db.Boolean(), default=False)
    category_id = Column(db.Integer, db.ForeignKey("category.id"))
    company_id = Column(db.Integer, db.ForeignKey("company.id"))
    co2offset = Column(db.Integer, nullable=False)
    chat_room_id = Column(db.String, db.ForeignKey("chat_rooms.room_id"))

    def __init__(self, challengename, description, active, category_id,
                 co2offset, **kwargs):
        """Create instance."""
        super().__init__(
            challengename=challengename,
            description=description,
            active=active,
            category_id=category_id,
            co2offset=co2offset,
            **kwargs,
        )

    def __repr__(self):
        """Represent instance as a unique string."""
        return f"<Challenge({self.challengename!r})>"
예제 #3
0
class Category(PkModel):
    """A role for a challenge."""

    __tablename__ = "category"
    name = Column(db.String(80), unique=True, nullable=False)
    parent_id = Column(db.Integer, db.ForeignKey("category.id"), nullable=True)
    challenges = relationship("Challenge", backref="category")

    def __init__(self, name, parent_id, **kwargs):
        """Create instance."""
        super().__init__(name=name, parent_id=parent_id, **kwargs)

    def __repr__(self):
        """Represent instance as a unique string."""
        return f"<Category({self.name})>"
예제 #4
0
class User_Challenge_Association(PkModel):
    __tablename__ = "user_challenge_association"

    user_id = Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    challenge_id = Column(db.Integer,
                          db.ForeignKey("challenges.id"),
                          nullable=False)
    commited_to_at = Column(db.DateTime,
                            nullable=False,
                            default=dt.datetime.utcnow)
    done_at = Column(db.DateTime, nullable=True)
    succeeded = Column(db.Boolean, nullable=False, default=False)
    challenges = relationship("Challenge", backref="user_challenges")
    users = relationship("User", backref="user_challenges")

    def __init__(self, user_id, challenge_id, **kwargs):
        super().__init__(user_id=user_id, challenge_id=challenge_id, **kwargs)
예제 #5
0
class ChatMessage(PkModel):
    """A ChatMessage ."""

    __tablename__ = "chat_messages"
    text = Column(db.String(180), unique=False, nullable=False)
    written_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)

    chat_room_id = reference_col("chat_rooms", nullable=True)
    room = relationship(
        "ChatRoom",
        backref="chat_messages",
        collection_class=ordering_list("written_at"),
    )

    user_id = reference_col("users", nullable=True)
    user = relationship("User", backref="chat_messages")

    def __init__(self, **kwargs):
        """Create instance."""
        super().__init__(**kwargs)

    def __repr__(self):
        """Represent instance as a unique string."""
        return f"<ChatMessage({self.text!r})>"

    def get_json(self):
        """A json representation of the chat message, who, when what."""
        # ISO 8601 datetime format
        d = {
            "text": self.text,
            "written_at": self.written_at.strftime("%Y-%m-%dT%H:%M:%S.%f%z"),
        }
        if self.user:
            d["user"] = self.user.username
            d["is_self"] = self.user == current_user
        else:
            d["user"] = "******"
            d["is_self"] = False
        return d
예제 #6
0
class ChatRoom(PkModel):
    """ChatRoom contains various ChatMessages."""

    __tablename__ = "chat_rooms"
    room_id = Column(db.String(80), unique=True, nullable=False)
    name = Column(db.String(80), unique=False, nullable=True)
    challenges = relationship("Challenge", backref="chat_room")

    def __init__(self, **kwargs):
        """Create instance."""
        super().__init__(**kwargs)

    def __repr__(self):
        """Represent instance as a unique string."""
        return f"<ChatRoom({self.room_id})>"

    def get_history(self, num=20):
        """History is a json representation of the last num chat messages in reverse order [{}, {}, ...]."""
        if len(self.chat_messages) > num:
            msg = self.chat_messages[-num:]
        else:
            msg = self.chat_messages
        return [m.get_json() for m in msg]
예제 #7
0
class Role(PkModel):
    """A role for a user."""

    __tablename__ = "roles"
    name = Column(db.String(80), unique=True, nullable=False)
    user_id = reference_col("users", nullable=True)
    user = relationship("User", backref="roles")

    def __init__(self, name, **kwargs):
        """Create instance."""
        super().__init__(name=name**kwargs)

    def __repr__(self):
        """Represent instance as a unique string."""
        return f"<Role({self.name})>"
예제 #8
0
class User(UserMixin, PkModel):
    """A user of the app."""

    __tablename__ = "users"
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(80), unique=True, nullable=False)
    password = Column(db.LargeBinary(128), nullable=True)
    country = Column(db.String(200), nullable=False)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    active = Column(db.Boolean(), default=False)
    is_admin = Column(db.Boolean(), default=False)

    def __init__(self, username, email, country, password=None, **kwargs):
        """Create instance."""
        super().__init__(username=username,
                         email=email,
                         country=country,
                         **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

    def set_password(self, password):
        """Set password."""
        self.password = bcrypt.generate_password_hash(password)

    def check_password(self, value):
        """Check password."""
        return bcrypt.check_password_hash(self.password, value)

    @property
    def full_name(self):
        """Full user name."""
        return f"{self.first_name} {self.last_name}"

    def __repr__(self):
        """Represent instance as a unique string."""
        return f"<User({self.username!r})>"