def modify_extract(request): """Modify an extract. Returns: HttpResponse """ data = request.POST try: extract_pk = request.POST['extract'] except KeyError: raise Http404 extract = get_object_or_404(Extract, pk=extract_pk) chapter = extract.chapter if 'delete' in data: old_pos = extract.position_in_chapter # Move other extracts in same chapter for extract_c in extract.chapter.get_extracts(): if old_pos <= extract_c.position_in_chapter: extract_c.position_in_chapter = extract_c.position_in_chapter \ - 1 extract_c.save() tutorial = extract.chapter.get_tutorial() extract.delete() tutorial.update = datetime.now() tutorial.save() return redirect(chapter.get_absolute_url()) elif 'move' in data: try: new_pos = int(request.POST['move_target']) # Error, the user misplayed with the move button except ValueError: return redirect(extract.get_absolute_url()) move(extract, new_pos, 'position_in_chapter', 'chapter', 'get_extracts') extract.save() tutorial = extract.chapter.get_tutorial() tutorial.update = datetime.now() tutorial.save() return redirect(extract.get_absolute_url()) raise PermissionDenied
def modify_part(request): """Modifiy a part. Returns: HttpResponse """ part_pk = request.POST['part'] part = get_object_or_404(Part, pk=part_pk) # Make sure the user is allowed to do that if request.user not in part.tutorial.authors.all(): raise PermissionDenied if 'move' in request.POST: try: new_pos = int(request.POST['move_target']) # Invalid conversion, maybe the user played with the move button except ValueError: return redirect(part.tutorial.get_absolute_url()) move(part, new_pos, 'position_in_tutorial', 'tutorial', 'get_parts') part.save() tutorial = part.tutorial tutorial.update = datetime.now() tutorial.save() elif 'delete' in request.POST: # Delete all chapters belonging to the part Chapter.objects.all().filter(part=part).delete() # Move other parts old_pos = part.position_in_tutorial for tut_p in part.tutorial.get_parts(): if old_pos <= tut_p.position_in_tutorial: tut_p.position_in_tutorial = tut_p.position_in_tutorial - 1 tut_p.save() # Actually delete the part tutorial = part.tutorial part.delete() tutorial.update = datetime.now() tutorial.save() return redirect(part.tutorial.get_absolute_url())
def modify_chapter(request): """Modify a chapter. Returns: HttpResponse """ data = request.POST try: chapter_pk = request.POST['chapter'] except KeyError: raise Http404 chapter = get_object_or_404(Chapter, pk=chapter_pk) # Make sure the user is allowed to do that if request.user not in chapter.get_tutorial().authors.all(): raise PermissionDenied if 'move' in data: try: new_pos = int(request.POST['move_target']) # User misplayed with the move button except ValueError: return redirect(chapter.get_absolute_url()) move(chapter, new_pos, 'position_in_part', 'part', 'get_chapters') chapter.update_position_in_tutorial() chapter.save() messages.info(request, u'Le chapitre a bien été déplacé.') tutorial = chapter.get_tutorial() tutorial.update = datetime.now() tutorial.save() elif 'delete' in data: old_pos = chapter.position_in_part old_tut_pos = chapter.position_in_tutorial # Move other chapters first for tut_c in chapter.part.get_chapters(): if old_pos <= tut_c.position_in_part: tut_c.position_in_part = tut_c.position_in_part - 1 tut_c.save() # Then delete the chapter chapter.delete() # Update all the position_in_tutorial fields for the next chapters for tut_c in Chapter.objects\ .filter(position_in_tutorial__gt=old_tut_pos): tut_c.update_position_in_tutorial() tut_c.save() messages.info(request, u'Le chapitre a bien été supprimé.') tutorial = chapter.get_tutorial() tutorial.update = datetime.now() tutorial.save() return redirect(chapter.part.get_absolute_url()) return redirect(chapter.get_absolute_url())