app/soc/models/seed_db.py
changeset 2167 91e622242f2d
parent 2160 3f9dd37d98a8
child 2175 8dd78e7015f9
equal deleted inserted replaced
2166:c9c7c6111988 2167:91e622242f2d
    53 
    53 
    54 
    54 
    55 class Error(Exception):
    55 class Error(Exception):
    56   """Base class for all exceptions raised by this module.
    56   """Base class for all exceptions raised by this module.
    57   """
    57   """
    58   
    58 
    59   pass
    59   pass
    60 
    60 
    61 
    61 
    62 def ensureUser():
    62 def ensureUser():
    63   """Returns the current user account and associated user object.
    63   """Returns the current user account and associated user object.
    79   current_user.put()
    79   current_user.put()
    80 
    80 
    81   return account, current_user
    81   return account, current_user
    82 
    82 
    83 
    83 
    84 def determine_index_of_seeded_entity(entity):
       
    85   """Determines the index of a seeded_entity.
       
    86 
       
    87   Because we seed entities in a predictable manner, we can look at an entity
       
    88     and determine which one it is.  This works iff entities are seeded with
       
    89     link_id's of the form: foo_%04d (where 4 is at least the number of digits
       
    90     of the index of the highest-seeded entity).
       
    91   """
       
    92 
       
    93 
       
    94 def seed_and_put_example_user(i):
    84 def seed_and_put_example_user(i):
    95   """Creates and Persists an example user identified by i.
    85   """Creates and persists an example user identified by i.
    96 
    86 
    97   Args:
    87   Args:
    98     i, int: the index of this example user.
    88     i, int: the index of this example user.
    99 
    89 
   100   Returns:
    90   Returns:
   556 
   546 
   557 def new_seed_many(request, *args, **kwargs):
   547 def new_seed_many(request, *args, **kwargs):
   558   """Seeds many instances of the specified type.
   548   """Seeds many instances of the specified type.
   559 
   549 
   560   Takes as URL parameters:
   550   Takes as URL parameters:
   561   seed_type: the type of entity to seed; should be a key in SEEDABLE_MODEL_TYPES
   551     seed_type: the type of entity to seed; should be a key in
   562   goal: the total number of entities desired
   552                SEEDABLE_MODEL_TYPES
       
   553     goal: the total number of entities desired
   563 
   554 
   564   This differs from seed_many. Instead of having to specify many parameters
   555   This differs from seed_many. Instead of having to specify many parameters
   565     that are the state of an in-flight process, simply say how many you want
   556     that are the state of an in-flight process, simply say how many you want
   566     to have (at least) at the end.  This will make progress towards that goal.
   557     to have (at least) at the end.  This will make progress towards that goal.
   567     In my test run, even adding 1001 users completed in far less than the
   558     In my test run, even adding 1001 users completed in far less than the
   605 
   596 
   606   # Insert from start_index to goal
   597   # Insert from start_index to goal
   607   logging.info("To insert: %d to %d" % (start_index, goal))
   598   logging.info("To insert: %d to %d" % (start_index, goal))
   608   seeded_entities = 0
   599   seeded_entities = 0
   609   for i in xrange(start_index, goal):
   600   for i in xrange(start_index, goal):
   610     logging.info("Inserting: %d of %d" % (i+1, goal))
   601     if i % 20 == 0:
       
   602       logging.info("Inserting: %d of %d" % (i+1, goal))
   611     seed_func(i)
   603     seed_func(i)
   612     seeded_entities += 1
   604     seeded_entities += 1
   613 
   605 
   614   return http.HttpResponse('Seeded %d entities.' % seeded_entities)
   606   return http.HttpResponse('Seeded %d entities.' % seeded_entities)
   615 
   607 
   686   # there no explicit ranker model anywhere, so make one for
   678   # there no explicit ranker model anywhere, so make one for
   687   # our own convenience to delete all rankers
   679   # our own convenience to delete all rankers
   688   class ranker(db.Model):
   680   class ranker(db.Model):
   689     """ranker model used with ranklist module.
   681     """ranker model used with ranklist module.
   690     """
   682     """
   691     
       
   692     pass
   683     pass
   693 
   684 
   694   # TODO(dbentley): If there are more than 1000 instances of any model,
   685   # TODO(dbentley): If there are more than 1000 instances of any model,
   695   # this method will not clear all instances.  Instead, it should continually
   686   # this method will not clear all instances.  Instead, it should continually
   696   # call .all(), delete all those, and loop until .all() is empty.
   687   # call .all(), delete all those, and loop until .all() is empty.