|
10 | 10 | import ckan.plugins.toolkit as toolkit |
11 | 11 | import ckan.model as model |
12 | 12 |
|
13 | | -from ckanext.toolbelt.decorators import Collector |
| 13 | +from ckanext.toolbelt.decorators import Collector, Cache |
14 | 14 |
|
15 | 15 | from ckanext.datavic_odp_theme import config as conf, const |
16 | 16 |
|
|
20 | 20 |
|
21 | 21 |
|
22 | 22 | @helper |
23 | | -def organization_list(): |
24 | | - org_list = toolkit.get_action("organization_list")({}, {}) |
25 | | - organizations = [] |
26 | | - for org in org_list: |
27 | | - org_dict = toolkit.get_action("organization_show")({}, {"id": org}) |
28 | | - organizations.append(org_dict) |
29 | | - |
30 | | - return organizations |
| 23 | +@Cache() |
| 24 | +def group_list(is_organization: bool) -> list[dict[str, str]]: |
| 25 | + """Returns a list of active groups or organizations. |
31 | 26 |
|
| 27 | + Results are cached separately for groups and organizations. |
| 28 | + Cache is invalidated on create, update, or delete actions |
| 29 | + (see `clear_group_list_cache` in plugins.py). |
| 30 | + """ |
| 31 | + groups = ( |
| 32 | + model.Session.query(model.Group.id, model.Group.title, model.Group.name) |
| 33 | + .filter(model.Group.is_organization.is_(is_organization)) |
| 34 | + .filter(model.Group.state == "active") |
| 35 | + .order_by(func.coalesce(model.Group.title, model.Group.name).asc()) |
| 36 | + .all() |
| 37 | + ) |
32 | 38 |
|
33 | | -@helper |
34 | | -def group_list(): |
35 | | - return toolkit.get_action("group_list")({}, {"all_fields": True}) |
| 39 | + return [ |
| 40 | + {"id": g.id, "display_name": g.title or g.name, "name": g.name} for g in groups |
| 41 | + ] |
36 | 42 |
|
37 | 43 |
|
38 | 44 | @helper |
39 | 45 | def format_list() -> list[str]: |
40 | | - """Return a list of all available resources on portal""" |
| 46 | + """Return a sorted list of unique resource formats.""" |
| 47 | + |
| 48 | + # Clean format: lower-case and trimmed |
| 49 | + cleaned_format = func.lower(func.trim(model.Resource.format)) |
41 | 50 |
|
42 | 51 | query = ( |
43 | | - model.Session.query(model.Resource.format) |
| 52 | + model.Session.query(func.distinct(cleaned_format)) |
44 | 53 | .filter(model.Resource.state == model.State.ACTIVE) |
45 | | - .group_by(model.Resource.format) |
46 | | - .order_by(func.lower(model.Resource.format)) |
| 54 | + .filter(model.Resource.format.isnot(None)) |
| 55 | + .filter(model.Resource.format != "") |
47 | 56 | ) |
48 | | - |
49 | | - formats = [ |
50 | | - resource.format.upper().split(".")[-1] for resource in query if resource.format |
51 | | - ] |
52 | | - unique_formats = set(formats) |
53 | | - |
54 | | - return sorted(list(unique_formats)) |
| 57 | + return sorted({fmt.upper().split(".")[-1] for (fmt,) in query}) |
55 | 58 |
|
56 | 59 |
|
57 | 60 | @helper |
@@ -136,19 +139,14 @@ def featured_resource_preview(package: dict[str, Any]) -> Optional[dict[str, Any |
136 | 139 |
|
137 | 140 |
|
138 | 141 | @helper |
139 | | -def get_digital_twin_resources(pkg_id: str) -> list[dict[str, Any]]: |
| 142 | +def get_digital_twin_resources(pkg: dict[str, Any]) -> list[dict[str, Any]]: |
140 | 143 | """Select resource suitable for DTV(Digital Twin Visualization). |
141 | 144 |
|
142 | 145 | Additional info: |
143 | 146 | https://gist.github.com/steve9164/b9781b517c99486624c02fdc7af0f186 |
144 | 147 | """ |
145 | 148 | supported_formats: set[str] = conf.get_dtv_supported_formats() |
146 | 149 |
|
147 | | - try: |
148 | | - pkg = toolkit.get_action("package_show")({}, {"id": pkg_id}) |
149 | | - except (toolkit.ObjectNotFound, toolkit.NotAuthorized): |
150 | | - return [] |
151 | | - |
152 | 150 | # Additional info #2 |
153 | 151 | if pkg["state"] != "active": |
154 | 152 | return [] |
@@ -307,9 +305,7 @@ def datavic_update_org_error_dict( |
307 | 305 | to show it as an error on the Logo field.""" |
308 | 306 | if error_dict.pop("upload", "") == ["File upload too large"]: |
309 | 307 | error_dict["Logo"] = [ |
310 | | - ( |
311 | | - f"File size is too large. Select an image which is no larger than {datavic_max_image_size()}MB." |
312 | | - ) |
| 308 | + f"File size is too large. Select an image which is no larger than {datavic_max_image_size()}MB." |
313 | 309 | ] |
314 | 310 | elif "Unsupported upload type" in error_dict.pop("image_upload", [""])[0]: |
315 | 311 | error_dict["Logo"] = [ |
|
0 commit comments