Example #1
0
def seed_many(request, *args, **kwargs):
    """Seeds many instances of the specified type.

    Understands the following GET args:
    start: where to start adding new users at
    end: where to stop adding new users at
    goal: how many users to add in total, implies user_only
    step: how many users to add per request, defaults to 15
    seed_type: the type of entity to seed, should be one of:
      user, org, org_app, mentor, student_proposal

    Redirects if end < goal, incrementing both start and end with step.
  """

    get_args = request.GET

    if not dicts.containsAll(get_args, ['goal', 'start', 'end', 'seed_type']):
        return http.HttpResponse('Missing get args.')

    seed_types = {
        'user': (seed_user, User),
        'org': (seed_org, GSoCOrganization),
        'org_app': (seed_org_app, OrgApplication),
        'mentor': (seed_mentor, GSoCMentor),
        'student': (seed_student, GSoCStudent),
        'student_proposal': (seed_student_proposal, StudentProposal),
        'survey': (seed_survey, Survey),
        'survey_answer': (seed_survey_answer, SurveyRecord),
    }

    goal = int(get_args['goal'])
    start = int(get_args['start'])
    end = int(get_args['end'])
    step = int(get_args.get('step', '15'))
    seed_type = get_args['seed_type']

    if not seed_type in seed_types:
        return http.HttpResponse('Unknown seed_type: "%s".' % seed_type)

    action, model = seed_types[seed_type]

    for i in range(start, end):
        try:
            props = action(request, i)
        except Error, error:
            return http.HttpResponse(error.message)

        for properties in props if isinstance(props, list) else [props]:
            entity = model(**properties)
            if seed_type == 'survey':
                survey_content = survey_logic.createSurvey(
                    properties['fields'],
                    properties['schema'],
                    this_survey=None)
                entity.this_survey = survey_content
            elif seed_type == 'survey_answer':
                record = SurveyRecord.gql(
                    "WHERE user = :1 AND this_survey = :2", properties['user'],
                    properties['_survey']).get()
                entity = survey_logic.updateSurveyRecord(
                    properties['user'], properties['_survey'], record,
                    properties['_fields'])
            entity.put()
            if seed_type == 'survey': survey_logic._onCreate(entity)