Esempio n. 1
0
def ajax_edit_advert(request):
    advert_id = request.POST['edit_id']
    adv = Advert.objects.get(pk=advert_id)

    value = adv.realtype.subtype_id
    #print 'advert_id=='+advert_id
    #print 'value=='+value
    value = str(value)



    if request.POST:
        if value=='1':
            advertform = AdvertFormEdit(request.POST, request.FILES, instance=adv)
            #print 'value1='+str(value)
            id = 'in1'

        if value=='2':
            advertform = AdvertForm2Edit(request.POST, request.FILES, instance=adv)
            #print 'value2='+str(value)
            id = 'in2'

        FormSet = PhotoFormSet2(request.POST, request.FILES)
        contactform = ContactInfoForm(request.POST, request.FILES)
        c={'form':advertform,'contactform':contactform,'formset':FormSet}
        c.update(csrf(request))



        if advertform.is_valid()  and contactform.is_valid() and  FormSet.is_valid():
            newadvert = advertform.save(commit=False)


            costall = request.POST['usercostforall']
            cost = request.POST['usercost']
            costall = str(costall)
            cost = str(cost)
            square = request.POST['square']
            ceill_height = request.POST['ceill_height']
            square = str(square)
            ceill_height = str(ceill_height)

            c = request.POST['comissionvalue']
            if c=='':
                c=0
            c = str(c)
            contactform.comissionvalue = Decimal(c)
            newadvert.usercost = Decimal(cost)
            newadvert.usercostforall = Decimal(costall)
            newadvert.square = Decimal(square)
            newadvert.ceill_height = Decimal(ceill_height)
            newadvert.user = request.user
            newadvert.contact = contactform.save(commit=False)

            newadvert.contact.logocompany = newadvert.contact.logocompany
            newadvert.contact = contactform.save()
            newadvert.save()
            #newadvert.advoptions.add(advertform.advoptions)
            advertform.save_m2m()

            instantces = FormSet.save(commit=False)
            for intance in instantces:
                intance.advert = newadvert
                intance.save()

            return HttpResponse(simplejson.dumps({'result': 'success'}))

        else:
            response = {}
            for k in advertform.errors:
                #print 'error advert==%s'% k
                response[k] = advertform.errors[k][0]

            for k in contactform.errors:
                #print 'error contact==%s'% k
                response[k] = contactform.errors[k][0]

            dict = {'response': response, 'result': 'error'}
            # c.update(dict);
            return HttpResponse(simplejson.dumps(dict))
Esempio n. 2
0
def send_form(request):
    if request.POST:
        value= request.POST['subrealtype']
        #print 'val=='+value
        if value=='1':
            advertform = AdvertForm(request.POST, request.FILES)
            #print 'value1='+str(value)
            id = 'in1'

        if value=='2':
            advertform = AdvertForm2(request.POST, request.FILES)
            #print 'value2='+str(value)
            id = 'in2'

        FormSet = PhotoFormSet(request.POST, request.FILES)
        contactform = ContactInfoForm(request.POST, request.FILES)
        c={'form':advertform,'contactform':contactform,'formset':FormSet}
        c.update(csrf(request))


        values = request.FILES.items()
        #values.sort()
        #html = []
        #for k, v in values:
        #    print '%s =%s' % (k, v)

        if advertform.is_valid()  and contactform.is_valid() and  FormSet.is_valid():
            newadvert = advertform.save(commit=False)
            if request.POST['add_type_cost']=='all':
                cost = float(request.POST['usercost']) / float(request.POST['square'])
                costall = request.POST['usercost']
                cost = str(cost)

            if request.POST['add_type_cost']=='m2':
                costall = float(request.POST['usercost']) * float(request.POST['square'])
                cost = request.POST['usercost']
                costall = str(costall)


            c = request.POST['comissionvalue']
            if c=='':
                c=0
            c = str(c)
            square = request.POST['square']
            ceill_height = request.POST['ceill_height']
            square = str(square)
            ceill_height = str(ceill_height)
            newadvert.square = Decimal(square)
            newadvert.ceill_height = Decimal(ceill_height)
            contactform.comissionvalue = Decimal(c)
            #print 'cost=='+str(cost)
            #print 'costall=='+str(costall)
            newadvert.usercost = Decimal(cost)
            newadvert.usercostforall = Decimal(costall)
            newadvert = advertform.save(commit=False)
            newadvert.planning = newadvert.planning
            newadvert.poster = newadvert.poster
            newadvert.user = request.user

            #contactform.logocompany = unicode(contactform.logocompany)

            newadvert.contact = contactform.save()
            newadvert.save()
            advertform.save_m2m()

            instantces = FormSet.save(commit=False)
            for intance in instantces:
                intance.advert = newadvert
                intance.save()

            return HttpResponse(simplejson.dumps({'result': 'success'}))
            #return HttpResponseRedirect('/advert/thanks/')

        else:
            response = {}
            for k in advertform.errors:
                #print 'error advert==%s'% k
                response[k] = advertform.errors[k][0]

            for k in contactform.errors:
                #print 'error contact==%s'% k
                response[k] = contactform.errors[k][0]

            dict = {'response': response, 'result': 'error'}
            # c.update(dict);

            return HttpResponse(simplejson.dumps(dict))