示例#1
0
def item_create(request):
    if request.method == 'GET':
        iform = ItemForm()
        recipe = []

    elif request.method == 'POST':
        recipe = request.POST['recipe']
        if recipe != '':
            recipe = recipe.split('/')
        else:
            recipe = []
        iform = ItemForm(request.POST)
        if iform.is_valid():
            item = iform.save()
            components = []
            for component_name in recipe:
                component = get_object_or_404(Item, name = component_name)
                components.append(component)
            for component in components:
                Composition(whole_id = item.id, component_id = component.id).save()
            return redirect('manager.views.manage')
    
    # this happens when get or post fails
    items = Item.objects.values('name').order_by('name')
    return render(request, 'manager/item_edit.html', {'ItemForm': iform, 'Recipe': recipe, 'Items': items})
示例#2
0
def item_edit(request, item_name):
    item = get_object_or_404(Item, name = item_name)
    if request.method == 'GET':
        iform = ItemForm(instance = item)
        recipe = [Item.objects.get(id = component.component_id).name for component in item.as_a_whole.all()]
    elif request.method == 'POST':
        recipe = request.POST['recipe']
        if recipe != '':
            recipe = recipe.split('/')
        else:
            recipe = []
        iform = ItemForm(request.POST, instance = item)
        if iform.is_valid():
            iform.save()
            components = []
            for component_name in recipe:
                component = get_object_or_404(Item, name = component_name)
                components.append(component)
            item.as_a_whole.all().delete()
            for component in components:
                Composition(whole_id = item.id, component_id = component.id).save()
            return redirect('manager.views.manage')

    items = Item.objects.values('name').order_by('name')
    return render(request, 'manager/item_edit.html', {'ItemForm': iform, 'Recipe': recipe, 'Items': items})