From 721894f072d583782aa39de349acab8e8fe7e494 Mon Sep 17 00:00:00 2001 From: Tudor Amariei Date: Thu, 18 Dec 2025 13:35:52 +0200 Subject: [PATCH] feat: implement add clipboard functionality for cause URL --- backend/donations/views/ngo_account/causes.py | 23 ++++++++++---- .../views/ngo_account/my_organization.py | 7 +++-- .../copy-to-clipboard/icon_and_text.html | 31 +++++++++++++++++++ .../v2/ngo-account/components/form-title.html | 6 ++-- 4 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 backend/templates/v2/components/copy-to-clipboard/icon_and_text.html diff --git a/backend/donations/views/ngo_account/causes.py b/backend/donations/views/ngo_account/causes.py index 4b726b512..cc4866ba8 100644 --- a/backend/donations/views/ngo_account/causes.py +++ b/backend/donations/views/ngo_account/causes.py @@ -30,12 +30,23 @@ def get_context_data(self, **kwargs): ngo: Ngo = context["ngo"] + cause: Cause | None = self.get_cause(ngo=ngo, cause_id=kwargs.get("cause_id")) + context["cause"] = cause + if cause: + context["absolute_cause_url"] = self.get_absolute_cause_url(cause_slug=cause.slug) + context["info_banner_items"] = self.get_ngo_cause_banner_list_items(ngo) context["visibility_choices"] = CauseVisibilityChoices.as_str_pretty() context["ngo_has_tax_account"] = ngo.has_online_tax_account return context + def get_absolute_cause_url(self, cause_slug: str) -> str: + return self.request.build_absolute_uri(reverse("twopercent", kwargs={"cause_slug": cause_slug})) + + def get_cause(self, *, ngo: Ngo, cause_id: int | None) -> Cause | None: + raise NotImplementedError + def get_ngo_cause_banner_list_items(self, ngo: Ngo) -> list[str]: banner_list_items = [ _("Organization name: ") + ngo.name, @@ -176,6 +187,9 @@ def get_context_data(self, **kwargs): return context + def get_cause(self, *, ngo: Ngo, cause_id: int | None) -> Cause | None: + return None + @method_decorator(login_required(login_url=reverse_lazy("login"))) def get(self, request, *args, **kwargs): user: User = request.user @@ -208,7 +222,7 @@ def get_context_data(self, **kwargs): page_title = _("Edit cause") - context["cause"] = self.get_cause(cause_id=kwargs["cause_id"], ngo=context["ngo"]) + context["cause"] = self.get_cause(ngo=context["ngo"], cause_id=kwargs["cause_id"]) context["django_form"] = CauseForm(instance=context["cause"], for_main_cause=self.is_main_cause) context["page_title"] = f'{page_title}: "{context["cause"].name}"' @@ -224,11 +238,8 @@ def get_context_data(self, **kwargs): return context - def get_cause(self, cause_id: int, ngo: Ngo) -> Cause: - if not ngo: - raise Http404 - - if not cause_id: + def get_cause(self, *, ngo: Ngo, cause_id: int | None) -> Cause | None: + if not (ngo and cause_id): raise Http404 cause = Cause.objects.filter(pk=cause_id, ngo=ngo).first() diff --git a/backend/donations/views/ngo_account/my_organization.py b/backend/donations/views/ngo_account/my_organization.py index b1b0e3c5e..646a77988 100644 --- a/backend/donations/views/ngo_account/my_organization.py +++ b/backend/donations/views/ngo_account/my_organization.py @@ -2,6 +2,7 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.core.exceptions import PermissionDenied +from django.http import Http404 from django.shortcuts import redirect, render from django.urls import reverse, reverse_lazy from django.utils.decorators import method_decorator @@ -164,7 +165,6 @@ def __init__(self, *args, **kwargs): def get_context_data(self, **kwargs): context = super().get_context_data(is_main_cause=True, **kwargs) - context["cause"] = self.get_main_cause(context.get("ngo")) context["is_main_cause"] = self.is_main_cause context["django_form"] = CauseForm(instance=context["cause"], for_main_cause=self.is_main_cause) @@ -179,7 +179,10 @@ def get_context_data(self, **kwargs): context["active_tab"] = self.tab_title return context - def get_main_cause(self, ngo: Ngo) -> Cause: + def get_cause(self, *, ngo: Ngo, cause_id: int | None) -> Cause | None: + if not ngo: + raise Http404 + return Cause.objects.filter(ngo=ngo, is_main=True).first() @method_decorator(login_required(login_url=reverse_lazy("login"))) diff --git a/backend/templates/v2/components/copy-to-clipboard/icon_and_text.html b/backend/templates/v2/components/copy-to-clipboard/icon_and_text.html new file mode 100644 index 000000000..8db56f1ab --- /dev/null +++ b/backend/templates/v2/components/copy-to-clipboard/icon_and_text.html @@ -0,0 +1,31 @@ +{% extends "components/copy-to-clipboard/base.html" %} + +{% load i18n %} + + +{% block clickable_content %} + + + + {% if icon_path %} + + {% else %} + + + {% endif %} + + + {% if button_text %} + {{ button_text }} + {% else %} + {{ block.super }} + {% endif %} + + +{% endblock %} diff --git a/backend/templates/v2/ngo-account/components/form-title.html b/backend/templates/v2/ngo-account/components/form-title.html index 0310e8466..dcdea0bd9 100644 --- a/backend/templates/v2/ngo-account/components/form-title.html +++ b/backend/templates/v2/ngo-account/components/form-title.html @@ -8,6 +8,8 @@

{% if not hide_public_page_button and ngo.can_create_causes and cause %} + {% include "components/copy-to-clipboard/icon_and_text.html" with clipboard_input=absolute_cause_url div_classes="px-3 py-2 text-sm font-semibold text-gray-700 no-underline border-2 rounded-md border-gray-300 bg-white hover:bg-gray-200 hover:cursor-pointer" %} + + d="M13.5 6H5.25A2.25 2.25 0 0 0 3 8.25v10.5A2.25 2.25 0 0 0 5.25 21h10.5A2.25 2.25 0 0 0 18 18.75V10.5m-10.5 6L21 3m0 0h-5.25M21 3v5.25"/> @@ -57,7 +59,7 @@

+ d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5M16.5 12 12 16.5m0 0L7.5 12m4.5 4.5V3"/>