Skip to content

Commit

Permalink
feat: add in-app constance settings panel, use only sites in django a…
Browse files Browse the repository at this point in the history
…dmin for setting site url and name (#435)

* feat: add in-app settings panel using django-constance

* chore: run black

* fix: revert irrelevant changes to settings

* fix: add config to email contexts

* fix: remove unused import

* chore: run black

* fix: remove SITE_URL and SITENAME from constance

* fix: undo changes to template code

* fix: small corrections to template

* fix: additional corrections

* fix: correct whitespace

* fix: make for statement single line

* fix: revert irrelevant changes

* fix: typo

* fix: remove trailing space

* feat: move account IBAN to constance

* chore: run black formatter

* chore: run black formatter

* feat: add config accessor

* fix: fix imports

* fix: format

* fix: run formatter

* fix: indent

* fix: revert bad format

* fix: make changes non-breaking

* fix: formatting

* fix: comment

* fix: revert whitespace change

* feat: make use of constance for basic things

* fix: format

* Update banktransaction.html
  • Loading branch information
braaar authored Dec 4, 2023
1 parent 431c368 commit f9dd167
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 23 deletions.
3 changes: 2 additions & 1 deletion api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from rest_framework_tracking.models import APIRequestLog
from users.models import CustomUser, MemberService, NFCCard, ServiceSubscription
from api.mulysaoauthvalidator import MulysaOAuth2Validator
from django.contrib.sites.models import Site

class TestOAuthValidator(APITestCase):
def setUp(self):
Expand Down Expand Up @@ -226,7 +227,7 @@ def test_access_phone_notok(self, mock):
mail.outbox[0].body,
"first ss state",
)
self.assertIn(config.SITE_URL, mail.outbox[0].body, "siteurl")
self.assertIn(Site.objects.get_current().domain, mail.outbox[0].body, "siteurl")
self.assertEqual(response.status_code, 481)

@override_settings(
Expand Down
2 changes: 0 additions & 2 deletions drfx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@
"Link to the guide for new members",
str,
),
"SITENAME": (SITENAME, "Name of the hacklab", str),
"SITE_URL": (SITE_URL, "URL of this site", str),
"PRIVACY_POLICY_URL": (
PRIVACY_POLICY_URL,
"Link to privacy policy",
Expand Down
6 changes: 4 additions & 2 deletions emails/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.translation import gettext_lazy as _
from autoslug import AutoSlugField
from mailer import send_mail
from django.contrib.sites.models import Site

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -62,12 +63,13 @@ def queue_to_recipients(self, qs):
)
)

site = Site.objects.get_current()
context = {
"user": user,
"config": config,
"email": self,
"SITENAME": config.SITENAME,
"SITE_URL": config.SITE_URL,
"SITENAME": site.name,
"SITE_URL": site.domain,
}
subject = self.subject
from_email = config.NOREPLY_FROM_ADDRESS
Expand Down
3 changes: 2 additions & 1 deletion nordigenautomation/jobs/notify_expiring_requisitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from drfx import config
from ..models import Requisition
from django.contrib.sites.models import Site


class Job(DailyJob):
Expand All @@ -17,7 +18,7 @@ def execute(self):
for r in Requisition.active.filter(valid_until__lte=in_fourteen_days):
print("sending alert")
send_mail(
f"[{config.SITENAME}] Requisition about to expire",
f"[{Site.objects.get_current().name}] Requisition about to expire",
f"Please update the requisition for config: {r.config.id}. It is valid until: {r.valid_until}",
config.NOREPLY_FROM_ADDRESS,
[config.MEMBERSHIP_APPLICATION_NOTIFY_ADDRESS],
Expand Down
12 changes: 6 additions & 6 deletions users/locale/fi/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,14 @@ msgstr ""
#~ msgid "BIC"
#~ msgstr "BIC"

#~ msgid "Best regards %(config.SITENAME)s hackers!"
#~ msgstr "Terveisin %(config.SITENAME)s!"
#~ msgid "Best regards %(site.name)s hackers!"
#~ msgstr "Terveisin %(site.name)s!"

#~ msgid "Your application to %(config.SITENAME)s has been rejected."
#~ msgstr "Hakemuksesi %(config.SITENAME)s:n on hylätty"
#~ msgid "Your application to %(site.name)s has been rejected."
#~ msgstr "Hakemuksesi %(site.name)s:n on hylätty"

#~ msgid "New membership application in %(config.SITENAME)s"
#~ msgstr "Uusi jäsenhakemus vastaanotettu %(config.SITENAME)s:n"
#~ msgid "New membership application in %(site.name)s"
#~ msgstr "Uusi jäsenhakemus vastaanotettu %(site.name)s:n"

#~ msgid "Format: DD.MM.YYYY"
#~ msgstr "Muoto: PP.KK.VVVV"
Expand Down
15 changes: 14 additions & 1 deletion users/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from utils import referencenumber
from . import models

from django.contrib.sites.models import Site

logger = logging.getLogger(__name__)

#
Expand Down Expand Up @@ -141,6 +143,7 @@ def send_application_received_email(
context = {
"user": instance.user,
"config": config,
"site": Site.objects.get_current(),
}
translation.activate(instance.user.language)
# TODO: maybe move this subject to settings?
Expand All @@ -164,6 +167,7 @@ def send_new_application_waiting_processing_email(
)
context = {
"user": instance.user,
"site": Site.objects.get_current(),
"config": config,
}
subject = _("New membership application received")
Expand All @@ -183,7 +187,13 @@ def send_application_approved_email(
instance, instance.user.language
)
)
context = {"user": instance.user, "config": config}

context = {
"user": instance.user,
"config": config,
"site": Site.objects.get_current(),
}

translation.activate(instance.user.language)
# TODO: maybe move this subject to settings?
subject = _("Your application has been approved")
Expand All @@ -199,7 +209,9 @@ def send_application_denied_email(
sender, instance: models.MembershipApplication, **kwargs
):
logger.info("Application denied, sending bye bye email {}".format(instance))

context = {"user": instance.user, "config": config}

translation.activate(instance.user.language)
# TODO: maybe move this subject to settings?
subject = _("Your application has been rejected")
Expand Down Expand Up @@ -257,6 +269,7 @@ def notify_user_door_access_denied(sender, user: models.CustomUser, method, **kw
"user": user,
"method": method,
"config": config,
"site": Site.objects.get_current(),
}
translation.activate(user.language)
subject = _("Door access denied")
Expand Down
2 changes: 1 addition & 1 deletion users/templates/mail/application_received.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'mail/email_base.txt' %}
{% load i18n %}
{% block content %}
{% blocktrans with siteurl=config.SITE_URL wikiurl=config.MEMBERS_GUIDE_URL %}
{% blocktrans with siteurl=site.domain wikiurl=config.MEMBERS_GUIDE_URL %}
Thank you for applying for membership!

We will check your application and get back to you after your application has
Expand Down
2 changes: 1 addition & 1 deletion users/templates/mail/door_access_denied.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'mail/email_base.txt' %}
{% load i18n %}
{% block content %}
{% blocktrans with first_name=user.first_name method=method siteurl=config.SITE_URL wikiurl=config.MEMBERS_GUIDE_URL %}
{% blocktrans with first_name=user.first_name method=method siteurl=site.domain wikiurl=config.MEMBERS_GUIDE_URL %}
Hi {{first_name}}!

We noticed that you tried to access the door by using {{method}} to open it.
Expand Down
2 changes: 1 addition & 1 deletion users/templates/mail/new_application.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{user.first_name}} {{user.last_name}}

{# TODO: the url should go to the correct application (Or maybe even to accept reject directly ? #}
{{config.SITE_URL}}{%url 'applications' %}
{{site.domain}}{%url 'applications' %}

{# TODO: show rest of the application information here #}
{% endblock %}
2 changes: 1 addition & 1 deletion users/templates/mail/welcome_and_next_steps.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'mail/email_base.txt' %}
{% load i18n %}
{% block content %}
{% blocktrans with first_name=user.first_name siteurl=config.SITE_URL wikiurl=config.MEMBERS_GUIDE_URL %}
{% blocktrans with first_name=user.first_name siteurl=site.domain wikiurl=config.MEMBERS_GUIDE_URL %}
Welcome aboard {{first_name}}!

Great to have you onboard! Your membership application has been
Expand Down
8 changes: 5 additions & 3 deletions users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from utils.businesslogic import BusinessLogic

from . import models, signals
from django.contrib.sites.models import Site
from drfx import config


Expand Down Expand Up @@ -212,6 +213,7 @@ def setUp(self):
def test_emails(self):
mail.outbox = []

site = Site.objects.get_current()
# create new application for our user
self.application = models.MembershipApplication.objects.create(
user=self.user, agreement=True
Expand All @@ -220,12 +222,12 @@ def test_emails(self):
len(mail.outbox), 2
) # because this sends one email to the member and one to admins
self.assertIn("Kiitos jäsenhakemuksestasi", mail.outbox[0].body, "Thanks")
self.assertIn(config.SITE_URL, mail.outbox[0].body, "siteurl")
self.assertIn(site.domain, mail.outbox[0].body, "siteurl")
self.assertIn(config.MEMBERS_GUIDE_URL, mail.outbox[0].body, "wikiurl")

# for completenes sake, check the admin email also
self.assertIn("FirstName LastName", mail.outbox[1].body, "Admin notification")
self.assertIn(config.SITE_URL, mail.outbox[1].body, "admin url")
self.assertIn(site.domain, mail.outbox[1].body, "admin url")

# empty mailbox for next test
mail.outbox = []
Expand All @@ -247,7 +249,7 @@ def test_emails(self):
mail.outbox[0].body,
"phone number found",
)
self.assertIn(config.SITE_URL, mail.outbox[0].body, "url")
self.assertIn(site.domain, mail.outbox[0].body, "url")
self.assertIn(config.MEMBERS_GUIDE_URL, mail.outbox[0].body, "wikiurl")


Expand Down
6 changes: 4 additions & 2 deletions www/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.contrib.sites.models import Site
from drfx import config

def external_urls(request):
site = Site.objects.get_current()
return {
"ASSOCIATION_RULES_URL": config.ASSOCIATION_RULES_URL,
"MEMBERS_GUIDE_URL": config.MEMBERS_GUIDE_URL,
"SITENAME": site.name,
"SITE_URL": site.domain,
"GITHUB_URL": config.GITHUB_URL,
"SITENAME": config.SITENAME,
"SITE_URL": config.SITE_URL,
"PRIVACY_POLICY_URL": config.PRIVACY_POLICY_URL,
}
2 changes: 1 addition & 1 deletion www/templates/www/banktransaction.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h2>{% trans 'Bank Transaction' %}</h2>

<div id="promoinfo">
<p>
{% blocktrans with name=config.SITENAME %}
{% blocktrans with name=site.name %}
Thank you and please tell your friends about {{ name }} :)
{% endblocktrans %}
</p>
Expand Down

0 comments on commit f9dd167

Please sign in to comment.