Added data download ability

This commit is contained in:
2023-02-19 21:10:00 +00:00
parent 48f080de7c
commit c850726f91
64 changed files with 12734 additions and 2 deletions

View File

@@ -0,0 +1,53 @@
from typing import List, Optional
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import SQLModel
from src.database.crud import delete, read_all_where, read_where, update
class SQLModelExtended(SQLModel):
__abstract__ = True
@classmethod
async def get_all(
self, session: AsyncSession, select_in_load: List = []
) -> List[SQLModel]:
res = await read_all_where(self, select_in_load=select_in_load, session=session)
return res
async def save(self, session: AsyncSession):
await update(self, session=session)
@classmethod
async def get_by_id(
self, id: int, session: AsyncSession, select_in_load: List = []
) -> Optional[SQLModel]:
res = await read_where(
self, self.id == id, select_in_load=select_in_load, session=session
)
return res
@classmethod
async def get_where(self, *args, session: AsyncSession):
res = await read_where(self, *args, session=session)
return res
@classmethod
async def get_all_where(self, *args, session: AsyncSession):
res = await read_all_where(self, *args, session=session)
return res
async def delete(self, session: AsyncSession) -> None:
await delete(self, session=session)
async def update_from(self, data, session: AsyncSession) -> None:
data_dict = data.dict(exclude_unset=True)
for key in data_dict.keys():
setattr(self, key, getattr(data, key))
await update(self, session=session)
@classmethod
async def delete_by_id(self, id: int, session: AsyncSession) -> None:
res = await read_where(self, self.id == id, session=session)
await delete(res, session=session)

View File

@@ -0,0 +1,26 @@
from typing import Optional
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import Field, Relationship
from src.database.crud import read_where
from src.models.SQLModelExtended import SQLModelExtended
class Login(BaseModel):
email: str
password: str
class Config:
orm_mode = True
class User(SQLModelExtended, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
email: str
hashed_password: str
@classmethod
async def get_by_email(self, email: str, session: AsyncSession) -> "User":
return await read_where(self, self.email == email, session=session)

View File

@@ -0,0 +1,61 @@
from typing import List
import requests
from pydantic import BaseModel
from sqlmodel import Field, Relationship, SQLModel
from src.exceptions.base_exception import BaseException
from src.models.signvideo import SignVideo
from src.models.SQLModelExtended import SQLModelExtended
class Sign(SQLModelExtended, table=True):
id: int = Field(primary_key=True)
url: str
name: str
sign_id: str
video_url: str
sign_videos: List[SignVideo] = Relationship(
back_populates="sign",
sa_relationship_kwargs={"lazy": "selectin"},
)
def __init__(self, url):
self.url = url
# get name and sign id from url
try:
t = self.url.split("/")[-1].split("?")
self.name = t[0]
self.sign_id = t[1].split("=")[1]
except:
raise BaseException(404, "Invalid url")
self.get_video_url()
def get_video_url(self):
try:
r = requests.get(f"https://woordenboek.vlaamsegebarentaal.be/api/glosses/{self.name}")
res_json = r.json()
# find the video url
for item in res_json["variants"]:
if str(item["signId"]) == str(self.sign_id):
self.video_url = item["video"]
break
except:
raise BaseException(404, "Invalid url")
# throw exception if video url not found
if self.video_url is None:
raise BaseException(404, "Video url not found")
class SignOut(BaseModel):
id: int
url: str
name: str
sign_id: str
video_url: str
sign_videos: List[SignVideo] = []

View File

@@ -0,0 +1,25 @@
from pydantic import BaseModel
from sqlmodel import Field, Relationship, SQLModel
from src.exceptions.base_exception import BaseException
from src.models.SQLModelExtended import SQLModelExtended
class SignVideo(SQLModelExtended, table=True):
id: int = Field(primary_key=True)
approved: bool = False
# foreign key to sign
sign_id: int = Field(default=None, foreign_key="sign.id")
sign: "Sign" = Relationship(
back_populates="sign_videos",
sa_relationship_kwargs={"lazy": "selectin"},
)
# path where video saved
path: str
class SignVideoOut(BaseModel):
id: int
approved: bool

View File

@@ -0,0 +1,10 @@
from pydantic import BaseModel
class TokenExtended(BaseModel):
"""an output model for the tokens (accessToken + refreshToken)"""
id: str
access_token: str
access_token_expiry: int
refresh_token: str