From 9953124a95ed051e1d21734517f998e268459e2c Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Mon, 24 May 2021 11:19:30 +0200 Subject: [PATCH] Replace Context by dict Since Django 1.10 template objects returned by get_template() and select_template() no longer accept a Context in their render() method. --- orchestra/admin/forms.py | 28 ++++---- orchestra/contrib/bills/models.py | 6 +- orchestra/contrib/webapps/backends/php.py | 42 ++++++------ orchestra/contrib/websites/backends/apache.py | 64 +++++++++---------- orchestra/utils/mail.py | 1 - 5 files changed, 70 insertions(+), 71 deletions(-) diff --git a/orchestra/admin/forms.py b/orchestra/admin/forms.py index 236b057c..2490873f 100644 --- a/orchestra/admin/forms.py +++ b/orchestra/admin/forms.py @@ -5,7 +5,7 @@ from django import forms from django.contrib.admin import helpers from django.core import validators from django.forms.models import modelformset_factory, BaseModelFormSet -from django.template import Template, Context +from django.template import Template from django.utils.translation import ugettext_lazy as _ from orchestra.forms.widgets import SpanWidget @@ -28,9 +28,9 @@ class AdminFormMixin(object): ' {% include "admin/includes/fieldset.html" %}' '{% endfor %}' ) - context = Context({ + context = { 'adminform': adminform - }) + } return template.render(context) @@ -71,9 +71,9 @@ class AdminFormSet(BaseModelFormSet): """) ) - context = Context({ + context = { 'formset': self - }) + } return template.render(context) @@ -93,7 +93,7 @@ class AdminPasswordChangeForm(forms.Form): required=False, validators=[validate_password]) password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput, required=False) - + def __init__(self, user, *args, **kwargs): self.related = kwargs.pop('related', []) self.raw = kwargs.pop('raw', False) @@ -109,7 +109,7 @@ class AdminPasswordChangeForm(forms.Form): self.fields['password2_%i' % ix] = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput, required=False) setattr(self, 'clean_password2_%i' % ix, partial(self.clean_password2, ix=ix)) - + def clean_password2(self, ix=''): if ix != '': ix = '_%i' % ix @@ -129,7 +129,7 @@ class AdminPasswordChangeForm(forms.Form): code='password_mismatch', ) return password2 - + def clean_password(self, ix=''): if ix != '': ix = '_%i' % ix @@ -146,14 +146,14 @@ class AdminPasswordChangeForm(forms.Form): code='bad_hash', ) return password - + def clean(self): if not self.password_provided: raise forms.ValidationError( self.error_messages['password_missing'], code='password_missing', ) - + def save(self, commit=True): """ Saves the new password. @@ -182,7 +182,7 @@ class AdminPasswordChangeForm(forms.Form): if commit: rel.save(update_fields=['password']) return self.user - + def _get_changed_data(self): data = super().changed_data for name in self.fields.keys(): @@ -202,7 +202,7 @@ class SendEmailForm(forms.Form): widget=forms.TextInput(attrs={'size': '118'})) message = forms.CharField(label=_("Message"), widget=forms.Textarea(attrs={'cols': 118, 'rows': 15})) - + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) initial = kwargs.get('initial') @@ -210,7 +210,7 @@ class SendEmailForm(forms.Form): self.fields['to'].widget = SpanWidget(original=initial['to']) else: self.fields.pop('to') - + def clean_comma_separated_emails(self, value): clean_value = [] for email in value.split(','): @@ -222,7 +222,7 @@ class SendEmailForm(forms.Form): raise validators.ValidationError("Comma separated email addresses.") clean_value.append(email) return clean_value - + def clean_extra_to(self): extra_to = self.cleaned_data['extra_to'] return self.clean_comma_separated_emails(extra_to) diff --git a/orchestra/contrib/bills/models.py b/orchestra/contrib/bills/models.py index b39661a9..90765ca3 100644 --- a/orchestra/contrib/bills/models.py +++ b/orchestra/contrib/bills/models.py @@ -6,7 +6,7 @@ from django.core.validators import ValidationError, RegexValidator from django.db import models from django.db.models import F, Sum from django.db.models.functions import Coalesce -from django.template import loader, Context +from django.template import loader from django.utils import timezone, translation from django.utils.encoding import force_text from django.utils.functional import cached_property @@ -303,7 +303,7 @@ class Bill(models.Model): with translation.override(language or self.account.language): if payment is False: payment = self.account.paymentsources.get_default() - context = Context({ + context = { 'bill': self, 'lines': self.lines.all().prefetch_related('sublines'), 'seller': self.seller, @@ -318,7 +318,7 @@ class Bill(models.Model): 'payment': payment and payment.get_bill_context(), 'default_due_date': self.get_due_date(payment=payment), 'now': timezone.now(), - }) + } template_name = 'BILLS_%s_TEMPLATE' % self.get_type() template = getattr(settings, template_name, settings.BILLS_DEFAULT_TEMPLATE) bill_template = loader.get_template(template) diff --git a/orchestra/contrib/webapps/backends/php.py b/orchestra/contrib/webapps/backends/php.py index d0383beb..360f3eb1 100644 --- a/orchestra/contrib/webapps/backends/php.py +++ b/orchestra/contrib/webapps/backends/php.py @@ -2,7 +2,7 @@ import os import textwrap from collections import OrderedDict -from django.template import Template, Context +from django.template import Template from django.utils.translation import ugettext_lazy as _ from orchestra.contrib.orchestration import ServiceController @@ -17,7 +17,7 @@ class PHPController(WebAppServiceMixin, ServiceController): It handles switching between these two PHP process management systemes. """ MERGE = settings.WEBAPPS_MERGE_PHP_WEBAPPS - + verbose_name = _("PHP FPM/FCGID") default_route_match = "webapp.type.endswith('php')" doc_settings = (settings, ( @@ -30,7 +30,7 @@ class PHPController(WebAppServiceMixin, ServiceController): 'WEBAPPS_PHPFPM_POOL_PATH', 'WEBAPPS_PHP_MAX_REQUESTS', )) - + def save(self, webapp): self.delete_old_config(webapp) context = self.get_context(webapp) @@ -81,7 +81,7 @@ class PHPController(WebAppServiceMixin, ServiceController): } """) % context ) - + def save_fcgid(self, webapp, context): self.append("mkdir -p %(wrapper_dir)s" % context) self.append(textwrap.dedent(""" @@ -118,7 +118,7 @@ class PHPController(WebAppServiceMixin, ServiceController): ) else: self.append("rm -f %(cmd_options_path)s\n" % context) - + def delete(self, webapp): context = self.get_context(webapp) self.delete_old_config(webapp) @@ -127,13 +127,13 @@ class PHPController(WebAppServiceMixin, ServiceController): # elif webapp.type_instance.is_fcgid: # self.delete_fcgid(webapp, context) self.delete_webapp_dir(context) - + def has_sibilings(self, webapp, context): return type(webapp).objects.filter( account=webapp.account_id, data__contains='"php_version":"%s"' % context['php_version'], ).exclude(id=webapp.pk).exists() - + def all_versions_to_delete(self, webapp, context, preserve=False): context_copy = dict(context) for php_version, verbose in settings.WEBAPPS_PHP_VERSIONS: @@ -144,13 +144,13 @@ class PHPController(WebAppServiceMixin, ServiceController): context_copy['php_version_number'] = php_version_number if not self.MERGE or not self.has_sibilings(webapp, context_copy): yield context_copy - + def delete_fpm(self, webapp, context, preserve=False): """ delete all pools in order to efectively support changing php-fpm version """ for context_copy in self.all_versions_to_delete(webapp, context, preserve): context_copy['fpm_path'] = settings.WEBAPPS_PHPFPM_POOL_PATH % context_copy self.append("rm -f %(fpm_path)s" % context_copy) - + def delete_fcgid(self, webapp, context, preserve=False): """ delete all pools in order to efectively support changing php-fcgid version """ for context_copy in self.all_versions_to_delete(webapp, context, preserve): @@ -160,13 +160,13 @@ class PHPController(WebAppServiceMixin, ServiceController): }) self.append("rm -f %(wrapper_path)s" % context_copy) self.append("rm -f %(cmd_options_path)s" % context_copy) - + def prepare(self): super(PHPController, self).prepare() self.append(textwrap.dedent(""" BACKEND="PHPController" echo "$BACKEND" >> /dev/shm/reload.apache2 - + function coordinate_apache_reload () { # Coordinate Apache reload with other concurrent backends (e.g. Apache2Controller) is_last=0 @@ -203,7 +203,7 @@ class PHPController(WebAppServiceMixin, ServiceController): fi }""") ) - + def commit(self): context = { 'reload_pool': settings.WEBAPPS_PHPFPM_RELOAD_POOL, @@ -217,7 +217,7 @@ class PHPController(WebAppServiceMixin, ServiceController): """) % context ) super(PHPController, self).commit() - + def get_fpm_config(self, webapp, context): options = webapp.type_instance.get_options() context.update({ @@ -231,11 +231,11 @@ class PHPController(WebAppServiceMixin, ServiceController): [{{ user }}-{{app_name}}] user = {{ user }} group = {{ group }} - + listen = {{ fpm_listen | safe }} listen.owner = {{ user }} listen.group = {{ group }} - + pm = ondemand pm.max_requests = {{ max_requests }} pm.max_children = {{ max_children }} @@ -245,8 +245,8 @@ class PHPController(WebAppServiceMixin, ServiceController): php_admin_value[{{ name | safe }}] = {{ value | safe }}{% endfor %} """ )) - return fpm_config.render(Context(context)) - + return fpm_config.render(context) + def get_fcgid_wrapper(self, webapp, context): opt = webapp.type_instance # Format PHP init vars @@ -268,7 +268,7 @@ class PHPController(WebAppServiceMixin, ServiceController): export PHP_INI_SCAN_DIR=%(php_ini_scan)s export PHP_FCGI_MAX_REQUESTS=%(max_requests)s exec %(php_binary_path)s%(php_init_vars)s""") % context - + def get_fcgid_cmd_options(self, webapp, context): options = webapp.type_instance.get_options() maps = OrderedDict( @@ -288,7 +288,7 @@ class PHPController(WebAppServiceMixin, ServiceController): ) % context cmd_options.insert(0, head) return ' \\\n '.join(cmd_options) - + def update_fcgid_context(self, webapp, context): wrapper_path = settings.WEBAPPS_FCGID_WRAPPER_PATH % context context.update({ @@ -301,14 +301,14 @@ class PHPController(WebAppServiceMixin, ServiceController): 'cmd_options_path': settings.WEBAPPS_FCGID_CMD_OPTIONS_PATH % context, }) return context - + def update_fpm_context(self, webapp, context): context.update({ 'fpm_config': self.get_fpm_config(webapp, context), 'fpm_path': settings.WEBAPPS_PHPFPM_POOL_PATH % context, }) return context - + def get_context(self, webapp): context = super().get_context(webapp) context.update({ diff --git a/orchestra/contrib/websites/backends/apache.py b/orchestra/contrib/websites/backends/apache.py index 2cee8c4e..ad664eea 100644 --- a/orchestra/contrib/websites/backends/apache.py +++ b/orchestra/contrib/websites/backends/apache.py @@ -2,7 +2,7 @@ import os import re import textwrap -from django.template import Template, Context +from django.template import Template from django.utils.translation import ugettext_lazy as _ from orchestra.contrib.orchestration import ServiceController @@ -20,7 +20,7 @@ class Apache2Controller(ServiceController): """ HTTP_PORT = 80 HTTPS_PORT = 443 - + model = 'websites.Website' related_models = ( ('websites.Content', 'website'), @@ -37,7 +37,7 @@ class Apache2Controller(ServiceController): 'WEBSITES_DEFAULT_IPS', 'WEBSITES_SAAS_DIRECTIVES', )) - + def get_extra_conf(self, site, context, ssl=False): extra_conf = self.get_content_directives(site, context) directives = site.get_directives() @@ -53,7 +53,7 @@ class Apache2Controller(ServiceController): # Order extra conf directives based on directives (longer first) extra_conf = sorted(extra_conf, key=lambda a: len(a[0]), reverse=True) return '\n'.join([conf for location, conf in extra_conf]) - + def render_virtual_host(self, site, context, ssl=False): context.update({ 'port': self.HTTPS_PORT if ssl else self.HTTP_PORT, @@ -78,8 +78,8 @@ class Apache2Controller(ServiceController): {{ line | safe }}{% endfor %} """) - ).render(Context(context)) - + ).render(context) + def render_redirect_https(self, context): context['port'] = self.HTTP_PORT return Template(textwrap.dedent(""" @@ -96,8 +96,8 @@ class Apache2Controller(ServiceController): RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} """) - ).render(Context(context)) - + ).render(context) + def save(self, site): context = self.get_context(site) if context['server_name']: @@ -133,7 +133,7 @@ class Apache2Controller(ServiceController): [[ $(a2dissite %(site_unique_name)s) =~ "already disabled" ]] || UPDATED_APACHE=1\ """) % context ) - + def delete(self, site): context = self.get_context(site) self.append(textwrap.dedent(""" @@ -142,14 +142,14 @@ class Apache2Controller(ServiceController): rm -f %(sites_available)s\ """) % context ) - + def prepare(self): super(Apache2Controller, self).prepare() # Coordinate apache restart with php backend in order not to overdo it self.append(textwrap.dedent(""" BACKEND="Apache2Controller" echo "$BACKEND" >> /dev/shm/reload.apache2 - + function coordinate_apache_reload () { # Coordinate Apache reload with other concurrent backends (e.g. PHPController) is_last=0 @@ -186,12 +186,12 @@ class Apache2Controller(ServiceController): fi }""") ) - + def commit(self): """ reload Apache2 if necessary """ self.append("coordinate_apache_reload") super(Apache2Controller, self).commit() - + def get_directives(self, directive, context): method, args = directive[0], directive[1:] try: @@ -200,7 +200,7 @@ class Apache2Controller(ServiceController): context = (self.__class__.__name__, method) raise AttributeError("%s does not has suport for '%s' directive." % context) return method(context, *args) - + def get_content_directives(self, site, context): directives = [] for content in site.content_set.all(): @@ -208,19 +208,19 @@ class Apache2Controller(ServiceController): self.set_content_context(content, context) directives += self.get_directives(directive, context) return directives - + def get_static_directives(self, context, app_path): context['app_path'] = os.path.normpath(app_path % context) directive = self.get_location_filesystem_map(context) return [ (context['location'], directive), ] - + def get_location_filesystem_map(self, context): if not context['location']: return 'DocumentRoot %(app_path)s' % context return 'Alias %(location)s %(app_path)s' % context - + def get_fpm_directives(self, context, socket, app_path): if ':' in socket: # TCP socket @@ -243,7 +243,7 @@ class Apache2Controller(ServiceController): return [ (context['location'], directives), ] - + def get_fcgid_directives(self, context, app_path, wrapper_path): context.update({ 'app_path': os.path.normpath(app_path), @@ -274,7 +274,7 @@ class Apache2Controller(ServiceController): return [ (context['location'], directives), ] - + def get_uwsgi_directives(self, context, socket): # requires apache2 mod_proxy_uwsgi context['socket'] = socket @@ -283,7 +283,7 @@ class Apache2Controller(ServiceController): return [ (context['location'], directives), ] - + def get_ssl(self, directives): cert = directives.get('ssl-cert') key = directives.get('ssl-key') @@ -305,7 +305,7 @@ class Apache2Controller(ServiceController): return [ ('', '\n'.join(ssl_config)), ] - + def get_security(self, directives): rules = [] location = '/' @@ -329,7 +329,7 @@ class Apache2Controller(ServiceController): """) % '\n '.join(rules) security.append((location, rules)) return security - + def get_redirects(self, directives): redirects = [] for redirect in directives.get('redirect', []): @@ -342,7 +342,7 @@ class Apache2Controller(ServiceController): (location, redirect) ) return redirects - + def get_proxies(self, directives): proxies = [] for proxy in directives.get('proxy', []): @@ -360,7 +360,7 @@ class Apache2Controller(ServiceController): (location, proxy) ) return proxies - + def get_saas(self, directives): saas = [] for name, values in directives.items(): @@ -372,20 +372,20 @@ class Apache2Controller(ServiceController): directive = settings.WEBSITES_SAAS_DIRECTIVES[name] saas += self.get_directives(directive, context) return saas - + def get_username(self, site): option = site.get_directives().get('user_group') if option: return option[0] return site.get_username() - + def get_groupname(self, site): option = site.get_directives().get('user_group') if option and ' ' in option: user, group = option.split() return group return site.get_groupname() - + def get_server_names(self, site): server_name = None server_alias = [] @@ -395,7 +395,7 @@ class Apache2Controller(ServiceController): else: server_alias.append(domain.name) return server_name, server_alias - + def get_context(self, site): base_apache_conf = settings.WEBSITES_BASE_APACHE_CONF sites_available = os.path.join(base_apache_conf, 'sites-available') @@ -419,7 +419,7 @@ class Apache2Controller(ServiceController): if not context['ips']: raise ValueError("WEBSITES_DEFAULT_IPS is empty.") return context - + def set_content_context(self, content, context): content_context = { 'type': content.webapp.type, @@ -442,7 +442,7 @@ class Apache2Traffic(ServiceMonitor): doc_settings = (settings, ('WEBSITES_TRAFFIC_IGNORE_HOSTS',) ) - + def prepare(self): super(Apache2Traffic, self).prepare() ignore_hosts = '\\|'.join(settings.WEBSITES_TRAFFIC_IGNORE_HOSTS) @@ -490,11 +490,11 @@ class Apache2Traffic(ServiceMonitor): }' || [[ $? == 1 ]] && true } | xargs echo ${OBJECT_ID} }""") % context) - + def monitor(self, site): context = self.get_context(site) self.append('monitor {object_id} "{last_date}" {log_file}'.format(**context)) - + def get_context(self, site): return { 'log_file': '%s{,.1}' % site.get_www_access_log_path(), diff --git a/orchestra/utils/mail.py b/orchestra/utils/mail.py index 25b256bf..71f08657 100644 --- a/orchestra/utils/mail.py +++ b/orchestra/utils/mail.py @@ -2,7 +2,6 @@ from urllib.parse import urlparse from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string -from django.template import Context def render_email_template(template, context):