Пример #1
0
 def _create_mail(self, maillist, days=0):
     return Mail.objects.create(
         maillist=maillist,
         subject='test-mail-subject-%s' % Mail.objects.count(),
         content=paragraph(),
         publish_date = datetime.now() + timedelta(days)
     )
Пример #2
0
class MovieFactory(factory.Factory):
    FACTORY_FOR = models.Movie

    title = factory.Sequence(lambda n: 'Movie%03d %s' % (int(n) + 1, words(2)))
    image = factory.Sequence(random_cover)

    short_description = factory.Sequence(lambda n: 'Movie%03d is %s' %
                                         (int(n) + 1, sentence()))
    description = factory.Sequence(lambda n: 'We can Movie%03d as %s' %
                                   (int(n) + 1, paragraph()))
    studio = factory.SubFactory(StudioFactory)

    played_times_day = factory.Sequence(
        lambda n: abs(int(n) + random.randint(5, 45)))
    played_times_month = factory.Sequence(
        lambda n: abs(int(n) + random.randint(150, 1500)))
    played_times_total = factory.Sequence(
        lambda n: abs(int(n) + random.randint(5000, 15000)))

    likes_day = factory.Sequence(lambda n: abs(int(n) + random.randint(0, 23)))
    likes_month = factory.Sequence(
        lambda n: abs(int(n) + random.randint(0, 750)))
    likes_total = factory.Sequence(
        lambda n: abs(int(n) + random.randint(0, 8000)))

    @factory.post_generation()
    def _create_movies(self, create, extracted, *args, **kwargs):
        for i in range(random.randint(2, 4)):
            trailer = TrailerFactory(movie=self)
            trailer.save()
Пример #3
0
 def handle(self, *args, **options):
     if len(args):
         num_events = int(args[0])
     else:
         num_events = 10
     
     self.stdout.write('Generating %i fake events for the next 5 days\n' % (num_events,))
     today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
     for day in range(6):
         for evtnum in range(num_events):
             new_event = Event()
             new_event.name = "{adj} {noun} {type}".format(
                 adj=random.choice(self._adjectives),
                 noun=random.choice(self._nouns),
                 type=random.choice(self._types)
             )
             
             new_event.start = today + datetime.timedelta(days=day, hours=8 + evtnum)
             new_event.end = new_event.start + datetime.timedelta(minutes=30)
             new_event.location = random.choice(self._locations)
             new_event.description = lorem_ipsum.paragraph()
             new_event.host = random.choice(self._hosts)
             new_event.url = "http://aspc.pomona.edu/"
             new_event.status = 'approved'
             
             new_event.save()
             self.stdout.write("Saved event '%s'" % (new_event.name,))
Пример #4
0
	def handle(self, *args, **options):
		call_command('reset', 'events', 'auth', interactive=False)
		
		print 'Creating test users...',
		# Create users
		test_user = User.objects.create(username="******", password="******", first_name="Patrick", last_name="Burt")
		print 'done'
		
		print 'Creating calendars for test users...',
		# Create calendars
		cal = test_user.owned_calendars.create(name="Test Calendar")
		print 'done'
		
		print 'Creating events for new calendars...',
		tag_choices = map(
			lambda t: Tag.objects.create(name=t),
			set(lorem_ipsum.words(20, False).lower().split())
		)
		contact_name_choices  = (None, 'Spork Belvadere', 'Captain ImABadGuy', 'Bill Paxton', 'Admiral Evildude')
		contact_phone_choices = (None, '407-123-3215', '563-456-4123', '123-456-4448')
		contact_email_choices = (None, '*****@*****.**', '*****@*****.**', '*****@*****.**')
		location_choices = (
			Location.objects.create(name="Student Union"),
			Location.objects.create(name="Library"),
			Location.objects.create(name="Arena"),
			Location.objects.create(name="Visual Arts Building"),
			Location.objects.create(name="Patrick's House"),
		)
		for i in range(1, 8):
			hour    = randint(8, 20)
			minutes = choice([15, 30, 45, 0, 0, 0])
			start   = datetime(datetime.now().year, 1, i, hour, minutes)
			end     = start + timedelta(hours=choice([1, 2, 3, 24, 25, 26, 48, 49, 50]))
			
			tags = list()
			for j in range(0, randint(1, 5)):
				tags.append(choice(tag_choices))
			
			event = cal.events.create(
				title=lorem_ipsum.words_cust(),
				description=lorem_ipsum.paragraph(),
				state=Event.Status.posted,
				owner=test_user,
				contact_name=choice(contact_name_choices),
				contact_phone=choice(contact_phone_choices),
				contact_email=choice(contact_email_choices)
			)
			event.tags.add(*tags)
			
			instance = event.instances.create(
				location=choice(location_choices),
				start=start,
				end=end,
				interval=EventInstance.Recurs.weekly,
				until=datetime(datetime.now().year + 1, 1, 1)
			)
		print 'done'
Пример #5
0
    def test_post(self):
        response = self.retrieve(
            self.host + "comment/post/",
            object_pk=1,
            content_type="derrickpetzold.post",
            title=lorem.words(random.randint(3, 6), common=False),
            text=lorem.paragraph(),
        )

        assert response.success == True, "Post failed: %s" % (response.error)
        self.output.writeln("Post: Ok (%.2f)" % (response.run_time))
Пример #6
0
 def createRubbish(self, topic=None):
     """
     :return: Post
     """
     p = Post(topic=topic)
     p.title = lorem_ipsum.words(random.randint(0, 5), False)
     p.body = p.body_markup = "<br/>\n".join(lorem_ipsum.paragraphs(random.randint(0, 7), False))
     p.summary = lorem_ipsum.paragraph()
     p.user = User.objects.all()[random.randint(0, 850)]
     if random.randint(1, 50) == 1:
         p.hidden = True
     if random.randint(1, 100) == 1:
         p.deleted = True
     return p
Пример #7
0
 def createRubbish(self, topic=None):
     """
     :return: Post
     """
     p = Post(topic=topic)
     p.title = lorem_ipsum.words(random.randint(0, 5), False)
     p.body = p.body_markup = "<br/>\n".join(lorem_ipsum.paragraphs(random.randint(0, 7), False))
     p.summary = lorem_ipsum.paragraph()
     p.user = User.objects.all()[random.randint(0, 850)]
     if random.randint(1, 50) == 1:
         p.hidden = True
     if random.randint(1, 100) == 1:
         p.deleted = True
     return p
Пример #8
0
class PrelectionFactory(factory.DjangoModelFactory):
    FACTORY_FOR = Prelection

    main_prelector = factory.SubFactory(ParticipantFactory)
    conference = factory.SubFactory(ConferenceFactory)
    title = factory.LazyAttribute(lambda o: words(5, False)[:50])
    description = factory.LazyAttribute(lambda o: paragraph())
    length = 60

    @factory.post_generation
    def other_prelectors(self, create, extracted, **kwargs):
        if not create:
            return

        if extracted:
            for prelector in extracted:
                self.other_prelectors.add(prelector)
Пример #9
0
  def handle_noargs(self, **options):
    Ticket.objects.all().delete()

    for i in xrange(1000):
      ticket_kwargs = {
        'location': random.choice(PLACES),
        'was_fair': random.random() > 0.25,
        'fine':     Decimal('%s' % (random.random() * 300),),
        'lat':      Decimal('%s' % (random.random() * 90),),
        'lng':      Decimal('%s' % (random.random() * 90),),
        'date':     datetime.today() - timedelta(days = int(random.random() * 30), hours = int(random.random() * 24))
      }
      
      if random.random() > 0.4:
        ticket_kwargs['description'] = lorem_ipsum.sentence()
      else:
        ticket_kwargs['description'] = lorem_ipsum.paragraph()
      
      Ticket.objects.create(**ticket_kwargs)
Пример #10
0
    def make_replies_for_post(self, post, count=None):
        if not count:
            max_count = 10
            if random.randrange(50) == 0:
                max_count = 20000
            elif random.randrange(10) == 0:
                max_count = 300
            elif random.randrange(3) == 0:
                max_count = 50
            count = random.randrange(0, max_count)

        for i in xrange(count):
            user = self.get_user()
            number = None
            if random.randrange(2) == 1:
                comments = Comment.objects.filter(post=post)
                if comments:
                    number = self.get_random_slice(comments).number
            pi = PostInterface(user)
            pi.add_reply(lorem_ipsum.paragraph(), post.id, number)
Пример #11
0
    def create_single_rubbish(self, reply_choice=0.9):
        p = Post()
        p.title = lorem_ipsum.words(random.randint(0, 5), False)
        p.body = p.body_markup = "<br/>\n".join(lorem_ipsum.paragraphs(random.randint(0, 7), False))
        p.summary = lorem_ipsum.paragraph()
        p.user = get_user_model().objects.get(pk=random.choice(self.userids))
        p.posted_by = p.user.username
        p.rating = random.randint(1, 30)

        #Trying to get a slightly more realistic rating distribution
        if p.rating <= 2:
            p.rating = random.randint(-5, 0)
        elif p.rating <= 20:
            p.rating /= 2
        else:
            p.rating -= 18

        if p.rating > 5 and random.randint(1, 20) == 1:
            p.homepage = True
        if p.rating < 1 and random.randint(1, 10) == 1:
            p.hidden = True
        if p.rating == -1 and random.randint(1, 10) == 1:
            p.deleted = True
        p.save()
        p.replies_count = 0
        while random.random() < reply_choice:
            p.replies.add(Post.objects.create_single_rubbish(reply_choice=reply_choice / 5))
            p.replies_count += 1
        if p.replies_count != 0:
            p.save()
        # tags = random.randint(0, 3)
        # tagscount = Tag.objects.count()
        # if tagscount > 0:
        #     for i in range(0, tags):
        #         TaggedObject.objects.get_or_create(tag=Tag.objects.get(pk=random.randint(1, tagscount)),
        #                                            object_id=p.pk,
        # content_type=ContentType.objects.get_for_model(Post))
        return p
Пример #12
0
def mock_case():
    """Simple test:  Create a case and verify that it exists in the database via the API"""
    # get the basic counts
    user1 = get_or_create_user(always_new=False)
    user2 = get_or_create_user(always_new=False)

    print "Got mock users"
    caregiver_creator = get_or_create_role(
        user1,
        "caregiver",
        title_string="some caregiver " + random_word(length=12),
        department_string="some department " + random_word(length=12),
        always_create=False,
    )
    provider_assigned = get_or_create_role(
        user2, "provider", title_string="some provider " + random_word(length=12), always_create=False
    )

    print "Got Roles"
    print "Creating Patient..."
    patient = get_or_create_patient(get_or_create_user(always_new=False))
    print "Got Patient"

    subs = Case.__subclasses__()

    print "Generating Case"
    newcase = generate_case(
        caregiver_creator,
        lorem.sentence(),
        lorem.paragraph(),
        provider_assigned,
        subtype=subs[random.randrange(0, len(subs))],
    )
    newcase.patient = patient.doc_id
    newcase.save()
    print "Created and saved Case"
    return newcase
                    raise Exception("No images found")
                url = images[0].getURL(size="Large", urlType="source")
                urlretrieve(url, image)
            except Exception, e:
                message = "Error [%s] for %s" % (e, data)
            else:
                message = "Successfully downloaded image for %s" % data
        remaining = "%s remaining" % queue.qsize()
        print remaining.ljust(20, "."), message

        # Create database records for the product.
        if exists(image):
            product = Category.objects.get(parent__title=main_category,
                title=sub_category).products.create(title=product,
                available=True, status=CONTENT_STATUS_PUBLISHED,
                content="<p>%s</p>" % paragraph())
            image = "product/%s.jpg" % product.title
            product.images.create(file=image)
            product.variations.create_from_options(product_options)
            product.variations.manage_empty()
            product.variations.update(unit_price=F("id") + "10000")
            product.variations.update(unit_price=F("unit_price") / "1000.0")
            product.copy_default_variation()

if __name__ == "__main__":

    # Load the Google Base data.
    category_url = "http://www.google.com/basepages/producttype/taxonomy.txt"
    try:
        category_data = urlopen(category_url).read()
    except Exception, e:
Пример #14
0
    def handle(self, *args, **options):
        num = int(args and args[0] or 10)
        with_ava = len(args) > 1

        for type in (
                documents.User,
                Message,
                FriendshipOffer,
                UserFriends,
                GroupUser,
        ):
            type.objects.delete()

        if with_ava:
            if not os.path.exists('faces94'):
                raise CommandError(
                    'faces database not exists, do\n'
                    'wget http://cswww.essex.ac.uk/mv/allfaces/faces94.zip && unzip faces94.zip'
                )

            faces = []

            def visit(arg, dirname, names):
                if len(re.split('/', dirname)) != 3:
                    return
                faces.append(os.path.join(dirname, random.choice(names)))

            os.path.walk('faces94', visit, None)
            random.shuffle(faces)

        # creating accounts
        male_first_names = u'''
        Сергей
        Александр
        Дмитрий
        Андрей
        Артур
        Игорь
        Армен
        Алексей
        Григорий
        '''.split()

        female_first_names = u'''
        Анна
        Анастасия
        Риана
        Елена
        Мария
        Дарина
        Кристина
        Марина
        Ангелина
        Полина
        '''.split()

        male_last_names = u'''
        Смирнов
        Иванов
        Кузнецов
        Соколов
        Попов
        Лебедев
        Козлов
        Новиков
        Морозов
        Петров
        Волков
        Соловьёв
        Васильев
        Зайцев
        Павлов
        Семёнов
        Голубев
        Виноградов
        Богданов
        Воробьёв
        Фёдоров
        Михайлов
        Беляев
        Тарасов
        '''.split()

        female_last_names = [u'%sа' % x for x in male_last_names]

        male_names = (male_first_names, male_last_names)
        female_names = (female_first_names, female_last_names)

        print
        for i in xrange(num):
            if not i % (num / 10 or 10):
                print '\rusers creation %002d%%' % (i * 100 / num)
                sys.stdout.flush()
            if with_ava:
                if faces[i].find('/female/') != -1:
                    names = female_names
                else:
                    names = male_names
            else:
                names = random.choice((male_names, female_names))

            first_name = random.choice(names[0])
            last_name = random.choice(names[1])

            email = '*****@*****.**' % i

            acc = documents.User.create_user(first_name=first_name,
                                             last_name=last_name,
                                             email=email,
                                             password='******',
                                             is_superuser=bool(not i))
            acc.save()
            if with_ava:
                client = Client()
                client.login(email=email, password='******')
                file = open(faces[i])
                with patch_settings(TASKS_ENABLED={}):
                    client.post(reverse('social:avatar_edit'), {'file': file})

        print '\rusers creation 100%'
        # friending & messaging
        print
        for i in xrange(num):
            max_friends_count = random.randint(0, num % 25 + 25)
            friends_numbers = set([
                random.randint(0, num - 1) for _ in xrange(max_friends_count)
            ]).difference(set([i]))
            this_user = documents.User.objects().order_by('username')[i]

            for fn in friends_numbers:
                friend = documents.User.objects().order_by('username')[fn]
                #this_user.reload()
                this_user.friends.offers.send(friend)
                random.randint(0, 3) and friend.friends.offers.send(
                    this_user)  # ~66%

            max_msg_sndrs_count = random.randint(0, num)
            msg_sndrs_numbers = set([
                random.randint(0, num - 1) for _ in xrange(max_msg_sndrs_count)
            ]).difference(set([i]))

            for sndr_num in msg_sndrs_numbers:
                messages_count = random.randint(0, 3)

                sndr = documents.User.objects().order_by('username')[sndr_num]
                for _ in xrange(messages_count):
                    Message.send(sndr, this_user, lorem_ipsum.paragraph())

            if not i % (num / 10 or 10):
                print '\rfriending and messaging %002d%%' % (i * 100 / num),
                sys.stdout.flush()
        print '\rfriending and messaging 100%'
        print

        CameraType(
            name='Axis only view',
            driver='apps.cam.drivers.axis.AxisDriver',
        ).save()
        CameraType(
            name='Axis manage',
            driver='apps.cam.drivers.axis.AxisDriver',
            is_controlled=True,
        ).save()
        print '\rCemeras 100%'
        print

        Tariff(
            name='View Package 1 min',
            description='View Package Description...',
            cost=1.0,
            duration=60.0,
            is_controlled=False,
        ).save()
        Tariff(
            name='View Package Time',
            description='View Package Description...',
            cost=1.0,
            is_controlled=False,
        ).save()
        Tariff(
            name='Manage Package 1 min',
            description='Manage Package Description...',
            cost=2.0,
            duration=60.0,
            is_controlled=True,
        ).save()
        Tariff(
            name='Manage Package Time',
            description='Manage Package Description...',
            cost=2.0,
            is_controlled=True,
        ).save()
        print '\rTariffs 100%'
Пример #15
0
 def _create_maillist(self):
     return Maillist.objects.create(
         name='test-maillist-name',
         description=paragraph()
     )
def load_data():
    print 'Starting load data'
    management.call_command('loaddata', 'lfs_all_countries.xml', verbosity=0)

    site = Site.objects.all()[0]
    site.name = site.domain = "www.example.com"
    site.save()

    ie = Country.objects.get(code="ie")
    gb = Country.objects.get(code="gb")
    de = Country.objects.get(code="de")
    us = Country.objects.get(code="us")
    fr = Country.objects.get(code="fr")
    nl = Country.objects.get(code="nl")

    shop, created = Shop.objects.get_or_create(name="lfs test", shop_owner="John Doe", description="Test Shop",
                                               default_country=ie)
    shop.save()

    # Tax
    thirteen_percent, created = Tax.objects.get_or_create(rate=13.5, description="13.5% rate")
    twentyone_percent, created = Tax.objects.get_or_create(rate=21, description="21% rate")

    for ic in Country.objects.all():
        shop.invoice_countries.add(ic)

    shop.shipping_countries.add(ie)
    shop.shipping_countries.add(gb)
    shop.shipping_countries.add(de)
    shop.shipping_countries.add(us)
    shop.shipping_countries.add(fr)
    shop.shipping_countries.add(nl)

    shop.save()

    tax = Tax.objects.create(rate=21)

    direct_debit = PaymentMethod.objects.create(
        name="Direct Debit",
        active=True,
        tax=tax,
        )

    cod = PaymentMethod.objects.create(
        name="Cash on delivery",
        active=True,
        tax=tax,
        )

    paypal = PaymentMethod.objects.create(
        name="PayPal",
        active=True,
        tax=tax,
        )

    prepayment = PaymentMethod.objects.create(
        name="Prepayment",
        active=True,
        tax=tax,
        )

    by_invoice = PaymentMethod.objects.create(
        name="By invoice",
        active=True,
        tax=tax,
        )

    categories = ['animals', 'transport', 'food']
    for cat in categories:
        category, created = Category.objects.get_or_create(name=cat, slug=slugify(cat))

    products = ["Cat", "Dog", "Mouse", "Surfboard", "Car", "Ship",
                "Chocolate", "Apple", "Orange"]
    i = 0
    for p in products:
        product, created = Product.objects.get_or_create(name=p, slug=slugify(p), price=(i + 1) * 10.0,
                                                         sku=i, active=True, manage_stock_amount=False)
        product.description = paragraph()

        # set every second product to a different tax rate
        if i % 2 == 0:
            product.tax = thirteen_percent
        else:
            product.tax = twentyone_percent

        if i % 3 == 0:
            product.price_calculator = "lfs.gross_price.GrossPriceCalculator"
        else:
            product.price_calculator = "lfs.net_price.NetPriceCalculator"
        product.save()

        category = Category.objects.all()[i / 3]
        category.products.add(product)
        category.save()
        ts, created = Topseller.objects.get_or_create(product=product, position=i)
        fp, created = FeaturedProduct.objects.get_or_create(product=product, position=i)
        i = i + 1


    # shipping prices setup
    # delivery_time, created = DeliveryTime.objects.get_or_create(min=2, max=7, unit=DELIVERY_TIME_UNIT_DAYS,
    #                                                             description="2 to 7 for delivery")

    # criterion for geo-specific delivery charges    
    # cc_ie_uk = CountryCriterion.objects.create(operator=IS)
    # cc_ie_uk.countries.add(ie)
    # cc_ie_uk.countries.add(gb)
    # cc_ie_uk.save()
    #
    # cc_not_ie_uk = CountryCriterion.objects.create(operator=IS_NOT)
    # cc_not_ie_uk.countries.add(ie)
    # cc_not_ie_uk.countries.add(gb)
    # cc_not_ie_uk.save()
    #
    # #Rest of World >1kg ?16.95
    # smp_1095, created = ShippingMethod.objects.get_or_create(name="Ireland & UK", price=10.95, active=True,
    #                                                          delivery_time=delivery_time, priority=1)
    # smp_1500, created = ShippingMethod.objects.get_or_create(name="International", price=15.00, active=True,
    #                                                          delivery_time=delivery_time, priority=1)
    #
    # co = CriteriaObjects(criterion=cc_ie_uk, content=smp_1095)
    # co.save()
    #
    # co = CriteriaObjects(criterion=cc_not_ie_uk, content=smp_1500)
    # co.save()

    left_slot, created = Slot.objects.get_or_create(name="Left")
    right_slot, created = Slot.objects.get_or_create(name="Right")

    cart_portlet, created = CartPortlet.objects.get_or_create(title="Cart")
    pages_portlet, created = PagesPortlet.objects.get_or_create(title="Information")
    categories_portlet, created = CategoriesPortlet.objects.get_or_create(title="Products")
    relatedproducts_portlet, created = RelatedProductsPortlet.objects.get_or_create(title="Related Products")
    recentproducts_portlet, created = RecentProductsPortlet.objects.get_or_create(title="Recent Products")

    PortletAssignment.objects.create(slot_id=left_slot.id, content=shop, portlet=categories_portlet, position=10)
    PortletAssignment.objects.create(slot_id=left_slot.id, content=shop, portlet=pages_portlet, position=15)
    PortletAssignment.objects.create(slot_id=right_slot.id, content=shop, portlet=cart_portlet, position=10)
    PortletAssignment.objects.create(slot_id=right_slot.id, content=shop, portlet=recentproducts_portlet, position=20)
    PortletAssignment.objects.create(slot_id=right_slot.id, content=shop, portlet=relatedproducts_portlet, position=30)
    print 'Finishing load data'
Пример #17
0
    def handle(self, *args, **options):
#        # neccessary:
        
        #Site(domain="pygameweb.no-ip.org", name="pygame web").save()
        #Site(domain="127.0.0.1:8000", name="localhost").save()
        testusers = []
        for i in xrange(30):
            u, c = User.objects.get_or_create(username="******"%i)
            u.save()
            testusers.append(u)
        
        for obj in Project.objects.all():
            obj.delete()
        tags = ("test %s"%words(15)).split()
        pnames = []
        for i in xrange(20):
            admins = [random.choice(testusers) for i in xrange(random.randint(2, 4))]
            members = [random.choice(testusers) for i in xrange(random.randint(2, 6))]
            while True:
                pname = words(2)
                if pname not in pnames:
                    pnames.append(pname)
                    break
            p = Project(name=pname,
                        markup="rst",
                        description="\n\n".join(paragraphs(4)),
                        summary=paragraph(),
                        homepage_link = "http://example.com",                        
                        creator=admins[0],
                        last_editor=admins[1],
                        tags=",".join([random.choice(tags) for i in xrange(random.randint(3, 10))]),)
            p.save()
            p.admins = admins
            p.members = members
            p.save()
           # s = Screenshot(name="test", project=p,
           #                uploaded_by=User.objects.all()[0],
           #                description="testimage", 
           #                image="screenshots/logo.png")
           # s.save()
            k = 1
            for j in xrange(random.choice((0,0,0,1,1,1,1,2,2,2,3,3,4))):
                r = Release(project=p,
                            version="%i.%i %s"%(k, random.randint(0,9), words(1)),
                            markup="rst",
                            summary="\n\n\n".join(paragraphs(2, False)),
                            changelog=paragraph(),
                            notes_url = "http://example.org",
                            download_url = "http://example.org",
                            creator=members[0],
                            last_editor=members[1])
                r.save()
 

        for obj in News.objects.all():
                    obj.delete()
        news_admins = User.objects.order_by("?")[:4]
        for i in xrange(20):
            n = News(title=words(random.randint(2,5)),
                     slug=slugify("News Test %i"%i),
                        markup="rst",
                    creator=random.choice(news_admins),
                    last_editor=User.objects.all()[0],
                    content="""
Hello,

.. sourcecode:: python

  while True:
    print "Hello World!"


*this site is* `pony powered <http://www.djangopony.com/>`_ 


%s""" % "\n\n".join(paragraphs(3))
                    
                            #    creator=User.objects.all()[0],
                             #   last_editor=User.objects.all()[0]
            )
            n.save()

        py, c = Language.objects.get_or_create(name="Python", language_code="py",
                        file_extension="py", mime_type="application/x-python")
        py.save()
        for i in xrange(10):
            s = Snippet(title=words(random.randint(3, 8)),
                        language=py,
                        author=random.choice(testusers),
                        tags=" ".join([random.choice(tags) for i in xrange(random.randint(2, 8))]),
                        description="".join(paragraphs(2)),
                        code=SAMPLE_CODE,
                        markup="rst"
                        )
            s.save()
            for y in xrange(3):
                b = Bookmark(user=random.choice(testusers), snippet=s)
                b.save()
                
        
             
                        
Пример #18
0
    def handle(self, *args, **options):
        verbose = int(options.get('verbosity'))
        interval = options.get('interval')
        nstreams = options.get('nstreams')
        types = options.get('types')
        flush = options.get('flush')
        demo = options.get('demo')
        span = options.get('span')
        norealtime = options.get('norealtime')

        if nstreams is None and types is None and not demo and flush:
            datastream.delete_streams()
            return
        elif flush:
            raise base.CommandError(
                "Do you really want to remove all datastream data from the database? Use only '--flush' parameter."
            )

        if nstreams is None and types is None and not flush and demo:
            types = DEMO_TYPE
            if span == '':
                span = DEMO_SPAN
        elif demo:
            raise base.CommandError("In demo mode other parameters are fixed.")

        if nstreams is None and types is None:
            nstreams = DEFAULT_NSTREAMS

        if types and check_types.match(types):
            types = split_types.findall(types)
            if nstreams is not None and len(types) != nstreams:
                raise base.CommandError(
                    "Number of stream types does not mach number of streams.")

            nstreams = len(types)

        elif types:
            raise base.CommandError(
                "Invalid stream types string. Must be a comma separated list of <int|float|enum>[(start,end)|(enum values)]."
            )

        streams = []
        for i in range(nstreams):
            if types is not None:
                typ = types[i]
            else:
                typ = ('int', '')

            if typ[0] == 'enum':
                value_type = 'nominal'
                downsamplers = ['count']
            elif typ[0] == 'graph':
                value_type = 'graph'
                downsamplers = ['count']
            else:
                value_type = 'numeric'
                downsamplers = datastream.backend.value_downsamplers

            visualization_value_downsamplers = []
            for downsampler in ['mean', 'min', 'max']:
                if downsampler in downsamplers:
                    visualization_value_downsamplers.append(downsampler)

            type_constructor, random_function, default_domain = TYPES[typ[0]]
            domain = typ[1] or default_domain
            domain_range = [type_constructor(d) for d in domain.split(',')]

            stream_id = datastream.ensure_stream(
                {'title': 'Stream %d' % i},
                {
                    'description': lorem_ipsum.paragraph(),
                    'unit_description': 'random, domain: %s' % domain,
                    'stream_number': i,
                    'visualization': {
                        'type':
                        'state' if typ is 'enum' else 'line',
                        'hidden':
                        True if typ is 'graph' else False,
                        'value_downsamplers':
                        visualization_value_downsamplers,
                        'time_downsamplers': ['mean'],
                        'minimum':
                        domain_range[0] if value_type == 'numeric' else None,
                        'maximum':
                        domain_range[1] if value_type == 'numeric' else None,
                    },
                },
                downsamplers,
                datastream.Granularity.Seconds,
                value_type=value_type,
            )

            streams.append((stream_id, typ))

        span = span.split(' ')
        if len(span) == 1 and span[0]:
            span = span[0]
            for val, key in (('days', 'd'), ('hours', 'h'), ('minutes', 'm'),
                             ('seconds', 's')):
                if span[-1] == key:
                    try:
                        s = int(span[:-1])
                    except ValueError:
                        raise base.CommandError(
                            "Time span value must be an integer.")

                    span_to = datetime.datetime.now(pytz.utc)
                    last_timestamp = self.last_timestamp(streams)

                    span_from = max(
                        span_to - datetime.timedelta(**{val: s}),
                        last_timestamp + datetime.timedelta(seconds=interval))

                    break
            else:
                raise base.CommandError("Unknown time span unit '%s'." %
                                        span[-1])

        elif len(span) == 2:
            try:
                # TODO: Support also timezone in the datetime format
                span_from, span_to = [
                    datetime.datetime.strptime(x, '%Y-%m-%dT%H:%M:%S')
                    for x in span
                ]
            except ValueError:
                raise base.CommandError(
                    "Use time format 'yyyy-mm-ddThh:mm:ss' (i.e. '2007-03-04T21:08:12')."
                )

        else:
            raise base.CommandError(
                "Invalid time span parameter. It should be one or two space-delimited values."
            )

        if span_from is not None and span_to is not None and span_from <= span_to:
            if verbose > 1:
                td = span_to - span_from
                self.stdout.write(
                    "Appending %d values from %s to %s.\n" %
                    (((td.seconds + td.days * 24 * 3600) // interval *
                      len(streams)), span_from, span_to))

            while span_from <= span_to:
                for stream_id, (typ, domain) in streams:
                    type_constructor, random_function, default_domain = TYPES[
                        typ]
                    value = random_function(*[
                        type_constructor(d)
                        for d in (domain or default_domain).split(',')
                    ])
                    datastream.append(stream_id, value, span_from)

                span_from += datetime.timedelta(seconds=interval)

            if verbose > 1:
                self.stdout.write("Done. Downsampling.\n")

            datastream.downsample_streams(until=span_to)

        if norealtime:
            return

        if verbose > 1:
            self.stdout.write(
                "Appending real-time value(s) to stream(s) every %s seconds.\n"
                % interval)

        while True:
            for stream_id, (typ, domain) in streams:
                type_constructor, random_function, default_domain = TYPES[typ]
                value = random_function(*[
                    type_constructor(d)
                    for d in (domain or default_domain).split(',')
                ])

                if verbose > 1:
                    self.stdout.write(
                        "Appending value '%s' to stream '%s'.\n" %
                        (value, stream_id))

                datastream.append(stream_id, value)

            datastream.downsample_streams()

            time.sleep(interval)
Пример #19
0
    def handle(self, *args, **options):
        num = int(args and args[0] or 10)
        with_ava = len(args) > 1

        for type in (documents.User, Message, FriendshipOffer, UserFriends, GroupUser):
            type.objects.delete()

        if with_ava:
            if not os.path.exists("faces94"):
                raise CommandError(
                    "faces database not exists, do\n"
                    "wget http://cswww.essex.ac.uk/mv/allfaces/faces94.zip && unzip faces94.zip"
                )

            faces = []

            def visit(arg, dirname, names):
                if len(re.split("/", dirname)) != 3:
                    return
                faces.append(os.path.join(dirname, random.choice(names)))

            os.path.walk("faces94", visit, None)
            random.shuffle(faces)

        # creating accounts
        male_first_names = u"""
        Сергей
        Александр
        Дмитрий
        Андрей
        Артур
        Игорь
        Армен
        Алексей
        Григорий
        """.split()

        female_first_names = u"""
        Анна
        Анастасия
        Риана
        Елена
        Мария
        Дарина
        Кристина
        Марина
        Ангелина
        Полина
        """.split()

        male_last_names = u"""
        Смирнов
        Иванов
        Кузнецов
        Соколов
        Попов
        Лебедев
        Козлов
        Новиков
        Морозов
        Петров
        Волков
        Соловьёв
        Васильев
        Зайцев
        Павлов
        Семёнов
        Голубев
        Виноградов
        Богданов
        Воробьёв
        Фёдоров
        Михайлов
        Беляев
        Тарасов
        """.split()

        female_last_names = [u"%sа" % x for x in male_last_names]

        male_names = (male_first_names, male_last_names)
        female_names = (female_first_names, female_last_names)

        print
        for i in xrange(num):
            if not i % (num / 10 or 10):
                print "\rusers creation %002d%%" % (i * 100 / num)
                sys.stdout.flush()
            if with_ava:
                if faces[i].find("/female/") != -1:
                    names = female_names
                else:
                    names = male_names
            else:
                names = random.choice((male_names, female_names))

            first_name = random.choice(names[0])
            last_name = random.choice(names[1])

            email = "*****@*****.**" % i

            acc = documents.User.create_user(
                first_name=first_name, last_name=last_name, email=email, password="******", is_superuser=bool(not i)
            )
            acc.save()
            if with_ava:
                client = Client()
                client.login(email=email, password="******")
                file = open(faces[i])
                with patch_settings(TASKS_ENABLED={}):
                    client.post(reverse("social:avatar_edit"), {"file": file})

        print "\rusers creation 100%"
        # friending & messaging
        print
        for i in xrange(num):
            max_friends_count = random.randint(0, num % 25 + 25)
            friends_numbers = set([random.randint(0, num - 1) for _ in xrange(max_friends_count)]).difference(set([i]))
            this_user = documents.User.objects().order_by("username")[i]

            for fn in friends_numbers:
                friend = documents.User.objects().order_by("username")[fn]
                # this_user.reload()
                this_user.friends.offers.send(friend)
                random.randint(0, 3) and friend.friends.offers.send(this_user)  # ~66%

            max_msg_sndrs_count = random.randint(0, num)
            msg_sndrs_numbers = set([random.randint(0, num - 1) for _ in xrange(max_msg_sndrs_count)]).difference(
                set([i])
            )

            for sndr_num in msg_sndrs_numbers:
                messages_count = random.randint(0, 3)

                sndr = documents.User.objects().order_by("username")[sndr_num]
                for _ in xrange(messages_count):
                    Message.send(sndr, this_user, lorem_ipsum.paragraph())

            if not i % (num / 10 or 10):
                print "\rfriending and messaging %002d%%" % (i * 100 / num),
                sys.stdout.flush()
        print "\rfriending and messaging 100%"
        print

        CameraType(name="Axis only view", driver="apps.cam.drivers.axis.AxisDriver").save()
        CameraType(name="Axis manage", driver="apps.cam.drivers.axis.AxisDriver", is_controlled=True).save()
        print "\rCemeras 100%"
        print

        Tariff(
            name="View Package 1 min",
            description="View Package Description...",
            cost=1.0,
            duration=60.0,
            is_controlled=False,
        ).save()
        Tariff(
            name="View Package Time", description="View Package Description...", cost=1.0, is_controlled=False
        ).save()
        Tariff(
            name="Manage Package 1 min",
            description="Manage Package Description...",
            cost=2.0,
            duration=60.0,
            is_controlled=True,
        ).save()
        Tariff(
            name="Manage Package Time", description="Manage Package Description...", cost=2.0, is_controlled=True
        ).save()
        print "\rTariffs 100%"
Пример #20
0
            except Exception, e:
                message = "Error [%s] for %s" % (e, data)
            else:
                message = "Successfully downloaded image for %s" % data
        remaining = "%s remaining" % queue.qsize()
        print remaining.ljust(20, "."), message

        # Create database records for the product.
        if exists(image):
            product = Category.objects.get(parent__title=main_category,
                                           title=sub_category).products.create(
                                               title=product,
                                               available=True,
                                               status=CONTENT_STATUS_PUBLISHED,
                                               content="<p>%s</p>" %
                                               paragraph())
            image = "product/%s.jpg" % product.title
            product.images.create(file=image)
            product.variations.create_from_options(product_options)
            product.variations.manage_empty()
            product.variations.update(unit_price=F("id") + "10000")
            product.variations.update(unit_price=F("unit_price") / "1000.0")
            product.copy_default_variation()


if __name__ == "__main__":

    # Load the Google Base data.
    category_url = "http://www.google.com/basepages/producttype/taxonomy.txt"
    try:
        category_data = urlopen(category_url).read()
Пример #21
0
def load_data():
    print 'Starting load data'
    management.call_command('loaddata', 'lfs_all_countries.xml', verbosity=0)

    site = Site.objects.all()[0]
    site.name = site.domain = "www.example.com"
    site.save()

    ie = Country.objects.get(code="ie")
    gb = Country.objects.get(code="gb")
    de = Country.objects.get(code="de")
    us = Country.objects.get(code="us")
    fr = Country.objects.get(code="fr")
    nl = Country.objects.get(code="nl")

    shop, created = Shop.objects.get_or_create(name="lfs test",
                                               shop_owner="John Doe",
                                               description="Test Shop",
                                               default_country=ie)
    shop.save()

    # Tax
    thirteen_percent, created = Tax.objects.get_or_create(
        rate=13.5, description="13.5% rate")
    twentyone_percent, created = Tax.objects.get_or_create(
        rate=21, description="21% rate")

    for ic in Country.objects.all():
        shop.invoice_countries.add(ic)

    shop.shipping_countries.add(ie)
    shop.shipping_countries.add(gb)
    shop.shipping_countries.add(de)
    shop.shipping_countries.add(us)
    shop.shipping_countries.add(fr)
    shop.shipping_countries.add(nl)

    shop.save()

    tax = Tax.objects.create(rate=21)

    direct_debit = PaymentMethod.objects.create(
        name="Direct Debit",
        active=True,
        tax=tax,
    )

    cod = PaymentMethod.objects.create(
        name="Cash on delivery",
        active=True,
        tax=tax,
    )

    paypal = PaymentMethod.objects.create(
        name="PayPal",
        active=True,
        tax=tax,
    )

    prepayment = PaymentMethod.objects.create(
        name="Prepayment",
        active=True,
        tax=tax,
    )

    by_invoice = PaymentMethod.objects.create(
        name="By invoice",
        active=True,
        tax=tax,
    )

    categories = ['animals', 'transport', 'food']
    for cat in categories:
        category, created = Category.objects.get_or_create(name=cat,
                                                           slug=slugify(cat))

    products = [
        "Cat", "Dog", "Mouse", "Surfboard", "Car", "Ship", "Chocolate",
        "Apple", "Orange"
    ]
    i = 0
    for p in products:
        product, created = Product.objects.get_or_create(
            name=p,
            slug=slugify(p),
            price=(i + 1) * 10.0,
            sku=i,
            active=True,
            manage_stock_amount=False)
        product.description = paragraph()

        # set every second product to a different tax rate
        if i % 2 == 0:
            product.tax = thirteen_percent
        else:
            product.tax = twentyone_percent

        if i % 3 == 0:
            product.price_calculator = "lfs.gross_price.GrossPriceCalculator"
        else:
            product.price_calculator = "lfs.net_price.NetPriceCalculator"
        product.save()

        category = Category.objects.all()[i / 3]
        category.products.add(product)
        category.save()
        ts, created = Topseller.objects.get_or_create(product=product,
                                                      position=i)
        fp, created = FeaturedProduct.objects.get_or_create(product=product,
                                                            position=i)
        i = i + 1

    # shipping prices setup
    delivery_time, created = DeliveryTime.objects.get_or_create(
        min=2,
        max=7,
        unit=DELIVERY_TIME_UNIT_DAYS,
        description="2 to 7 for delivery")

    # criterion for geo-specific delivery charges
    cc_ie_uk = CountryCriterion.objects.create(operator=IS)
    cc_ie_uk.countries.add(ie)
    cc_ie_uk.countries.add(gb)
    cc_ie_uk.save()

    cc_not_ie_uk = CountryCriterion.objects.create(operator=IS_NOT)
    cc_not_ie_uk.countries.add(ie)
    cc_not_ie_uk.countries.add(gb)
    cc_not_ie_uk.save()

    #Rest of World >1kg ?16.95
    smp_1095, created = ShippingMethod.objects.get_or_create(
        name="Ireland & UK",
        price=10.95,
        active=True,
        delivery_time=delivery_time,
        priority=1)
    smp_1500, created = ShippingMethod.objects.get_or_create(
        name="International",
        price=15.00,
        active=True,
        delivery_time=delivery_time,
        priority=1)

    co = CriteriaObjects(criterion=cc_ie_uk, content=smp_1095)
    co.save()

    co = CriteriaObjects(criterion=cc_not_ie_uk, content=smp_1500)
    co.save()

    left_slot, created = Slot.objects.get_or_create(name="Left")
    right_slot, created = Slot.objects.get_or_create(name="Right")

    cart_portlet, created = CartPortlet.objects.get_or_create(title="Cart")
    pages_portlet, created = PagesPortlet.objects.get_or_create(
        title="Information")
    categories_portlet, created = CategoriesPortlet.objects.get_or_create(
        title="Products")
    relatedproducts_portlet, created = RelatedProductsPortlet.objects.get_or_create(
        title="Related Products")
    recentproducts_portlet, created = RecentProductsPortlet.objects.get_or_create(
        title="Recent Products")

    PortletAssignment.objects.create(slot_id=left_slot.id,
                                     content=shop,
                                     portlet=categories_portlet,
                                     position=10)
    PortletAssignment.objects.create(slot_id=left_slot.id,
                                     content=shop,
                                     portlet=pages_portlet,
                                     position=15)
    PortletAssignment.objects.create(slot_id=right_slot.id,
                                     content=shop,
                                     portlet=cart_portlet,
                                     position=10)
    PortletAssignment.objects.create(slot_id=right_slot.id,
                                     content=shop,
                                     portlet=recentproducts_portlet,
                                     position=20)
    PortletAssignment.objects.create(slot_id=right_slot.id,
                                     content=shop,
                                     portlet=relatedproducts_portlet,
                                     position=30)
    print 'Finishing load data'
Пример #22
0
    def handle(self, *args, **options):
        verbose = int(options.get('verbosity'))
        interval = options.get('interval')
        nstreams = options.get('nstreams')
        types = options.get('types')
        flush = options.get('flush')
        demo = options.get('demo')
        span = options.get('span')

        if nstreams is None and types is None and not demo and flush:
            datastream.delete_streams()
            return
        elif flush:
            raise base.CommandError("Do you really want to remove all datastream data from the database? Use only '--flush' parameter.")

        if nstreams is None and types is None and not flush and demo:
            types = DEMO_TYPE
            if span == '':
                span = DEMO_SPAN
        elif demo:
            raise base.CommandError("In demo mode other parameters are fixed.")

        if nstreams is None and types is None:
            nstreams = DEFAULT_NSTREAMS

        if types and check_types.match(types):
            types = split_types.findall(types)
            if nstreams is not None and len(types) != nstreams:
                raise base.CommandError("Number of stream types does not mach number of streams.")

            nstreams = len(types)

        elif types:
            raise base.CommandError("Invalid stream types string. Must be a comma separated list of <int|float|enum>[(start,end)|(enum values)].")

        streams = []
        for i in range(nstreams):
            if types is None or types[i][0] != 'enum':
                downsamplers = datastream.backend.value_downsamplers
            else:
                downsamplers = []

            stream_id = datastream.ensure_stream(
                ({'name': 'stream_%d' % i},),
                (
                    'foobar',
                    {'stream_number': i},
                    {'description': lorem_ipsum.paragraph()},
                ),
                downsamplers,
                datastream.Granularity.Seconds,
            )

            if types is not None:
                streams.append((stream_id, types[i]))
            else:
                streams.append((stream_id, ('int', '')))

        span = span.split(' ')
        if len(span) == 1:
            span = span[0]
            for val, key in (('days', 'd'), ('hours', 'h')):
                if span[-1] == key:
                    try:
                        s = int(span[:-1])
                    except ValueError:
                        raise base.CommandError("Time span value must be an integer.")

                    span_to =  datetime.datetime.now(pytz.utc)
                    last_timestamp = self.last_timestamp(streams)
                    span_from = max(
                        span_to - datetime.timedelta(**{val: s}),
                        last_timestamp + datetime.timedelta(seconds=interval)
                    )

                    break
            else:
                raise base.CommandError("Unknown time span unit '%s'." % span[-1])

        elif len(span) == 2:
            try:
                # TODO: Support also timezone in the datetime format
                span_from, span_to = map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%dT%H:%M:%S'), span)
            except ValueError:
                raise base.CommandError("Use time format 'yyyy-mm-ddThh:mm:ss' (i.e. '2007-03-04T21:08:12').")

        else:
            raise base.CommandError("Invalid time span parameter. It should be one or two space-delimited values.")

        if span_from is not None and span_to is not None:
            if verbose > 1:
                td = span_to - span_from
                self.stdout.write("Appending %d values from %s to %s.\n" % (((td.seconds + td.days * 24 * 3600) // interval * len(streams)), span_from, span_to))

            while span_from <= span_to:
                for stream_id, (type, domain) in streams:
                    type_constructor, random_function, default_domain = TYPES[type]
                    value = random_function(*map(type_constructor, (domain or default_domain).split(',')))
                    datastream.append(stream_id, value, span_from)

                span_from += datetime.timedelta(seconds=interval)

            if verbose > 1:
                self.stdout.write("Downsampling.\n")

            datastream.downsample_streams(until=span_to)

        if verbose > 1:
            self.stdout.write("Appending real-time value(s) to stream(s) every %s seconds.\n" % interval)

        while True:
            for stream_id, (type, domain) in streams:
                type_constructor, random_function, default_domain = TYPES[type]
                value = random_function(*map(type_constructor, (domain or default_domain).split(',')))

                if verbose > 1:
                    self.stdout.write("Appending value '%s' to stream '%s'.\n" % (value, stream_id))

                datastream.append(stream_id, value)

            datastream.downsample_streams()

            time.sleep(interval)
Пример #23
0
 def generate_fish(self):
     while True:
         user = self.get_user()
         pi = PostInterface(user)
         post = pi.add_post(lorem_ipsum.paragraph(), tags=self.get_tags())
         self.make_replies_for_post(post)
Пример #24
0
    def make_generator(self, model, data, context):
        fields = {}
        m2ms = defaultdict(list)

        if data:
            for key, value in data.items():
                if isinstance(value, (str, unicode)):
                    if '{{' in value:
                        v = self.render(value, context)
                    else:
                        v = value

                    if v.startswith("'") and v.endswith("'"):
                        v = unicode(v[1:-1])
                    else:
                        match = LIPSUM_PARAGRAPH.match(v)
                        if match:
                            groups = match.groups()
                            if groups[0]:
                                v = paragraph(int(groups[0]))
                            else:
                                v = paragraph()
                        else:
                            match = LIPSUM_WORDS.match(v)
                            if match:
                                groups = match.groups()
                                if groups[0]:
                                    v = words(int(groups[0]), False).capitalize()
                                else:
                                    v = words(1, False).capitalize()
                else:
                    if isinstance(value, dict):
                        ff = model._meta.get_field_by_name(key)
                        if isinstance(ff[0], ManyToManyField):
                            c = value.get('count', 1)
                            o = ff[0].rel.to

                            if 'fields' in value:
                                self._indent += 1

                                gg = self.make_generator(o, value['fields'],
                                    {
                                        'parent': context
                                    }
                                )

                                for ag in gg(o, c, self._log):
                                    m2ms[key].append(ag)

                                self._indent -= 1
                            elif 'from' in value or 'at' in value or 'where' in value or 'exclude' in value:
                                m2ms[key] = self.query(
                                    o,
                                    context = context,
                                    **value
                                )
                            else:
                                raise CommandError(
                                    'No data specified for many-to-many relationship between %s and %s' % (
                                        unicode(model._meta.verbose_name),
                                        unicode(o._meta.verbose_name)
                                    )
                                )

                            continue

                        if isinstance(ff[0], (OneToOneField, ForeignKey)):
                            o = ff[0].rel.to
                            if 'fields' in value:
                                self._indent += 1

                                gg = self.make_generator(o, value['fields'],
                                    {
                                        'parent': context
                                    }
                                )

                                for ag in gg(o, 1, self._log):
                                    v = ag

                                self._indent -= 1
                            elif 'at' in value or 'where' in value or 'exclude' in value:
                                try:
                                    v = self.query(
                                        o,
                                        count = 1,
                                        context = context,
                                        **value
                                    )[0]
                                except IndexError:
                                    raise CommandError(
                                        'No %s found to add' % (
                                            unicode(o._meta.verbose_name_plural)
                                        )
                                    )
                            else:
                                raise CommandError(
                                    'No data specified for foreign key relationship between %s and %s' % (
                                        unicode(model._meta.verbose_name),
                                        unicode(o._meta.verbose_name)
                                    )
                                )
                    else:
                        v = value

                fields[key] = v

        obj = context.get('object')
        if isinstance(obj, Model):
            for field in model._meta.get_all_field_names():
                af = model._meta.get_field_by_name(field)
                if isinstance(af[0], (OneToOneField, ForeignKey)):
                    if not field in fields:
                        if issubclass(af[0].rel.to, type(obj)):
                            fields[field] = obj
                elif isinstance(af[0], FileField):
                    filename = fields.get(field)
                    if filename:
                        if filename.startswith('http:') or filename.startswith('https:'):
                            fields[field] = self.download(filename)
                        else:
                            if not filename.startswith('/'):
                                filename = path.join(
                                    path.dirname(
                                        get_app(model._meta.app_label).__file__
                                    ),
                                    filename
                                )

                            if not path.exists(filename):
                                raise CommandError('Cannot find file %s' % filename)

                            fields[field] = File(
                                open(filename, 'r')
                            )

        def g(model, count, log, **kwargs):
            for x in range(0, count):
                obj = model(**fields)

                if 'password' in fields:
                    obj.set_password(
                        fields.pop('password')
                    )

                try:
                    obj.full_clean()
                except ValidationError, ex:
                    raise CommandError(
                        u'Error creating %s' % (
                            model._meta.verbose_name
                        ),
                        ex
                    )

                obj.save()
                yield obj

                for m, i in m2ms.items():
                    field = getattr(obj, m)
                    field.add(*i)

                    c = len(i)
                    if isinstance(i, QuerySet):
                        self._log(
                            'Added %d %s' % (
                                c,
                                c != 1 and unicode(
                                    i.model._meta.verbose_name_plural
                                ) or unicode(
                                    i.model._meta.verbose_name
                                )
                            ),
                            1
                        )
                    else:
                        self._log(
                            'Created %d %s' % (
                                c,
                                c != 1 and unicode(
                                    i[0]._meta.verbose_name_plural
                                ) or unicode(
                                    i[0]._meta.verbose_name
                                )
                            ),
                            1
                        )
Пример #25
0
    def handle(self, *args, **options):
        verbose = int(options.get('verbosity'))
        interval = options.get('interval')
        nstreams = options.get('nstreams')
        types = options.get('types')
        flush = options.get('flush')
        demo = options.get('demo')
        span = options.get('span')

        if nstreams is None and types is None and not demo and flush:
            datastream.delete_streams()
            return
        elif flush:
            raise base.CommandError(
                "Do you really want to remove all datastream data from the database? Use only '--flush' parameter."
            )

        if nstreams is None and types is None and not flush and demo:
            types = DEMO_TYPE
            if span == '':
                span = DEMO_SPAN
        elif demo:
            raise base.CommandError("In demo mode other parameters are fixed.")

        if nstreams is None and types is None:
            nstreams = DEFAULT_NSTREAMS

        if types and check_types.match(types):
            types = split_types.findall(types)
            if nstreams is not None and len(types) != nstreams:
                raise base.CommandError(
                    "Number of stream types does not mach number of streams.")

            nstreams = len(types)

        elif types:
            raise base.CommandError(
                "Invalid stream types string. Must be a comma separated list of <int|float|enum>[(start,end)|(enum values)]."
            )

        streams = []
        for i in range(nstreams):
            if types is None or types[i][0] != 'enum':
                downsamplers = datastream.backend.value_downsamplers
            else:
                downsamplers = []

            stream_id = datastream.ensure_stream(
                ({
                    'name': 'stream_%d' % i
                }, ),
                (
                    'foobar',
                    {
                        'stream_number': i
                    },
                    {
                        'description': lorem_ipsum.paragraph()
                    },
                ),
                downsamplers,
                datastream.Granularity.Seconds,
            )

            if types is not None:
                streams.append((stream_id, types[i]))
            else:
                streams.append((stream_id, ('int', '')))

        span = span.split(' ')
        if len(span) == 1:
            span = span[0]
            for val, key in (('days', 'd'), ('hours', 'h')):
                if span[-1] == key:
                    try:
                        s = int(span[:-1])
                    except ValueError:
                        raise base.CommandError(
                            "Time span value must be an integer.")

                    span_to = datetime.datetime.now(pytz.utc)
                    last_timestamp = self.last_timestamp(streams)
                    span_from = max(
                        span_to - datetime.timedelta(**{val: s}),
                        last_timestamp + datetime.timedelta(seconds=interval))

                    break
            else:
                raise base.CommandError("Unknown time span unit '%s'." %
                                        span[-1])

        elif len(span) == 2:
            try:
                # TODO: Support also timezone in the datetime format
                span_from, span_to = map(
                    lambda x: datetime.datetime.strptime(
                        x, '%Y-%m-%dT%H:%M:%S'), span)
            except ValueError:
                raise base.CommandError(
                    "Use time format 'yyyy-mm-ddThh:mm:ss' (i.e. '2007-03-04T21:08:12')."
                )

        else:
            raise base.CommandError(
                "Invalid time span parameter. It should be one or two space-delimited values."
            )

        if span_from is not None and span_to is not None:
            if verbose > 1:
                td = span_to - span_from
                self.stdout.write(
                    "Appending %d values from %s to %s.\n" %
                    (((td.seconds + td.days * 24 * 3600) // interval *
                      len(streams)), span_from, span_to))

            while span_from <= span_to:
                for stream_id, (type, domain) in streams:
                    type_constructor, random_function, default_domain = TYPES[
                        type]
                    value = random_function(*map(type_constructor, (
                        domain or default_domain).split(',')))
                    datastream.append(stream_id, value, span_from)

                span_from += datetime.timedelta(seconds=interval)

            if verbose > 1:
                self.stdout.write("Downsampling.\n")

            datastream.downsample_streams(until=span_to)

        if verbose > 1:
            self.stdout.write(
                "Appending real-time value(s) to stream(s) every %s seconds.\n"
                % interval)

        while True:
            for stream_id, (type, domain) in streams:
                type_constructor, random_function, default_domain = TYPES[type]
                value = random_function(*map(type_constructor, (
                    domain or default_domain).split(',')))

                if verbose > 1:
                    self.stdout.write(
                        "Appending value '%s' to stream '%s'.\n" %
                        (value, stream_id))

                datastream.append(stream_id, value)

            datastream.downsample_streams()

            time.sleep(interval)
Пример #26
0
    def handle(self, *args, **options):
        verbose = int(options.get('verbosity'))
        interval = options.get('interval')
        nstreams = options.get('nstreams')
        types = options.get('types')
        flush = options.get('flush')
        demo = options.get('demo')
        span = options.get('span')
        norealtime = options.get('norealtime')

        if nstreams is None and types is None and not demo and flush:
            datastream.delete_streams()
            return
        elif flush:
            raise base.CommandError("Do you really want to remove all datastream data from the database? Use only '--flush' parameter.")

        if nstreams is None and types is None and not flush and demo:
            types = DEMO_TYPE
            if span == '':
                span = DEMO_SPAN
        elif demo:
            raise base.CommandError("In demo mode other parameters are fixed.")

        if nstreams is None and types is None:
            nstreams = DEFAULT_NSTREAMS

        if types and check_types.match(types):
            types = split_types.findall(types)
            if nstreams is not None and len(types) != nstreams:
                raise base.CommandError("Number of stream types does not mach number of streams.")

            nstreams = len(types)

        elif types:
            raise base.CommandError("Invalid stream types string. Must be a comma separated list of <int|float|enum>[(start,end)|(enum values)].")

        streams = []
        for i in range(nstreams):
            if types is not None:
                typ = types[i]
            else:
                typ = ('int', '')

            if typ[0] == 'enum':
                value_type = 'nominal'
                downsamplers = ['count']
            elif typ[0] == 'graph':
                value_type = 'graph'
                downsamplers = ['count']
            else:
                value_type = 'numeric'
                downsamplers = datastream.backend.value_downsamplers

            visualization_value_downsamplers = []
            for downsampler in ['mean', 'min', 'max']:
                if downsampler in downsamplers:
                    visualization_value_downsamplers.append(downsampler)

            type_constructor, random_function, default_domain = TYPES[typ[0]]
            domain = typ[1] or default_domain
            domain_range = [type_constructor(d) for d in domain.split(',')]

            stream_id = datastream.ensure_stream(
                {'title': 'Stream %d' % i},
                {
                    'description': lorem_ipsum.paragraph(),
                    'unit_description': 'random, domain: %s' % domain,
                    'stream_number': i,
                    'visualization': {
                        'type': 'state' if typ is 'enum' else 'line',
                        'hidden': True if typ is 'graph' else False,
                        'value_downsamplers': visualization_value_downsamplers,
                        'time_downsamplers': ['mean'],
                        'minimum': domain_range[0] if value_type == 'numeric' else None,
                        'maximum': domain_range[1] if value_type == 'numeric' else None,
                    },
                },
                downsamplers,
                datastream.Granularity.Seconds,
                value_type=value_type,
            )

            streams.append((stream_id, typ))

        span = span.split(' ')
        if len(span) == 1 and span[0]:
            span = span[0]
            for val, key in (('days', 'd'), ('hours', 'h'), ('minutes', 'm'), ('seconds', 's')):
                if span[-1] == key:
                    try:
                        s = int(span[:-1])
                    except ValueError:
                        raise base.CommandError("Time span value must be an integer.")

                    span_to = datetime.datetime.now(pytz.utc)
                    last_timestamp = self.last_timestamp(streams)

                    span_from = max(
                        span_to - datetime.timedelta(**{val: s}),
                        last_timestamp + datetime.timedelta(seconds=interval)
                    )

                    break
            else:
                raise base.CommandError("Unknown time span unit '%s'." % span[-1])

        elif len(span) == 2:
            try:
                # TODO: Support also timezone in the datetime format
                span_from, span_to = [datetime.datetime.strptime(x, '%Y-%m-%dT%H:%M:%S') for x in span]
            except ValueError:
                raise base.CommandError("Use time format 'yyyy-mm-ddThh:mm:ss' (i.e. '2007-03-04T21:08:12').")

        else:
            raise base.CommandError("Invalid time span parameter. It should be one or two space-delimited values.")

        if span_from is not None and span_to is not None and span_from <= span_to:
            if verbose > 1:
                td = span_to - span_from
                self.stdout.write("Appending %d values from %s to %s.\n" % (((td.seconds + td.days * 24 * 3600) // interval * len(streams)), span_from, span_to))

            while span_from <= span_to:
                for stream_id, (typ, domain) in streams:
                    type_constructor, random_function, default_domain = TYPES[typ]
                    value = random_function(*[type_constructor(d) for d in (domain or default_domain).split(',')])
                    datastream.append(stream_id, value, span_from)

                span_from += datetime.timedelta(seconds=interval)

            if verbose > 1:
                self.stdout.write("Done. Downsampling.\n")

            datastream.downsample_streams(until=span_to)

        if norealtime:
            return

        if verbose > 1:
            self.stdout.write("Appending real-time value(s) to stream(s) every %s seconds.\n" % interval)

        while True:
            for stream_id, (typ, domain) in streams:
                type_constructor, random_function, default_domain = TYPES[typ]
                value = random_function(*[type_constructor(d) for d in (domain or default_domain).split(',')])

                if verbose > 1:
                    self.stdout.write("Appending value '%s' to stream '%s'.\n" % (value, stream_id))

                datastream.append(stream_id, value)

            datastream.downsample_streams()

            time.sleep(interval)
Пример #27
0
 def paragraph(self):
     """Random text with variable number of words, several sentences."""
     return lorem_ipsum.paragraph()