|
22 | 22 | from zope.intid.interfaces import IIntIds |
23 | 23 |
|
24 | 24 | import json |
| 25 | +import logging |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
25 | 28 |
|
26 | 29 |
|
27 | 30 | class RelatedBaseView(BrowserView): |
@@ -257,3 +260,89 @@ def __call__(self): |
257 | 260 | status="done", |
258 | 261 | ) |
259 | 262 | ) |
| 263 | + |
| 264 | + |
| 265 | +class StatisticsView(BrowserView): |
| 266 | + def __call__(self): |
| 267 | + self.stats = {} |
| 268 | + |
| 269 | + if self.request.get("REQUEST_METHOD", "GET") == "POST" and self.request.get( |
| 270 | + "form.submitted" |
| 271 | + ): |
| 272 | + media_root = get_media_root(self.context) |
| 273 | + self.media_root_path = media_root.absolute_url_path() |
| 274 | + items = self.context.portal_catalog.unrestrictedSearchResults( |
| 275 | + path=self.media_root_path, portal_type=["Image", "File"] |
| 276 | + ) |
| 277 | + _all = len(items) |
| 278 | + filter_keyword = self.request.get("filter", "").lower() |
| 279 | + unrelated = [] |
| 280 | + relation_stats = {} |
| 281 | + purge = self.request.get("purge_unrelated", False) |
| 282 | + |
| 283 | + for idx, it in enumerate(items, 1): |
| 284 | + obj = it.getObject() |
| 285 | + match = True |
| 286 | + |
| 287 | + logger.info(f"Processing {idx}/{_all}: {obj.absolute_url()}") |
| 288 | + |
| 289 | + if filter_keyword: |
| 290 | + match = ( |
| 291 | + filter_keyword in obj.title_or_id().lower() |
| 292 | + or filter_keyword in obj.absolute_url() |
| 293 | + ) |
| 294 | + |
| 295 | + relations = api.relation.get(target=obj, unrestricted=True) |
| 296 | + |
| 297 | + if not relations and match: |
| 298 | + |
| 299 | + if purge: |
| 300 | + media_root.manage_delObjects([obj.getId()]) |
| 301 | + logger.warning(f"Purged unrelated media: {obj.absolute_url()}") |
| 302 | + continue |
| 303 | + |
| 304 | + unrelated.append( |
| 305 | + dict( |
| 306 | + source=dict( |
| 307 | + title=obj.title_or_id(), url=obj.absolute_url_path() |
| 308 | + ), |
| 309 | + target=dict(title=None, url=None), |
| 310 | + ) |
| 311 | + ) |
| 312 | + |
| 313 | + for relation in relations: |
| 314 | + |
| 315 | + if relation.from_attribute not in relation_stats: |
| 316 | + relation_stats[relation.from_attribute] = [] |
| 317 | + |
| 318 | + source_obj = relation.from_object |
| 319 | + |
| 320 | + if filter_keyword and not match: |
| 321 | + # check if source or target matches the filter keyword |
| 322 | + match = ( |
| 323 | + filter_keyword in source_obj.title_or_id().lower() |
| 324 | + or filter_keyword in source_obj.absolute_url() |
| 325 | + ) |
| 326 | + |
| 327 | + if match: |
| 328 | + relation_stats[relation.from_attribute].append( |
| 329 | + dict( |
| 330 | + source=dict( |
| 331 | + title=obj.title_or_id(), |
| 332 | + url=obj.absolute_url_path(), |
| 333 | + ), |
| 334 | + target=dict( |
| 335 | + title=source_obj.title_or_id(), |
| 336 | + url=source_obj.absolute_url_path(), |
| 337 | + ), |
| 338 | + ) |
| 339 | + ) |
| 340 | + |
| 341 | + relation_stats["unrelated"] = unrelated |
| 342 | + |
| 343 | + self.stats = { |
| 344 | + "relations": relation_stats, |
| 345 | + "total": _all, |
| 346 | + } |
| 347 | + |
| 348 | + return self.index() |
0 commit comments