Skip to content

Commit

Permalink
Merge pull request #49 from maykinmedia/feature/update-code
Browse files Browse the repository at this point in the history
Applied black, flake8 and isort, plus some code updates
  • Loading branch information
Bartvaderkin authored Feb 28, 2024
2 parents ec8a84a + e409ac0 commit 2730270
Show file tree
Hide file tree
Showing 30 changed files with 712 additions and 431 deletions.
9 changes: 9 additions & 0 deletions fix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

isort --profile black ./mail_editor
autoflake --in-place --remove-all-unused-imports -r ./mail_editor
black ./mail_editor

isort --profile black ./tests
autoflake --in-place --remove-all-unused-imports -r ./tests
black ./tests
121 changes: 71 additions & 50 deletions mail_editor/admin.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
from django.conf import settings as django_settings
from django.urls import re_path
from django.contrib import admin, messages
from django.urls import path, reverse
from django.urls import re_path, reverse
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _

from .forms import MailTemplateForm
from .models import MailTemplate
from .settings import settings
from .views import TemplateBrowserPreviewView, TemplateEmailPreviewFormView, TemplateVariableView
from .views import (
TemplateBrowserPreviewView,
TemplateEmailPreviewFormView,
TemplateVariableView,
)


@admin.register(MailTemplate)
class MailTemplateAdmin(admin.ModelAdmin):
list_display = (
'template_type',
'internal_name',
'language',
'get_preview_link',
'get_description',
'subject',
'base_template_path',
"template_type",
"internal_name",
"language",
"get_preview_link",
"get_description",
"subject",
"base_template_path",
)
list_filter = (
'template_type',
'language',
'internal_name',
"template_type",
"language",
"internal_name",
)
readonly_fields = (
'get_variable_help_text',
'get_preview_link',
"get_variable_help_text",
"get_preview_link",
)
search_fields = (
'internal_name',
'template_type',
'subject',
"internal_name",
"template_type",
"subject",
)
actions = [
"reload_templates",
Expand All @@ -44,23 +47,29 @@ class MailTemplateAdmin(admin.ModelAdmin):

def get_fieldsets(self, request, obj=None):
fieldset = [
(None, {
'fields': [
'internal_name',
'template_type',
'language',
'get_preview_link',
'subject',
'body',
'base_template_path',
],
}),
(_('Help'), {
'fields': [
'get_variable_help_text',
'remarks',
],
}),
(
None,
{
"fields": [
"internal_name",
"template_type",
"language",
"get_preview_link",
"subject",
"body",
"base_template_path",
],
},
),
(
_("Help"),
{
"fields": [
"get_variable_help_text",
"remarks",
],
},
),
]
return fieldset

Expand All @@ -80,15 +89,21 @@ def get_preview_url(self, obj=None):
def get_urls(self):
# reminder: when using admin templates also add self.admin_site.each_context(request)
return [
re_path(r'^variables/(?P<template_type>[-\w]+)/$',
re_path(
r"^variables/(?P<template_type>[-\w]+)/$",
self.admin_site.admin_view(TemplateVariableView.as_view()),
name='mailtemplate_variables'),
re_path(r'^preview/(?P<pk>[0-9]+)/$',
name="mailtemplate_variables",
),
re_path(
r"^preview/(?P<pk>[0-9]+)/$",
self.admin_site.admin_view(TemplateBrowserPreviewView.as_view()),
name='mailtemplate_render'),
re_path(r'^email/(?P<pk>[0-9]+)/$',
name="mailtemplate_render",
),
re_path(
r"^email/(?P<pk>[0-9]+)/$",
self.admin_site.admin_view(TemplateEmailPreviewFormView.as_view()),
name='mailtemplate_preview'),
name="mailtemplate_preview",
),
] + super().get_urls()

def formfield_for_dbfield(self, db_field, request, **kwargs):
Expand All @@ -100,27 +115,33 @@ def formfield_for_dbfield(self, db_field, request, **kwargs):

def get_type_display(self, obj):
conf = settings.TEMPLATES.get(obj.template_type)
return conf.get('name')
return conf.get("name")

get_type_display.short_description = _('Template Type')
get_type_display.short_description = _("Template Type")

def get_description(self, obj):
conf = settings.TEMPLATES.get(obj.template_type)
return conf.get('description')
return conf.get("description")

get_description.short_description = _('Type Description')
get_description.short_description = _("Type Description")

def get_variable_help_text(self, obj):
if not obj.template_type:
return _('Please save the template to load the variables')
return _("Please save the template to load the variables")
return obj.get_variable_help_text()

get_variable_help_text.short_description = _('Subject variables')
get_variable_help_text.short_description = _("Subject variables")

def reload_templates(self, request, queryset):
for template in queryset:
template.reload_template()
template.save()
self.message_user(request, _("Template '{name}' is reset").format(name=template.template_type), level=messages.SUCCESS)

reload_templates.short_description = _('Reset templates (WARNING: overwrites current content)')
self.message_user(
request,
_("Template '{name}' is reset").format(name=template.template_type),
level=messages.SUCCESS,
)

reload_templates.short_description = _(
"Reset templates (WARNING: overwrites current content)"
)
14 changes: 7 additions & 7 deletions mail_editor/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django import forms
from django.apps import apps
from django.contrib.sites.shortcuts import get_current_site
from django.template import loader

Expand All @@ -12,22 +11,23 @@
class MailTemplateForm(forms.ModelForm):
class Meta:
model = MailTemplate
fields = ('template_type', 'remarks', 'subject', 'body')
fields = ("template_type", "remarks", "subject", "body")
widgets = {
'body': CKEditorWidget(config_name='mail_editor'),
'template_type': forms.Select(choices=[])
"body": CKEditorWidget(config_name="mail_editor"),
"template_type": forms.Select(choices=[]),
}

def __init__(self, *args, **kwargs):
super(MailTemplateForm, self).__init__(*args, **kwargs)

self.fields["template_type"].widget.choices = get_choices()

template = loader.get_template('mail/_outer_table.html')
template = loader.get_template("mail/_outer_table.html")
# TODO: This only works when sites-framework is installed.
try:
current_site = get_current_site(None)
domain = current_site.domain
except Exception as e:
domain = ''
self.fields['body'].initial = template.render({'domain': domain}, None)
domain = ""

self.fields["body"].initial = template.render({"domain": domain}, None)
41 changes: 25 additions & 16 deletions mail_editor/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from django.template import loader
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from .settings import settings

from .models import MailTemplate
from .settings import settings

try:
from django.template.exceptions import TemplateDoesNotExist, TemplateSyntaxError
Expand All @@ -18,13 +19,19 @@

def find_template(template_name, language=None):
if language:
template, _created = MailTemplate.objects.get_or_create(template_type=template_name, language=language, defaults={
'subject': get_subject(template_name),
'body': get_body(template_name),
'base_template_path': get_base_template_path(template_name),
})
template, _created = MailTemplate.objects.get_or_create(
template_type=template_name,
language=language,
defaults={
"subject": get_subject(template_name),
"body": get_body(template_name),
"base_template_path": get_base_template_path(template_name),
},
)
else:
base_qs = MailTemplate.objects.filter(template_type=template_name, language__isnull=True)
base_qs = MailTemplate.objects.filter(
template_type=template_name, language__isnull=True
)
if base_qs.exists():
template = base_qs.first()
else:
Expand All @@ -43,39 +50,41 @@ def get_subject(template_name):

template_config = config.get(template_name)
if template_config:
subject = template_config.get('subject_default')
subject = template_config.get("subject_default")
if subject:
return subject

return _('Please fix this template')
return _("Please fix this template")


def get_body(template_name):
config = settings.TEMPLATES

template_config = config.get(template_name)
default = _('Your content here...')
default = _("Your content here...")
if template_config:
body = template_config.get('body_default')
body = template_config.get("body_default")
if body:
default = body

template = loader.get_template('mail/_outer_table.html')
template = loader.get_template("mail/_outer_table.html")
current_site = get_current_site(None)
return template.render({'domain': current_site.domain, 'default': mark_safe(default)}, None)
return template.render(
{"domain": current_site.domain, "default": mark_safe(default)}, None
)


def get_base_template_path(template_name):
config = settings.TEMPLATES

template_config = config.get(template_name)
if template_config:
return template_config.get('base_template', '')
return ''
return template_config.get("base_template", "")
return ""


def base_template_loader(template_path, context):
default_path = 'mail/_base.html'
default_path = "mail/_base.html"

if not template_path:
template_path = default_path
Expand Down
Loading

0 comments on commit 2730270

Please sign in to comment.