コード例 #1
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class Chat(BaseModel):
    __tablename__ = "chat"

    id = db.Column(db.String(length=32),
                   nullable=False,
                   unique=True,
                   default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    visitor_id = db.Column(db.String(length=32), nullable=False, unique=True)
    tags = db.Column(ARRAY(JSON()), nullable=False, server_default="{}")
    severity_level = db.Column(db.SmallInteger,
                               nullable=False,
                               default=DEFAULT_SEVERITY_LEVEL_OF_CHAT)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_chat_id = db.Index("idx_chat_id", "id")
    _idx_chat_visitor = db.Index("idx_chat_visitor", "visitor_id")
    _idx_chat_severity_level = db.Index("idx_chat_severity_level",
                                        "severity_level")

    @classmethod
    async def get_or_create(cls, **kwargs):
        created_attempt = await get_one(cls, **kwargs)

        # Update the number of attempts in Quiz, if no previous attempts of the user are found
        if not created_attempt:
            data = await create_one(cls, **kwargs)
            return serialize_to_dict(data)

        return serialize_to_dict(created_attempt)
コード例 #2
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class Setting(BaseModel):
    """A global settings for the app, such as turning on/off the toggles."""

    __tablename__ = "setting"

    id = db.Column(db.String(length=32), nullable=False, default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    key = db.Column(db.String, nullable=False, unique=True)
    value = db.Column(db.SmallInteger, nullable=False)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_settings_id = db.Index("idx_settings_id", "id")
    _idx_settings_key = db.Index("idx_settings_key", "key")

    @classmethod
    async def modify_if_exists(cls, get_kwargs, update_kwargs):
        payload = await get_one(cls, **get_kwargs)
        if not payload:
            return None

        data = await update_one(payload, **update_kwargs)
        return serialize_to_dict(data)
コード例 #3
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class ChatMessage(BaseModel):
    __tablename__ = "chat_message"

    id = db.Column(db.String(length=32),
                   primary_key=True,
                   default=generate_uuid)
    sequence_num = db.Column(db.BigInteger, nullable=False, default=0)
    chat_id = db.Column(db.String(length=32), nullable=False)
    type_id = db.Column(db.SmallInteger, nullable=False, default=1)
    sender = db.Column(db.String(length=32), nullable=True)
    content = db.Column(JSON(), nullable=False, server_default="{}")
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_chat_msg_id = db.Index("idx_chat_msg_id", "id")
    _idx_chat_msg_chat_id = db.Index("idx_chat_msg_chat_id", "chat_id")
    _idx_chat_msg_sender = db.Index("idx_chat_msg_sender", "sender")

    @classmethod
    async def get(cls, *, chat_id, **kwargs):
        messages = await get_messages(cls, User, chat_id=chat_id, **kwargs)
        return messages

    @classmethod
    async def get_first_message_of_chat(cls, chat_id, **kwargs):
        data = await get_one_oldest(cls,
                                    chat_id=chat_id,
                                    order_by="sequence_num")
        return serialize_to_dict(data)
コード例 #4
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class Organisation(BaseModel):
    __tablename__ = "organisation"
    permissions = {
        "GET": ROLES,
        "POST": {},
        "PUT": {},
        "PATCH": {},
        "DELETE": {}
    }

    id = db.Column(db.String(length=32),
                   nullable=False,
                   unique=True,
                   default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    name = db.Column(db.String, nullable=False)
    disabled = db.Column(db.Boolean, nullable=False, default=False)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_org_id = db.Index("idx_org_id", "id")
    _idx_org_disabled = db.Index("idx_org_disabled", "disabled")
コード例 #5
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class StaffSubscriptionChat(BaseModel):
    __tablename__ = "staff_subscription_chat"

    id = db.Column(db.String(length=32),
                   primary_key=True,
                   default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    visitor_id = db.Column(db.String(length=32), nullable=False)
    staff_id = db.Column(db.String(length=32), nullable=False)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_bookmark_visitor_id = db.Index("idx_staff_subscription_chat_id", "id")
    _idx_bookmark_visitor_staff_id = db.Index(
        "idx_staff_subscription_chat_staff_id", "staff_id")
    _idx_bookmark_visitor_staff_visitor = db.Index(
        "idx_staff_subscription_visitor_staff_chat",
        "staff_id",
        "visitor_id",
        unique=True,
    )

    @classmethod
    async def get_or_create(cls, **kwargs):
        payload = await get_one(cls, **kwargs)
        if payload:
            return serialize_to_dict(payload)

        # Create
        data = await create_one(cls,
                                staff_id=kwargs["staff_id"],
                                chat_id=kwargs["chat_id"])
        return serialize_to_dict(data)

    @classmethod
    async def update_or_create(cls, get_kwargs, update_kwargs):
        payload = await get_one(cls, **get_kwargs)

        if not payload:
            data = await create_one(
                cls,
                **update_kwargs,
                staff_id=get_kwargs["staff_id"],
                chat_id=get_kwargs["chat_id"],
            )
            return serialize_to_dict(data)
        # Update the existing one
        data = await update_one(payload, **update_kwargs)
        return serialize_to_dict(data)
コード例 #6
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class ChatMessageSeen(BaseModel):
    __tablename__ = "chat_message_seen"

    id = db.Column(db.String(length=32),
                   nullable=False,
                   unique=True,
                   default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    staff_id = db.Column(db.String(length=32), nullable=False)
    chat_id = db.Column(db.String(length=32), nullable=False)
    last_seen_msg_id = db.Column(db.String(length=32), nullable=True)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_chat_msg_seen_id = db.Index("idx_chat_msg_seen_id", "id")
    _idx_chat_msg_seen_staff_chat = db.Index("idx_chat_msg_seen_staff_chat",
                                             "staff_id", "chat_id")

    @classmethod
    async def get_or_create(cls, **kwargs):
        payload = await get_one(cls, **kwargs)
        if payload:
            return serialize_to_dict(payload)

        # Create
        data = await create_one(cls,
                                staff_id=kwargs["staff_id"],
                                chat_id=kwargs["chat_id"])
        return serialize_to_dict(data)

    @classmethod
    async def update_or_create(cls, get_kwargs, update_kwargs):
        payload = await get_one(cls, **get_kwargs)

        if not payload:
            data = await create_one(
                cls,
                **update_kwargs,
                staff_id=get_kwargs["staff_id"],
                chat_id=get_kwargs["chat_id"],
            )
            return serialize_to_dict(data)
        # Update the existing one
        data = await update_one(payload, **update_kwargs)
        return serialize_to_dict(data)
コード例 #7
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class ChatUnhandled(BaseModel):
    __tablename__ = "chat_unhandled"

    id = db.Column(db.String(length=32), nullable=False, default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            autoincrement=True,
                            primary_key=True,
                            nullable=False)
    visitor_id = db.Column(db.String(length=32), nullable=False, unique=True)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_chat_unhandled_id = db.Index("idx_chat_unhandled_id", "id")
    _idx_chat_unhandled_vistor_id = db.Index("idx_chat_unhandled_vistor_id",
                                             "visitor_id")
コード例 #8
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class StaffNotificationSetting(BaseModel):
    __tablename__ = "staff_notification_setting"

    id = db.Column(db.String(length=32), nullable=False, default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    staff_id = db.Column(db.String, nullable=False)
    receive_emails = db.Column(db.Boolean, nullable=False, default=False)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_staff_notification_setting_id = db.Index(
        "idx_staff_notification_setting_id", "id")
    _idx_staff_notification_setting_staff_id = db.Index(
        "idx_staff_notification_setting_staff_id", "staff_id")
コード例 #9
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class RolePermission(BaseModel):
    __tablename__ = "role_permission"

    id = db.Column(db.String(length=32), nullable=False, default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    name = db.Column(db.String, nullable=False)
    role_id = db.Column(db.SmallInteger,
                        db.ForeignKey("user_role.id"),
                        nullable=False)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_role_permission_id = db.Index("idx_role_permission_id", "id")
    _idx_role_permission_role_id = db.Index("idx_role_permission_role_id",
                                            "role_id")
    _idx_role_permission_name = db.Index("idx_role_permission_key", "name")
    _idx_role_permission_name_role_id = db.Index(
        "idx_role_permission_key_role_id", "name", "role_id", unique=True)
コード例 #10
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class Visitor(BaseUser):
    __tablename__ = "visitor"

    id = db.Column(db.String(length=32),
                   nullable=False,
                   unique=True,
                   default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    name = db.Column(db.String, nullable=False)
    email = db.Column(db.String, unique=True, nullable=True)
    password = db.Column(db.String, nullable=True)
    is_anonymous = db.Column(db.Boolean, nullable=False, default=False)
    disabled = db.Column(db.Boolean, nullable=False, default=False)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_visitor_id = db.Index("idx_visitor_id", "id")
    _idx_visitor_email = db.Index("idx_visitor_email", "email")
コード例 #11
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class NotificationStaff(BaseModel):
    __tablename__ = "notification_staff"

    id = db.Column(db.String(length=32), nullable=False, default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    staff_id = db.Column(db.String, nullable=False)
    content = db.Column(JSON(), nullable=False, server_default="{}")
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_notification_staff_id = db.Index("idx_notification_staff_id", "id")
    _idx_notification_staff_staff_id = db.Index(
        "idx_notification_staff_staff_id", "staff_id")

    @classmethod
    async def bulk_upsert(cls, notifications):
        await insert(
            cls.__table__
        ).values(notifications).on_conflict_do_nothing().gino.scalar()
コード例 #12
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class User(BaseUser):
    __tablename__ = "user"
    permissions = {
        "GET": ROLES,
        "POST": {"supervisor", "admin"},
        "PUT": {"supervisor", "admin"},
        "PATCH": {"supervisor", "admin"},
        "DELETE": {"supervisor", "admin"},
    }

    id = db.Column(db.String(length=32),
                   nullable=False,
                   unique=True,
                   default=generate_uuid)
    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    full_name = db.Column(db.String, nullable=False)
    display_name = db.Column(db.String)
    role_id = db.Column(db.SmallInteger,
                        db.ForeignKey("user_role.id"),
                        nullable=False,
                        default=3)
    email = db.Column(db.String, unique=True, nullable=False)
    password = db.Column(db.String, nullable=False)
    disabled = db.Column(db.Boolean, nullable=False, default=False)
    organisation_id = db.Column(db.String(length=32),
                                db.ForeignKey("organisation.id"),
                                nullable=False)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_user_id = db.Index("idx_user_id", "id")
    _idx_user_email = db.Index("idx_user_email", "email")
    _idx_user_role_id = db.Index("idx_user_role_id", "role_id")
    _idx_user_organisation_id = db.Index("_idx_user_organisation_id",
                                         "organisation_id")
コード例 #13
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class UserRole(BaseModel):
    __tablename__ = "user_role"

    id = db.Column(db.BigInteger,
                   primary_key=True,
                   autoincrement=False,
                   unique=True)
    internal_id = db.Column(db.BigInteger, autoincrement=True, unique=True)
    name = db.Column(db.String, nullable=False)
    created_at = db.Column(db.BigInteger, nullable=False, default=unix_time)
    updated_at = db.Column(db.BigInteger, onupdate=unix_time)

    # Index
    _idx_user_role_table_id = db.Index("idx_user_role_table_id", "id")
コード例 #14
0
ファイル: models.py プロジェクト: snguyenthanh/ora-backend
class NotificationStaffRead(BaseModel):
    __tablename__ = "notification_staff_read"

    internal_id = db.Column(db.BigInteger,
                            primary_key=True,
                            autoincrement=True)
    staff_id = db.Column(db.String, unique=True, nullable=False)
    last_read_internal_id = db.Column(db.BigInteger, nullable=True)

    # Index
    _idx_notification_staff_read_staff_id = db.Index(
        "idx_notification_staff_read_staff_id", "staff_id")

    @classmethod
    async def get_or_create(cls, serialized=True, **kwargs):
        payload = await get_one(cls, **kwargs)
        if payload:
            return serialize_to_dict(payload) if serialized else payload

        # Create
        data = await create_one(cls, staff_id=kwargs["staff_id"])
        return serialize_to_dict(data) if serialized else data

    @classmethod
    async def update_or_create(cls, get_kwargs, update_kwargs):
        payload = await get_one(cls, **get_kwargs)

        if not payload:
            data = await create_one(cls,
                                    **update_kwargs,
                                    staff_id=get_kwargs["staff_id"])
            return serialize_to_dict(data)

        # Update the existing one
        data = await update_one(payload, **update_kwargs)
        return serialize_to_dict(data)