def _create_ads(self): def get_or_create_product_category(): return ProductCategory.objects.get_or_create(pk=random.randint(1,10), defaults={ 'title':''.join([random.choice(string.ascii_letters) for x in range(15)]), })[0] def get_or_create_business_domain(): return BusinessDomain.objects.get_or_create(pk=random.randint(1,10), defaults={ 'title':''.join([random.choice(string.ascii_letters) for x in range(15)]), })[0] for i in range(30): currency, created = Currency.objects.get_or_create( code='EUR', defaults={ 'code':'EUR', 'name':'Euro', 'symbol': '€', 'factor':1, 'is_active':True, 'is_base': True, 'is_default': True } ) product_type, created = ProductType.objects.get_or_create(pk=random.randint(1,10), defaults={ 'title':''.join([random.choice(string.ascii_letters) for x in range(15)]) }) product_type.product_categories = [get_or_create_product_category() for x in range(3)] product_type.save() user = random.choice(self.users) ad = Ad( title=''.join([random.choice(string.ascii_letters) for x in range(15)]), price=''.join([random.choice(string.digits) for x in range(4)]), currency=currency, amount=random.randint(1,10), published=True, product_type=product_type, active=True, creator=user.get_userprofile(), owner=user.get_business_profile() ) ad.save() ad.business_domains.add(*[get_or_create_business_domain() for x in range(3)]) ad.save()
user = User.objects.using(database).get(id=row[1]) except: print "FAILED user: " + row[1] failed.append(row[0]) user_profile = user.get_userprofile() active_business_profile = user_profile.active_profile product_type = ProductType.objects.using(database).get(id=row[2]) ad = Ad( id=row[0], creator=user_profile, owner=active_business_profile, product_type=product_type, title=row[3], description=row[4] or 'Ingen beskrivning', price=float(row[5]), updated=ts_to_datetime(row[10]), created=ts_to_datetime(row[11]), active=row[12], published=row[12], # col: created #member_level=member_level, youtube_code=row[16], amount=row[18], currency=sek_currency, is_request=False, ) ad.save(using=database) Ad.objects.using(database).filter(id=row[0]).update( updated=ts_to_datetime(row[10]), created=ts_to_datetime(row[11]), ) except csv.Error, e: exit('file %s, line %d: %s' % (filename, reader.line_num, e))