From 3ba0e7597f36d5c4850e830f4038668e30b66ce3 Mon Sep 17 00:00:00 2001 From: mtyton Date: Fri, 1 Sep 2023 15:15:16 +0200 Subject: [PATCH] Added setup page, modified admin view --- artel/artel/settings/base.py | 1 + .../templates/menu/custom_main_menu.html | 6 +- artel/artel/urls.py | 8 +- artel/setup/admin.py | 23 ++++- artel/setup/forms.py | 50 +++++------ artel/setup/middleware.py | 29 +++++++ artel/setup/templates/setup/config.html | 85 ++++++++++++++----- artel/setup/views.py | 29 +++++-- 8 files changed, 161 insertions(+), 70 deletions(-) diff --git a/artel/artel/settings/base.py b/artel/artel/settings/base.py index 3bb00f9..bcf177b 100644 --- a/artel/artel/settings/base.py +++ b/artel/artel/settings/base.py @@ -74,6 +74,7 @@ INSTALLED_APPS = [ MIDDLEWARE = [ "setup.middleware.CheckSetupMiddleware", + "setup.middleware.CheckShopMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", diff --git a/artel/artel/templates/menu/custom_main_menu.html b/artel/artel/templates/menu/custom_main_menu.html index 1a01b8f..15aa028 100644 --- a/artel/artel/templates/menu/custom_main_menu.html +++ b/artel/artel/templates/menu/custom_main_menu.html @@ -3,7 +3,7 @@
- Portal Logo + Portal Logo

- {% if store_enabled %} - Koszyk + {% if shop_enabled %} + Koszyk {% endif %}
diff --git a/artel/artel/urls.py b/artel/artel/urls.py index 613aed4..d9c10ed 100644 --- a/artel/artel/urls.py +++ b/artel/artel/urls.py @@ -22,14 +22,10 @@ urlpatterns = [ path("admin/", include(wagtailadmin_urls)), path("documents/", include(wagtaildocs_urls)), path("search/", search_views.search, name="search"), - path('setup/', setup_views.SetupPageView.as_view(), name='setup') + path('setup/', setup_views.SetupPageView.as_view(), name='setup'), + path('store-app/', include('store.urls')), ] -store_app_enabled = getattr(settings, 'SHOP_ENABLED', False) -if store_app_enabled: - urlpatterns += [ - path('store-app/', include('store.urls')), - ] if settings.DEBUG: from django.conf.urls.static import static diff --git a/artel/setup/admin.py b/artel/setup/admin.py index 8c38f3f..048204c 100644 --- a/artel/setup/admin.py +++ b/artel/setup/admin.py @@ -1,3 +1,22 @@ -from django.contrib import admin +from wagtail.contrib.modeladmin.options import ( + ModelAdmin, + ModelAdminGroup, + modeladmin_register +) -# Register your models here. +from setup.models import ComfyConfig + +class ConfigAdmin(ModelAdmin): + model = ComfyConfig + list_display = ("updated", ) + + +class SetupModelAdminGroup(ModelAdminGroup): + menu_label = "Setup" + menu_icon = 'folder-open-inverse' + menu_order = 200 + items = ( + ConfigAdmin, + ) + +modeladmin_register(SetupModelAdminGroup) diff --git a/artel/setup/forms.py b/artel/setup/forms.py index 903ee39..b062bb6 100644 --- a/artel/setup/forms.py +++ b/artel/setup/forms.py @@ -1,36 +1,26 @@ from django import forms from django.conf import settings -from colorfield.fields import ColorField -import os + +from setup.models import ( + ComfyConfig, + NavbarPosition +) -class SiteConfigurationForm(forms.Form): - logo = forms.ImageField(required=False) - shop_enabled = forms.BooleanField(required=False) +class SiteConfigurationForm(forms.ModelForm): + class Meta: + model = ComfyConfig + fields = [ + "logo", "navbar_position", "shop_enabled" + ] + widgets = { + "logo": forms.FileInput(attrs={"class": "form-control"}), + "navbar_position": forms.Select(attrs={"class": "form-control"}), + "shop_enabled": forms.CheckboxInput(attrs={"class": "form-check-input"}), + } + navbar_position = forms.ChoiceField( - choices=[ - ('left', 'Left'), - ('right', 'Right'), - ('top', 'Top'), - ], - initial='left', - widget=forms.RadioSelect + choices=NavbarPosition.choices, + widget=forms.Select(attrs={"class": "form-control"}), + initial=NavbarPosition.LEFT.value ) - skin = forms.ChoiceField( - choices=[ - ('light', 'Light'), - ('dark', 'Dark'), - ('custom', 'Custom'), - ], - widget=forms.Select - ) - - def save_logo(self): - if self.cleaned_data['logo']: - logo = self.cleaned_data['logo'] - filename = os.path.join(settings.MEDIA_ROOT, 'images/icons', logo.name) - with open(filename, 'wb') as f: - for chunk in logo.chunks(): - f.write(chunk) - return os.path.join('images/icons', logo.name) - return None diff --git a/artel/setup/middleware.py b/artel/setup/middleware.py index add589f..f4d3e2b 100644 --- a/artel/setup/middleware.py +++ b/artel/setup/middleware.py @@ -1,6 +1,9 @@ import logging from django.shortcuts import redirect +from django.conf import settings +from django.http import HttpResponse +from store.models import ProductListPage from setup.models import ComfyConfig logger = logging.getLogger(__name__) @@ -26,3 +29,29 @@ class CheckSetupMiddleware(object): response = self.get_response(request) return response + + +class CheckShopMiddleware(object): + def __init__(self, get_response): + self.get_response = get_response + + def _check_if_store_request(self, request): + if request.path_info.startswith('/store-app/'): + return True + if request.path_info == "/": + return False + return ProductListPage.objects.filter(url_path__endswith=request.path_info).exists() + + def __call__(self, request): + config = ComfyConfig.objects.first() + print(self._check_if_store_request(request)) + if config and not config.shop_enabled and self._check_if_store_request(request): + if settings.DEBUG: + return HttpResponse( + status=500, content="Store is not enabled please turn it on in admin panel" + ) + else: + return redirect('/') + + response = self.get_response(request) + return response diff --git a/artel/setup/templates/setup/config.html b/artel/setup/templates/setup/config.html index 2cb13c1..d0a6ee0 100644 --- a/artel/setup/templates/setup/config.html +++ b/artel/setup/templates/setup/config.html @@ -1,24 +1,65 @@ -{% extends "modeladmin/create.html" %} -{% load i18n wagtailadmin_tags %} - -{% block header %} - {% include "modeladmin/includes/header_with_history.html" with title=view.get_page_title subtitle=view.get_page_subtitle icon=view.header_icon merged=1 latest_log_entry=latest_log_entry history_url=history_url %} -{% endblock %} - -{% block form_action %}{{ view.edit_url }}{% endblock %} - -{% block form_actions %} -