-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
149 lines (121 loc) · 4.44 KB
/
Copy pathmodels.py
File metadata and controls
149 lines (121 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import datetime
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Annotated, Literal, Self
from uuid import UUID
from pydantic import AfterValidator, AnyUrl, Field, model_validator
from cms_backend.api.routes.fields import (
Base64Str,
GraphemeLength,
LangCode,
LimitFieldMax200,
NotEmptyString,
SkipField,
ZimFlavour,
)
from cms_backend.context import Context
from cms_backend.roles import RoleEnum
from cms_backend.schemas import BaseModel
from cms_backend.schemas.orms import BaseTitleCollectionSchema
ZIM_TITLE_NAME_REGEX = re.compile(
r"^[a-z0-9\-\.]+?_[a-z]{2}(?:-[a-z]{2,10})?_[a-z0-9\-\.]+?$"
)
class ZimUrlSchema(BaseModel):
"""Schema for a single zim URL"""
kind: Literal["view", "download", "zimcheck"]
url: AnyUrl
collection: str
class ZimUrlsSchema(BaseModel):
urls: dict[UUID, list[ZimUrlSchema]]
class GetBooksSchema(BaseModel):
skip: SkipField = 0
limit: LimitFieldMax200 = 20
id: NotEmptyString | None = None
name: NotEmptyString | None = None
flavour: NotEmptyString | None = None
has_title: bool | None = None
needs_processing: bool | None = None
has_error: bool | None = None
needs_file_operation: bool | None = None
location_kinds: list[NotEmptyString] | None = None
needs_attention: bool | None = None
has_backup: bool | None = None
updated_before: datetime.datetime | None = None
updated_after: datetime.datetime | None = None
created_before: datetime.datetime | None = None
omit_book_ids: list[UUID] | None = None
offliner: NotEmptyString | None = None
issue: NotEmptyString | None = None
class BookLanguagesSchema(BaseModel):
languages: list[str]
@dataclass(eq=True, frozen=True)
class FileLocation:
warehouse_id: UUID
path: Path
filename: str
is_backup: bool = False
class AccountUpdateSchema(BaseModel):
"""
Schema for updating an account
"""
role: RoleEnum | None = None
username: NotEmptyString | None = None
idp_sub: NotEmptyString | None = None
display_name: NotEmptyString | None = None
class CollectionUpdateSchema(BaseModel):
name: NotEmptyString | None = None
download_base_url: AnyUrl | None = None
view_base_url: AnyUrl | None = None
comment: NotEmptyString | None = None
article_count_change_threshold: float | None = Field(ge=0.0, le=1.0, default=None)
media_count_change_threshold: float | None = Field(ge=0.0, le=1.0, default=None)
class BaseTitleCreateUpdateSchema(BaseModel):
collection_titles: list[BaseTitleCollectionSchema] | None = None
long_description: NotEmptyString | None = None
license: NotEmptyString | None = None
relation: NotEmptyString | None = None
source: NotEmptyString | None = None
title: (
Annotated[
NotEmptyString,
AfterValidator(GraphemeLength(max=Context.zim_title_max_length)),
]
| None
) = None
creator: NotEmptyString | None = None
description: (
Annotated[
NotEmptyString,
AfterValidator(GraphemeLength(max=Context.zim_description_max_length)),
]
| None
) = None
publisher: NotEmptyString | None = None
language: LangCode | None = None
illustration_48x48_at_1: Base64Str | None = None
flavours: list[ZimFlavour] | None = None
archived: bool | None = None
@model_validator(mode="after")
def validate_unique_collection_titles(self) -> Self:
if self.collection_titles:
seen: set[str] = set()
for entry in self.collection_titles:
if entry.collection_name in seen:
raise ValueError(
f"Collection title {entry.collection_name} duplicated, "
"cannot use a collection twice in a given title"
)
else:
seen.add(entry.collection_name)
return self
class TitleCreateSchema(BaseTitleCreateUpdateSchema):
name: NotEmptyString = Field(pattern=ZIM_TITLE_NAME_REGEX)
maturity: Literal["unstable", "stable"] = "unstable"
class TitleUpdateSchema(BaseTitleCreateUpdateSchema):
name: NotEmptyString | None = Field(pattern=ZIM_TITLE_NAME_REGEX, default=None)
maturity: Literal["unstable", "stable"] | None = None
comment: NotEmptyString | None = None
class BookUpdateSchema(BaseModel):
comment: NotEmptyString | None = None
flavour: NotEmptyString | None = None