1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 23:55:24 +00:00
webvirtcloud/appsettings/views.py

104 lines
3.7 KiB
Python
Raw Normal View History

import os
2020-10-15 14:18:45 +00:00
import sass
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
2020-10-15 14:18:45 +00:00
from django.shortcuts import render
2023-05-10 08:23:06 +00:00
from django.utils.translation import gettext_noop as _
from logs.views import addlogmsg
2020-10-15 14:18:45 +00:00
from appsettings.models import AppSettings
@login_required
def appsettings(request):
"""
:param request:
:return:
"""
main_css = "wvc-main.min.css"
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-themes")
except FileNotFoundError as err:
2020-10-15 14:18:45 +00:00
messages.error(request, err)
2021-05-31 08:39:09 +00:00
addlogmsg(request.user.username, "-", "", err)
# Bootstrap settings related with filesystems, because of that they are excluded from other settings
2022-11-02 05:54:35 +00:00
appsettings = AppSettings.objects.exclude(
description__startswith="Bootstrap"
).order_by("name")
if request.method == "POST":
if "SASS_DIR" in request.POST:
try:
sass_dir.value = request.POST.get("SASS_DIR", "")
sass_dir.save()
2022-11-02 05:54:35 +00:00
msg = _("SASS directory path is changed. Now: %(dir)s") % {
"dir": sass_dir.value
}
messages.success(request, msg)
except Exception as err:
msg = err
2020-10-15 14:18:45 +00:00
messages.error(request, msg)
2021-05-31 08:39:09 +00:00
addlogmsg(request.user.username, "-", "", msg)
return HttpResponseRedirect(request.get_full_path())
if "BOOTSTRAP_THEME" in request.POST:
theme = request.POST.get("BOOTSTRAP_THEME", "")
scss_var = f"@import '{sass_dir.value}/wvc-themes/{theme}/variables';"
2022-11-02 05:54:35 +00:00
# scss_boot = f"@import '{sass_dir.value}/bootstrap/bootstrap.scss';"
scss_boot = f"@import '{sass_dir.value}/bootstrap-overrides.scss';"
2022-11-02 05:54:35 +00:00
scss_bootswatch = (
f"@import '{sass_dir.value}/wvc-themes/{theme}/bootswatch';"
2022-11-02 05:54:35 +00:00
)
try:
with open(sass_dir.value + "/wvc-main.scss", "w") as main:
2022-11-02 05:54:35 +00:00
main.write(
scss_var + "\n" + scss_boot + "\n" + scss_bootswatch + "\n"
)
css_compressed = sass.compile(
string=scss_var + "\n" + scss_boot + "\n" + scss_bootswatch,
output_style="compressed",
)
with open("static/css/" + main_css, "w") as css:
css.write(css_compressed)
bootstrap_theme.value = theme
bootstrap_theme.save()
msg = _("Theme is changed. Now: %(theme)s") % {"theme": theme}
messages.success(request, msg)
except Exception as err:
msg = err
2020-10-15 14:18:45 +00:00
messages.error(request, msg)
2021-05-31 08:39:09 +00:00
addlogmsg(request.user.username, "-", "", msg)
return HttpResponseRedirect(request.get_full_path())
for setting in appsettings:
if setting.key in request.POST:
try:
setting.value = request.POST.get(setting.key, "")
setting.save()
2022-11-02 05:54:35 +00:00
msg = _("%(setting)s is changed. Now: %(value)s") % {
"setting": setting.name,
"value": setting.value,
}
messages.success(request, msg)
except Exception as err:
msg = err
2020-10-15 14:18:45 +00:00
messages.error(request, msg)
2021-05-31 08:39:09 +00:00
addlogmsg(request.user.username, "-", "", msg)
return HttpResponseRedirect(request.get_full_path())
return render(request, "appsettings.html", locals())