Added setup page, modified admin view

feature/optionalization
mtyton 2023-09-01 15:15:16 +02:00
rodzic b76782715e
commit 3ba0e7597f
8 zmienionych plików z 161 dodań i 70 usunięć

Wyświetl plik

@ -74,6 +74,7 @@ INSTALLED_APPS = [
MIDDLEWARE = [ MIDDLEWARE = [
"setup.middleware.CheckSetupMiddleware", "setup.middleware.CheckSetupMiddleware",
"setup.middleware.CheckShopMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware", "django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware", "django.middleware.csrf.CsrfViewMiddleware",

Wyświetl plik

@ -3,7 +3,7 @@
<div class="d-flex flex-column flex-shrink-0 p-3 mr-5"> <div class="d-flex flex-column flex-shrink-0 p-3 mr-5">
<img src="{{ logo_url }}" class="img-fluid rounded mx-auto d-block mt-3" style="width: 10rem; height: 10 rem;" alt="Portal Logo"/> <img src="{{logo}}" class="img-fluid rounded mx-auto d-block mt-3" style="width: 10rem; height: 10 rem;" alt="Portal Logo"/>
<hr> <hr>
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
{% for item in menu_items %} {% for item in menu_items %}
@ -22,7 +22,7 @@
</div> </div>
<div class="d-flex flex-column flex-shrink-0 p-3 mr-5"> <div class="d-flex flex-column flex-shrink-0 p-3 mr-5">
<hr> <hr>
{% if store_enabled %} {% if shop_enabled %}
<a href={% url 'cart' %} alt="Koszyk" > Koszyk </a> <a href={% url 'cart' %} alt="Koszyk" > Koszyk </a>
{% endif %} {% endif %}
</div> </div>

Wyświetl plik

@ -22,14 +22,10 @@ urlpatterns = [
path("admin/", include(wagtailadmin_urls)), path("admin/", include(wagtailadmin_urls)),
path("documents/", include(wagtaildocs_urls)), path("documents/", include(wagtaildocs_urls)),
path("search/", search_views.search, name="search"), 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: if settings.DEBUG:
from django.conf.urls.static import static from django.conf.urls.static import static

Wyświetl plik

@ -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)

Wyświetl plik

@ -1,36 +1,26 @@
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from colorfield.fields import ColorField
import os from setup.models import (
ComfyConfig,
NavbarPosition
)
class SiteConfigurationForm(forms.Form): class SiteConfigurationForm(forms.ModelForm):
logo = forms.ImageField(required=False) class Meta:
shop_enabled = forms.BooleanField(required=False) 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( navbar_position = forms.ChoiceField(
choices=[ choices=NavbarPosition.choices,
('left', 'Left'), widget=forms.Select(attrs={"class": "form-control"}),
('right', 'Right'), initial=NavbarPosition.LEFT.value
('top', 'Top'),
],
initial='left',
widget=forms.RadioSelect
) )
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

Wyświetl plik

@ -1,6 +1,9 @@
import logging import logging
from django.shortcuts import redirect 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 from setup.models import ComfyConfig
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -26,3 +29,29 @@ class CheckSetupMiddleware(object):
response = self.get_response(request) response = self.get_response(request)
return response 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

Wyświetl plik

@ -1,24 +1,65 @@
{% extends "modeladmin/create.html" %} {% extends 'base.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 %}
<div class="dropdown dropup dropdown-button match-width">
<button type="submit" class="button action-save button-longrunning" data-clicked-text="{% trans 'Saving…' %}">
{% icon name="spinner" %}<em>{% trans 'Save' %}</em>
</button>
{% if user_can_delete %}
<div class="dropdown-toggle">{% icon name="arrow-up" %}</div>
<ul>
<li><a href="{{ view.delete_url }}" class="shortcut">{% trans "Delete" %}</a></li>
</ul>
{% endif %}
{% load static %}
{% block content %}
<div class="text-center">
<div class="card text-center">
<div class="card-header">
<img src="{% static 'images/icons/temp_comfy.png' %}" width="15%">
</div>
<form enctype="multipart/form-data" action="" method="POST">
{% csrf_token %}
<div class="card-body">
<h1>Configure Your Comfy</h1>
<div class="row mb-3 mt-5">
<div class="col-2">
</div>
<div class="col-4">
<h3>
<label for="id_logo" class="form-label">{{form.logo.label}}</label>
</h3>
</div>
<div class="col-6">
{{form.logo}}
</div>
</div>
<div class="row mb-3">
<div class="col-2">
</div>
<div class="col-4">
<h3>
<label for="id_logo" class="form-label">{{form.navbar_position.label}}</label>
</h3>
</div>
<div class="col-6">
{{form.navbar_position}}
</div>
</div>
<div class="row mb-3">
<div class="col-2">
</div>
<div class="col-4">
<h3>
<label for="id_logo" class="form-label">{{form.shop_enabled.label}}</label>
</h3>
</div>
<div class="col-2">
{{form.shop_enabled}}
</div>
</div>
{% if form.errors %}
<div class="alert alert-danger">
{{ form.errors }}
</div>
{% endif %}
</div>
<div class="card-footer text-muted">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</form>
</div>
</div> </div>
{% endblock %} {% endblock %}

Wyświetl plik

@ -1,14 +1,29 @@
import os from django.shortcuts import (
import json render,
redirect
from django.shortcuts import render, redirect )
from django.views import View from django.views import View
from django.conf import settings
from setup.forms import SiteConfigurationForm
class SetupPageView(View): class SetupPageView(View):
template_name = 'setup/config.html' template_name = 'setup/config.html'
def get(self, request): def get_context_data(self, **kwargs):
return render(request, self.template_name) return {
"form": SiteConfigurationForm()
}
def get(self, request):
return render(request, self.template_name, self.get_context_data())
def post(self, request):
form = SiteConfigurationForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('wagtailadmin_home')
context = self.get_context_data()
context['form'] = form
return render(request, self.template_name, context)