def cost_next(): for ci in Kharkovs.objects.all(): try: print 'do', ci.id advert = Advert.objects.get(id=ci.id) if ci.currency and ci.currency == '1': advert.price_usd = parse_int(ci.cost) print '$', advert.price_usd elif ci.currency and ci.currency == '2': advert.price_uah = parse_int(ci.cost) print u'uah', advert.price_uah else: advert.price_uah = parse_int(ci.cost) print u'uah', advert.price_uah except Advert.DoesNotExist: print u'Нету'
def clean_phone(self): str_phones = self.cleaned_data['phone'].split(',') phones = [] for str_phone in str_phones: phone = parse_int(str_phone) if len(str(phone)) == 9: phones.append(phone) if len(phones) == 0: raise forms.ValidationError( u'Короткий номер, возможно нужно дописать код корода')
def form_valid(self, form): str_phones = self.request.POST['phone'].split(',') instance = form.save(commit=False) instance.author = self.request.user instance.save() print instance for str_phone in str_phones: phone = parse_int(str_phone) if len(str(phone)) == 9: phone_obj, created = Phone.objects.get_or_create(phone=phone) print phone_obj.phone phone_obj.agent = 5 phone_obj.save() instance.phone.add(phone_obj) return redirect('/black_list')
def save_to_base(grab, task, title, text, deteil, phone, cost=False, room=False, save_url=False): if title: text = title+'. '+text+deteil+' '+phone.decode('utf-8') else: text = text+deteil+' '+phone.decode('utf-8') print 'type(text)=================', type(text) piece = text[12:40] adv = Slandos.objects.filter(text__contains=piece, link=save_url, cat=str(task.cat)).first() if not adv: print 'write!!!!!!!!!!!!!' propr = Slandos() propr.cat = task.cat propr.text = text propr.status = 0 propr.date = datetime.datetime.now().strftime("%Y-%m-%d") propr.link = save_url try: propr.phone1 = parse_int(phone.split(',')[0]) except IndexError, ex: print u"don't take phone1", ex try: propr.phone2 = parse_int(phone.split(',')[1]) except IndexError, ex: print u"don't take phone2", ex
def save(self, *args, **kwargs): """Транслитерация имени для урла""" if self.title is None or len(self.title) == 0: sub = self.sublocality if self.sublocality else "" try: rooms = u", %s-комнатная" % self.extraflat.rooms_number except: rooms = u"" self.title = u"%s %s%s" % (self.category.name if self.category else "", sub, rooms) if self.slug is None or len(self.slug) == 0: self.slug = slugify(self.title) if self.price_usd is None and self.price_uah: self.price_usd = uah_to_usd(self.price_uah) if self.price_uah is None and self.price_usd: self.price_uah = usd_to_uah(self.price_usd) # if self.author and not self.author.is_admin: if not self.author or not self.author.is_admin: for cut in CuttingWords.objects.all(): self.main_text = self.main_text.replace(cut.cut_words, '') super(Advert, self).save(*args, **kwargs) if self.raw_phones: current_phones = self.phone_set.all() current_numbers = current_phones.values_list('phone', flat=True) phones = [parse_int(x) for x in self.raw_phones.split(',')] for phone in phones: if phone not in current_numbers: phone, c = Phone.objects.get_or_create(phone=phone) self.phone_set.add(phone) for current_phone in current_phones: if current_phone.phone not in phones: self.phone_set.remove(current_phone) super(Advert, self).save(*args, **kwargs)
def import_db(count=1000, citi=Kharkovs): # groups() # import_db_user() # import_db_city() # import_db_category() # this data created # import_region() # import_db_ObjectType() for ci in citi.objects.all()[0:count]: print citi # STRAT multy city if ci.cityeng == u'kharkov': try: advert = Advert.objects.get(id=ci.id) except Advert.DoesNotExist: advert = Advert() advert.id = ci.id elif ci.cityeng == u'kiev': try: advert = Advert.objects.get(id=ci.id + 200000) except Advert.DoesNotExist: advert = Advert() advert.id = ci.id + 200000 else: try: advert = Advert.objects.get(id=ci.id + 300000) except Advert.DoesNotExist: advert = Advert() advert.id = ci.id + 300000 # END multy city print ci.id, ci.cityeng, ci.cat_id, # , ci.user_id try: advert.author = UserData.objects.get(id=ci.user_id) except UserData.DoesNotExist: pass advert.creation_date = ci.data advert.date_of_update = ci.data advert.main_text = ci.additionally #advert.non_admin_sublocality = ci.region_id if ci.currency and ci.currency == '1': advert.price_usd = parse_int(ci.cost) elif ci.currency and ci.currency == '2': advert.price_uah = parse_int(ci.cost) else: advert.price_uah = parse_int(ci.cost) advert.contact_name = ci.contact # foreign key------------------------------------------------------------------------------------------------------- try: metro = Metro.objects.get(id=ci.metro) except: metro = None advert.metro = metro try: city = City.objects.get(id=ci.city) except: city = None advert.city = city try: name_region = Kharkovregions.objects.get(id=ci.region_id) region = Sublocality.objects.get(name=name_region) except: region = None advert.sublocality = region if ci.cat_id in EXCLUDE_CAT: cat_id = EXCLUDE_CAT[ci.cat_id] cat_id = Category.objects.get(id=cat_id) else: if Category.objects.get(id=ci.cat_id) and ci.cat_id != 0: cat_id = Category.objects.get(id=ci.cat_id) advert.category = cat_id try: advert.save() except: pass # continue # extra key--------------------------------------------------------------------------------------------------------- advert.extraflat = ExtraFlat( floor=ci.flor, floors=ci.allfloor, total_area=parse_int(ci.s), rooms_number=ci.komnat, condition=ci.state, ) try: advert.extraflat.save() except Exception, ex: pass # print ex advert.extrahouse = ExtraHouse( floors=ci.allfloor, total_area=parse_int(ci.s), lot_area=ci.splant, condition=ci.state, gaz=not_empty(ci.gens), water=not_empty(ci.water), electricity=not_empty(ci.electro), ) try: advert.extrahouse.save() except: pass advert.extralot = ExtraLot( lot_area=parse_int(ci.splant), gaz=not_empty(ci.gens), water=not_empty(ci.water), electricity=not_empty(ci.electro), ) try: advert.extralot.save() except: pass advert.extrarent = ExtraRent(term=ci.period_rent, ) try: advert.extrarent.save() except: pass advert.objecttype = ObjectType(name=ci.typeobject, ) try: advert.ebjecttype.save() except: pass if ci.phone2: advert.raw_phones = ",".join( [str(parse_int(ci.phone1)), str(parse_int(ci.phone2))]) else: advert.raw_phones = ",".join([ str(parse_int(ci.phone1)), ]) print 'phone = ', advert.raw_phones # for tel in phones: # if tel>0: # owner = advert.author # phone, c = Phone.objects.get_or_create(phone=tel, # defaults={'owner':owner}) # print c # # advert.phones.add(phone) advert.save() #START make image # for (count, khimg) in enumerate(Kharkovimages.objects.filter(city_id=ci.id)): # try: # ph = "%s/%s" % (khimg.name[:2], khimg.name) # Photo.objects.get_or_create(id=khimg.id, advert=advert, photo=ph) # except Exception, ex: # print ex # print "Some else with images" # print 'count_image =', count send_image(citi, ci, advert)
propr = Slandos() propr.cat = task.cat propr.text = text propr.status = 0 propr.date = datetime.datetime.now().strftime("%Y-%m-%d") propr.link = save_url try: propr.phone1 = parse_int(phone.split(',')[0]) except IndexError, ex: print u"don't take phone1", ex try: propr.phone2 = parse_int(phone.split(',')[1]) except IndexError, ex: print u"don't take phone2", ex try: propr.phone3 = parse_int(phone.split(',')[2]) except IndexError, ex: print u"don't take phone3", ex if cost: propr.cost = cost if room: propr.room = room propr.site = 'slando.ua.test' propr.save() print u'Успешное сохранение в БД' return propr else: print u'НЕ нуждается в сохранении в БД' return None