mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Remove custom context processors (#8540)
* Remove custom context processors - Only merge after 0.17.0 release - Remove code which injects custom context variables into CUI requests - Not needed for new API-based PUI code - Speeds up requests - remove unnecessary DB hits * Remove broken import * Remove custom staticfile processing - No longer needed as CUI is gone
This commit is contained in:
		@@ -1,84 +0,0 @@
 | 
				
			|||||||
"""Provides extra global data to all templates."""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import InvenTree.helpers_email
 | 
					 | 
				
			||||||
import InvenTree.ready
 | 
					 | 
				
			||||||
import InvenTree.status
 | 
					 | 
				
			||||||
from generic.states.custom import get_custom_classes
 | 
					 | 
				
			||||||
from users.models import RuleSet, check_user_role
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def health_status(request):
 | 
					 | 
				
			||||||
    """Provide system health status information to the global context.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    - Not required for AJAX requests
 | 
					 | 
				
			||||||
    - Do not provide if it is already provided to the context
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    if request.path.endswith('.js'):
 | 
					 | 
				
			||||||
        # Do not provide to script requests
 | 
					 | 
				
			||||||
        return {}  # pragma: no cover
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if hasattr(request, '_inventree_health_status'):
 | 
					 | 
				
			||||||
        # Do not duplicate efforts
 | 
					 | 
				
			||||||
        return {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    request._inventree_health_status = True
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    status = {
 | 
					 | 
				
			||||||
        'django_q_running': InvenTree.status.is_worker_running(),
 | 
					 | 
				
			||||||
        'email_configured': InvenTree.helpers_email.is_email_configured(),
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # The following keys are required to denote system health
 | 
					 | 
				
			||||||
    health_keys = ['django_q_running']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    all_healthy = True
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for k in health_keys:
 | 
					 | 
				
			||||||
        if status[k] is not True:
 | 
					 | 
				
			||||||
            all_healthy = False
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    status['system_healthy'] = all_healthy
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    status['up_to_date'] = InvenTree.version.isInvenTreeUpToDate()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return status
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def status_codes(request):
 | 
					 | 
				
			||||||
    """Provide status code enumerations."""
 | 
					 | 
				
			||||||
    if hasattr(request, '_inventree_status_codes'):
 | 
					 | 
				
			||||||
        # Do not duplicate efforts
 | 
					 | 
				
			||||||
        return {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    request._inventree_status_codes = True
 | 
					 | 
				
			||||||
    get_custom = InvenTree.ready.isRebuildingData() is False
 | 
					 | 
				
			||||||
    return {
 | 
					 | 
				
			||||||
        cls.__name__: cls.template_context() for cls in get_custom_classes(get_custom)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def user_roles(request):
 | 
					 | 
				
			||||||
    """Return a map of the current roles assigned to the user.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Roles are denoted by their simple names, and then the permission type.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Permissions can be access as follows:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    - roles.part.view
 | 
					 | 
				
			||||||
    - roles.build.delete
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Each value will return a boolean True / False
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    user = request.user
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    roles = {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for role in RuleSet.get_ruleset_models():
 | 
					 | 
				
			||||||
        permissions = {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for perm in ['view', 'add', 'change', 'delete']:
 | 
					 | 
				
			||||||
            permissions[perm] = user.is_superuser or check_user_role(user, role, perm)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        roles[role] = permissions
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return {'roles': roles}
 | 
					 | 
				
			||||||
@@ -211,18 +211,6 @@ PLUGIN_FILE_HASH = ''
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
STATICFILES_DIRS = []
 | 
					STATICFILES_DIRS = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Translated Template settings
 | 
					 | 
				
			||||||
STATICFILES_I18_PREFIX = 'i18n'
 | 
					 | 
				
			||||||
STATICFILES_I18_SRC = BASE_DIR.joinpath('templates', 'js', 'translated')
 | 
					 | 
				
			||||||
STATICFILES_I18_TRG = BASE_DIR.joinpath('InvenTree', 'static_i18n')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Create the target directory if it does not exist
 | 
					 | 
				
			||||||
if not STATICFILES_I18_TRG.exists():
 | 
					 | 
				
			||||||
    STATICFILES_I18_TRG.mkdir(parents=True)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
STATICFILES_DIRS.append(STATICFILES_I18_TRG)
 | 
					 | 
				
			||||||
STATICFILES_I18_TRG = STATICFILES_I18_TRG.joinpath(STATICFILES_I18_PREFIX)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Append directory for compiled react files if debug server is running
 | 
					# Append directory for compiled react files if debug server is running
 | 
				
			||||||
if DEBUG and 'collectstatic' not in sys.argv:
 | 
					if DEBUG and 'collectstatic' not in sys.argv:
 | 
				
			||||||
    web_dir = BASE_DIR.joinpath('..', 'web', 'static').absolute()
 | 
					    web_dir = BASE_DIR.joinpath('..', 'web', 'static').absolute()
 | 
				
			||||||
@@ -235,10 +223,6 @@ if DEBUG and 'collectstatic' not in sys.argv:
 | 
				
			|||||||
        STATICFILES_DIRS.append(BASE_DIR.joinpath('plugin', 'samples', 'static'))
 | 
					        STATICFILES_DIRS.append(BASE_DIR.joinpath('plugin', 'samples', 'static'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        print('-', STATICFILES_DIRS[-1])
 | 
					        print('-', STATICFILES_DIRS[-1])
 | 
				
			||||||
STATFILES_I18_PROCESSORS = ['InvenTree.context.status_codes']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Color Themes Directory
 | 
					 | 
				
			||||||
STATIC_COLOR_THEMES_DIR = STATIC_ROOT.joinpath('css', 'color-themes').resolve()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Database backup options
 | 
					# Database backup options
 | 
				
			||||||
# Ref: https://django-dbbackup.readthedocs.io/en/master/configuration.html
 | 
					# Ref: https://django-dbbackup.readthedocs.io/en/master/configuration.html
 | 
				
			||||||
@@ -553,10 +537,6 @@ TEMPLATES = [
 | 
				
			|||||||
                'django.template.context_processors.i18n',
 | 
					                'django.template.context_processors.i18n',
 | 
				
			||||||
                'django.contrib.auth.context_processors.auth',
 | 
					                'django.contrib.auth.context_processors.auth',
 | 
				
			||||||
                'django.contrib.messages.context_processors.messages',
 | 
					                'django.contrib.messages.context_processors.messages',
 | 
				
			||||||
                # Custom InvenTree context processors
 | 
					 | 
				
			||||||
                'InvenTree.context.health_status',
 | 
					 | 
				
			||||||
                'InvenTree.context.status_codes',
 | 
					 | 
				
			||||||
                'InvenTree.context.user_roles',
 | 
					 | 
				
			||||||
            ],
 | 
					            ],
 | 
				
			||||||
            'loaders': [
 | 
					            'loaders': [
 | 
				
			||||||
                (
 | 
					                (
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,12 +1,10 @@
 | 
				
			|||||||
"""This module provides template tags for extra functionality, over and above the built-in Django tags."""
 | 
					"""This module provides template tags for extra functionality, over and above the built-in Django tags."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
from datetime import date, datetime
 | 
					from datetime import date, datetime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django import template
 | 
					from django import template
 | 
				
			||||||
from django.conf import settings as djangosettings
 | 
					from django.conf import settings as djangosettings
 | 
				
			||||||
from django.templatetags.static import StaticNode
 | 
					 | 
				
			||||||
from django.urls import NoReverseMatch, reverse
 | 
					from django.urls import NoReverseMatch, reverse
 | 
				
			||||||
from django.utils.safestring import mark_safe
 | 
					from django.utils.safestring import mark_safe
 | 
				
			||||||
from django.utils.translation import gettext_lazy as _
 | 
					from django.utils.translation import gettext_lazy as _
 | 
				
			||||||
@@ -444,72 +442,6 @@ def inventree_customize(reference, *args, **kwargs):
 | 
				
			|||||||
    return djangosettings.CUSTOMIZE.get(reference, '')
 | 
					    return djangosettings.CUSTOMIZE.get(reference, '')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class I18nStaticNode(StaticNode):
 | 
					 | 
				
			||||||
    """Custom StaticNode.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Replaces a variable named *lng* in the path with the current language
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def render(self, context):  # pragma: no cover
 | 
					 | 
				
			||||||
        """Render this node with the determined locale context."""
 | 
					 | 
				
			||||||
        self.original = getattr(self, 'original', None)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if not self.original:
 | 
					 | 
				
			||||||
            # Store the original (un-rendered) path template, as it gets overwritten below
 | 
					 | 
				
			||||||
            self.original = self.path.var
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if hasattr(context, 'request'):
 | 
					 | 
				
			||||||
            # Convert the "requested" language code to a standard format
 | 
					 | 
				
			||||||
            language_code = context.request.LANGUAGE_CODE.lower().strip()
 | 
					 | 
				
			||||||
            language_code = language_code.replace('_', '-')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            # Find the first "best" match:
 | 
					 | 
				
			||||||
            # - First, try the original requested code, e.g. 'pt-br'
 | 
					 | 
				
			||||||
            # - Next, try a simpler version of the code e.g. 'pt'
 | 
					 | 
				
			||||||
            # - Finally, fall back to english
 | 
					 | 
				
			||||||
            options = [language_code, language_code.split('-')[0], 'en']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            for lng in options:
 | 
					 | 
				
			||||||
                lng_file = os.path.join(
 | 
					 | 
				
			||||||
                    djangosettings.STATIC_ROOT, self.original.format(lng=lng)
 | 
					 | 
				
			||||||
                )
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if os.path.exists(lng_file):
 | 
					 | 
				
			||||||
                    self.path.var = self.original.format(lng=lng)
 | 
					 | 
				
			||||||
                    break
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        ret = super().render(context)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return ret
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# use the dynamic url - tag if in Debugging-Mode
 | 
					 | 
				
			||||||
if settings.DEBUG:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @register.simple_tag()
 | 
					 | 
				
			||||||
    def i18n_static(url_name):
 | 
					 | 
				
			||||||
        """Simple tag to enable {% url %} functionality instead of {% static %}."""
 | 
					 | 
				
			||||||
        return reverse(url_name)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
else:  # pragma: no cover
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @register.tag('i18n_static')
 | 
					 | 
				
			||||||
    def do_i18n_static(parser, token):
 | 
					 | 
				
			||||||
        """Overrides normal static, adds language - lookup for prerenderd files #1485.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        Usage (like static):
 | 
					 | 
				
			||||||
        {% i18n_static path [as varname] %}
 | 
					 | 
				
			||||||
        """
 | 
					 | 
				
			||||||
        bits = token.split_contents()
 | 
					 | 
				
			||||||
        loc_name = settings.STATICFILES_I18_PREFIX
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # change path to called resource
 | 
					 | 
				
			||||||
        bits[1] = f"'{loc_name}/{{lng}}.{bits[1][1:-1]}'"
 | 
					 | 
				
			||||||
        token.contents = ' '.join(bits)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return I18nStaticNode.handle_token(parser, token)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@register.simple_tag()
 | 
					@register.simple_tag()
 | 
				
			||||||
def admin_index(user):
 | 
					def admin_index(user):
 | 
				
			||||||
    """Return a URL for the admin interface."""
 | 
					    """Return a URL for the admin interface."""
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user