app/soc/models/seed_db.py
changeset 2669 c28e2dc4cd51
parent 2367 19ca23d78701
child 2813 6f7af233b5b0
equal deleted inserted replaced
2668:7fd0951332ef 2669:c28e2dc4cd51
    17 """Seeds or clears the datastore.
    17 """Seeds or clears the datastore.
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22     'JamesLevy" <jamesalexanderlevy@gmail.com>',
    22   ]
    23   ]
    23 
    24 
    24 
    25 
    25 import itertools
    26 import itertools
    26 import logging
    27 import logging
    27 import random
    28 import random
       
    29 import datetime
    28 
    30 
    29 from google.appengine.api import users
    31 from google.appengine.api import users
    30 from google.appengine.api import memcache
    32 from google.appengine.api import memcache
    31 from google.appengine.ext import db
    33 from google.appengine.ext import db
    32 
    34 
    33 from django import http
    35 from django import http
    34 
    36 
       
    37 from soc.logic.models.document import logic as document_logic
    35 from soc.logic.models.ranker_root import logic as ranker_root_logic
    38 from soc.logic.models.ranker_root import logic as ranker_root_logic
       
    39 from soc.logic.models.survey import logic as survey_logic
       
    40 from soc.logic.models.user import logic as user_logic
       
    41 
    36 from soc.logic import accounts
    42 from soc.logic import accounts
    37 from soc.logic import dicts
    43 from soc.logic import dicts
    38 from soc.models import student_proposal
    44 from soc.models import student_proposal
    39 from soc.models.document import Document
    45 from soc.models.document import Document
    40 from soc.models.host import Host
    46 from soc.models.host import Host
    45 from soc.models.org_app import OrgApplication
    51 from soc.models.org_app import OrgApplication
    46 from soc.models.program import Program
    52 from soc.models.program import Program
    47 from soc.models.ranker_root import RankerRoot
    53 from soc.models.ranker_root import RankerRoot
    48 from soc.models.site import Site
    54 from soc.models.site import Site
    49 from soc.models.student import Student
    55 from soc.models.student import Student
       
    56 from soc.models.student_project import StudentProject
    50 from soc.models.student_proposal import StudentProposal
    57 from soc.models.student_proposal import StudentProposal
       
    58 from soc.models.survey import Survey, SurveyContent
       
    59 from soc.models.survey_record import SurveyRecord
    51 from soc.models.sponsor import Sponsor
    60 from soc.models.sponsor import Sponsor
    52 from soc.models.timeline import Timeline
    61 from soc.models.timeline import Timeline
    53 from soc.models.user import User
    62 from soc.models.user import User
    54 
       
    55 
    63 
    56 class Error(Exception):
    64 class Error(Exception):
    57   """Base class for all exceptions raised by this module.
    65   """Base class for all exceptions raised by this module.
    58   """
    66   """
    59 
    67 
   127         'name': 'User %04d' % i,
   135         'name': 'User %04d' % i,
   128         }
   136         }
   129     entity = User(**user_properties)
   137     entity = User(**user_properties)
   130     if entities is None:
   138     if entities is None:
   131       entity.put()
   139       entity.put()
       
   140       user_logic._onCreate(entity)
   132     else:
   141     else:
   133       entities.append(entity)
   142       entities.append(entity)
   134 
   143 
   135   def commonSeedArgs(self, request):
   144   def commonSeedArgs(self, request):
   136     return {}
   145     return {}
   234       org_application.put()
   243       org_application.put()
   235     else:
   244     else:
   236       entities.append(org_application)
   245       entities.append(org_application)
   237 
   246 
   238 
   247 
       
   248 
       
   249 
   239 def seed(request, *args, **kwargs):
   250 def seed(request, *args, **kwargs):
   240   """Seeds the datastore with some default values.
   251   """Seeds the datastore with some default values.
   241   """
   252   """
   242 
   253 
   243   site_properties = {
   254   site_properties = {
   422   group_properties.update({
   433   group_properties.update({
   423     'scope_path': 'google/gsoc2009',
   434     'scope_path': 'google/gsoc2009',
   424     'scope': gsoc2009,
   435     'scope': gsoc2009,
   425     })
   436     })
   426 
   437 
       
   438   orgs = []
   427   for i in range(15):
   439   for i in range(15):
   428     group_properties.update({
   440     group_properties.update({
   429         'key_name': 'google/gsoc2009/org_%d' % i,
   441         'key_name': 'google/gsoc2009/org_%d' % i,
   430         'link_id': 'org_%d' % i,
   442         'link_id': 'org_%d' % i,
   431         'name': 'Organization %d' % i,
   443         'name': 'Organization %d' % i,
   432         'short_name': 'Org %d' % i,
   444         'short_name': 'Org %d' % i,
   433         'description': 'Organization %d!' % i,
   445         'description': 'Organization %d!' % i,
   434         })
   446         })
   435 
   447 
   436     entity = Organization(**group_properties)
   448     entity = Organization(**group_properties)
       
   449     orgs.append(entity)
   437     entity.put()
   450     entity.put()
   438     # create a new ranker
   451     # create a new ranker
   439     ranker_root_logic.create(student_proposal.DEF_RANKER_NAME, entity,
   452     ranker_root_logic.create(student_proposal.DEF_RANKER_NAME, entity,
   440         student_proposal.DEF_SCORE, 100)
   453         student_proposal.DEF_SCORE, 100)
   441 
   454 
   453         org_1_admin = OrgAdmin(**role_properties)
   466         org_1_admin = OrgAdmin(**role_properties)
   454         org_1_admin.put()
   467         org_1_admin.put()
   455 
   468 
   456       # Only a mentor for the second org
   469       # Only a mentor for the second org
   457       if i == 1:
   470       if i == 1:
       
   471         org_1_admin = OrgAdmin(**role_properties)
       
   472         org_1_admin.put()
   458         org_1_mentor = Mentor(**role_properties)
   473         org_1_mentor = Mentor(**role_properties)
   459         org_1_mentor.put()
   474         org_1_mentor.put()
   460 
   475 
   461   role_properties.update({
   476   role_properties.update({
   462       'key_name': 'google/ghop2009/melange/test',
   477       'key_name': 'google/ghop2009/melange/test',
   470   melange_admin.put()
   485   melange_admin.put()
   471 
   486 
   472   melange_mentor = Mentor(**role_properties)
   487   melange_mentor = Mentor(**role_properties)
   473   melange_mentor.put()
   488   melange_mentor.put()
   474 
   489 
   475 
   490   student_id = 'test'
       
   491   student_properties = {
       
   492       'key_name': gsoc2009.key().name() + "/" + student_id,
       
   493       'link_id': student_id, 
       
   494       'scope_path': gsoc2009.key().name(),
       
   495       'scope': gsoc2009,
       
   496       'program': gsoc2009,
       
   497       'user': current_user,
       
   498       'given_name': 'test',
       
   499       'surname': 'test',
       
   500       'birth_date': db.DateProperty.now(),
       
   501       'email': 'test@email.com',
       
   502       'im_handle': 'test_im_handle',
       
   503       'major': 'test major',
       
   504       'name_on_documents': 'test',
       
   505       'res_country': 'United States',
       
   506       'res_city': 'city',
       
   507       'res_street': 'test street',
       
   508       'res_postalcode': '12345',
       
   509       'publish_location': True,
       
   510       'blog': 'http://www.blog.com/',
       
   511       'home_page': 'http://www.homepage.com/',
       
   512       'photo_url': 'http://www.photosite.com/thumbnail.png',
       
   513       'ship_state': None,
       
   514       'expected_graduation': 2009,
       
   515       'school_country': 'United States',
       
   516       'school_name': 'Test School', 
       
   517       'tshirt_size': 'XS',
       
   518       'tshirt_style': 'male',
       
   519       'degree': 'Undergraduate',
       
   520       'phone': '1650253000',
       
   521       'can_we_contact_you': True, 
       
   522       'program_knowledge': 'I heard about this program through a friend.'
       
   523       }
       
   524 
       
   525   melange_student = Student(**student_properties)
       
   526   melange_student.put()
       
   527 
       
   528   student_id = 'test2'
       
   529   student_properties.update({
       
   530       'key_name': gsoc2009.key().name() + "/" + student_id,
       
   531       'link_id': student_id,
       
   532       'user': current_user 
       
   533       })
       
   534 
       
   535   melange_student2 = Student(**student_properties)
       
   536   melange_student2.put()
       
   537                                        
       
   538   project_id = 'test_project'
       
   539   project_properties = {
       
   540       'key_name':  gsoc2009.key().name() + "/org_1/" + project_id,
       
   541       'link_id': project_id, 
       
   542       'scope_path': gsoc2009.key().name() + "/org_1",
       
   543       'scope': orgs[1].key(),
       
   544 
       
   545       'title': 'test project',
       
   546       'abstract': 'test abstract',
       
   547       'status': 'accepted',
       
   548       'student': melange_student,
       
   549       'mentor': org_1_mentor,
       
   550       'program':  gsoc2009
       
   551        }
       
   552 
       
   553   melange_project = StudentProject(**project_properties)
       
   554   melange_project.put()
       
   555 
       
   556   project_id = 'test_project2'
       
   557   project_properties.update({
       
   558       'key_name':  gsoc2009.key().name() + "/org_1/" + project_id,
       
   559       'link_id': project_id,
       
   560       'student': melange_student2,
       
   561       'title': 'test project2'
       
   562       })
       
   563       
       
   564   melange_project2 = StudentProject(**project_properties)
       
   565   melange_project2.put()
       
   566     
   476   document_properties = {
   567   document_properties = {
   477       'key_name': 'site/site/home',
   568       'key_name': 'site/site/home',
   478       'link_id': 'home',
   569       'link_id': 'home',
   479       'scope_path': 'site',
   570       'scope_path': 'site',
   480       'scope': site,
   571       'scope': site,
   486       'modified_by': current_user,
   577       'modified_by': current_user,
   487       }
   578       }
   488 
   579 
   489   home_document = Document(**document_properties)
   580   home_document = Document(**document_properties)
   490   home_document.put()
   581   home_document.put()
       
   582   document_logic._onCreate(home_document) 
   491 
   583 
   492 
   584 
   493   document_properties = {
   585   document_properties = {
   494       'key_name': 'user/test/notes',
   586       'key_name': 'user/test/notes',
   495       'link_id': 'notes',
   587       'link_id': 'notes',
   503       'modified_by': current_user,
   595       'modified_by': current_user,
   504       }
   596       }
   505 
   597 
   506   notes_document = Document(**document_properties)
   598   notes_document = Document(**document_properties)
   507   notes_document.put()
   599   notes_document.put()
       
   600   document_logic._onCreate(home_document) 
   508 
   601 
   509   site.home = home_document
   602   site.home = home_document
   510   site.put()
   603   site.put()
   511   # pylint: disable-msg=E1101
   604   # pylint: disable-msg=E1101
   512   memcache.flush_all()
   605   memcache.flush_all()
   596       'phone': '1-555-BANANA',
   689       'phone': '1-555-BANANA',
   597       'ideas': 'http://code.google.com/p/soc/issues',
   690       'ideas': 'http://code.google.com/p/soc/issues',
   598       }
   691       }
   599 
   692 
   600   return properties
   693   return properties
       
   694 
       
   695 DEADLINE = datetime.datetime.now() + datetime.timedelta(10)
       
   696 
       
   697 def seed_survey(request, i):
       
   698   """Returns the properties for a new survey.
       
   699   """
       
   700 
       
   701   _, current_user = ensureUser()
       
   702   gsoc2009 = Program.get_by_key_name('google/gsoc2009')
       
   703 
       
   704   if not gsoc2009:
       
   705     raise Error('Run seed_db first')
       
   706   link_id = 'survey_%d' % i
       
   707   fields = {'SelectionQ': [u'SelectionQ Option 2 for %s' % link_id,
       
   708                            u'SelectionQ Option 1 for %s'  % link_id
       
   709                            ],
       
   710             'PickMultipleQ': ['PickMultipleQ Checkbox 1 for %s' % link_id,
       
   711                               'PickMultipleQ Checkbox 2 for %s' % link_id,
       
   712                               ],
       
   713             'LongQ': 'LongQ Custom Prompt for %s' % link_id,
       
   714             'ShortQ': 'ShortQ Custom Prompt for %s' % link_id,
       
   715             }
       
   716   schema = {u'PickMultipleQ': {'index': 5, 'type': 'pick_multi',
       
   717                                'render': 'multi_checkbox'},
       
   718             u'LongQ': {'index': 2, 'type': 'long_answer'},
       
   719             u'ShortQ': {'index': 3, 'type': 'short_answer'},
       
   720             u'SelectionQ': {'index': 4, 'type': 'selection',
       
   721                             'render': 'single_select'}
       
   722             }
       
   723   properties = {
       
   724       'key_name': 'program/google/gsoc2009/%s' % link_id,
       
   725       'link_id': link_id,
       
   726       'scope_path': 'google/gsoc2009',
       
   727       'scope': None,
       
   728       'prefix': 'program',
       
   729       'author': current_user,
       
   730       'title': 'My Survey %d' % i,
       
   731       'short_name': 'Survey %d' % i,
       
   732       'modified_by': current_user,
       
   733       'is_featured': True,
       
   734       'fields': fields,
       
   735       'schema': schema,
       
   736       'deadline': DEADLINE,
       
   737       'taking_access': 'everyone',
       
   738       }
       
   739   return properties
       
   740 
       
   741 
       
   742 def seed_survey_answer(request, i):
       
   743   """Returns the properties of a student's survey answers.
       
   744   """
       
   745 
       
   746   ensureUser()
       
   747   survey = Survey.get_by_key_name('program/google/gsoc2009/survey_%d' % i)
       
   748   user = User.get_by_key_name('user_%d' % i)
       
   749   #student = Student.get_by_key_name('google/gsoc2009/student_%d' % i)
       
   750 
       
   751   if not user:
       
   752     raise Error('Run seed_many for at least %d users first.' % i)
       
   753 
       
   754   if not survey:
       
   755     raise Error('Run seed_many for at least %d surveys first.' % i)
       
   756 
       
   757   all_properties = []
       
   758   scope_path = 'google/gsoc2009/'
       
   759   checkbox = 'PickMultipleQ Checkbox 2 for survey_%d' % i
       
   760   # pylint: disable-msg=E1103
       
   761   for i in range(5):
       
   762     #student = Student.get_by_key_name('google/gsoc2009/student_%d' % i)
       
   763     user = User.get_by_key_name('user_%d' % i)
       
   764 
       
   765     properties = {
       
   766         'scope_path': scope_path,
       
   767         'user': user,
       
   768         'project': None,
       
   769         '_survey': survey,
       
   770         '_fields': {'ShortQ':'Test', 'SelectionQ': u'SelectionQ Option 2',
       
   771                    'LongQ': 'Long answer... \n' * 10,
       
   772                    'PickMultipleQ': checkbox,
       
   773                    }
       
   774         }
       
   775 
       
   776     all_properties.append(properties)
       
   777 
       
   778   return all_properties
   601 
   779 
   602 
   780 
   603 def seed_mentor(request, i):
   781 def seed_mentor(request, i):
   604   """Returns the properties of a new student proposal.
   782   """Returns the properties of a new student proposal.
   605   """
   783   """
   829     'org': (seed_org, Organization),
  1007     'org': (seed_org, Organization),
   830     'org_app': (seed_org_app, OrgApplication),
  1008     'org_app': (seed_org_app, OrgApplication),
   831     'mentor': (seed_mentor, Mentor),
  1009     'mentor': (seed_mentor, Mentor),
   832     'student': (seed_student, Student),
  1010     'student': (seed_student, Student),
   833     'student_proposal': (seed_student_proposal, StudentProposal),
  1011     'student_proposal': (seed_student_proposal, StudentProposal),
       
  1012     'survey': (seed_survey, Survey),
       
  1013     'survey_answer': (seed_survey_answer, SurveyRecord),
   834     }
  1014     }
   835 
  1015 
   836   goal = int(get_args['goal'])
  1016   goal = int(get_args['goal'])
   837   start = int(get_args['start'])
  1017   start = int(get_args['start'])
   838   end = int(get_args['end'])
  1018   end = int(get_args['end'])
   850     except Error, error:
  1030     except Error, error:
   851       return http.HttpResponse(error.message)
  1031       return http.HttpResponse(error.message)
   852 
  1032 
   853     for properties in props if isinstance(props, list) else [props]:
  1033     for properties in props if isinstance(props, list) else [props]:
   854       entity = model(**properties)
  1034       entity = model(**properties)
       
  1035       if seed_type == 'survey':
       
  1036         survey_content = survey_logic.createSurvey(properties['fields'],
       
  1037                                                     properties['schema'],
       
  1038                                                     this_survey=None
       
  1039                                                     )
       
  1040         entity.this_survey = survey_content
       
  1041       elif seed_type == 'survey_answer':
       
  1042         record = SurveyRecord.gql("WHERE user = :1 AND this_survey = :2",
       
  1043                                   properties['user'], properties['_survey']
       
  1044                                   ).get()
       
  1045         entity = survey_logic.updateSurveyRecord(properties['user'],
       
  1046                                                    properties['_survey'],
       
  1047                                                    record,
       
  1048                                                    properties['_fields']
       
  1049                                                    )
   855       entity.put()
  1050       entity.put()
       
  1051       if seed_type == 'survey': survey_logic._onCreate(entity) 
   856 
  1052 
   857   if end < goal:
  1053   if end < goal:
   858     info = {
  1054     info = {
   859         'start': start + step,
  1055         'start': start + step,
   860         'end': end + step,
  1056         'end': end + step,
   886   # call .all(), delete all those, and loop until .all() is empty.
  1082   # call .all(), delete all those, and loop until .all() is empty.
   887   entities = itertools.chain(*[
  1083   entities = itertools.chain(*[
   888       Notification.all(),
  1084       Notification.all(),
   889       Mentor.all(),
  1085       Mentor.all(),
   890       Student.all(),
  1086       Student.all(),
       
  1087       Survey.all(),
       
  1088       SurveyContent.all(),
       
  1089       SurveyRecord.all(),
   891       OrgAdmin.all(),
  1090       OrgAdmin.all(),
   892       ranker.all(),
  1091       ranker.all(),
   893       RankerRoot.all(),
  1092       RankerRoot.all(),
   894       StudentProposal.all(),
  1093       StudentProposal.all(),
   895       Organization.all(),
  1094       Organization.all(),