editor: edit slugs and redirecting slugs
This commit is contained in:
parent
04dc3ec589
commit
f6cb959041
4 changed files with 64 additions and 5 deletions
|
@ -2,11 +2,13 @@ import json
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.forms import CharField, ModelForm, ValidationError
|
from django.forms import CharField, ModelForm, Textarea, ValidationError
|
||||||
from django.forms.widgets import HiddenInput
|
from django.forms.widgets import HiddenInput
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from shapely.geometry.geo import mapping
|
from shapely.geometry.geo import mapping
|
||||||
|
|
||||||
|
from c3nav.mapdata.models.locations import LocationRedirect, LocationSlug
|
||||||
|
|
||||||
|
|
||||||
class MapitemFormMixin(ModelForm):
|
class MapitemFormMixin(ModelForm):
|
||||||
def __init__(self, *args, request=None, **kwargs):
|
def __init__(self, *args, request=None, **kwargs):
|
||||||
|
@ -49,6 +51,30 @@ class MapitemFormMixin(ModelForm):
|
||||||
self.fields.move_to_end('title_' + language, last=False)
|
self.fields.move_to_end('title_' + language, last=False)
|
||||||
self.titles = titles
|
self.titles = titles
|
||||||
|
|
||||||
|
self.redirect_slugs = None
|
||||||
|
self.add_redirect_slugs = None
|
||||||
|
self.remove_redirect_slugs = None
|
||||||
|
if 'slug' in self.fields:
|
||||||
|
self.redirect_slugs = sorted(self.instance.redirects.values_list('slug', flat=True))
|
||||||
|
self.fields['redirect_slugs'] = CharField(label=_('Redirecting Slugs (comma seperated)'), required=False,
|
||||||
|
initial=','.join(self.redirect_slugs))
|
||||||
|
self.fields.move_to_end('redirect_slugs', last=False)
|
||||||
|
self.fields.move_to_end('slug', last=False)
|
||||||
|
|
||||||
|
def clean_redirect_slugs(self):
|
||||||
|
old_redirect_slugs = set(self.redirect_slugs)
|
||||||
|
new_redirect_slugs = set(s for s in (s.strip() for s in self.cleaned_data['redirect_slugs'].split(',')) if s)
|
||||||
|
|
||||||
|
self.add_redirect_slugs = new_redirect_slugs - old_redirect_slugs
|
||||||
|
self.remove_redirect_slugs = old_redirect_slugs - new_redirect_slugs
|
||||||
|
|
||||||
|
for slug in self.add_redirect_slugs:
|
||||||
|
self.fields['slug'].run_validators(slug)
|
||||||
|
if LocationSlug.objects.filter(slug=slug).exists():
|
||||||
|
raise ValidationError(
|
||||||
|
_('Can not add redirecting slug “%s”: it is already used elsewhere.') % slug
|
||||||
|
)
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
if 'geometry' in self.fields:
|
if 'geometry' in self.fields:
|
||||||
if not self.cleaned_data.get('geometry'):
|
if not self.cleaned_data.get('geometry'):
|
||||||
|
@ -58,11 +84,12 @@ class MapitemFormMixin(ModelForm):
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
_('You have to select a title in at least one language.')
|
_('You have to select a title in at least one language.')
|
||||||
)
|
)
|
||||||
|
|
||||||
super().clean()
|
super().clean()
|
||||||
|
|
||||||
|
|
||||||
def create_editor_form(editor_model):
|
def create_editor_form(editor_model):
|
||||||
possible_fields = ['name', 'altitude', 'level', 'category', 'width', 'groups', 'color', 'public',
|
possible_fields = ['slug', 'name', 'altitude', 'level', 'category', 'width', 'groups', 'color', 'public',
|
||||||
'can_search', 'can_describe', 'outside', 'stuffed', 'geometry',
|
'can_search', 'can_describe', 'outside', 'stuffed', 'geometry',
|
||||||
'left', 'top', 'right', 'bottom']
|
'left', 'top', 'right', 'bottom']
|
||||||
field_names = [field.name for field in editor_model._meta.get_fields()]
|
field_names = [field.name for field in editor_model._meta.get_fields()]
|
||||||
|
|
|
@ -185,6 +185,14 @@ def edit(request, pk=None, model=None, section=None, space=None, explicit_edit=F
|
||||||
if title:
|
if title:
|
||||||
obj.titles[language] = title
|
obj.titles[language] = title
|
||||||
|
|
||||||
|
if form.redirect_slugs is not None:
|
||||||
|
for slug in form.add_redirect_slugs:
|
||||||
|
obj.redirects.create(slug=slug)
|
||||||
|
|
||||||
|
for slug in form.remove_redirect_slugs:
|
||||||
|
obj.redirects.filter(slug=slug).delete()
|
||||||
|
|
||||||
|
|
||||||
if not settings.DIRECT_EDITING:
|
if not settings.DIRECT_EDITING:
|
||||||
# todo: suggest changes
|
# todo: suggest changes
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
24
src/c3nav/mapdata/migrations/0005_auto_20170527_1556.py
Normal file
24
src/c3nav/mapdata/migrations/0005_auto_20170527_1556.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.10.7 on 2017-05-27 15:56
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mapdata', '0004_space_level_category_name'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterModelOptions(
|
||||||
|
name='locationslug',
|
||||||
|
options={'verbose_name': 'Location with Slug', 'verbose_name_plural': 'Lucation with Slug'},
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='locationslug',
|
||||||
|
name='slug',
|
||||||
|
field=models.SlugField(null=True, unique=True, verbose_name='slug'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -18,7 +18,7 @@ class LocationSlug(SerializableMixin, models.Model):
|
||||||
'LocationGroup': 'g'
|
'LocationGroup': 'g'
|
||||||
}
|
}
|
||||||
LOCATION_TYPE_BY_CODE = {code: model_name for model_name, code in LOCATION_TYPE_CODES.items()}
|
LOCATION_TYPE_BY_CODE = {code: model_name for model_name, code in LOCATION_TYPE_CODES.items()}
|
||||||
slug = models.SlugField(_('name'), unique=True, null=True, max_length=50)
|
slug = models.SlugField(_('slug'), unique=True, null=True, max_length=50)
|
||||||
|
|
||||||
def get_child(self):
|
def get_child(self):
|
||||||
# todo: cache this
|
# todo: cache this
|
||||||
|
@ -38,8 +38,8 @@ class LocationSlug(SerializableMixin, models.Model):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _('Slug for Location')
|
verbose_name = _('Location with Slug')
|
||||||
verbose_name_plural = _('Slugs für Locations')
|
verbose_name_plural = _('Lucation with Slug')
|
||||||
default_related_name = 'locationslugs'
|
default_related_name = 'locationslugs'
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue