def new_ad(request):
    if request.method == 'POST':
        form = AdForm(request.POST, request.FILES)

        if form.is_valid():
            title_url = title_url1 = slugify(form.cleaned_data['title'])
            tmp = 0
            while Ad.objects.filter(title_url=title_url1).count() > 0:
                tmp += 1
                title_url1 = title_url + '-' + str(tmp)
            title_url = title_url1
            
            ad = Ad(user_id=request.user.id, title = form.cleaned_data['title'])
            ad.city = form.cleaned_data['city']
            ad.address = form.cleaned_data['address']
            ad.psc = form.cleaned_data['psc']
            ad.category = form.cleaned_data['category']
            ad.text_of_ad = form.cleaned_data['text_of_ad']
            ad.type_of_ad = form.cleaned_data['type_of_ad']
            ad.is_new = form.cleaned_data['is_new']
            ad.is_antique = form.cleaned_data['is_antique']
            ad.is_inventory = form.cleaned_data['is_inventory']
            ad.can_be_reservated = form.cleaned_data['can_be_reservated']
            ad.time_of_publication = datetime.datetime.now()
            ad.title_url = title_url
            
            ad.latitude, ad.longitude = getGPS(ad.address, ad.city.city, ad.psc)
            ad.save()
            
            if form.cleaned_data['image1'] != None:
                image1 = AdImage(ad_id=ad.id, image=form.cleaned_data['image1'])
                image1.save()
            if form.cleaned_data['image2'] != None:
                image2 = AdImage(ad_id=ad.id, image=form.cleaned_data['image2'])
                image2.save()
            if form.cleaned_data['image3'] != None:
                image3 = AdImage(ad_id=ad.id, image=form.cleaned_data['image3'])
                image3.save()
            if form.cleaned_data['image4'] != None:
                image4 = AdImage(ad_id=ad.id, image=form.cleaned_data['image4'])
                image4.save()
            
            messages.info(request, 'Vaše inzerát bol pridaný')

            return HttpResponseRedirect('/')
    else:
        form = AdForm(initial={'type_of_ad': True})
    page_info = {}
    page_info['title'] = 'Informácie o Vás - Váš profil'
    page_info['page'] = 0
    page_info['form_name'] = 'new_ad'
    page_info['form_action'] = '/pridat-inzerat/'

    return render_to_response('pridaj_inzerat.html', {'form': form, 'countInfo': countInfo, 'recentNews': recentNews, 
                                               'page_info': page_info}, 
                              context_instance=RequestContext(request))
Esempio n. 2
0
def edit(request, ad_id=1):
    ad = get_object_or_404(Ad, id=ad_id)
    
    if request.POST:
        form = AdForm(request.POST, instance=ad)
        if form.is_valid():
            form.save()
            return redirect('/')
    else:
        form = AdForm(instance=ad)

    return render(request, 'ads/edit-ad.html', ({
        'form': form, 'ad': ad,
    }))
Esempio n. 3
0
def create(request):
    if request.POST:
        form = AdForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')
    else:
        form = AdForm

    args = {}
    args.update(csrf(request))

    args['form'] = form

    return render(request, 'ads/create-ad.html', args)