resolved most of issues

feature/optionalization
KarolG 2023-06-27 04:23:55 +02:00
rodzic 8ec0bc7ad1
commit b8657c1594
12 zmienionych plików z 75 dodań i 116 usunięć

Wyświetl plik

@ -13,6 +13,8 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import json
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
@ -53,7 +55,12 @@ INSTALLED_APPS = [
"phonenumber_field",
]
# Optionalization
# Default settings
SHOP_ENABLED = False
LOGO = 'nologo'
NAVBAR_POSITION = 'left'
if os.path.exists('config.json'):
with open(os.path.join(BASE_DIR, 'config.json'), 'r') as file:
config_data = json.load(file)
@ -61,6 +68,8 @@ if os.path.exists('config.json'):
if shop_enabled:
INSTALLED_APPS.append('store')
SHOP_ENABLED = True
LOGO = config_data.get('logo', False)
NAVBAR_POSITION = config_data.get('navbar_position', False)
MIDDLEWARE = [
@ -86,9 +95,7 @@ TEMPLATES = [
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
'setup.context_processors.logo_url',
'setup.context_processors.menu_position',
'setup.context_processors.store_enabled',
'setup.context_processors.SetupContextProcessor',
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",

Wyświetl plik

@ -37,7 +37,13 @@
{% wagtailuserbar %}
<div class="container">
<div class="row">
{% if menu_left %}
{% if navbar_position == 'top' %}
<div class="col-md-12 mt-5 ml-2">
{% main_menu max_levels=3 template="menu/custom_main_menu.html" %}
</div>
{% endif %}
{% if navbar_position == 'left' %}
<div class="col-md-3 mt-5 ml-2">
{% main_menu max_levels=3 template="menu/custom_main_menu.html" %}
</div>
@ -45,7 +51,7 @@
<div class="col-md-9 mt-5 ml-2">
{% block content %}{% endblock %}
</div>
{% if not menu_left %}
{% if navbar_position == 'right' %}
<div class="col-md-3 mt-5 ml-2">
{% main_menu max_levels=3 template="menu/custom_main_menu.html" %}
</div>

Wyświetl plik

@ -25,5 +25,4 @@
{% if store_enabled %}
<a href={% url 'cart' %} alt="Koszyk" > Koszyk </a>
{% endif %}
<a href="/setup" alt="Koszyk" > setup </a>
</div>

Wyświetl plik

@ -12,14 +12,14 @@ from wagtail.documents import urls as wagtaildocs_urls
from search import views as search_views
from setup.views import setup_page as setup_view
from setup.views import SetupPageView as setup_view
urlpatterns = [
path("django-admin/", admin.site.urls),
path("admin/", include(wagtailadmin_urls)),
path("documents/", include(wagtaildocs_urls)),
path("search/", search_views.search, name="search"),
path('setup/', setup_view, name='setup_page'),
path('setup/', setup_view.as_view(), name='setup_page'),
]
store_app_enabled = getattr(settings, 'SHOP_ENABLED', False)

Wyświetl plik

@ -1,25 +1,12 @@
from .models import SiteConfiguration
from django.conf import settings
def logo_url(request):
config = SiteConfiguration.objects.first()
if config.logo:
logo_url = config.logo.url
else:
None
return {'logo_url': logo_url}
def menu_position(request):
config = SiteConfiguration.objects.first()
if config.navbar_position == 'left':
menu_left = True
else:
menu_left = False
return {'menu_left': menu_left}
def store_enabled(request):
store_enabled = getattr(settings, 'SHOP_ENABLED', False)
return {'store_enabled': store_enabled}
def SetupContextProcessor(request):
navbar_position = settings.NAVBAR_POSITION
logo = settings.LOGO
shop_enabled = settings.SHOP_ENABLED
return {
'navbar_position': navbar_position,
'logo_url': logo,
'shop_enabled': shop_enabled,
}

Wyświetl plik

@ -1,12 +1,27 @@
from django import forms
from .models import SiteConfiguration
from django.conf import settings
import os
class SiteConfigurationForm(forms.ModelForm):
class Meta:
model = SiteConfiguration
exclude = ()
class SiteConfigurationForm(forms.Form):
logo = forms.ImageField(required=False)
shop_enabled = forms.BooleanField(required=False)
navbar_position = forms.ChoiceField(
choices=[
('left', 'Left'),
('right', 'Right'),
('top', 'Top'),
],
initial='left',
widget=forms.RadioSelect
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['navbar_position'].widget.attrs['class'] = 'custom-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

@ -10,5 +10,7 @@ class CheckSetupMiddleware(object):
if not os.path.exists('config.json') and not request.path_info.startswith('/setup'):
return redirect('/setup/')
if os.path.exists('config.json') and request.path_info.startswith('/setup'):
return redirect('/')
response = self.get_response(request)
return response

Wyświetl plik

@ -1,22 +0,0 @@
# Generated by Django 4.1.9 on 2023-06-14 00:03
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="SiteConfiguration",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("logo", models.ImageField(blank=True, null=True, upload_to="logo/")),
("shop_enabled", models.BooleanField(default=True)),
("homepage", models.CharField(blank=True, max_length=255, null=True)),
("navbar_position", models.CharField(default="left", max_length=20)),
],
),
]

Wyświetl plik

@ -1,27 +0,0 @@
# Generated by Django 4.1.9 on 2023-06-15 01:33
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("setup", "0001_initial"),
]
operations = [
migrations.AddField(
model_name="siteconfiguration",
name="isSetup",
field=models.BooleanField(default=True),
),
migrations.AlterField(
model_name="siteconfiguration",
name="logo",
field=models.ImageField(blank=True, null=True, upload_to="images/icons"),
),
migrations.AlterField(
model_name="siteconfiguration",
name="navbar_position",
field=models.CharField(choices=[("left", "Left"), ("right", "Right")], default="left", max_length=20),
),
]

Wyświetl plik

@ -1,13 +0,0 @@
from django.db import models
class SiteConfiguration(models.Model):
logo = models.ImageField(upload_to='images/icons', null=True, blank=True)
shop_enabled = models.BooleanField(default=True)
homepage = models.CharField(max_length=255, null=True, blank=True)
NAVBAR_POSITION_CHOICES = [
('left', 'Left'),
('right', 'Right'),
]
navbar_position = models.CharField(max_length=20, choices=NAVBAR_POSITION_CHOICES, default='left')

Wyświetl plik

@ -1,18 +1,25 @@
from django.shortcuts import render, redirect
from .models import SiteConfiguration
from django.views import View
from .forms import SiteConfigurationForm
import json
def setup_page(request):
config = SiteConfiguration.objects.first()
class SetupPageView(View):
template_name = 'setup.html'
if request.method == 'POST':
form = SiteConfigurationForm(request.POST, request.FILES, instance=config)
def get(self, request):
form = SiteConfigurationForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = SiteConfigurationForm(request.POST, request.FILES)
if form.is_valid():
form.save()
form_data = form.cleaned_data
form_data['logo'] = str(form_data['logo'])
logo_path = form.save_logo()
if logo_path:
form_data['logo'] = 'media/' + logo_path
else:
form_data['logo'] = None
json_data = json.dumps(form_data, indent=4)
@ -21,6 +28,4 @@ def setup_page(request):
return redirect('/')
else:
form = SiteConfigurationForm(instance=config)
return render(request, 'setup.html', {'form': form})
return render(request, self.template_name, {'form': form})