Beispiel #1
0
def add_item(request):
    print("Request for new item is being processed...")
    print(request.POST['name'])
    print(request.POST['author'])
    print(request.POST['store'])


    i = Item()
    i.item_name = request.POST['name']
    i.item_author = request.POST['author']
    i.item_store = request.POST['store']

    if request.POST.get('priority', 'false') == 'true' or request.POST.get('priority', 'false') == 'yes' or request.POST.get('priority', 'false') == 'on':
            i.item_is_priority = True
    elif request.POST.get('priority', 'false') == 'false' or request.POST.get('priority', 'false') == 'no'\
            or request.POST.get('priority', 'false') is None:
        i.item_is_priority = False

    i.item_category = request.POST['category']

    i.item_purchased = False
    i.item_date_added = timezone.now()


    i.save()

    print("Request processed.")

    return HttpResponseRedirect(reverse('index'))
Beispiel #2
0
def api_add_new_item(request):
    if request.POST:
        if request.POST.get('name', None) is None:
            return HttpResponse("Invalid request (You're missing a required field)", content_type="text/plain")

        i = Item()
        i.item_name = request.POST.get('name', '')
        i.item_author = request.POST.get('author', '')
        i.item_store = request.POST.get('store', '')

        if request.POST.get('priority', 'false') == 'true' or request.POST.get('priority', 'false') == 'yes':
            i.item_is_priority = True
        elif request.POST.get('priority', 'false') == 'false' or request.POST.get('priority', 'false') == 'no'\
                or request.POST.get('priority', 'false') is None:
            i.item_is_priority = False

        i.item_category = request.POST.get('category', '')

        i.item_purchased = False
        i.item_date_added = datetime.datetime.today()

        i.save()

        return HttpResponse(serializers.serialize('json', Item.objects.filter(pk=i.pk)),
                            content_type="application/json", status=200)

    else:
        return HttpResponse("Invalid request (Missing POST data)", content_type="text/plain")