Editor: edit & delete features

This commit is contained in:
Laura Klünder 2016-09-26 13:32:05 +02:00
parent 6ce96148d7
commit 576b33fd1a
13 changed files with 239 additions and 31 deletions

View file

@ -1,10 +1,12 @@
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.http.response import Http404
from django.shortcuts import render
from django.shortcuts import get_object_or_404, render
from c3nav.editor.forms import FeatureForm
from c3nav.mapdata.models.feature import FEATURE_TYPES
from c3nav.mapdata.models.feature import FEATURE_TYPES, Feature
from c3nav.mapdata.permissions import can_access_package
def add_feature(request, feature_type):
@ -37,3 +39,47 @@ def add_feature(request, feature_type):
'path': request.path,
'new': True
})
def edit_feature(request, name):
feature = get_object_or_404(Feature, name=name)
if not can_access_package(feature.package):
raise PermissionDenied
feature_type = FEATURE_TYPES.get(feature.feature_type)
if request.method == 'POST':
if request.POST.get('delete') == '1':
if request.POST.get('delete_confirm') == '1':
feature.delete()
return render(request, 'editor/feature_success.html', {})
return render(request, 'editor/feature_delete.html', {
'name': feature.name,
'feature_type': feature_type,
'path': request.path
})
form = FeatureForm(instance=feature, data=request.POST, feature_type=feature_type)
if form.is_valid():
if not settings.DIRECT_EDITING:
return render(request, 'editor/feature_success.html', {})
with transaction.atomic():
feature = form.instance
feature.feature_type = feature_type.name
feature.titles = {}
for language, title in form.titles.items():
if title:
feature.titles[language] = title
feature.save()
return render(request, 'editor/feature_success.html', {})
else:
form = FeatureForm(instance=feature, feature_type=feature_type)
return render(request, 'editor/feature.html', {
'form': form,
'feature_type': feature_type,
'path': request.path,
'new': False
})