def details(request, item_pk): # Generic check to see if user has access to dashboard. (In Komiteer or superuser) if not has_access(request): raise PermissionDenied # Create the base context needed for the sidebar context = get_base_context(request) context['item'] = get_object_or_404(Item, pk=item_pk) if request.method == 'POST': if 'inventory.change_item' not in context['user_permissions']: raise PermissionDenied item_form = ItemForm(request.POST, instance=context['item']) if not item_form.is_valid(): messages.error(request, 'Noen av de påkrevde feltene inneholder feil.') else: item_form.save() messages.success(request, 'Varen ble oppdatert') context['form'] = item_form else: context['form'] = ItemForm(instance=context['item']) context['new_batch_form'] = BatchForm() context['batch_forms'] = [ (b.id, BatchForm(instance=b)) for b in Batch.objects.filter(item=context['item']) ] return render(request, 'inventory/dashboard/details.html', context)
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