Просмотр исходного кода

Small improvements for fake data generators

rafalp 6 лет назад
Родитель
Сommit
459710b6bc

+ 22 - 13
misago/core/management/progressbar.py

@@ -6,18 +6,27 @@ def show_progress(command, step, total, since=None):
     filled = progress // 2
     blank = 50 - filled
 
-    line = '\r%s%% [%s%s]'
-    rendered_line = line % (str(progress).rjust(3), '=' * filled, ' ' * blank)
+    template = '\r%(step)s (%(progress)s%%) [%(progressbar)s]%(estimation)s'
+    variables = {
+        "step": str(step).rjust(len(str(total))),
+        "progress": str(progress).rjust(3),
+        "progressbar": "".join(['=' * filled, ' ' * blank]),
+        "estimation": get_estimation_str(since, progress, step, total),
+    }
 
-    if since:
-        progress_float = float(step) * 100.0 / float(total)
-        if progress_float > 0:
-            step_time = (time.time() - since) / progress_float
-            estimated_time = (100 - progress) * step_time
-            clock = time.strftime('%H:%M:%S', time.gmtime(estimated_time))
-            rendered_line = '%s %s est.' % (rendered_line, clock)
-        else:
-            rendered_line = '%s --:--:-- est.' % rendered_line
-
-    command.stdout.write(rendered_line, ending='')
+    command.stdout.write(template % variables, ending='')
     command.stdout.flush()
+
+
+def get_estimation_str(since, progress, step, total):
+    if not since:
+        return ""
+    
+    progress_float = float(step) * 100.0 / float(total)
+    if progress_float == 0:
+        return ' --:--:-- est.'
+
+    step_time = (time.time() - since) / progress_float
+    estimated_time = (100 - progress) * step_time
+    clock = time.strftime('%H:%M:%S', time.gmtime(estimated_time))
+    return ' %s est.' % clock

+ 1 - 2
misago/faker/management/commands/createfakebans.py

@@ -93,8 +93,6 @@ class Command(BaseCommand):
         message = 'Creating %s fake bans...\n'
         self.stdout.write(message % fake_bans_to_create)
 
-        message = '\n\nSuccessfully created %s fake bans'
-
         created_count = 0
         show_progress(self, created_count, fake_bans_to_create)
         for _ in range(fake_bans_to_create):
@@ -120,4 +118,5 @@ class Command(BaseCommand):
             created_count += 1
             show_progress(self, created_count, fake_bans_to_create)
 
+        message = '\n\nSuccessfully created %s fake bans'
         self.stdout.write(message % created_count)

+ 1 - 2
misago/faker/management/commands/createfakecategories.py

@@ -44,8 +44,6 @@ class Command(BaseCommand):
         message = 'Creating %s fake categories...\n'
         self.stdout.write(message % items_to_create)
 
-        message = '\n\nSuccessfully created %s fake categories in %s'
-
         created_count = 0
         start_time = time.time()
         show_progress(self, created_count, items_to_create)
@@ -91,4 +89,5 @@ class Command(BaseCommand):
 
         total_time = time.time() - start_time
         total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
+        message = '\n\nSuccessfully created %s fake categories in %s'
         self.stdout.write(message % (created_count, total_humanized))

+ 1 - 2
misago/faker/management/commands/createfakefollowers.py

@@ -19,8 +19,6 @@ class Command(BaseCommand):
         message = 'Adding fake followers to %s users...\n'
         self.stdout.write(message % total_users)
 
-        message = '\nSuccessfully added %s fake followers in %s'
-
         total_followers = 0
         processed_count = 0
 
@@ -54,4 +52,5 @@ class Command(BaseCommand):
 
         total_time = time.time() - start_time
         total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
+        message = '\nSuccessfully added %s fake followers in %s'
         self.stdout.write(message % (total_followers, total_humanized))

+ 5 - 4
misago/faker/management/commands/createfakethreads.py

@@ -26,7 +26,7 @@ corpus_short = EnglishCorpus(max_length=150)
 
 
 class Command(BaseCommand):
-    help = 'Creates random threads and posts for dev and testing purposes.'
+    help = 'Creates random threads for dev and testing purposes.'
 
     def add_arguments(self, parser):
         parser.add_argument(
@@ -44,9 +44,8 @@ class Command(BaseCommand):
 
         fake = Factory.create()
 
-        self.stdout.write('Creating fake threads...\n')
-
-        message = '\nSuccessfully created %s fake threads in %s'
+        message = 'Creating %s fake threads...\n'
+        self.stdout.write(message % items_to_create)
 
         created_threads = 0
         start_time = time.time()
@@ -160,6 +159,7 @@ class Command(BaseCommand):
 
         pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
         self.stdout.write('\nPinning %s threads...' % pinned_threads)
+        
         for _ in range(0, pinned_threads):
             thread = Thread.objects.order_by('?')[:1][0]
             if random.randint(0, 100) > 75:
@@ -174,6 +174,7 @@ class Command(BaseCommand):
 
         total_time = time.time() - start_time
         total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
+        message = '\nSuccessfully created %s fake threads in %s'
         self.stdout.write(message % (created_threads, total_humanized))
 
     def fake_post_content(self):

+ 9 - 3
misago/faker/management/commands/createfakeusers.py

@@ -38,16 +38,21 @@ class Command(BaseCommand):
         message = 'Creating %s fake user accounts...\n'
         self.stdout.write(message % items_to_create)
 
-        message = '\n\nSuccessfully created %s fake user accounts in %s'
-
         created_count = 0
         start_time = time.time()
         show_progress(self, created_count, items_to_create)
 
         while created_count < items_to_create:
             try:
-                user = UserModel.objects.create_user(
+                possible_usernames = [
                     fake.first_name(),
+                    fake.last_name(),
+                    fake.name().replace(' ', ''),
+                    fake.user_name(),
+                ]
+
+                user = UserModel.objects.create_user(
+                    random.choice(possible_usernames),
                     fake.email(),
                     'pass123',
                     set_default_avatar=False,
@@ -64,4 +69,5 @@ class Command(BaseCommand):
 
         total_time = time.time() - start_time
         total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
+        message = '\n\nSuccessfully created %s fake user accounts in %s'
         self.stdout.write(message % (created_count, total_humanized))