def ad_add(request): addform = AddForm() section = 'new' errors = [] user = request.user msg = u'<a href="/reg">Зарегистрируйтесь</a> или '\ u'<a href="/login">войдите</a>, '\ u'чтобы добавлять объявления.' if not user.is_authenticated(): return render_to_response('msg.html', locals()) if request.method == 'POST' and request.user.is_authenticated(): addform = AddForm(request.POST) if addform.is_valid(): cd = addform.cleaned_data is_sell = True if int(cd['sell_or_buy']) == 0 else False ad = Advert(user=request.user, name=cd['name'], text=cd['text'], category=Category.objects.get(id=int(cd['category'])), price=cd['price'], place=cd['place'], sell=is_sell) if u"post" in request.POST.keys(): ad.save() regenerate_widget() cache.delete('all_ads') cache.delete('main') return HttpResponseRedirect('/') elif u"preview" in request.POST.keys(): preview = True return render_to_response('ad_add.html', locals(), context_instance=RequestContext(request, processors=[common_processor]))
def ad_add(request): addform = AddForm() section = 'new' errors = [] user = request.user msg = u'<a href="/reg">Зарегистрируйтесь</a> или '\ u'<a href="/login">войдите</a>, '\ u'чтобы добавлять объявления.' if not user.is_authenticated(): return render_to_response('msg.html', locals()) if request.method == 'POST' and request.user.is_authenticated(): addform = AddForm(request.POST) if addform.is_valid(): cd = addform.cleaned_data is_sell = True if int(cd['sell_or_buy']) == 0 else False ad = Advert(user=request.user, name=cd['name'], text=cd['text'], category=Category.objects.get(id=int(cd['category'])), price=cd['price'], place=cd['place'], sell=is_sell) if u"post" in request.POST.keys(): ad.save() regenerate_widget() cache.delete('all_ads') cache.delete('main') try: post_to_vk(ad) except Exception as e: print 'Failed to post to vk: ', e subject = 'Failed to post to vk' message = '%s' % e # TODO: remove when this works. send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, ['*****@*****.**'], True) return HttpResponseRedirect('/') elif u"preview" in request.POST.keys(): preview = True return render_to_response('ad_add.html', locals(), context_instance=RequestContext(request, processors=[common_processor]))
def ad_edit(request, num): try: ad = Advert.objects.get(id=num) except Advert.DoesNotExist: raise Http404 user = request.user if ad.user != user: msg = "Нельзя редактировать чужие объявления." return render_to_response("msg.html", locals()) addform = AddForm( { "sell_or_buy": 0 if ad.sell else 1, "name": ad.name, "text": ad.text, "category": ad.category.id, "price": ad.price, "place": ad.place, } ) if request.method == "POST": addform = AddForm(request.POST) if addform.is_valid(): cd = addform.cleaned_data ad.sell = True if int(cd["sell_or_buy"]) == 0 else False ad.name = cd["name"] ad.text = cd["text"] ad.category = Category.objects.get(id=int(cd["category"])) ad.price = cd["price"] ad.place = cd["place"] if u"post" in request.POST.keys(): ad.save() cache.delete("all_ads") cache.delete("main") return HttpResponseRedirect("/") elif u"preview" in request.POST.keys(): preview = True ad_edit = True return render_to_response( "ad_add.html", locals(), context_instance=RequestContext(request, processors=[common_processor]) )
def ad_add(request): addform = AddForm() section = "new" errors = [] user = request.user msg = u'<a href="/reg">Зарегистрируйтесь</a> или ' u'<a href="/login">войдите</a>, ' u"чтобы добавлять объявления." if not user.is_authenticated(): return render_to_response("msg.html", locals()) if request.method == "POST" and request.user.is_authenticated(): addform = AddForm(request.POST) if addform.is_valid(): cd = addform.cleaned_data is_sell = True if int(cd["sell_or_buy"]) == 0 else False ad = Advert( user=request.user, name=cd["name"], text=cd["text"], category=Category.objects.get(id=int(cd["category"])), price=cd["price"], place=cd["place"], sell=is_sell, ) if u"post" in request.POST.keys(): ad.save() regenerate_widget() cache.delete("all_ads") cache.delete("main") return HttpResponseRedirect("/") elif u"preview" in request.POST.keys(): preview = True return render_to_response( "ad_add.html", locals(), context_instance=RequestContext(request, processors=[common_processor]) )
def ad_edit(request, num): try: ad = Advert.objects.get(id=num) except Advert.DoesNotExist: raise Http404 user = request.user if ad.user != user: msg = 'Нельзя редактировать чужие объявления.' return render_to_response('msg.html', locals()) addform = AddForm({'sell_or_buy': 0 if ad.sell else 1, 'name': ad.name, 'text': ad.text, 'category': ad.category.id, 'price': ad.price, 'place': ad.place}) if request.method == 'POST': addform = AddForm(request.POST) if addform.is_valid(): cd = addform.cleaned_data ad.sell = True if int(cd['sell_or_buy']) == 0 else False ad.name = cd['name'] ad.text = cd['text'] ad.category = Category.objects.get(id=int(cd['category'])) ad.price = cd['price'] ad.place = cd['place'] if u"post" in request.POST.keys(): ad.save() cache.delete('all_ads') cache.delete('main') return HttpResponseRedirect('/') elif u"preview" in request.POST.keys(): preview = True ad_edit = True return render_to_response('ad_add.html', locals(), context_instance=RequestContext(request, processors=[common_processor]))