Skip to content

Commit 5160e11

Browse files
authored
Merge pull request #321 from docat-org/feat/62-hidden-versions
Feat(docat): Add API to hide project versions
2 parents 7e1f306 + ba76e2f commit 5160e11

File tree

3 files changed

+487
-2
lines changed

3 files changed

+487
-2
lines changed

docat/docat/app.py

+87-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,21 @@ class ProjectDetailResponse(BaseModel):
7575
def get_projects():
7676
if not DOCAT_UPLOAD_FOLDER.exists():
7777
return ProjectsResponse(projects=[])
78-
return ProjectsResponse(projects=[str(x.relative_to(DOCAT_UPLOAD_FOLDER)) for x in DOCAT_UPLOAD_FOLDER.iterdir() if x.is_dir()])
78+
79+
def has_not_hidden_versions(project):
80+
path = DOCAT_UPLOAD_FOLDER / project
81+
return any(
82+
(path / version).is_dir() and not (path / version / ".hidden").exists() for version in (DOCAT_UPLOAD_FOLDER / project).iterdir()
83+
)
84+
85+
return ProjectsResponse(
86+
projects=list(
87+
filter(
88+
has_not_hidden_versions,
89+
[str(project.relative_to(DOCAT_UPLOAD_FOLDER)) for project in DOCAT_UPLOAD_FOLDER.iterdir() if project.is_dir()],
90+
)
91+
)
92+
)
7993

8094

8195
@app.get(
@@ -106,7 +120,7 @@ def get_project(project):
106120
tags=[str(t.relative_to(docs_folder)) for t in tags if t.resolve() == x],
107121
)
108122
for x in docs_folder.iterdir()
109-
if x.is_dir() and not x.is_symlink()
123+
if x.is_dir() and not x.is_symlink() and not (docs_folder / x.name / ".hidden").exists()
110124
],
111125
key=lambda k: k.name,
112126
reverse=True,
@@ -155,6 +169,77 @@ def upload_icon(
155169
return ApiResponse(message="Icon successfully uploaded")
156170

157171

172+
@app.post("/api/{project}/{version}/hide", response_model=ApiResponse, status_code=status.HTTP_200_OK)
173+
@app.post("/api/{project}/{version}/hide/", response_model=ApiResponse, status_code=status.HTTP_200_OK)
174+
def hide_version(
175+
project: str,
176+
version: str,
177+
response: Response,
178+
docat_api_key: Optional[str] = Header(None),
179+
db: TinyDB = Depends(get_db),
180+
):
181+
project_base_path = DOCAT_UPLOAD_FOLDER / project
182+
version_path = project_base_path / version
183+
hidden_file = version_path / ".hidden"
184+
185+
if not project_base_path.exists():
186+
response.status_code = status.HTTP_404_NOT_FOUND
187+
return ApiResponse(message=f"Project {project} not found")
188+
189+
if not version_path.exists():
190+
response.status_code = status.HTTP_404_NOT_FOUND
191+
return ApiResponse(message=f"Version {version} not found")
192+
193+
if hidden_file.exists():
194+
response.status_code = status.HTTP_400_BAD_REQUEST
195+
return ApiResponse(message=f"Version {version} is already hidden")
196+
197+
token_status = check_token_for_project(db, docat_api_key, project)
198+
if not token_status.valid:
199+
response.status_code = status.HTTP_401_UNAUTHORIZED
200+
return ApiResponse(message=token_status.reason)
201+
202+
with open(hidden_file, "w") as f:
203+
f.close()
204+
205+
return ApiResponse(message=f"Version {version} is now hidden")
206+
207+
208+
@app.post("/api/{project}/{version}/show", response_model=ApiResponse, status_code=status.HTTP_200_OK)
209+
@app.post("/api/{project}/{version}/show/", response_model=ApiResponse, status_code=status.HTTP_200_OK)
210+
def show_version(
211+
project: str,
212+
version: str,
213+
response: Response,
214+
docat_api_key: Optional[str] = Header(None),
215+
db: TinyDB = Depends(get_db),
216+
):
217+
project_base_path = DOCAT_UPLOAD_FOLDER / project
218+
version_path = project_base_path / version
219+
hidden_file = version_path / ".hidden"
220+
221+
if not project_base_path.exists():
222+
response.status_code = status.HTTP_404_NOT_FOUND
223+
return ApiResponse(message=f"Project {project} not found")
224+
225+
if not version_path.exists():
226+
response.status_code = status.HTTP_404_NOT_FOUND
227+
return ApiResponse(message=f"Version {version} not found")
228+
229+
if not hidden_file.exists():
230+
response.status_code = status.HTTP_400_BAD_REQUEST
231+
return ApiResponse(message=f"Version {version} is not hidden")
232+
233+
token_status = check_token_for_project(db, docat_api_key, project)
234+
if not token_status.valid:
235+
response.status_code = status.HTTP_401_UNAUTHORIZED
236+
return ApiResponse(message=token_status.reason)
237+
238+
os.remove(hidden_file)
239+
240+
return ApiResponse(message=f"Version {version} is now shown")
241+
242+
158243
@app.post("/api/{project}/{version}", response_model=ApiResponse, status_code=status.HTTP_201_CREATED)
159244
@app.post("/api/{project}/{version}/", response_model=ApiResponse, status_code=status.HTTP_201_CREATED)
160245
def upload(

docat/tests/conftest.py

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def client():
2020
temp_dir.cleanup()
2121

2222

23+
@pytest.fixture
24+
def upload_folder_path():
25+
return docat.DOCAT_UPLOAD_FOLDER
26+
27+
2328
@pytest.fixture
2429
def client_with_claimed_project(client):
2530
table = docat.db.table("claims")

0 commit comments

Comments
 (0)