Skip to content

Commit

Permalink
#649: replace json_constants with django-solo, rewrite all occurrence…
Browse files Browse the repository at this point in the history
…s and add translations
  • Loading branch information
Fasand committed Dec 15, 2024
1 parent 9f091c8 commit aeb7aa2
Show file tree
Hide file tree
Showing 18 changed files with 186 additions and 230 deletions.
6 changes: 6 additions & 0 deletions Seeder/core/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from solo.admin import SingletonModelAdmin
from core.models import SiteConfiguration


admin.site.register(SiteConfiguration, SingletonModelAdmin)
29 changes: 5 additions & 24 deletions Seeder/core/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from ckeditor.widgets import CKEditorWidget

from .json_constants import FieldType, load_constants, get_type_for_key
from . import models


class UserForm(forms.ModelForm):
Expand All @@ -27,25 +26,7 @@ def save(self, commit=True):
return self.instance.delete()
return super().save()


class UpdateJsonConstantsForm(forms.Form):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Dynamically create a field for each available constant
for key, value in load_constants().items():
field_type = get_type_for_key(key)
if field_type == FieldType.BOOL:
self.fields[key] = forms.BooleanField(
label=key, initial=value, required=False)
elif field_type == FieldType.LONG:
self.fields[key] = forms.CharField(
label=key, initial=value, required=True,
widget=forms.Textarea())
elif field_type == FieldType.RICH:
self.fields[key] = forms.CharField(
label=key, initial=value, required=True,
widget=CKEditorWidget(config_name="json_constants"))
else: # SHORT
self.fields[key] = forms.CharField(
max_length=128, required=True, label=key, initial=value)
class SiteConfigurationForm(forms.ModelForm):
class Meta:
model = models.SiteConfiguration
fields = "__all__"
85 changes: 0 additions & 85 deletions Seeder/core/json_constants.py

This file was deleted.

28 changes: 28 additions & 0 deletions Seeder/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 2.2.28 on 2024-08-29 14:18

import ckeditor.fields
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='SiteConfiguration',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('webarchive_size', models.CharField(default='595 TB', max_length=128)),
('wayback_maintenance', models.BooleanField(default=False)),
('wayback_maintenance_text_cs', ckeditor.fields.RichTextField(default='\n<p><span style="font-size:24px">Pokud vidíte tuto stránku, <strong>probíhá údržba dat</strong> a v archivu nelze nyní vyhledávat. Některé linky vrátí chybu.</span></p>\n<p><span style="font-size:24px">Prosím zkuste načíst Webarchiv později.</span></p>\n')),
('wayback_maintenance_text_en', ckeditor.fields.RichTextField(default='\n<p><span style="font-size:24px">If you see this page, <strong>we are currently doing maintenance</strong> and it is not possible to search the archive. Some links may return an error.</span></p>\n<p><span style="font-size:24px">Please, try to load Webarchiv again later.</span></p>\n')),
],
options={
'verbose_name': 'Site Configuration',
},
),
]
Empty file.
27 changes: 26 additions & 1 deletion Seeder/core/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.db import models
from django import forms
from django.utils import timezone
from django.forms.widgets import DateTimeInput, SplitDateTimeWidget
from django.utils.translation import gettext_lazy as _
from ckeditor.fields import RichTextField
from solo.models import SingletonModel

from core import widgets

Expand Down Expand Up @@ -41,3 +43,26 @@ class BaseModel(models.Model):
class Meta:
abstract = True
ordering = ('-last_changed', )

class SiteConfiguration(SingletonModel):
""" Singleton model for Site Configuration """
webarchive_size = models.CharField(max_length=128, default="595 TB")
wayback_maintenance = models.BooleanField(default=False)
wayback_maintenance_text_cs = RichTextField(
config_name='site_configuration',
default="""
<p><span style=\"font-size:24px\">Pokud vidíte tuto stránku, <strong>probíhá údržba dat</strong> a v archivu nelze nyní vyhledávat. Některé linky vrátí chybu.</span></p>
<p><span style=\"font-size:24px\">Prosím zkuste načíst Webarchiv později.</span></p>
""")
wayback_maintenance_text_en = RichTextField(
config_name='site_configuration',
default="""
<p><span style=\"font-size:24px\">If you see this page, <strong>we are currently doing maintenance</strong> and it is not possible to search the archive. Some links may return an error.</span></p>
<p><span style=\"font-size:24px\">Please, try to load Webarchiv again later.</span></p>
""")

def __str__(self):
return str(_("Site Configuration"))

class Meta:
verbose_name = _("Site Configuration")
9 changes: 9 additions & 0 deletions Seeder/core/templatetags/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django import template
from django.conf import settings
from django.utils.html import format_html
from core.models import SiteConfiguration

register = template.Library()

Expand Down Expand Up @@ -32,3 +33,11 @@ def user_in_group(user, group):
''' Check the provided user belongs to the provided group; all lowered '''
return (str.lower(group)
in map(str.lower, user.groups.values_list("name", flat=True)))

@register.simple_tag
def config(key):
"""
Retrieve a saved constant - returns a default/None if key doesn't exist
"""
config = SiteConfiguration.get_solo()
return getattr(config, key)
12 changes: 0 additions & 12 deletions Seeder/core/templatetags/json_constants.py

This file was deleted.

4 changes: 2 additions & 2 deletions Seeder/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
path('profile', UserProfileEdit.as_view(), name='user_edit'),
path('crash_test', CrashTestView.as_view(), name='crash_test'),
path('dev', DevNotesView.as_view(), name='dev_notes'),
path('json_constants', EditJsonConstantsView.as_view(),
name='json_constants'),
path('site_configuration', EditSiteConfigurationView.as_view(),
name='site_configuration'),
path('toggle-wayback-maintenance', ToggleWaybackMaintenanceView.as_view(),
name='toggle_wayback_maintenance'),
]
30 changes: 16 additions & 14 deletions Seeder/core/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from .json_constants import store_constants
from . import forms

from django.http.response import HttpResponseRedirect
from django.views.generic.base import RedirectView, TemplateView, View
from django.utils.translation import ugettext_lazy as _
Expand All @@ -10,7 +7,7 @@
from django.utils import translation
from django.views.generic.edit import FormView, UpdateView

from core.json_constants import get_constant, update_constant
from . import forms, models
from .generic_views import LoginMixin, MessageView, SuperuserRequiredMixin
from .dashboard_data import get_cards, cards_registry, REVERSE_SESSION

Expand Down Expand Up @@ -115,27 +112,30 @@ class DevNotesView(LoginMixin, TemplateView):
template_name = 'dev_notes.html'


class EditJsonConstantsView(SuperuserRequiredMixin, FormView, MessageView):
class EditSiteConfigurationView(
SuperuserRequiredMixin, UpdateView, MessageView):
"""
View for superusers where they can change constants accessed elsewhere in
the application.
e.g. "webarchive_size" on WWW Index
"""
form_class = forms.UpdateJsonConstantsForm
form_class = forms.SiteConfigurationForm
template_name = 'edit_form.html'
success_url = reverse_lazy('core:json_constants')
view_name = "json_constants"
success_url = reverse_lazy('core:site_configuration')
view_name = "site_configuration"

def get_object(self):
return models.SiteConfiguration.get_solo()

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
# This will show up in the page title & header w/ edit_form.html
ctx["object"] = _("Constants")
ctx["object"] = _("Site Configuration")
return ctx

def form_valid(self, form):
# Fields are created using constants' keys, so just store everything
store_constants(form.cleaned_data)
self.add_message(_("Constants successfully updated"), messages.SUCCESS)
self.add_message(_("Site Configuration successfully updated"),
messages.SUCCESS)
return super().form_valid(form)


Expand All @@ -145,8 +145,10 @@ class ToggleWaybackMaintenanceView(LoginMixin, View, MessageView):
"""

def post(self, request, *args, **kwargs):
maintenance = get_constant("wayback_maintenance")
update_constant("wayback_maintenance", not maintenance)
config = models.SiteConfiguration.get_solo()
maintenance = config.wayback_maintenance
config.wayback_maintenance = not maintenance
config.save()
# Based on the original value before update
if maintenance:
self.add_message(_('Wayback maintenance turned OFF'),
Expand Down
Loading

0 comments on commit aeb7aa2

Please sign in to comment.