init
This commit is contained in:
42
app/db/models/__init__.py
Normal file
42
app/db/models/__init__.py
Normal file
@ -0,0 +1,42 @@
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import as_declarative, declared_attr
|
||||
|
||||
|
||||
@as_declarative()
|
||||
class Base:
|
||||
id: Any
|
||||
__name__: str
|
||||
|
||||
def create(self, db):
|
||||
db.add(self)
|
||||
db.commit()
|
||||
db.refresh(self)
|
||||
return self
|
||||
|
||||
@classmethod
|
||||
def get(cls, db, rid: int):
|
||||
return db.query(cls).filter(cls.id == rid).first()
|
||||
|
||||
def update(self, db, payload: dict):
|
||||
payload = {k: v for k, v in payload.items() if v is not None}
|
||||
for key, value in payload.items():
|
||||
setattr(self, key, value)
|
||||
db.commit()
|
||||
db.refresh(self)
|
||||
|
||||
@classmethod
|
||||
def delete(cls, db, rid):
|
||||
db.query(cls).filter(cls.id == rid).delete()
|
||||
db.commit()
|
||||
|
||||
@classmethod
|
||||
def list(cls, db):
|
||||
return db.query(cls).all()
|
||||
|
||||
def to_dict(self):
|
||||
return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}
|
||||
|
||||
@declared_attr
|
||||
def __tablename__(self) -> str:
|
||||
return self.__name__.lower()
|
BIN
app/db/models/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
app/db/models/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/db/models/__pycache__/site.cpython-310.pyc
Normal file
BIN
app/db/models/__pycache__/site.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/db/models/__pycache__/subscribe.cpython-310.pyc
Normal file
BIN
app/db/models/__pycache__/subscribe.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/db/models/__pycache__/systemconfig.cpython-310.pyc
Normal file
BIN
app/db/models/__pycache__/systemconfig.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/db/models/__pycache__/user.cpython-310.pyc
Normal file
BIN
app/db/models/__pycache__/user.cpython-310.pyc
Normal file
Binary file not shown.
28
app/db/models/site.py
Normal file
28
app/db/models/site.py
Normal file
@ -0,0 +1,28 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Boolean, Column, Integer, String, Sequence
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.models import Base
|
||||
|
||||
|
||||
class Site(Base):
|
||||
id = Column(Integer, Sequence('id'), primary_key=True, index=True)
|
||||
name = Column(String, nullable=False)
|
||||
domain = Column(String, index=True)
|
||||
url = Column(String, nullable=False)
|
||||
pri = Column(Integer)
|
||||
rss = Column(String)
|
||||
cookie = Column(String)
|
||||
ua = Column(String)
|
||||
filter = Column(String)
|
||||
note = Column(String)
|
||||
limit_interval = Column(Integer)
|
||||
limit_count = Column(Integer)
|
||||
limit_seconds = Column(Integer)
|
||||
is_active = Column(Boolean(), default=True)
|
||||
lst_mod_date = Column(String, default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
||||
|
||||
@staticmethod
|
||||
def get_by_domain(db: Session, domain: str):
|
||||
return db.query(Site).filter(Site.domain == domain).first()
|
36
app/db/models/subscribe.py
Normal file
36
app/db/models/subscribe.py
Normal file
@ -0,0 +1,36 @@
|
||||
from sqlalchemy import Column, Integer, String, Sequence
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.models import Base
|
||||
|
||||
|
||||
class Subscribe(Base):
|
||||
id = Column(Integer, Sequence('id'), primary_key=True, index=True)
|
||||
name = Column(String, nullable=False, index=True)
|
||||
year = Column(String)
|
||||
type = Column(String)
|
||||
keyword = Column(String)
|
||||
tmdbid = Column(String, index=True)
|
||||
doubanid = Column(String)
|
||||
season = Column(Integer)
|
||||
image = Column(String)
|
||||
description = Column(String)
|
||||
filter = Column(String)
|
||||
include = Column(String)
|
||||
exclude = Column(String)
|
||||
total_episode = Column(Integer)
|
||||
start_episode = Column(Integer)
|
||||
lack_episode = Column(Integer)
|
||||
note = Column(String)
|
||||
state = Column(String, nullable=False, index=True, default='N')
|
||||
|
||||
@staticmethod
|
||||
def exists(db: Session, tmdbid: str, season: int = None):
|
||||
if season:
|
||||
return db.query(Subscribe).filter(Subscribe.tmdbid == tmdbid,
|
||||
Subscribe.season == season).first()
|
||||
return db.query(Subscribe).filter(Subscribe.tmdbid == tmdbid).first()
|
||||
|
||||
@staticmethod
|
||||
def get_by_state(db: Session, state: str):
|
||||
return db.query(Subscribe).filter(Subscribe.state == state).all()
|
19
app/db/models/systemconfig.py
Normal file
19
app/db/models/systemconfig.py
Normal file
@ -0,0 +1,19 @@
|
||||
from sqlalchemy import Column, Integer, String, Sequence
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.models import Base
|
||||
|
||||
|
||||
class SystemConfig(Base):
|
||||
id = Column(Integer, Sequence('id'), primary_key=True, index=True)
|
||||
key = Column(String, index=True)
|
||||
value = Column(String, nullable=True)
|
||||
|
||||
@staticmethod
|
||||
def get_by_key(db: Session, key: str):
|
||||
return db.query(SystemConfig).filter(SystemConfig.key == key).first()
|
||||
|
||||
@staticmethod
|
||||
def delete_by_key(db: Session, key: str):
|
||||
db.query(SystemConfig).filter(SystemConfig.key == key).delete()
|
||||
db.commit()
|
27
app/db/models/user.py
Normal file
27
app/db/models/user.py
Normal file
@ -0,0 +1,27 @@
|
||||
from sqlalchemy import Boolean, Column, Integer, String, Sequence
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.security import verify_password
|
||||
from app.db.models import Base
|
||||
|
||||
|
||||
class User(Base):
|
||||
id = Column(Integer, Sequence('id'), primary_key=True, index=True)
|
||||
full_name = Column(String, index=True)
|
||||
email = Column(String, unique=True, index=True, nullable=False)
|
||||
hashed_password = Column(String, nullable=False)
|
||||
is_active = Column(Boolean(), default=True)
|
||||
is_superuser = Column(Boolean(), default=False)
|
||||
|
||||
@staticmethod
|
||||
def authenticate(db: Session, email: str, password: str):
|
||||
user = db.query(User).filter(User.email == email).first()
|
||||
if not user:
|
||||
return None
|
||||
if not verify_password(password, str(user.hashed_password)):
|
||||
return None
|
||||
return user
|
||||
|
||||
@staticmethod
|
||||
def get_by_email(db: Session, email: str):
|
||||
return db.query(User).filter(User.email == email).first()
|
Reference in New Issue
Block a user