|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import datetime |
|
|
|
from api.db.db_models import DB |
|
from api.db.db_models import File, Document, File2Document |
|
from api.db.services.common_service import CommonService |
|
from api.utils import current_timestamp, datetime_format |
|
|
|
|
|
class File2DocumentService(CommonService): |
|
model = File2Document |
|
|
|
@classmethod |
|
@DB.connection_context() |
|
def get_by_file_id(cls, file_id): |
|
objs = cls.model.select().where(cls.model.file_id == file_id) |
|
return objs |
|
|
|
@classmethod |
|
@DB.connection_context() |
|
def get_by_document_id(cls, document_id): |
|
objs = cls.model.select().where(cls.model.document_id == document_id) |
|
return objs |
|
|
|
@classmethod |
|
@DB.connection_context() |
|
def insert(cls, obj): |
|
if not cls.save(**obj): |
|
raise RuntimeError("Database error (File)!") |
|
e, obj = cls.get_by_id(obj["id"]) |
|
if not e: |
|
raise RuntimeError("Database error (File retrieval)!") |
|
return obj |
|
|
|
@classmethod |
|
@DB.connection_context() |
|
def delete_by_file_id(cls, file_id): |
|
return cls.model.delete().where(cls.model.file_id == file_id).execute() |
|
|
|
@classmethod |
|
@DB.connection_context() |
|
def delete_by_document_id(cls, doc_id): |
|
return cls.model.delete().where(cls.model.document_id == doc_id).execute() |
|
|
|
@classmethod |
|
@DB.connection_context() |
|
def update_by_file_id(cls, file_id, obj): |
|
obj["update_time"] = current_timestamp() |
|
obj["update_date"] = datetime_format(datetime.now()) |
|
num = cls.model.update(obj).where(cls.model.id == file_id).execute() |
|
e, obj = cls.get_by_id(cls.model.id) |
|
return obj |
|
|