app/soc/models/seed_db.py
changeset 2048 236f37777764
parent 2027 199bcab679df
child 2074 5c75312566d5
equal deleted inserted replaced
2047:7e9656691c8e 2048:236f37777764
    22   ]
    22   ]
    23 
    23 
    24 
    24 
    25 import datetime
    25 import datetime
    26 import itertools
    26 import itertools
       
    27 import logging
    27 import random
    28 import random
    28 
    29 
    29 from google.appengine.api import users
    30 from google.appengine.api import users
    30 from google.appengine.api import memcache
    31 from google.appengine.api import memcache
    31 from google.appengine.ext import db
    32 from google.appengine.ext import db
    76   current_user.put()
    77   current_user.put()
    77 
    78 
    78   return account, current_user
    79   return account, current_user
    79 
    80 
    80 
    81 
       
    82 def determine_index_of_seeded_entity(entity):
       
    83   """Determines the index of a seeded_entity.
       
    84 
       
    85   Because we seed entities in a predictable manner, we can look at an entity
       
    86     and determine which one it is.  This works iff entities are seeded with
       
    87     link_id's of the form: foo_%04d (where 4 is at least the number of digits
       
    88     of the index of the highest-seeded entity).
       
    89   """
       
    90 
       
    91 
       
    92 def seed_and_put_example_user(i):
       
    93   """Creates and Persists an example user identified by i.
       
    94 
       
    95   Args:
       
    96     i, int: the index of this example user.
       
    97 
       
    98   Returns:
       
    99     None
       
   100 
       
   101   Side Effects:
       
   102     Persists a user to the datastore.
       
   103   """
       
   104   user_properties = {
       
   105       'key_name': 'user_%04d' % i,
       
   106       'link_id': 'user_%04d' % i,
       
   107       'account': users.User(email='user_%04d@example.com' % i),
       
   108       'name': 'User %04d' % i,
       
   109       }
       
   110   entity = User(**user_properties)
       
   111   entity.put()
       
   112 
       
   113 
    81 def seed(request, *args, **kwargs):
   114 def seed(request, *args, **kwargs):
    82   """Seeds the datastore with some default values.
   115   """Seeds the datastore with some default values.
    83   """
   116   """
    84 
   117 
    85   site_properties = {
   118   site_properties = {
    93 
   126 
    94   account, current_user = ensureUser()
   127   account, current_user = ensureUser()
    95 
   128 
    96 
   129 
    97   for i in range(15):
   130   for i in range(15):
    98     user_properties = {
   131     seed_and_put_example_user(i)
    99         'key_name': 'user_%d' % i,
       
   100         'link_id': 'user_%d' % i,
       
   101         'account': users.User(email='user_%d@example.com' % i),
       
   102         'name': 'User %d' % i,
       
   103         }
       
   104     entity = User(**user_properties)
       
   105     entity.put()
       
   106 
       
   107 
   132 
   108   group_properties = {
   133   group_properties = {
   109        'key_name': 'google',
   134        'key_name': 'google',
   110        'link_id': 'google',
   135        'link_id': 'google',
   111        'name': 'Google Inc.',
   136        'name': 'Google Inc.',
   294           'link_id': 'test',
   319           'link_id': 'test',
   295           'scope_path': 'google/gsoc2009/org_%d' % i,
   320           'scope_path': 'google/gsoc2009/org_%d' % i,
   296           'scope': entity,
   321           'scope': entity,
   297           'program': gsoc2009,
   322           'program': gsoc2009,
   298           })
   323           })
   299  
   324 
   300       # Admin for the first org 
   325       # Admin for the first org
   301       if i == 0:
   326       if i == 0:
   302         org_1_admin = OrgAdmin(**role_properties)
   327         org_1_admin = OrgAdmin(**role_properties)
   303         org_1_admin.put()
   328         org_1_admin.put()
   304 
   329 
   305       # Only a mentor for the second org
   330       # Only a mentor for the second org
   361   memcache.flush_all()
   386   memcache.flush_all()
   362 
   387 
   363   return http.HttpResponse('Done')
   388   return http.HttpResponse('Done')
   364 
   389 
   365 
   390 
   366 def seed_user(request, i):
   391 def seed_user(unused_request, i):
   367   """Returns the properties for a new user entity.
   392   """Returns the properties for a new user entity.
   368   """
   393   """
   369 
       
   370   properties = {
   394   properties = {
   371       'key_name': 'user_%(num)d' % i,
   395       'key_name': 'user_%(num)d' % i,
   372       'link_id': 'user_%(num)d' % i,
   396       'link_id': 'user_%(num)d' % i,
   373       'account': users.User(email='user_%(num)d@example.com' % i),
   397       'account': users.User(email='user_%(num)d@example.com' % i),
   374       'name': 'User %(num)d' % i,
   398       'name': 'User %(num)d' % i,
   520 
   544 
   521     all_properties.append(properties)
   545     all_properties.append(properties)
   522 
   546 
   523   return all_properties
   547   return all_properties
   524 
   548 
       
   549 
       
   550 SEEDABLE_MODEL_TYPES = {
       
   551     'user' : (User, seed_and_put_example_user),
       
   552     }
       
   553 
       
   554 
       
   555 def new_seed_many(request, *args, **kwargs):
       
   556   """Seeds many instances of the specified type.
       
   557 
       
   558   Takes as URL parameters:
       
   559   seed_type: the type of entity to seed; should be a key in SEEDABLE_MODEL_TYPES
       
   560   goal: the total number of entities desired
       
   561 
       
   562   This differs from seed_many. Instead of having to specify many parameters
       
   563     that are the state of an in-flight process, simply say how many you want
       
   564     to have (at least) at the end.  This will make progress towards that goal.
       
   565     In my test run, even adding 1001 users completed in far less than the
       
   566     limit for one request, so pagination was unnecessary.
       
   567   """
       
   568   # First, figure out which model we're interested in.
       
   569   if ('seed_type' not in request.GET or
       
   570       request.GET['seed_type'] not in SEEDABLE_MODEL_TYPES):
       
   571     return http.HttpResponse(
       
   572         ('Missing or invalid required argument "seed_type" (which model'
       
   573         ' type to populate). '
       
   574         'Valid values are: %s') % SEEDABLE_MODEL_TYPES.keys())
       
   575 
       
   576   (model_class, seed_func) = SEEDABLE_MODEL_TYPES[request.GET['seed_type']]
       
   577 
       
   578   if 'goal' not in request.GET:
       
   579     return http.HttpResponse(
       
   580         'Missing required argument "goal" (how many entities of '
       
   581         'this type you want to have in the datastore).'
       
   582         )
       
   583   goal = int(request.GET['goal'])
       
   584 
       
   585   # Get the highest instance of this model so that we know
       
   586   # where to start seeding new ones.
       
   587   query = db.Query(model_class)
       
   588   query.order('-link_id')
       
   589   # TODO(dbentley): filter for ones < user_9999
       
   590   highest_instance = query.get()
       
   591   if not highest_instance:
       
   592     start_index = 0
       
   593   else:
       
   594     # We know that seeded entities have link_id's of the form foo_%04d
       
   595     # so, we look for what's after the _ and turn it into an int.
       
   596     link_id = highest_instance.link_id
       
   597     if '_' in link_id:
       
   598       start_index = int(link_id.split('_')[1]) + 1
       
   599     else:
       
   600       # couldn't find seeded_entities; guessing there are none
       
   601       start_index = 0
       
   602 
       
   603 
       
   604   # Insert from start_index to goal
       
   605   logging.info("To insert: %d to %d" % (start_index, goal))
       
   606   seeded_entities = 0
       
   607   for i in xrange(start_index, goal):
       
   608     logging.info("Inserting: %d of %d" % (i+1, goal))
       
   609     seed_func(i)
       
   610     seeded_entities += 1
       
   611 
       
   612   return http.HttpResponse('Seeded %d entities.' % seeded_entities)
       
   613 
       
   614 
   525 def seed_many(request, *args, **kwargs):
   615 def seed_many(request, *args, **kwargs):
   526   """Seeds many instances of the specified type.
   616   """Seeds many instances of the specified type.
   527 
   617 
   528     Understands the following GET args:
   618     Understands the following GET args:
   529     start: where to start adding new users at
   619     start: where to start adding new users at
   594   # there no explicit ranker model anywhere, so make one for
   684   # there no explicit ranker model anywhere, so make one for
   595   # our own convenience to delete all rankers
   685   # our own convenience to delete all rankers
   596   class ranker(db.Model):
   686   class ranker(db.Model):
   597     pass
   687     pass
   598 
   688 
       
   689   # TODO(dbentley): If there are more than 1000 instances of any model,
       
   690   # this method will not clear all instances.  Instead, it should continually
       
   691   # call .all(), delete all those, and loop until .all() is empty.
   599   entities = itertools.chain(*[
   692   entities = itertools.chain(*[
   600       Notification.all(),
   693       Notification.all(),
   601       Mentor.all(),
   694       Mentor.all(),
   602       OrgAdmin.all(),
   695       OrgAdmin.all(),
   603       ranker.all(),
   696       ranker.all(),