forked from City-of-Helsinki/respa
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add maintenance mode (#281)
* Add maintenance mode * Create maintenance app * Add user perm test during maintenance mode
- Loading branch information
Showing
25 changed files
with
354 additions
and
82 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from django import forms | ||
from django.contrib import admin | ||
from django.db.models import Q | ||
from django.contrib.admin import site as admin_site | ||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from modeltranslation.admin import TranslationAdmin | ||
|
||
|
||
from .models import MaintenanceMessage, MaintenanceMode | ||
|
||
|
||
class MaintenanceModeAdmin(admin.ModelAdmin): | ||
pass | ||
|
||
class MaintenanceModeInline(admin.TabularInline): | ||
model = MaintenanceMode | ||
fields = ('start', 'end', ) | ||
verbose_name = _('maintenance mode') | ||
verbose_name_plural = _('maintenance modes') | ||
extra = 0 | ||
|
||
|
||
class MaintenanceMessageAdminForm(forms.ModelForm): | ||
class Meta: | ||
model = MaintenanceMessage | ||
fields = ('start', 'end', 'message', ) | ||
|
||
def clean(self): | ||
start = self.cleaned_data['start'] | ||
end = self.cleaned_data['end'] | ||
query = Q(end__gt=start, start__lt=end) | ||
if self.instance and self.instance.pk: | ||
query &= ~Q(pk=self.instance.pk) | ||
collision = MaintenanceMessage.objects.filter(query) | ||
if collision.exists(): | ||
raise ValidationError(_('maintenance message already exists.')) | ||
|
||
class MaintenanceMessageAdmin(TranslationAdmin): | ||
form = MaintenanceMessageAdminForm | ||
inlines = ( MaintenanceModeInline, ) | ||
fieldsets = ( | ||
(_('General'), { | ||
'fields': ( | ||
'start', | ||
'end', | ||
'message' | ||
), | ||
}), | ||
) | ||
|
||
|
||
admin_site.register(MaintenanceMessage, MaintenanceMessageAdmin) | ||
admin_site.register(MaintenanceMode, MaintenanceModeAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .announcements import MaintenanceMessageViewSet |
4 changes: 2 additions & 2 deletions
4
resources/api/announcements.py → maintenance/api/announcements.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class MaintenanceConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'maintenance' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Generated by Django 3.2.19 on 2023-09-04 06:19 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='MaintenanceMessage', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Time of creation')), | ||
('modified_at', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Time of modification')), | ||
('message', models.TextField(verbose_name='Message')), | ||
('message_fi', models.TextField(null=True, verbose_name='Message')), | ||
('message_en', models.TextField(null=True, verbose_name='Message')), | ||
('message_sv', models.TextField(null=True, verbose_name='Message')), | ||
('start', models.DateTimeField(verbose_name='Begin time')), | ||
('end', models.DateTimeField(verbose_name='End time')), | ||
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='maintenancemessage_created', to=settings.AUTH_USER_MODEL, verbose_name='Created by')), | ||
('modified_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='maintenancemessage_modified', to=settings.AUTH_USER_MODEL, verbose_name='Modified by')), | ||
], | ||
options={ | ||
'verbose_name': 'maintenance message', | ||
'verbose_name_plural': 'maintenance messages', | ||
'ordering': ('start',), | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='MaintenanceMode', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Time of creation')), | ||
('modified_at', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Time of modification')), | ||
('start', models.DateTimeField(verbose_name='Begin time')), | ||
('end', models.DateTimeField(verbose_name='End time')), | ||
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='maintenancemode_created', to=settings.AUTH_USER_MODEL, verbose_name='Created by')), | ||
('maintenance_message', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='maintenance.maintenancemessage')), | ||
('modified_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='maintenancemode_modified', to=settings.AUTH_USER_MODEL, verbose_name='Modified by')), | ||
], | ||
options={ | ||
'verbose_name': 'maintenance mode', | ||
'verbose_name_plural': 'maintenance modes', | ||
'ordering': ('start',), | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import datetime | ||
|
||
|
||
from django.db import models | ||
from django.utils import timezone | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.core.exceptions import ValidationError | ||
|
||
|
||
from resources.models.base import ModifiableModel | ||
|
||
|
||
class MaintenanceMessageQuerySet(models.QuerySet): | ||
def active(self): | ||
return self.filter(start__lt=timezone.now(), end__gt=timezone.now()) | ||
|
||
class MaintenanceMessage(ModifiableModel): | ||
message = models.TextField(verbose_name=_('Message'), null=False, blank=False) | ||
start = models.DateTimeField(verbose_name=_('Begin time'), null=False, blank=False) | ||
end = models.DateTimeField(verbose_name=_('End time'), null=False, blank=False) | ||
|
||
|
||
objects = MaintenanceMessageQuerySet.as_manager() | ||
class Meta: | ||
verbose_name = _('maintenance message') | ||
verbose_name_plural = _('maintenance messages') | ||
ordering = ('start', ) | ||
|
||
|
||
def __str__(self): | ||
return f"{_('maintenance message')} \ | ||
{timezone.localtime(self.start).replace(tzinfo=None)} - \ | ||
{timezone.localtime(self.end).replace(tzinfo=None)}" \ | ||
.capitalize() | ||
|
||
|
||
def clean(self): | ||
super().clean() | ||
if self.end <= self.start: | ||
raise ValidationError(_("Invalid start or end time")) | ||
|
||
class MaintenanceModeQuerySet(models.QuerySet): | ||
def active(self): | ||
return self.filter(start__lt=timezone.now(), end__gt=timezone.now()) | ||
|
||
|
||
class MaintenanceMode(ModifiableModel): | ||
start = models.DateTimeField(verbose_name=_('Begin time'), null=False, blank=False) | ||
end = models.DateTimeField(verbose_name=_('End time'), null=False, blank=False) | ||
maintenance_message = models.ForeignKey(MaintenanceMessage, on_delete=models.CASCADE, null=True, blank=True) | ||
|
||
objects = MaintenanceModeQuerySet.as_manager() | ||
|
||
class Meta: | ||
verbose_name = _('maintenance mode') | ||
verbose_name_plural = _('maintenance modes') | ||
ordering = ('start', ) | ||
|
||
def clean(self): | ||
super().clean() | ||
if self.end <= self.start: | ||
raise ValidationError(_("Invalid start or end time")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from modeltranslation.translator import TranslationOptions, register | ||
from .models import MaintenanceMessage | ||
|
||
|
||
@register(MaintenanceMessage) | ||
class MaintenanceMessageTranslationOptions(TranslationOptions): | ||
fields = ('message', ) | ||
required_languages = ('fi', 'en', 'sv', ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.shortcuts import render | ||
|
||
# Create your views here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Generated by Django 3.2.19 on 2023-09-01 09:35 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('resources', '0147_reserver_id_only_business'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='MaintenanceMode', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Time of creation')), | ||
('modified_at', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Time of modification')), | ||
('start', models.DateTimeField(verbose_name='Begin time')), | ||
('end', models.DateTimeField(verbose_name='End time')), | ||
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='maintenancemode_created', to=settings.AUTH_USER_MODEL, verbose_name='Created by')), | ||
('maintenance_message', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='resources.maintenancemessage')), | ||
('modified_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='maintenancemode_modified', to=settings.AUTH_USER_MODEL, verbose_name='Modified by')), | ||
], | ||
options={ | ||
'verbose_name': 'maintenance mode', | ||
'verbose_name_plural': 'maintenance modes', | ||
'ordering': ('start',), | ||
}, | ||
), | ||
] |
Oops, something went wrong.