|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from flask import Blueprint |
| 4 | + |
| 5 | +import ckan.plugins.toolkit as tk |
| 6 | +from ckan.common import current_user |
| 7 | + |
| 8 | +__all__ = ["bp"] |
| 9 | + |
| 10 | +bp = Blueprint("idb_theme", __name__, url_prefix="/idb-theme") |
| 11 | + |
| 12 | + |
| 13 | +# Temporary hotfix for https://github.com/ckan/ckan/pull/9394. |
| 14 | +def _follow_group(id: str, *, is_organization: bool, follow: bool) -> str: |
| 15 | + group_type = "organization" if is_organization else "group" |
| 16 | + data_dict = { |
| 17 | + "id": id, |
| 18 | + "include_datasets": True, |
| 19 | + "include_users": False, |
| 20 | + } |
| 21 | + if not follow: |
| 22 | + data_dict["include_followers"] = True |
| 23 | + |
| 24 | + extra_vars = { |
| 25 | + "current_user": current_user, |
| 26 | + "show_nums": True, |
| 27 | + } |
| 28 | + |
| 29 | + try: |
| 30 | + action = "organization_show" if is_organization else "group_show" |
| 31 | + group_dict = tk.get_action(action)({}, data_dict) |
| 32 | + except (tk.ObjectNotFound, tk.NotAuthorized): |
| 33 | + msg = tk._(f"{group_type} not found or you have no permission to view it") |
| 34 | + tk.abort(404, msg) |
| 35 | + |
| 36 | + error_message = "" |
| 37 | + try: |
| 38 | + if follow: |
| 39 | + tk.get_action("follow_group")({}, {"id": id}) |
| 40 | + extra_vars["am_following"] = True |
| 41 | + else: |
| 42 | + tk.get_action("unfollow_group")({}, {"id": id}) |
| 43 | + extra_vars["am_following"] = False |
| 44 | + except tk.ValidationError as error: |
| 45 | + error_message = error.error_summary |
| 46 | + |
| 47 | + extra_vars["error_message"] = error_message |
| 48 | + |
| 49 | + if is_organization: |
| 50 | + extra_vars["organization"] = group_dict |
| 51 | + return tk.render("organization/snippets/info.html", extra_vars) |
| 52 | + |
| 53 | + extra_vars["group"] = group_dict |
| 54 | + return tk.render("group/snippets/info.html", extra_vars) |
| 55 | + |
| 56 | + |
| 57 | +@bp.post("/group/follow/<id>") |
| 58 | +def group_follow(id: str) -> str: |
| 59 | + return _follow_group(id, is_organization=False, follow=True) |
| 60 | + |
| 61 | + |
| 62 | +@bp.post("/group/unfollow/<id>") |
| 63 | +def group_unfollow(id: str) -> str: |
| 64 | + return _follow_group(id, is_organization=False, follow=False) |
| 65 | + |
| 66 | + |
| 67 | +@bp.post("/organization/follow/<id>") |
| 68 | +def organization_follow(id: str) -> str: |
| 69 | + return _follow_group(id, is_organization=True, follow=True) |
| 70 | + |
| 71 | + |
| 72 | +@bp.post("/organization/unfollow/<id>") |
| 73 | +def organization_unfollow(id: str) -> str: |
| 74 | + return _follow_group(id, is_organization=True, follow=False) |
0 commit comments