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.
Merge pull request #287 from City-of-Turku/develop
Release tku-v1.8
- Loading branch information
Showing
46 changed files
with
910 additions
and
216 deletions.
There are no files selected for viewing
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
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 |
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,36 @@ | ||
from django.utils import timezone | ||
from rest_framework import viewsets, serializers | ||
from resources.api.base import TranslatedModelSerializer, register_view | ||
from maintenance.models import MaintenanceMessage | ||
|
||
|
||
|
||
class MaintenanceMessageSerializer(TranslatedModelSerializer): | ||
is_maintenance_mode_on = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = MaintenanceMessage | ||
fields = ('message', 'is_maintenance_mode_on') | ||
|
||
def get_is_maintenance_mode_on(self, obj): | ||
maintenance_modes = obj.maintenancemode_set.all() | ||
now = timezone.now() | ||
|
||
for maintenance_mode in maintenance_modes: | ||
if maintenance_mode.start <= now <= maintenance_mode.end: | ||
return True | ||
|
||
return False | ||
|
||
|
||
|
||
class MaintenanceMessageViewSet(viewsets.ReadOnlyModelViewSet): | ||
queryset = MaintenanceMessage.objects.all() | ||
serializer_class = MaintenanceMessageSerializer | ||
|
||
|
||
def get_queryset(self): | ||
return self.queryset.active() | ||
|
||
|
||
register_view(MaintenanceMessageViewSet, 'announcements', base_name='announcements') |
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
Oops, something went wrong.