Esempio n. 1
0
    def bulkCreateTasks(self, request, *args, **kwargs):
        """Task that creates GCI Tasks from bulk data specified in the POST dict.

    The POST dict should have the following information present:
        bulk_create_key: the key of the bulk_create entity
    """
        import settings

        # keep track of our own timelimit (20 seconds)
        timelimit = 20000
        timekeeper = Timekeeper(timelimit)

        post_dict = request.POST

        bulk_create_key = post_dict.get('bulk_create_key')
        if not bulk_create_key:
            return error_handler.logErrorAndReturnOK(
                'Not all POST data specified in: %s' % post_dict)

        bulk_data = GCIBulkCreateData.get(bulk_create_key)
        if not bulk_data:
            return error_handler.logErrorAndReturnOK(
                'No valid data found for key: %s' % bulk_create_key)

        # note that we only query for the quota once
        org_admin = ndb.Key.from_old_key(
            GCIBulkCreateData.created_by.get_value_for_datastore(
                bulk_data)).get()
        org = bulk_data.org
        task_quota = getRemainingTaskQuota(org)

        # TODO(ljvderijk): Add transactions

        tasks = bulk_data.tasks
        while len(tasks) > 0:
            try:
                # check if we have time
                timekeeper.ping()

                if settings.GCI_TASK_QUOTA_LIMIT_ENABLED and task_quota <= 0:
                    return error_handler.logErrorAndReturnOK(
                        'Task quota reached for %s' % (org.name))

                # remove the first task
                task_as_string = tasks.pop(0)

                loaded_task = json.loads(task_as_string)
                task = {}
                for key, value in loaded_task.iteritems():
                    # If we don't do this python will complain about kwargs not being
                    # strings when we try to save the new task.
                    task[key.encode('UTF-8')] = value

                logging.info('Uncleaned task: %s', task)
                # clean the data
                errors = self._cleanTask(task, org)

                if errors:
                    logging.warning(
                        'Invalid task data uploaded, the following errors occurred: %s',
                        errors)
                    bulk_data.errors.append(db.Text(
                        'The task in row %i contains the following errors.\n %s' \
                        %(bulk_data.tasksRemoved(), '\n'.join(errors))))

                # at-most-once semantics for creating tasks
                bulk_data.put()

                if errors:
                    # do the next task
                    continue

                # set other properties
                task['org'] = org

                # TODO(daniel): access program in more efficient way
                task['program'] = org_admin.program.to_old_key()
                task['status'] = task_model.UNPUBLISHED
                task['created_by'] = org_admin.to_old_key()
                task['modified_by'] = org_admin.to_old_key()
                # TODO(ljv): Remove difficulty level completely if needed.
                # Difficulty is hardcoded to easy since GCI2012 has no difficulty.
                task['difficulty_level'] = DifficultyLevel.EASY

                subscribers_entities = task['mentor_entities'] + [org_admin]
                task['subscribers'] = list(
                    set([
                        ent.key() for ent in subscribers_entities
                        if ent.automatic_task_subscription
                    ]))

                # create the new task
                logging.info('Creating new task with fields: %s', task)
                task_entity = GCITask(**task)
                task_entity.put()
                task_quota = task_quota - 1
            except DeadlineExceededError:
                # time to bail out
                break

        if len(tasks) == 0:
            # send out a message
            notifications.sendBulkCreationCompleted(bulk_data)
            bulk_data.delete()
        else:
            # there is still work to be done, do a non 500 response and requeue
            task_params = {'bulk_create_key': bulk_data.key()}
            new_task = taskqueue.Task(params=task_params, url=BULK_CREATE_URL)
            # add to the gci queue
            new_task.add(queue_name='gci-update')

        # we're done here
        return http.HttpResponse('OK')
Esempio n. 2
0
def seed(request, *args, **kwargs):
    """Seeds the datastore with some default values.
  """

    site_properties = {
        'key_name': 'site',
        'latest_gsoc': 'google/gsoc2014',
        'latest_gci': 'google/gci2013',
    }
    site = Site(**site_properties)
    site.put()

    account = accounts.getCurrentAccount()

    if not account:
        account = users.User(email='*****@*****.**')

    user_properties = {
        'id': 'test',
        'account_id': account.user_id(),
        'account': account,
    }
    current_user = user.User(**user_properties)
    current_user.put()

    group_properties = {
        'key_name': 'google',
        'link_id': 'google',
        'name': 'Google Inc.',
        'short_name': 'Google',
        'home_page': 'http://www.google.com',
        'email': '*****@*****.**',
        'description': 'This is the profile for Google.',
        'contact_street': 'Some Street',
        'contact_city': 'Some City',
        'contact_country': 'United States',
        'contact_postalcode': '12345',
        'phone': '15551110000',
        'status': 'active',
    }
    google = Sponsor(**group_properties)
    google.put()

    now = datetime.datetime.now()
    before = now - datetime.timedelta(365)
    after = now + datetime.timedelta(365)
    past_before = before - datetime.timedelta(2 * 365)
    past_after = after - datetime.timedelta(2 * 365)

    timeline_properties = {
        'key_name': 'google/gsoc2014',
        'link_id': 'gsoc2014',
        'scope': google,
        'program_start': before,
        'program_end': after,
        'accepted_organization_announced_deadline': before,
        'accepted_students_announced_deadline': after,
        'student_signup_start': before,
        'student_signup_end': after,
        'application_review_deadline': after,
        'student_application_matched_deadline': after,
        'accepted_students_announced_deadline': after,
        'form_submission_start': before,
    }
    gsoc2014_timeline = GSoCTimeline(**timeline_properties)
    gsoc2014_timeline.put()

    program_properties = {
        'key_name': 'google/gsoc2014',
        'link_id': 'gsoc2014',
        'program_id': 'gsoc2014',
        'sponsor': google,
        'scope': google,
        'name': 'Google Summer of Code 2014',
        'short_name': 'GSoC 2014',
        'description': 'This is the program for GSoC 2014.',
        'apps_tasks_limit': 42,
        'slots': 42,
        'timeline': gsoc2014_timeline,
        'status': program_model.STATUS_VISIBLE,
    }
    gsoc2014 = GSoCProgram(**program_properties)
    gsoc2014.put()

    timeline_properties.update({
        'key_name': 'google/gsoc2010',
        'link_id': 'gsoc2010',
        'program_start': past_before,
        'program_end': past_after,
        'accepted_organization_announced_deadline': past_before,
        'accepted_students_announced_deadline': past_after,
        'student_signup_start': past_before,
        'student_signup_end': past_after,
        'application_review_deadline': past_after,
        'student_application_matched_deadline': past_after,
        'accepted_students_announced_deadline': past_after,
        'form_submission_start': past_before,
    })
    gsoc2010_timeline = GSoCTimeline(**timeline_properties)
    gsoc2010_timeline.put()

    program_properties.update({
        'key_name': 'google/gsoc2010',
        'link_id': 'gsoc2010',
        'program_id': 'gsoc2010',
        'name': 'Google Summer of Code 2010',
        'description': 'This is the program for GSoC 2010.',
        'short_name': 'GSoC 2010',
        'timeline': gsoc2010_timeline,
    })
    gsoc2010 = GSoCProgram(**program_properties)
    gsoc2010.put()

    # TODO(drew): Replace gsoc2014.prefix with whatever its replacement becomes
    # once prefix is removed from program and no longer used in the query for
    # OrgAppSurvey in soc.views.helper.RequestData._getProgramWideFields().
    org_app_survey_properties = {
        'key_name': '%s/%s/orgapp' % (gsoc2014.prefix, gsoc2014.key().name()),
        'program': gsoc2014,
        'title': 'Org App Survey',
        'content': 'Here is some content.',
        'modified_by': current_user.key.to_old_key(),
        'survey_start': before,
        'survey_end': after
    }
    org_app_survey_model.OrgAppSurvey(**org_app_survey_properties).put()

    org_app_survey_properties['key_name'] = (
        '%s/%s/orgapp' % (gsoc2010.prefix, gsoc2010.key().name()))
    org_app_survey_properties['program'] = gsoc2010
    org_app_survey_properties['survey_start'] = past_before
    org_app_survey_properties['survey_end'] = past_after
    org_app_survey_model.OrgAppSurvey(**org_app_survey_properties).put()

    timeline_properties = {
        'key_name': 'google/gci2013',
        'link_id': 'gci2013',
        'scope': google,
        'program_start': before,
        'program_end': after,
        'accepted_organization_announced_deadline': before,
        'student_signup_start': before,
        'student_signup_end': after,
        'tasks_publicly_visible': before,
        'task_claim_deadline': after,
        'stop_all_work_deadline': after,
    }
    gci2013_timeline = GCITimeline(**timeline_properties)
    gci2013_timeline.put()

    program_properties.update({
        'key_name': 'google/gci2013',
        'link_id': 'gci2013',
        'program_id': 'gci2013',
        'name': 'Google Code In Contest 2013',
        'short_name': 'GCI 2009',
        'description': 'This is the program for GCI 2013.',
        'timeline': gci2013_timeline,
    })
    gci2013 = GCIProgram(**program_properties)
    gci2013.put()

    site.active_program = gci2013
    site.put()

    current_user.host_for = [
        ndb.Key.from_old_key(gsoc2010.key()),
        ndb.Key.from_old_key(gsoc2014.key()),
        ndb.Key.from_old_key(gci2013.key())
    ]
    current_user.put()

    group_properties.update({
        'key_name': 'google/gci2013/melange',
        'link_id': 'melange',
        'name': 'Melange Development Team',
        'short_name': 'Melange',
        'scope': gci2013,
        'program': gci2013,
        'sponsor': google,
        'home_page': 'http://code.google.com/p/soc',
        'description': 'Melange, share the love!',
        'license_name': 'Apache License',
        'ideas': 'http://code.google.com/p/soc/issues',
    })
    melange = GCIOrganization(**group_properties)
    melange.put()

    group_properties.update({
        'scope': gsoc2014,
        'program': gsoc2014,
    })

    address_properties = address.Address(street='1 Test St.',
                                         city='Some City',
                                         country='United States',
                                         postal_code='12345')
    address_properties.put()

    contact_info = contact.Contact(email='*****@*****.**')
    contact_info.put()

    gsoc_delta = datetime.timedelta(days=(365 * 18))

    profile_properties = {
        'id': gsoc2014.key().name() + '/' + current_user.key.id(),
        'parent': current_user.key,
        'public_name': 'test',
        'program': ndb.Key.from_old_key(gsoc2014.key()),
        'first_name': 'Test',
        'last_name': 'Example',
        'contact': contact_info,
        'residential_address': address_properties,
        'shipping_address': address_properties,
        'birth_date': datetime.date.today() - gsoc_delta,
        'program_knowledge': 'Friend referral',
    }
    profile = profile_model.Profile(**profile_properties)

    ndb_orgs = []
    for i in range(15):
        group_properties.update({
            'key_name': 'google/gsoc2014/org_%d' % i,
            'link_id': 'org_%d' % i,
            'name': 'Organization %d' % i,
            'short_name': 'Org %d' % i,
            'description': 'Organization %d!' % i,
        })

        org_properties = {
            'name': 'Organization %d' % i,
            'org_id': 'org_%d' % i,
            'program': ndb.Key.from_old_key(gsoc2014.key()),
            'description': 'Organization %d!' % i,
        }
        org = soc_org_model.SOCOrganization(id='google/gsoc2014/org_%d' % i,
                                            **org_properties)
        org.put()
        ndb_orgs.append(org)

        # Admin (and thus mentor) for the first org
        if i == 0:
            profile.admin_for.append(org.key)
            profile.mentor_for.append(org.key)
            profile.put()

        # Mentor for the second org
        if i == 1:
            profile.mentor_for.append(org.key)
            profile.put()

    profile_properties.update({
        'id':
        gci2013.key().name() + '/' + current_user.key.id(),
        'parent':
        current_user.key,
        'program':
        ndb.Key.from_old_key(gci2013.key()),
        'admin_for': [ndb.Key.from_old_key(melange.key())],
        'mentor_for': [ndb.Key.from_old_key(melange.key())],
    })
    melange_admin = profile_model.Profile(**profile_properties)
    # TODO: add GCI orgs
    melange_admin.put()

    task_properties = {
        'status': 'Open',
        'modified_by': melange_admin.key.to_old_key(),
        'subscribers': [melange_admin.key.to_old_key()],
        'title': 'Awesomeness',
        'created_by': melange_admin.key.to_old_key(),
        'created_on': now,
        'program': gci2013,
        'time_to_complete': 1337,
        'modified_on': now,
        'org': melange.key(),
        'description': '<p>AWESOME</p>',
        'difficulty_level': DifficultyLevel.MEDIUM,
        'types': ['Code']
    }
    gci_task = GCITask(**task_properties)
    gci_task.put()

    user_properties = {
        'id': 'student',
        'account_id': '12345',
        'account': users.User(email='*****@*****.**'),
    }
    student_user = user.User(**user_properties)
    student_user.put()

    gci_delta = datetime.timedelta(days=(365 * 14))

    contact_properties = contact.Contact(email='*****@*****.**',
                                         web_page='http://www.homepage.com/',
                                         blog='http://www.blog.com/',
                                         phone='1650253000')
    contact_properties.put()

    graduation_year = datetime.date.today() + datetime.timedelta(days=365)

    student_data = soc_profile.SOCStudentData(education=education.Education(
        school_id="123",
        school_country="United States",
        expected_graduation=int(graduation_year.strftime('%Y')),
        major='Some Major',
        degree=education.Degree.UNDERGRADUATE))
    student_data.put()

    student_id = 'student'
    student_properties = {
        'id': gsoc2014.key().name() + "/" + student_id,
        'parent': student_user.key,
        'program': ndb.Key.from_old_key(gsoc2014.key()),
        'public_name': 'Student',
        'first_name': 'Student',
        'last_name': 'Student',
        'contact': contact_properties,
        'residential_address': address_properties,
        'shipping_address': address_properties,
        'birth_date': datetime.date.today() - gci_delta,
        'tee_size': profile_model.TeeSize.L,
        'tee_style': profile_model.TeeStyle.MALE,
        'gender': profile_model.Gender.MALE,
        'program_knowledge': 'Friend referral.',
        'student_data': student_data,
    }
    melange_student = profile_model.Profile(**student_properties)
    melange_student.put()

    student_id = 'student2'
    user_properties = {
        'id': student_id,
        'account_id': 'student2',
        'account': users.User(email='*****@*****.**'),
    }
    student_user2 = user.User(**user_properties)
    student_user2.put()

    student_properties.update({
        'id': gsoc2014.key().name() + "/" + student_id,
        'parent': student_user2.key,
        'first_name': 'Student 2',
        'last_name': 'Example'
    })
    melange_student2 = profile_model.Profile(**student_properties)
    melange_student2.put()

    proposal_properties = {
        'parent': melange_student.key.to_old_key(),
        'program': gsoc2014,
        'title': 'test proposal',
        'abstract': 'test abstract',
        'content': 'test content',
        'mentor': profile.key.to_old_key(),
        'status': 'accepted',
        'has_mentor': True,
        'org': ndb_orgs[0].key.to_old_key(),
        'possible_mentors': [profile.key.to_old_key()]
    }
    melange_proposal = GSoCProposal(**proposal_properties)
    melange_proposal.put()

    project_properties = {
        'title': 'test project',
        'abstract': 'test abstract',
        'status': 'accepted',
        'parent': melange_student.key.to_old_key(),
        'mentors': [profile.key.to_old_key()],
        'program': gsoc2014,
        'org': ndb_orgs[0].key.to_old_key(),
        'proposal': melange_proposal.key(),
    }
    melange_project = GSoCProject(**project_properties)
    melange_project.put()
    ndb_orgs[1].slot_allocation = 1
    ndb_orgs[1].put()

    student_data.number_of_projects = 1
    student_data.number_of_proposals = 1
    student_data.project_for_orgs = [ndb_orgs[1].key]

    melange_student.put()
    melange_student2.put()

    project_properties.update({
        'student': melange_student2,
        'title': 'test project2'
    })
    melange_project2 = GSoCProject(**project_properties)
    melange_project2.put()
    ndb_orgs[1].slot_allocation += 1
    ndb_orgs[1].put()

    student_id = 'student'
    student_properties.update({
        'id': gci2013.key().name() + '/' + student_id,
        'parent': student_user.key,
        'program': ndb.Key.from_old_key(gci2013.key()),
    })
    gci_student = profile_model.Profile(**student_properties)
    gci_student.put()

    score_properties = {
        'parent': gci_student.key.to_old_key(),
        'program': gci2013,
        'points': 5,
        'tasks': [gci_task.key()]
    }
    score = GCIScore(**score_properties)
    score.put()

    document_properties = {
        'key_name': 'site/site/home',
        'link_id': 'home',
        'scope': site,
        'prefix': 'site',
        'author': current_user.key.to_old_key(),
        'title': 'Home Page',
        'content': 'This is the Home Page',
        'modified_by': current_user.key.to_old_key(),
    }
    home_document = Document(**document_properties)
    home_document.put()

    document_properties = {
        'key_name': 'user/test/notes',
        'link_id': 'notes',
        'scope': current_user.key.to_old_key(),
        'prefix': 'user',
        'author': current_user.key.to_old_key(),
        'title': 'My Notes',
        'content': 'These are my notes',
        'modified_by': current_user.key.to_old_key(),
    }
    notes_document = Document(**document_properties)
    notes_document.put()

    site.home = home_document
    site.put()

    memcache.flush_all()

    return http.HttpResponse('Done')