2020-05-25 19:23:33 +00:00
|
|
|
import sass
|
|
|
|
import os
|
|
|
|
|
2020-05-24 16:20:43 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.urls import reverse
|
2020-10-14 12:27:57 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-24 16:20:43 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2020-05-25 19:23:33 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
|
2020-05-24 16:20:43 +00:00
|
|
|
from appsettings.models import AppSettings
|
2020-05-25 19:23:33 +00:00
|
|
|
from logs.views import addlogmsg
|
|
|
|
|
2020-05-24 16:20:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def appsettings(request):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
error_messages = []
|
2020-06-12 11:46:17 +00:00
|
|
|
main_css = "wvc-main.min.css"
|
2020-05-28 21:43:26 +00:00
|
|
|
sass_dir = AppSettings.objects.get(key="SASS_DIR")
|
|
|
|
bootstrap_theme = AppSettings.objects.get(key="BOOTSTRAP_THEME")
|
|
|
|
try:
|
|
|
|
themes_list = os.listdir(sass_dir.value + "/wvc-theme")
|
|
|
|
except FileNotFoundError as err:
|
|
|
|
error_messages.append(err)
|
2020-10-14 12:27:57 +00:00
|
|
|
addlogmsg(request.user.username, "", err)
|
2020-05-28 21:43:26 +00:00
|
|
|
|
|
|
|
# Bootstrap settings related with filesystems, because of that they are excluded from other settings
|
|
|
|
appsettings = AppSettings.objects.exclude(description__startswith="Bootstrap").order_by("name")
|
2020-10-14 12:27:57 +00:00
|
|
|
|
2020-05-24 16:20:43 +00:00
|
|
|
|
2020-05-28 21:43:26 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
if 'SASS_DIR' in request.POST:
|
|
|
|
try:
|
|
|
|
sass_dir.value = request.POST.get("SASS_DIR", "")
|
|
|
|
sass_dir.save()
|
|
|
|
|
|
|
|
msg = _(f"SASS directory path is changed. Now: {sass_dir.value}")
|
|
|
|
messages.success(request, msg)
|
|
|
|
except Exception as err:
|
|
|
|
msg = err
|
|
|
|
error_messages.append(msg)
|
2020-10-14 12:27:57 +00:00
|
|
|
|
2020-05-28 21:43:26 +00:00
|
|
|
addlogmsg(request.user.username, "", msg)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2020-05-24 16:20:43 +00:00
|
|
|
|
|
|
|
if 'BOOTSTRAP_THEME' in request.POST:
|
|
|
|
theme = request.POST.get("BOOTSTRAP_THEME", "")
|
|
|
|
scss_var = f"@import '{sass_dir.value}/wvc-theme/{theme}/variables';"
|
2020-10-14 12:27:57 +00:00
|
|
|
scss_bootswatch = f"@import '{sass_dir.value}/wvc-theme/{theme}/bootswatch';"
|
2020-05-24 16:20:43 +00:00
|
|
|
scss_boot = f"@import '{sass_dir.value}/bootstrap-overrides.scss';"
|
2020-05-25 19:23:33 +00:00
|
|
|
|
2020-10-14 12:27:57 +00:00
|
|
|
try:
|
2020-05-25 19:23:33 +00:00
|
|
|
with open(sass_dir.value + "/wvc-main.scss", "w") as main:
|
2020-06-08 06:15:06 +00:00
|
|
|
main.write(scss_var + "\n" + scss_boot + "\n" + scss_bootswatch + "\n")
|
2020-10-14 12:27:57 +00:00
|
|
|
|
2020-05-25 19:23:33 +00:00
|
|
|
css_compressed = sass.compile(string=scss_var + "\n"+ scss_boot + "\n" + scss_bootswatch, output_style='compressed')
|
2020-06-12 11:46:17 +00:00
|
|
|
with open("static/css/" + main_css, "w") as css:
|
2020-10-14 12:27:57 +00:00
|
|
|
css.write(css_compressed)
|
2020-05-25 19:23:33 +00:00
|
|
|
|
|
|
|
bootstrap_theme.value = theme
|
|
|
|
bootstrap_theme.save()
|
|
|
|
|
|
|
|
msg = _(f"Theme changed. Now: {theme}")
|
|
|
|
messages.success(request, msg)
|
|
|
|
except Exception as err:
|
|
|
|
msg = err
|
|
|
|
error_messages.append(msg)
|
2020-10-14 12:27:57 +00:00
|
|
|
|
2020-05-25 19:23:33 +00:00
|
|
|
addlogmsg(request.user.username, "", msg)
|
2020-05-24 16:20:43 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
|
2020-05-28 21:43:26 +00:00
|
|
|
for setting in appsettings:
|
|
|
|
if setting.key in request.POST:
|
|
|
|
try:
|
|
|
|
setting.value = request.POST.get(setting.key, "")
|
|
|
|
setting.save()
|
|
|
|
|
|
|
|
msg = _(f"{setting.name} is changed. Now: {setting.value}")
|
|
|
|
messages.success(request, msg)
|
|
|
|
except Exception as err:
|
|
|
|
msg = err
|
|
|
|
error_messages.append(msg)
|
2020-10-14 12:27:57 +00:00
|
|
|
|
2020-05-28 21:43:26 +00:00
|
|
|
addlogmsg(request.user.username, "", msg)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2020-05-24 16:20:43 +00:00
|
|
|
|
|
|
|
return render(request, 'appsettings.html', locals())
|