Browse Source

Exception handling, if current item is removed from parent view by user action

master
Dirk Alders 4 years ago
parent
commit
7287643d98
2 changed files with 16 additions and 6 deletions
  1. 8
    5
      views/__init__.py
  2. 8
    1
      views/userviews.py

+ 8
- 5
views/__init__.py View File

@@ -23,7 +23,7 @@ from users.views import profile_pre_actions, profile_post_actions
23 23
 from users.forms import UserProfileFormLanguageOnly
24 24
 from .userviews import get_wrapper_instance as get_userview_wrapper_instance
25 25
 import zipfile
26
-from pygal.views.userviews import query_view
26
+from pygal.views.userviews import query_view, ItemDoesNotEsistWithinParent
27 27
 
28 28
 
29 29
 def pygal_item_does_not_exist(request, context):
@@ -155,7 +155,10 @@ def pygal_userview_infoview(request, responsetype, rel_path):
155 155
     #
156 156
     if w.may_read():
157 157
         context.set_additional_title(w.name)
158
-        return w.render(context)
158
+        try:
159
+            return w.render(context)
160
+        except ItemDoesNotEsistWithinParent:
161
+            return redirect('?'.join([request.META['PATH_INFO'].replace(rel_path, ''), request.META['QUERY_STRING']]))
159 162
     else:
160 163
         return pygal_access_denied(request, context)
161 164
 
@@ -244,7 +247,7 @@ def pygal_addtag(request, rel_path):
244 247
         tag_context_adaption(context, w, request, get_item_type(full_path) in [TYPE_IMAGE])
245 248
         if request.POST:
246 249
             tag = Tag(item=w.item)
247
-            form = TagForm(request.POST, instance=tag, factor_to_original=context['factor_to_original'])
250
+            form = TagForm(request.POST, instance=tag, factor_to_original=context.get('factor_to_original'))
248 251
             if form.is_valid():
249 252
                 form.save()
250 253
                 messages.info(request, _('Thanks for adding a Tag to %s') % w.name)
@@ -277,7 +280,7 @@ def pygal_tagedit(request, tag_id):
277 280
         context['enable_delete_button'] = True
278 281
         if request.POST:
279 282
             if request.POST.get('save'):
280
-                form = TagForm(request.POST, instance=t, factor_to_original=context['factor_to_original'])
283
+                form = TagForm(request.POST, instance=t, factor_to_original=context.get('factor_to_original'))
281 284
                 if form.is_valid():
282 285
                     form.save()
283 286
                     messages.info(request, _('Thanks for editing Tag %(tag_id)d to %(item_name)s') % {'tag_id': tag_id, 'item_name': w.name})
@@ -287,7 +290,7 @@ def pygal_tagedit(request, tag_id):
287 290
                 messages.info(request, _('Tag %(tag_id)d of %(item_name)s has been deleted.') % {'tag_id': tag_id, 'item_name': w.name})
288 291
                 return redirect(get_next(request))
289 292
         else:
290
-            form = TagForm(instance=t, factor_to_original=context['factor_to_original'])
293
+            form = TagForm(instance=t, factor_to_original=context.get('factor_to_original'))
291 294
         context['form'] = form
292 295
         return render(request, 'pygal/tagedit.html', context=context)
293 296
 

+ 8
- 1
views/userviews.py View File

@@ -20,6 +20,10 @@ REPEAT_ENTRY = 'repeat-main'
20 20
 SHUFFLE_ENTRY = 'shuffle-main'
21 21
 
22 22
 
23
+class ItemDoesNotEsistWithinParent(Exception):
24
+    pass
25
+
26
+
23 27
 def get_wrapper_class(full_path):
24 28
     return {
25 29
         TYPE_FOLDER: folder_view,
@@ -260,7 +264,10 @@ class base_view(object):
260 264
         if direction not in [-1, 1]:
261 265
             raise ValueError("Parameter direction is incorrect: %s" % repr(direction))
262 266
         fp_il = random_copy(self.request, self.__parent__().sorted_fullpathlist())
263
-        i = fp_il.index(self.full_path)
267
+        try:
268
+            i = fp_il.index(self.full_path)
269
+        except ValueError as e:
270
+            raise ItemDoesNotEsistWithinParent(e)
264 271
         lgt = len(fp_il)
265 272
         for i in range(i + direction, i + direction * lgt, direction):
266 273
             full_path = fp_il[i % lgt]

Loading…
Cancel
Save