attachment class
This commit is contained in:
1 parent
335eb8c4ac
commit
a69b15a3e8
5 files changed
+144
-79
No files matched your search
+37
-8
@@ -1,4 +1,13 @@
|
||||
from sqlalchemy import create_engine, Column, Integer, String, JSON, Uuid
|
||||
from uuid import UUID
|
||||
import os
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy.orm import Mapped
|
||||
from sqlalchemy.orm import mapped_column
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from db.base import Base
|
||||
|
||||
@@ -6,10 +15,30 @@ from db.base import Base
|
||||
class Attachment(Base):
|
||||
__tablename__ = 'attachments'
|
||||
|
||||
id = Column(Uuid, primary_key=True)
|
||||
msg_id = Column(Integer, nullable=False, index=True)
|
||||
attributes = Column(JSON, nullable=False)
|
||||
vision = Column(JSON, nullable=False)
|
||||
message = Column(JSON, nullable=False)
|
||||
guild = Column(JSON, nullable=False)
|
||||
channel = Column(JSON, nullable=False)
|
||||
# can be multiple attachments on a msg
|
||||
# so we make our own ids for attachments
|
||||
id: Mapped[UUID] = mapped_column(primary_key=True)
|
||||
msg_id: Mapped[int] = mapped_column(nullable=False, index=True)
|
||||
|
||||
url: Mapped[str]
|
||||
|
||||
message: Mapped[dict[str, Any]]
|
||||
guild: Mapped[dict[str, Any]]
|
||||
channel: Mapped[dict[str, Any]]
|
||||
vision: Mapped[Optional[dict[str, Any]]] = mapped_column(default=None)
|
||||
|
||||
def file_details(self) -> tuple[str, str]:
|
||||
return os.path.splitext(os.path.basename(self.url))
|
||||
|
||||
def object_name(self) -> str:
|
||||
(filename, ext) = self.file_details()
|
||||
if self.convert_to_webp():
|
||||
return f"{self.id}.webp"
|
||||
|
||||
return f"{id}{ext}"
|
||||
|
||||
def convert_to_webp(self) -> bool:
|
||||
(filename, ext) = self.file_details()
|
||||
convert_extensions = ['jpg', 'png', 'jpeg', 'webp']
|
||||
return ext.lower()[1:] in convert_extensions
|
||||
|
||||
+10
-2
@@ -1,3 +1,11 @@
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from typing import Any
|
||||
|
||||
Base = declarative_base()
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy.orm import MappedAsDataclass
|
||||
from sqlalchemy.types import JSON
|
||||
|
||||
class Base(DeclarativeBase, MappedAsDataclass):
|
||||
type_annotation_map = {
|
||||
dict[str, Any]: JSON,
|
||||
list[str]: JSON,
|
||||
}
|
||||
+11
-5
@@ -1,4 +1,10 @@
|
||||
from sqlalchemy import Column, Integer, String, JSON, Uuid
|
||||
from typing import List
|
||||
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy.orm import Mapped
|
||||
from sqlalchemy.orm import mapped_column
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from db.base import Base
|
||||
|
||||
@@ -6,7 +12,7 @@ from db.base import Base
|
||||
class Meme(Base):
|
||||
__tablename__ = 'memes'
|
||||
|
||||
id = Column(Uuid, primary_key=True)
|
||||
url = Column(String, nullable=False)
|
||||
text = Column(String, nullable=False)
|
||||
tags = Column(JSON, nullable=False)
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
url: Mapped[str] = mapped_column(nullable=False)
|
||||
text: Mapped[str] = mapped_column(nullable=False)
|
||||
tags: Mapped[list[str]] = mapped_column(nullable=False)
|
||||
Reference in new issue
Block a user