예제 #1
0
def item_save(request, item_id):
    categories = Category.objects.all().order_by('name')
    error = False
    error_message = []

    if(item_id == 0 or item_id == '0'):
        item = Item()
        item.state = STATE_AVAILABLE
    else:
        item = get_object_or_404(Item, pk=item_id)
    item.name = request.POST['name']
    item.description = request.POST['description']
    item.condition = request.POST['condition']

    if len(item.name) == 0:
        error = True
        error_message.append("El nombre es requerido")

    if len(item.condition) == 0:
        error = True
        error_message.append("La condicion es requerida")

    article_id = request.POST['article']
    if article_id != 0 and article_id != '0':
        item.article = Article.objects.get(id=article_id)
    else:
        item.article = None

    if error:
        articles = Article.objects.all().order_by('name')
        context = {
            'categories': categories,
            'item': item,
            'error_message': error_message,
            'articles': articles,
        }
        return render(request, 'item_add.html', context)
    else:
        item.save()
        article = Article.objects.get(pk=item.article.id)
        context = {
            'categories': categories,
            'article': article,
            'success_message': 'El item ' + item.name + ' ha sido eliminado exitosamente.',
        }
        return render(request, 'article.html', context)
예제 #2
0
def item_add(request, article_id):
    categories = Category.objects.all().order_by('name')
    item = Item()
    item.article = get_object_or_404(Article, pk=article_id)
    articles = Article.objects.all().order_by('name')
    context = {
        'categories': categories,
        'item': item,
        'articles': articles,
        'STATE_CHOICES': STATE_CHOICES,
        'CONDITION_CHOICES': CONDITION_CHOICES,
    }
    return render(request, 'item_add.html', context)