def move_modify(request, move_id): """Modify an existing move in the database: :move_id: the unique id of the move being modified""" current_move = get_object_or_404(Move, pk=move_id) demo_vids = MoveDemoVideoFormset(request.POST or None, instance=current_move, prefix='demo') tutorial_vids = MoveTutorialVideoFormset(request.POST or None, instance=current_move, prefix='tutorial') try: #load tips if possible existing_tips = MoveTips.objects.get(move=current_move) tips_form = TipsForm(request.POST or {'tips': existing_tips.tips.raw}) except MoveTips.DoesNotExist: existing_tips = None tips_form = TipsForm(request.POST or None) data = { 'name': current_move.name, } edit_form = MoveEditForm(data) component_sequence = ComponentSequenceFormset(request.POST or None, instance=current_move, prefix='components') if demo_vids.is_valid() and tutorial_vids.is_valid() and component_sequence.is_valid() and edit_form.is_valid() and tips_form.is_valid(): demo_vids.save() tutorial_vids.save() component_sequence.save() if existing_tips: existing_tips.tips.raw = tips_form.cleaned_data.get("tips") existing_tips.save() else: MoveTips.objects.create( move=current_move, tips=tips_form.cleaned_data.get("tips"), tips_markup_type='markdown', ) return HttpResponseRedirect(reverse('move_detail', args=[current_move.slug])) context = RequestContext(request, { 'add_new': False, #Flag for template 'move_name': current_move.name, 'edit_form': edit_form, 'component_sequence': component_sequence, 'tips_form': tips_form, 'demo_vids': demo_vids, 'tutorial_vids': tutorial_vids, }) template = loader.get_template('footbagmoves/move_modify.html') return HttpResponse(template.render(context))
def move_new(request): """Add a new move to the database""" new_move = Move() edit_form = MoveEditForm(request.POST or None) component_sequence = ComponentSequenceFormset(request.POST or None, instance=new_move, prefix='components') tips_form = TipsForm(request.POST or None) demo_vids = MoveDemoVideoFormset(request.POST or None, instance=new_move, prefix='demo') tutorial_vids = MoveTutorialVideoFormset(request.POST or None, instance=new_move, prefix='tutorial') if demo_vids.is_valid() and tutorial_vids.is_valid() and component_sequence.is_valid() and edit_form.is_valid(): new_move.name = edit_form.cleaned_data.get("name") existing_moves = Move.objects.filter(slug=slugify(new_move.name)) if existing_moves: return HttpResponse("Error saving: move with slug {0} already exists!".format(slugify(new_move.name))) else: new_move.save() demo_vids.save() component_sequence.save() if tips_form.is_valid(): MoveTips.objects.create( move=new_move, tips=tips_form.cleaned_data.get("tips"), tips_markup_type='markdown', ) return HttpResponseRedirect(reverse('move_detail', args=[new_move.slug])) context = RequestContext(request, { 'add_new': True, #Flag for template 'edit_form': edit_form, 'component_sequence': component_sequence, 'tips_form': tips_form, 'demo_vids': demo_vids, 'tutorial_vids': tutorial_vids, }) template = loader.get_template('footbagmoves/move_modify.html') return HttpResponse(template.render(context))