def page(request, num=None): unit = Unit.objects.get(id=int(num)) if request.method == 'GET': items = {} for item in Item.objects.filter(unit=unit): if item.item_type in items: items[item.item_type].append(item) else: items[item.item_type] = [item] item_types = get_item_types() c = { "unit": unit, "item_types": item_types, "item_types_dict": dict(item_types), "items": items } c.update(csrf(request)) return render_to_response("unit.html", c, context_instance=RequestContext(request)) elif request.method == 'POST': item = Item(content=request.POST.get("content"), item_type=request.POST.get("item_type"), unit=unit, created_by=request.user, updated_by=request.user) item.save() return redirect(reverse(page, kwargs={"num": int(unit.id)}))
def save_item(request, unit_id=None, item_id=None): unit = Unit.objects.get(id=int(unit_id)) if request.method == 'GET': item = Item.objects.get(id=int(item_id)) if item_id is not None else None item_types = get_item_types() c = { "unit": unit, "item_types": item_types, "item": item, "itype": request.GET.get("item_type") } c.update(csrf(request)) return render_to_response("save_item.html", c, context_instance=RequestContext(request)) elif request.method == 'POST': if item_id is None: item = Item(content=request.POST.get("content"), item_type=request.POST.get("item_type"), unit=unit, created_by=request.user, updated_by=request.user) else: item = Item.objects.get(id=int(item_id), unit=unit) item.item_type = request.POST.get("item_type") item.content = request.POST.get("content") if request.POST.get("set_color") == "1": item.color = request.POST.get("color") item.save() return redirect(reverse(page, kwargs={"num": int(unit.id)}))