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
 from users.forms import UserProfileFormLanguageOnly
23
 from users.forms import UserProfileFormLanguageOnly
24
 from .userviews import get_wrapper_instance as get_userview_wrapper_instance
24
 from .userviews import get_wrapper_instance as get_userview_wrapper_instance
25
 import zipfile
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
 def pygal_item_does_not_exist(request, context):
29
 def pygal_item_does_not_exist(request, context):
155
     #
155
     #
156
     if w.may_read():
156
     if w.may_read():
157
         context.set_additional_title(w.name)
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
     else:
162
     else:
160
         return pygal_access_denied(request, context)
163
         return pygal_access_denied(request, context)
161
 
164
 
244
         tag_context_adaption(context, w, request, get_item_type(full_path) in [TYPE_IMAGE])
247
         tag_context_adaption(context, w, request, get_item_type(full_path) in [TYPE_IMAGE])
245
         if request.POST:
248
         if request.POST:
246
             tag = Tag(item=w.item)
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
             if form.is_valid():
251
             if form.is_valid():
249
                 form.save()
252
                 form.save()
250
                 messages.info(request, _('Thanks for adding a Tag to %s') % w.name)
253
                 messages.info(request, _('Thanks for adding a Tag to %s') % w.name)
277
         context['enable_delete_button'] = True
280
         context['enable_delete_button'] = True
278
         if request.POST:
281
         if request.POST:
279
             if request.POST.get('save'):
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
                 if form.is_valid():
284
                 if form.is_valid():
282
                     form.save()
285
                     form.save()
283
                     messages.info(request, _('Thanks for editing Tag %(tag_id)d to %(item_name)s') % {'tag_id': tag_id, 'item_name': w.name})
286
                     messages.info(request, _('Thanks for editing Tag %(tag_id)d to %(item_name)s') % {'tag_id': tag_id, 'item_name': w.name})
287
                 messages.info(request, _('Tag %(tag_id)d of %(item_name)s has been deleted.') % {'tag_id': tag_id, 'item_name': w.name})
290
                 messages.info(request, _('Tag %(tag_id)d of %(item_name)s has been deleted.') % {'tag_id': tag_id, 'item_name': w.name})
288
                 return redirect(get_next(request))
291
                 return redirect(get_next(request))
289
         else:
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
         context['form'] = form
294
         context['form'] = form
292
         return render(request, 'pygal/tagedit.html', context=context)
295
         return render(request, 'pygal/tagedit.html', context=context)
293
 
296
 

+ 8
- 1
views/userviews.py View File

20
 SHUFFLE_ENTRY = 'shuffle-main'
20
 SHUFFLE_ENTRY = 'shuffle-main'
21
 
21
 
22
 
22
 
23
+class ItemDoesNotEsistWithinParent(Exception):
24
+    pass
25
+
26
+
23
 def get_wrapper_class(full_path):
27
 def get_wrapper_class(full_path):
24
     return {
28
     return {
25
         TYPE_FOLDER: folder_view,
29
         TYPE_FOLDER: folder_view,
260
         if direction not in [-1, 1]:
264
         if direction not in [-1, 1]:
261
             raise ValueError("Parameter direction is incorrect: %s" % repr(direction))
265
             raise ValueError("Parameter direction is incorrect: %s" % repr(direction))
262
         fp_il = random_copy(self.request, self.__parent__().sorted_fullpathlist())
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
         lgt = len(fp_il)
271
         lgt = len(fp_il)
265
         for i in range(i + direction, i + direction * lgt, direction):
272
         for i in range(i + direction, i + direction * lgt, direction):
266
             full_path = fp_il[i % lgt]
273
             full_path = fp_il[i % lgt]

Loading…
Cancel
Save