@@ -75,7 +75,21 @@ class ProjectDetailResponse(BaseModel):
75
75
def get_projects ():
76
76
if not DOCAT_UPLOAD_FOLDER .exists ():
77
77
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
+ )
79
93
80
94
81
95
@app .get (
@@ -106,7 +120,7 @@ def get_project(project):
106
120
tags = [str (t .relative_to (docs_folder )) for t in tags if t .resolve () == x ],
107
121
)
108
122
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 ()
110
124
],
111
125
key = lambda k : k .name ,
112
126
reverse = True ,
@@ -155,6 +169,77 @@ def upload_icon(
155
169
return ApiResponse (message = "Icon successfully uploaded" )
156
170
157
171
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
+
158
243
@app .post ("/api/{project}/{version}" , response_model = ApiResponse , status_code = status .HTTP_201_CREATED )
159
244
@app .post ("/api/{project}/{version}/" , response_model = ApiResponse , status_code = status .HTTP_201_CREATED )
160
245
def upload (
0 commit comments