def item_edit(request, item_id, response_format='html'): "Item edit page" item = get_object_or_404(Item, pk=item_id) if not request.user.get_profile().has_permission(item, mode="w"): return user_denied(request, message="You don't have write access to this Item", response_format=response_format) if request.POST: if not 'cancel' in request.POST: form = ItemForm(request.user.get_profile(), item.item_type, request.POST, files=request.FILES, instance=item) if form.is_valid(): item = form.save(request) return HttpResponseRedirect(reverse('infrastructure_item_view', args=[item.id])) else: return HttpResponseRedirect(reverse('infrastructure_item_view', args=[item.id])) else: form = ItemForm(request.user.get_profile(), item.item_type, instance=item) context = _get_default_context(request) context.update({'item': item, 'form': form }) return render_to_response('infrastructure/item_edit', context, context_instance=RequestContext(request), response_format=response_format)
def update(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST pkfield = kwargs.get(self.model._meta.pk.name) or request.data.get(self.model._meta.pk.name) if not pkfield: return rc.BAD_REQUEST item = getOrNone(self.model, pkfield) if not item: return rc.NOT_FOUND if not request.user.get_profile().has_permission(item, mode="w"): return rc.FORBIDDEN attrs = self.flatten_dict(request) form = ItemForm( item_type=item.item_type, instance=item, **attrs) if form.is_valid(): item = form.save(request) return item else: self.status = 400 return form.errors
def item_add_typed(request, type_id, response_format='html'): "Item add with preselected type" item_type = get_object_or_404(ItemType, pk=type_id) if not request.user.get_profile().has_permission(item_type, mode='x'): return user_denied(request, message="You don't have access to create " + unicode(item_type), response_format=response_format) if request.POST: if not 'cancel' in request.POST: form = ItemForm(request.user.get_profile(), item_type, request.POST, files=request.FILES) if form.is_valid(): item = form.save(request) return HttpResponseRedirect(reverse('infrastructure_item_view', args=[item.id])) else: return HttpResponseRedirect(reverse('infrastructure_index')) else: form = ItemForm(request.user.get_profile(), item_type) context = _get_default_context(request) context.update({'item_type': item_type, 'form': form}) return render_to_response('infrastructure/item_add_typed', context, context_instance=RequestContext(request), response_format=response_format)
def create(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST item_type = getOrNone(ItemType, request.data.get('type')) if not item_type or not request.user.get_profile().has_permission(item_type, mode='x'): return rc.FORBIDDEN attrs = self.flatten_dict(request) form = ItemForm( item_type=item_type, **attrs) if form.is_valid(): item = form.save(request) return item else: self.status = 400 return form.errors