def edit_advert(request, advert_id): adv = Advert.objects.get(pk=advert_id) contact = ContactInfo.objects.get(pk=adv.contact.id) if request.method == 'POST': advertform = AdvertForm(request.POST, request.FILES, instance=adv) contactform = ContactInfoForm(request.POST, request.FILES, instance=contact) if advertform.is_valid() and contactform.is_valid(): newadvert = advertform.save(commit=False) newadvert.planning = unicode(newadvert.planning) newadvert.poster = unicode(newadvert.poster) #realtypeid = request.POST['realtype'] realtypeid = 2 newadvert.realtype = RealType.objects.get(pk=realtypeid) streetid = request.POST['street'] newadvert.street = Street.objects.get(pk=streetid) newadvert.save() newadvert.save_m2m() contact = contactform.save() contact.save() return HttpResponseRedirect('/advert/%s/' % advert_id) else: advertform = AdvertForm(instance=adv,error_class=DivErrorList) contactform = ContactInfoForm(instance=contact,error_class=DivErrorList) return { 'form': advertform, 'contactform':contactform, 'logform': RegistrationFormUniqueEmail, }
def new_advert(request): if request.method == 'POST': advertform = AdvertForm(request.POST, request.FILES) contactform = ContactInfoForm(request.POST, request.FILES) if advertform.is_valid() and contactform.is_valid(): newadvert = advertform.save(commit=False) newadvert.planning = newadvert.planning newadvert.poster = newadvert.poster newadvert.user = request.user newadvert.contact = contactform.save() newadvert.save() newadvert.save_m2m() return HttpResponseRedirect('/advert/thanks/') else: advertform = AdvertForm( initial = {'user':request.user}, error_class=DivErrorList, # instance='advert_add', ) contactform = ContactInfoForm( error_class=DivErrorList, # instance='contact_from', ) return { 'form': advertform, 'contactform':contactform, 'logform': RegistrationFormUniqueEmail, }
def new_advert(): ''' Create new advert ''' form = AdvertForm() if form.validate_on_submit(): my_advert = Advert() form.populate_obj(my_advert) db.session.add(my_advert) db.session.commit() flash('Advert created correctly', 'success') try: # User info desc = my_advert.description return redirect(url_for('tweet',desc=desc)) except: db.session.rollback() flash('Error generating advert.', 'danger') return render_template('web/new_advert.html', form=form)
def edit_advert(id): ''' Edit advert :param id: Id from advert ''' my_advert = Advert.query.filter_by(id=id).first() my_advert.date = datetime.strptime(my_advert.date, '%Y-%m-%d') date = my_advert.date form = AdvertForm(obj=my_advert) if form.validate_on_submit(): try: # Update contact form.populate_obj(my_advert) db.session.add(my_advert) db.session.commit() # User info flash('Saved successfully', 'success') except: db.session.rollback() flash('Error update advert.', 'danger') return render_template( 'web/edit_advert.html',date = date, form=form)