def batch_new(request, item_pk): if not has_access(request): raise PermissionDenied # Field mapper fieldmap = { 'amount': 'Mengde', 'expiration_date': 'Utløpsdato', } item = get_object_or_404(Item, pk=item_pk) if request.method == 'POST': batch_form = BatchForm(request.POST) if not batch_form.is_valid(): # Dirty hack to display errors since the form is not passed in redirect context error_reply = "Feil i felt:" for field, error in batch_form.errors.items(): error_reply += ' ' + fieldmap[ field] + ' (' + batch_form.error_class.as_text( error) + '),' messages.error(request, error_reply.rstrip(',')) else: b = batch_form.save(commit=False) b.item = item b.save() messages.success(request, 'Batchen ble lagt til.') return redirect(details, item_pk=item_pk) raise PermissionDenied
def batch_new(request, item_pk): if not has_access(request): raise PermissionDenied # Field mapper fieldmap = {"amount": "Mengde", "expiration_date": "Utløpsdato"} item = get_object_or_404(Item, pk=item_pk) if request.method == "POST": batch_form = BatchForm(request.POST) if not batch_form.is_valid(): # Dirty hack to display errors since the form is not passed in redirect context error_reply = "Feil i felt:" for field, error in batch_form.errors.items(): error_reply += (" " + fieldmap[field] + " (" + batch_form.error_class.as_text(error) + "),") messages.error(request, error_reply.rstrip(",")) else: b = batch_form.save(commit=False) b.item = item b.save() messages.success(request, "Batchen ble lagt til.") return redirect(details, item_pk=item_pk) raise PermissionDenied
def batch(request, item_pk, batch_pk): if not has_access(request): raise PermissionDenied # Get base context get_object_or_404(Item, pk=item_pk) b = get_object_or_404(Batch, pk=batch_pk) if request.method == 'POST': batch_form = BatchForm(request.POST, instance=b) if not batch_form.is_valid(): messages.error(request, 'Noen av de påkrevde feltene inneholder feil.') else: batch_form.save() messages.success(request, 'Batchen ble oppdatert.') return redirect(details, item_pk=item_pk) raise PermissionDenied