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 seed_and_put_example_user(i): |
84 class Seeder(object): |
85 """Creates and persists an example user identified by i. |
85 """A Seeder can seed Melange types. |
86 |
86 """ |
87 Args: |
87 def type(self): |
88 i, int: the index of this example user. |
88 """Returns the type to be seeded.""" |
89 |
89 raise NotImplementedError |
90 Returns: |
90 |
91 None |
91 def seed(self, i, entities=None, **kwargs): |
92 |
92 """Seed the ith instance of this type. |
93 Side Effects: |
93 |
94 Persists a user to the datastore. |
94 Args: |
95 """ |
95 i, int: which to seed |
96 user_properties = { |
96 entities, list of type()'s: if None, persist at the end of this call. |
97 'key_name': 'user_%04d' % i, |
97 if non-None, append the created entity to entities instead of |
98 'link_id': 'user_%04d' % i, |
98 persisting. |
99 'account': users.User(email='user_%04d@example.com' % i), |
99 kwargs: the dictionary returned by commonSeedArgs |
100 'name': 'User %04d' % i, |
100 """ |
101 } |
101 raise NotImplementedError |
102 entity = User(**user_properties) |
102 |
103 entity.put() |
103 def commonSeedArgs(self, request): |
|
104 """Find common information for seeding that's common across entities |
|
105 seeded in one request. |
|
106 |
|
107 Returns: |
|
108 dictionary of str->value; passed to each call of seed() for this |
|
109 request |
|
110 """ |
|
111 raise NotImplementedError |
|
112 |
|
113 |
|
114 class UserSeeder(Seeder): |
|
115 def type(self): |
|
116 return User |
|
117 |
|
118 def seed(self, i, entities=None): |
|
119 user_properties = { |
|
120 'key_name': 'user_%04d' % i, |
|
121 'link_id': 'user_%04d' % i, |
|
122 'account': users.User(email='user_%04d@example.com' % i), |
|
123 'name': 'User %04d' % i, |
|
124 } |
|
125 entity = User(**user_properties) |
|
126 if entities is None: |
|
127 entity.put() |
|
128 else: |
|
129 entities.append(entity) |
|
130 |
|
131 def commonSeedArgs(self, request): |
|
132 return {} |
|
133 |
|
134 |
|
135 class OrganizationSeeder(Seeder): |
|
136 def type(self): |
|
137 return Organization |
|
138 |
|
139 def seed(self, i, entities=None, current_user=None, gsoc2009=None): |
|
140 properties = { |
|
141 'key_name': 'google/gsoc2009/%04d' % i, |
|
142 'link_id': 'org_%04d' % i, |
|
143 'name': 'Organization %04d' % i, |
|
144 'short_name': 'Org %04d' % i, |
|
145 'founder': current_user, |
|
146 'scope_path': 'google/gsoc2009', |
|
147 'scope': gsoc2009, |
|
148 'status': 'active', |
|
149 'email': 'org_%04d@example.com' % i, |
|
150 'home_page': 'http://code.google.com/p/soc', |
|
151 'description': 'Melange, share the love!', |
|
152 'license_name': 'Apache License', |
|
153 'contact_street': 'Some Street', |
|
154 'contact_city': 'Some City', |
|
155 'contact_country': 'United States', |
|
156 'contact_postalcode': '12345', |
|
157 'phone': '1-555-BANANA', |
|
158 'ideas': 'http://code.google.com/p/soc/issues', |
|
159 } |
|
160 |
|
161 org = Organization(**properties) |
|
162 if entities is None: |
|
163 org.put() |
|
164 else: |
|
165 entities.append(org) |
|
166 |
|
167 def commonSeedArgs(self, request): |
|
168 _, current_user = ensureUser() |
|
169 gsoc2009 = Program.get_by_key_name('google/gsoc2009') |
|
170 |
|
171 if not gsoc2009: |
|
172 raise Error('Run seed_db first') |
|
173 |
|
174 return dict(current_user=current_user, |
|
175 gsoc2009=gsoc2009) |
|
176 |
104 |
177 |
105 |
178 |
106 def seed(request, *args, **kwargs): |
179 def seed(request, *args, **kwargs): |
107 """Seeds the datastore with some default values. |
180 """Seeds the datastore with some default values. |
108 """ |
181 """ |
564 return http.HttpResponse( |
639 return http.HttpResponse( |
565 ('Missing or invalid required argument "seed_type" (which model' |
640 ('Missing or invalid required argument "seed_type" (which model' |
566 ' type to populate). ' |
641 ' type to populate). ' |
567 'Valid values are: %s') % SEEDABLE_MODEL_TYPES.keys()) |
642 'Valid values are: %s') % SEEDABLE_MODEL_TYPES.keys()) |
568 |
643 |
569 (model_class, seed_func) = SEEDABLE_MODEL_TYPES[request.GET['seed_type']] |
644 seeder = SEEDABLE_MODEL_TYPES[request.GET['seed_type']] |
570 |
645 |
571 if 'goal' not in request.GET: |
646 if 'goal' not in request.GET: |
572 return http.HttpResponse( |
647 return http.HttpResponse( |
573 'Missing required argument "goal" (how many entities of ' |
648 'Missing required argument "goal" (how many entities of ' |
574 'this type you want to have in the datastore).' |
649 'this type you want to have in the datastore).' |
575 ) |
650 ) |
576 goal = int(request.GET['goal']) |
651 goal = int(request.GET['goal']) |
577 |
652 |
578 # Get the highest instance of this model so that we know |
653 # Get the highest instance of this model so that we know |
579 # where to start seeding new ones. |
654 # where to start seeding new ones. |
580 query = db.Query(model_class) |
655 query = db.Query(seeder.type()) |
581 query.order('-link_id') |
656 query.order('-link_id') |
582 # TODO(dbentley): filter for ones < user_9999 |
657 # TODO(dbentley): filter for ones < user_9999 |
583 highest_instance = query.get() |
658 highest_instance = query.get() |
584 if not highest_instance: |
659 if not highest_instance: |
585 start_index = 0 |
660 start_index = 0 |
591 start_index = int(link_id.split('_')[1]) + 1 |
666 start_index = int(link_id.split('_')[1]) + 1 |
592 else: |
667 else: |
593 # couldn't find seeded_entities; guessing there are none |
668 # couldn't find seeded_entities; guessing there are none |
594 start_index = 0 |
669 start_index = 0 |
595 |
670 |
|
671 common_args = seeder.commonSeedArgs(request) |
596 |
672 |
597 # Insert from start_index to goal |
673 # Insert from start_index to goal |
598 logging.info("To insert: %d to %d" % (start_index, goal)) |
674 logging.info("To insert: %d to %d" % (start_index, goal)) |
599 seeded_entities = 0 |
675 seeded_entities = [] |
|
676 total = 0 |
600 for i in xrange(start_index, goal): |
677 for i in xrange(start_index, goal): |
601 if i % 20 == 0: |
678 if i % 20 == 0: |
602 logging.info("Inserting: %d of %d" % (i+1, goal)) |
679 logging.info("Inserting: %d of %d" % (i+1, goal)) |
603 seed_func(i) |
680 if len(seeded_entities) % 100 == 0: |
604 seeded_entities += 1 |
681 db.put(seeded_entities) |
605 |
682 total += len(seeded_entities) |
606 return http.HttpResponse('Seeded %d entities.' % seeded_entities) |
683 seeded_entities = [] |
|
684 seeder.seed(i, entities=seeded_entities, **common_args) |
|
685 |
|
686 db.put(seeded_entities) |
|
687 total += len(seeded_entities) |
|
688 return http.HttpResponse('Seeded %d entities.' % total) |
607 |
689 |
608 |
690 |
609 def seed_many(request, *args, **kwargs): |
691 def seed_many(request, *args, **kwargs): |
610 """Seeds many instances of the specified type. |
692 """Seeds many instances of the specified type. |
611 |
693 |