Exemplo n.º 1
0
def card_add(request, did):
    description = Description.objects.get(pk=did)
    StageFormset = inlineformset_factory(Card, Stage, form=StageForm, extra=1)
    if request.method == 'POST':
        cardform = CardForm(request.POST)
        table1form = Table1Form(request.POST)
        table2form = Table2Form(request.POST)
        table3form = Table3Form(request.POST)
        table4form = Table4Form(request.POST)
        table31form = Table31Form(request.POST)
        table32form = Table32Form(request.POST)
        stageformset = StageFormset(request.POST)
        if cardform.is_valid() and table1form.is_valid() and table2form.is_valid() and table3form.is_valid() and table4form.is_valid() and table31form.is_valid() and table32form.is_valid():
            card = cardform.save(commit=False)
            card.description = description
            card.save()
            table1 = table1form.save(commit=False)
            table1.card = card
            table1.save()
            table2 = table2form.save(commit=False)
            table2.card = card
            table2.save()
            table3 = table3form.save(commit=False)
            table3.card = card
            table3.save()
            table4 = table4form.save(commit=False)
            table4.card = card
            table4.save()
            table31 = table31form.save(commit=False)
            table31.card = card
            table31.save()
            table32 = table32form.save(commit=False)
            table32.card = card
            table32.save()
            stageformset = StageFormset(request.POST, instance=card)
            if stageformset.is_valid():
                stageformset.save()
            return HttpResponseRedirect(card.get_absolute_url())
    else:
        cardform = CardForm()
        table1form = Table1Form()
        table2form = Table2Form()
        table3form = Table3Form()
        table4form = Table4Form()
        table31form = Table31Form()
        table32form = Table32Form()
        stageformset = StageFormset()
    content = {
        'description': description,
        'cardform': cardform,
        'table1form': table1form,
        'table2form': table2form,
        'table3form': table3form,
        'table4form': table4form,
        'table31form': table31form,
        'table32form': table32form,
        'stageformset': stageformset,
    }
    content.update(csrf(request))
    return render(request, 'card_edit.html', content)
Exemplo n.º 2
0
def card_edit(request, did, cid):
    description = Description.objects.get(pk=did)
    card = Card.objects.get(pk=cid)
    table1 = Table1.objects.get(card=card)
    table2 = Table2.objects.get(card=card)
    table3 = Table3.objects.get(card=card)
    table4 = Table4.objects.get(card=card)
    table31 = Table31.objects.get(card=card)
    table32 = Table32.objects.get(card=card)
    StageFormset = inlineformset_factory(Card, Stage, form=StageForm, extra=1)
    if request.method == 'POST':
        cardform = CardForm(request.POST, instance=card)
        table1form = Table1Form(request.POST, instance=table1)
        table2form = Table2Form(request.POST, instance=table2)
        table3form = Table3Form(request.POST, instance=table3)
        table4form = Table4Form(request.POST, instance=table4)
        table31form = Table31Form(request.POST, instance=table31)
        table32form = Table32Form(request.POST, instance=table32)
        stageformset = StageFormset(request.POST, instance=card)
        if cardform.is_valid() and table1form.is_valid(
        ) and table2form.is_valid() and table3form.is_valid(
        ) and table4form.is_valid() and table31form.is_valid(
        ) and table32form.is_valid() and stageformset.is_valid():
            cardform.save()
            table1form.save()
            table2form.save()
            table3form.save()
            table4form.save()
            table31form.save()
            table32form.save()
            stageformset.save()
            return HttpResponseRedirect(card.get_absolute_url())
    else:
        cardform = CardForm(instance=card)
        table1form = Table1Form(instance=table1)
        table2form = Table2Form(instance=table2)
        table3form = Table3Form(instance=table3)
        table4form = Table4Form(instance=table4)
        table31form = Table31Form(instance=table31)
        table32form = Table32Form(instance=table32)
        stageformset = StageFormset(instance=card)
    content = {
        'description': description,
        'card': card,
        'cardform': cardform,
        'table1form': table1form,
        'table2form': table2form,
        'table3form': table3form,
        'table4form': table4form,
        'table31form': table31form,
        'table32form': table32form,
        'stageformset': stageformset,
    }
    content.update(csrf(request))
    return render(request, 'card_edit.html', content)
Exemplo n.º 3
0
def create_card(request):
	if request.method == "POST":
		formset = CardForm(data=request.POST)
		if formset.is_valid():
			formset.save()
			return redirect(to='list_cards')
	else:
		formset = CardForm()

	context = {
		'formset':formset
	}
	return render(request, 'create_card.html', context)
Exemplo n.º 4
0
def add_edit_card(request, deck_id=None, card_id=None):
    if card_id:
        card = get_object_or_404(Card, pk=card_id)
    else:
        card = Card()
    if request.method == "GET":
        card_form = CardForm(instance=card)
    elif request.method == "POST":
        if deck_id is None:
            deck_id = card.deck.id
        card_form = CardForm(data=request.POST,
                             instance=card,
                             deck_id=deck_id)
        if card_form.is_valid():
            card = card_form.save(commit=False)
            if not hasattr(card, "deck"):
                # Card is added.
                deck = get_object_or_404(Deck, pk=deck_id)
                card.deck = deck
                card.save()

                # If new card is added then we need also to add it
                # to train pools so user can train it also from now.
                trainpools = TrainPool.objects.filter(deck=deck)
                for train_pool in trainpools:
                    train_card = TrainCard(card=card)
                    train_card.save()
                    train_pool.train_cards.add(train_card)
                    train_pool.save()
                return redirect(request.path)
            else:
                # Card is edited.
                card.save()
                return redirect(reverse("pamietacz.views.show_deck",
                                        args=(deck_id,)))
    return render(request,
                  "add_edit_card.html",
                  {"card_form": card_form,
                   "action": request.get_full_path()})
Exemplo n.º 5
0
def add_edit_card(request, deck_id=None, card_id=None):
    if card_id:
        card = get_object_or_404(Card, pk=card_id)
    else:
        card = Card()
    if request.method == "GET":
        card_form = CardForm(instance=card)
    elif request.method == "POST":
        if deck_id is None:
            deck_id = card.deck.id
        card_form = CardForm(data=request.POST, instance=card, deck_id=deck_id)
        if card_form.is_valid():
            card = card_form.save(commit=False)
            if not hasattr(card, "deck"):
                # Card is added.
                deck = get_object_or_404(Deck, pk=deck_id)
                card.deck = deck
                card.save()

                # If new card is added then we need also to add it
                # to train pools so user can train it also from now.
                trainpools = TrainPool.objects.filter(deck=deck)
                for train_pool in trainpools:
                    train_card = TrainCard(card=card)
                    train_card.save()
                    train_pool.train_cards.add(train_card)
                    train_pool.save()
                return redirect(request.path)
            else:
                # Card is edited.
                card.save()
                return redirect(
                    reverse("pamietacz.views.show_deck", args=(deck_id, )))
    return render(request, "add_edit_card.html", {
        "card_form": card_form,
        "action": request.get_full_path()
    })
Exemplo n.º 6
0
 def post(self, request):
     print('you go there!')
     form = CardForm(request.POST)
     if form.is_valid():
         form.save()
     return redirect('card-index')