Commits

chrislawlor committed 2cae293

Mgmt command improvements: generate_data accepts args, added generate_ratings

  • Participants
  • Parent commits 09539a7

Comments (0)

Files changed (2)

murphy/management/commands/generate_data.py

 import datetime
 import os
 import random
+from optparse import make_option
 
 now = datetime.datetime.now()
 
     
 
 class Command(BaseCommand):
-    NUM_PLUGINS=1000
-    NUM_USERS=100
-    TAGS_PER_PLUGIN=3
+    help = "Fills the DB with random plugins and users for testing purposes"
+    args = '[--plugins <int>] [--users <int>] [--tags <int>]'
+    option_list = BaseCommand.option_list + (
+        make_option('--plugins', '-p', default=1000, dest='plugins',
+                    help='The number of plugins to create.'),
+        make_option('--users', '-u', default=100, dest='users',
+                    help='The number of users to create.'),
+        make_option('--tags', '-t', default=3, dest='tags',
+                    help='The number of tags to create per plugin'),
+    )
     
-    ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In auctor dictum lacus, eu scelerisque tortor consequat ut. Vivamus pellentesque orci id velit rutrum elementum. Nulla facilisi. Aenean scelerisque sapien elit. Donec semper nibh vitae diam porta vel vulputate eros volutpat. Curabitur luctus neque non ante rhoncus ut fermentum mauris pellentesque. Cras eu sapien dolor. In semper rhoncus lacus, vel egestas ligula blandit ac. Mauris dolor urna, consectetur et pellentesque a, suscipit non nisl. Aenean ultricies est sit amet felis blandit adipiscing egestas urna mollis. Duis sodales varius lacinia. Maecenas eu placerat magna. Etiam ligula augue, mollis at lacinia sit amet, interdum at massa. Maecenas consequat, ligula eu porta adipiscing, arcu nisi eleifend mauris, sit amet ultricies orci sem id odio. Proin cursus, tellus sit amet sollicitudin pharetra, urna nisi gravida tellus, at mattis leo urna a mauris. Nunc luctus, neque rhoncus iaculis placerat, magna est ultricies arcu, a elementum mauris nulla ac purus."
+    def handle(self, **options):
+        
+        NUM_PLUGINS= int(options.get('plugins', 1000))
+        NUM_USERS= int(options.get('users', 100))
+        TAGS_PER_PLUGIN=int(options.get('tags', 3))
+        
+        print "About to create %d users, %d plugins, with %d tag(s) per plugin." % (NUM_USERS, NUM_PLUGINS, TAGS_PER_PLUGIN)
+        
+        ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In auctor dictum lacus, eu scelerisque tortor consequat ut. Vivamus pellentesque orci id velit rutrum elementum. Nulla facilisi. Aenean scelerisque sapien elit. Donec semper nibh vitae diam porta vel vulputate eros volutpat. Curabitur luctus neque non ante rhoncus ut fermentum mauris pellentesque. Cras eu sapien dolor. In semper rhoncus lacus, vel egestas ligula blandit ac. Mauris dolor urna, consectetur et pellentesque a, suscipit non nisl. Aenean ultricies est sit amet felis blandit adipiscing egestas urna mollis. Duis sodales varius lacinia. Maecenas eu placerat magna. Etiam ligula augue, mollis at lacinia sit amet, interdum at massa. Maecenas consequat, ligula eu porta adipiscing, arcu nisi eleifend mauris, sit amet ultricies orci sem id odio. Proin cursus, tellus sit amet sollicitudin pharetra, urna nisi gravida tellus, at mattis leo urna a mauris. Nunc luctus, neque rhoncus iaculis placerat, magna est ultricies arcu, a elementum mauris nulla ac purus."
+        
+        categories = Category.objects.all()
+        licenses = License.objects.all()
+        
+        HERE = os.path.abspath(os.path.dirname(__file__))
+        
+        # build tag list
+        taglist = []
+        try:
+            tagfile = open(os.path.join(HERE, 'tags.txt'), 'r')
+        except IOError:
+            sys.exit(1)
     
-    categories = Category.objects.all()
-    licenses = License.objects.all()
+        for line in tagfile:
+            taglist.append(line.strip())
+        tagfile.close()
+        
+        # build name list
+        names = []
+        try:
+            namefile = open(os.path.join(HERE, 'names.txt'), 'r')
+        except IOError:
+            sys.exit()
+            
+        for name in namefile:
+            names.append(name.strip())
+        namefile.close()
+        try:
+            # create users
+            users = []
+            used_names = []
+            for i in range(0, NUM_USERS):
+                username = get_random_from(names)
+                
+                # make sure name is available
+                while username in used_names:
+                    username += str(i)
+                    
+                user = User.objects.create_user(username, '%s@domain.com' % username, 'password')
+                user.save()
+                users.append(user)
+                used_names.append(username)
+            
+            print "%d users created." % len(users)
+            
+            
+            # create Plugins
+            for pindex in range(0, NUM_PLUGINS):
+                pname = get_some_from(names, 2)
+                plugin = Plugin(title=' '.join(pname),
+                                slug='-'.join(pname),
+                                category=get_random_from(categories),
+                                author= get_random_from(users),
+                                summary = ipsum[:100],
+                                description = ipsum,
+                                install_details = ipsum,
+                                license = get_random_from(licenses),
+                                tags = ' '.join(get_some_from(taglist)))
+                plugin.save()
+                
+                release = Release(file='/static/dummy_download.txt',
+                                  plugin=plugin,
+                                  version=random.randint(1,10),
+                                  vim_version=random.randint(5,7),
+                                  release_notes=ipsum[:100],
+                                  pub_date=get_random_date())
+                release.save()
+                
+                # create download activity
+                num_downloads = random.randint(0,20)
+                for i in range(0,num_downloads):
+                    
+                    dl = Download(user=get_random_from(users),
+                                  date=get_random_date(max_days_ago=(now - release.pub_date).days),
+                                  plugin=plugin,
+                                  release=release)
+                    dl.save()
     
-    HERE = os.path.abspath(os.path.dirname(__file__))
-    # build tag list
-    taglist = []
-    try:
-        tagfile = open(os.path.join(HERE, 'tags.txt'), 'r')
-    except IOError:
-        sys.exit(1)
-
-    for line in tagfile:
-        taglist.append(line.strip())
-    tagfile.close()
-    
-    # build name list
-    names = []
-    try:
-        namefile = open(os.path.join(HERE, 'names.txt'), 'r')
-    except IOError:
-        sys.exit()
+                if pindex % 10 == 0:
+                    print "%s plugins created." % str(pindex+1)
+            
+                
+        print "Database now holds:"
+        print "%d users" % User.objects.all().count()
+        print "%d plugins" % Plugin.objects.all().count()
         
-    for name in namefile:
-        names.append(name.strip())
-    namefile.close()
-    try:
-        # create users
-        users = []
-        used_names = []
-        for i in range(0, NUM_USERS):
-            username = get_random_from(names)
+        except Exception, ex:
+            import pdb; pdb.set_trace()
             
-            # make sure name is available
-            while username in used_names:
-                username += str(i)
-                
-            user = User.objects.create_user(username, '%s@domain.com' % username, 'password')
-            user.save()
-            users.append(user)
-            used_names.append(username)
-        
-        print "%d users created." % len(users)
-        
-        
-        # create Plugins
-        for pindex in range(0, NUM_PLUGINS):
-            pname = get_some_from(names, 2)
-            plugin = Plugin(title=' '.join(pname),
-                            slug='-'.join(pname),
-                            category=get_random_from(categories),
-                            author= get_random_from(users),
-                            summary = ipsum[:100],
-                            description = ipsum,
-                            install_details = ipsum,
-                            license = get_random_from(licenses),
-                            tags = ' '.join(get_some_from(taglist)))
-            plugin.save()
-            
-            release = Release(file='/static/dummy_download.txt',
-                              plugin=plugin,
-                              version=random.randint(1,10),
-                              vim_version=random.randint(5,7),
-                              release_notes=ipsum[:100],
-                              pub_date=get_random_date())
-            release.save()
-            
-            # create download activity
-            num_downloads = random.randint(0,20)
-            for i in range(0,num_downloads):
-                
-                dl = Download(user=get_random_from(users),
-                              date=get_random_date(max_days_ago=(now - release.pub_date).days),
-                              plugin=plugin,
-                              release=release)
-                dl.save()
-
-            if pindex % 100 == 0:
-                print "%s plugins created." % str(pindex+1)
-        
-            
-        
-    
-    except Exception, ex:
-        import pdb; pdb.set_trace()
-        
                         

murphy/management/commands/generate_ratings.py

+from django.core.management.base import BaseCommand
+from django.contrib.auth.models import User
+from djangoratings.models import Vote
+from murphy.models import Plugin, RATING_CHOICES
+
+import datetime
+import random
+from optparse import make_option
+
+def get_random_rating():
+    rating = random.randint(0,len(RATING_CHOICES)-1)
+    return RATING_CHOICES[rating][0]
+
+class Command(BaseCommand):
+    """
+    Create a number of random ratings, on random plugins
+    """
+    
+    help = "Creates a number of random ratings, on random plugins\nDefaults to 500"
+    args = '--count <int>'
+    option_list = BaseCommand.option_list + (
+        make_option('--count', '-c', default=500, dest='count',
+                    help='The number of ratings to create.'),
+    )
+    
+    def handle (self, **options):
+        
+        num_ratings = int(options.get('count', 500))
+        
+        plugins = Plugin.objects.all()
+        num_plugins = plugins.count()
+        
+        users = User.objects.all()
+        num_users = users.count()
+        
+        # ratings needs an IP address, let's see if we can
+        # just use the same one for all of them
+        ip_address = '192.168.1.1'
+        
+        for i in range(0, num_ratings):
+            plugin = plugins[random.randint(0,num_plugins-1)]
+            user = users[random.randint(0,num_users-1)]
+            plugin.rating.add(score=get_random_rating(),
+                              user=user,
+                              ip_address=ip_address)
+            
+            if i % 10 == 0:
+                print "%s ratings created." % str(i)
+        
+        print "DB now holds %d ratings" % Vote.objects.all().count()
+            
+