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.', |
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(), |