Skip to content

Commit

Permalink
fix: templates saving + fix date format (#514)
Browse files Browse the repository at this point in the history
Signed-off-by: Trey <73353716+TreyWW@users.noreply.github.com>
  • Loading branch information
TreyWW committed Oct 14, 2024
1 parent 4ac95a5 commit 8e98090
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
4 changes: 2 additions & 2 deletions backend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
FileStorageFile,
MultiFileUpload,
)

from backend.finance.models import (
Invoice,
InvoiceURL,
Expand All @@ -36,7 +35,7 @@
ReceiptDownloadToken,
)

from backend.clients.models import Client
from backend.clients.models import Client, DefaultValues

from settings.settings import BILLING_ENABLED

Expand Down Expand Up @@ -68,6 +67,7 @@
InvoiceRecurringProfile,
FileStorageFile,
MultiFileUpload,
DefaultValues,
]
)

Expand Down
3 changes: 3 additions & 0 deletions backend/core/api/public/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"team_permissions:write",
"team:invite",
"team:kick",
"account_defaults:write",
"email_templates:read",
"email_templates:write",
}
Expand All @@ -40,6 +41,7 @@
"team:kick": {"team:kick", "team:invite"},
"email_templates:read": {"email_templates:read"},
"email_templates:write": {"email_templates:read", "email_templates:write"},
"account_defaults:write": {"account_defaults:write"},
}

SCOPE_DESCRIPTIONS = {
Expand All @@ -50,6 +52,7 @@
"team_permissions": {"description": "Access team permissions", "options": {"read": "Read only", "write": "Read and write"}},
"team": {"description": "Invite team members", "options": {"invite": "Invite members"}},
"email_templates": {"description": "Access email templates", "options": {"read": "Read only", "write": "Read and write"}},
"account_defaults": {"description": "Modify account defaults", "options": {"write": "Read and write"}},
}

if settings.BILLING_ENABLED:
Expand Down
31 changes: 27 additions & 4 deletions backend/core/api/settings/email_templates.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
from django.views.decorators.http import require_GET
from django.contrib import messages
from django.shortcuts import render
from django.views.decorators.http import require_POST

from backend.core.service.defaults.get import get_account_defaults
from backend.decorators import web_require_scopes
from backend.core.types.requests import WebRequest


@require_GET
@web_require_scopes("email_templates:read")
def get_current_email_template(request: WebRequest): ...
@require_POST
@web_require_scopes(["email_templates:write", "account_defaults:write"], True, True)
def save_email_template(request: WebRequest, template: str):
template = template.lower().strip()
content = request.POST.get("content")

if template not in ["invoice_created", "invoice_overdue", "invoice_cancelled"]:
messages.error(request, f"Invalid template: {template}")
return render(request, "base/toast.html")

if content is None:
messages.error(request, f"Missing content for template: {template}")
return render(request, "base/toast.html")

acc_defaults = get_account_defaults(request.actor)

setattr(acc_defaults, f"email_template_recurring_invoices_{template}", content)

acc_defaults.save()

messages.success(request, f"Email template '{template}' saved successfully")

return render(request, "base/toast.html")
2 changes: 2 additions & 0 deletions backend/core/api/settings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from . import change_name, profile_picture, preferences
from .api_keys import generate_api_key_endpoint, revoke_api_key_endpoint
from .defaults import handle_client_defaults_endpoints, remove_client_default_logo_endpoint
from .email_templates import save_email_template

urlpatterns = [
path(
Expand All @@ -22,6 +23,7 @@
path("client_defaults/", handle_client_defaults_endpoints, name="client_defaults without client"),
path("client_defaults/remove_default_logo/", remove_client_default_logo_endpoint, name="client_defaults remove logo without client"),
path("client_defaults/remove_default_logo/<int:client_id>", remove_client_default_logo_endpoint, name="client_defaults remove logo"),
path("email_templates/<str:template>/save/", save_email_template, name="email_template save"),
]

app_name = "settings"
2 changes: 1 addition & 1 deletion backend/core/service/invoices/common/emails/on_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def on_create_invoice_email_service(users_email: str, invoice: Invoice) -> OnCre
"first_name": invoice.client_to.name.split(" ")[0] if invoice.client_to else invoice.client_name,
"invoice_id": invoice.id,
"invoice_ref": invoice.reference or invoice.invoice_number or invoice.id,
"due_date": invoice.date_due.strftime("%a %m %Y"),
"due_date": invoice.date_due.strftime("%A, %B %d, %Y"),
"amount_due": invoice.get_total_price(),
"currency": invoice.currency,
"currency_symbol": invoice.get_currency_symbol(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
</li>
</ul>
{% for template in 'invoice_created,invoice_overdue,invoice_cancelled'|split:"," %}
<div class="m-0 flex grow flex-col gap-y-2"
<form class="m-0 flex grow flex-col gap-y-2"
hx-swap="none"
hx-post="{% url 'api:settings:email_template save' template=template %}"
:class="{ 'hidden': activeTab !== '{{ template }}'}">
<p>{{ template|split:"_"|join:" "|title }}</p>
{% spaceless %}
<textarea class="m-0 h-full w-full textarea textarea-block textarea-bordered">
{{ email_templates.recurring_invoices|dict_get:template }}
</textarea>
{% endspaceless %}
{% with text=email_templates.recurring_invoices|dict_get:template %}
<textarea class="m-0 h-full w-full textarea textarea-bordered" name="content">{{ text }}</textarea>
{% endwith %}
<button class="w-fit self-end px-4 btn btn-sm btn-primary">Save</button>
</div>
</form>
{% endfor %}
</div>
</div>
Expand Down

0 comments on commit 8e98090

Please sign in to comment.