Exemple #1
0
def edit(request, slug=""):
    item = get_object_or_404(Item, slug=slug)
    if(item.seller != request.user):
        request.flash['message'] = "Sorry you are not authorised to edit this item"
        return HttpResponseRedirect(reverse('my_listing'))

    form = ItemForm(initial={
        'name': item.name,
        'price': round(float(item.price),2),
        'description': item.description,
        'condition': item.condition,
        'longitude': item.longitude,
        'latitude': item.latitude,
        'category': item.category
    })

    if request.method == "POST":
        form = ItemForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.cleaned_data
            image1 = data['image1']
            image2 = data['image2']
            image3 = data['image3']
            if not ((is_image(image1) and is_image(image2) and is_image(image3))):
                request.flash['message'] = "Sorry please upload a picture in an acceptable format"
                return HttpResponseRedirect(reverse('add_item'))
            item.name = data['name']
            item.description = data['description']
            if data['condition']:
                item.condition = data['condition']
            else:
                item.condition = 0
            item.price = round(float(data['price']),2)
            item.longitude = data['longitude'] or 0
            item.latitude = data['latitude'] or 0
            item.seller = request.user
            item.category = data['category']
            if data['is_service']:
                item.is_service = data['is_service']
            else:
                item.is_service = False

            item.save()
            upload_item_images(image1, image2, image3, item)
            request.flash['message'] = "Item saved successfully"

            return HttpResponseRedirect(reverse('my_listing'))
        else:
            request.flash['message'] = "Form data is not valid"

    return render_to_response("items/edit.html", {'form': form, 'slug': slug, 'item': item},
        context_instance=RequestContext(request))
Exemple #2
0
def add(request):
#    user, item = get_user_profile(request.user.id)
    form = ItemForm()
    if request.method == "POST":
        form = ItemForm(request.POST, request.FILES)
        if form.is_valid():
            form.cleaned_data['seller'] = request.user
            data = form.cleaned_data
            image1 = data['image1']
            image2 = data['image2']
            image3 = data['image3']
            if not (is_image(image1) and is_image(image2) and is_image(image3)):
                request.flash['message'] = "Sorry please upload a picture in an acceptable format"
                return HttpResponseRedirect(reverse('add_item'))
            item = Item(
                name=data['name'],
                description=data['description'],
                condition=data['condition']or 0,
                price=round(float(data['price']),2),
                longitude=data['longitude'] or 0,
                latitude=data['latitude'] or 0,
                seller=request.user,
                category=data['category'],
                location=data['location'],
                is_published = True,
                is_service = data['is_service']
            )

            item_obj = item.save()
            upload_item_images(image1, image2, image3, item)
            request.flash['message'] = "Item saved successfully"

            #            else:
            #                request.flash['message'] = "Sorry can't save item"
            return HttpResponseRedirect(reverse('my_listing'))
        else:
            request.flash['message'] = "Invalid data"

    return render_to_response("items/add.html", {'form': form}, context_instance=RequestContext(request))