Example #1
0
  def canResign(self, entity):
    """Checks if the Mentor is able to resign.

    Checks if there are no Student Proposals or Student Projects that
    have this mentor assigned to it.

    Args:
      entity: a Mentor entity

    """

    from soc.logic.models.student_project import logic as \
        student_project_logic
    from soc.logic.models.student_proposal import logic as \
        student_proposal_logic

    fields = {'mentor': entity}

    student_project_entity = student_project_logic.getForFields(fields,
                                                                unique=True)
    if student_project_entity:
      return DEF_ALREADY_MENTORING_RPOJECT_MSG

    student_proposal_entity = student_proposal_logic.getForFields(fields,
                                                                  unique=True)

    if student_proposal_entity:
      return DEF_ALREADY_MENTORING_PROPOSAL_MSG

    return super(Logic, self).canResign(entity)
Example #2
0
def _getMentoredProjects(mentor):
    """Returns a list of projects which are mentored by a given mentor.
  """

    from soc.logic.models.student_project import logic as project_logic

    filter = {"mentor": mentor}

    return project_logic.getForFields(filter=filter)
Example #3
0
def _getMentoredProjects(mentor):
    """Returns a list of projects which are mentored by a given mentor.
  """

    from soc.logic.models.student_project import logic as project_logic

    filter = {'mentor': mentor}

    return project_logic.getForFields(filter=filter)
Example #4
0
def _getStudentProject(entity):
    """Returns a project for a given student.
  """

    from soc.logic.models.student_project import logic as project_logic

    filter = {"student": entity, "status": "accepted"}

    return project_logic.getForFields(filter=filter, unique=True)
Example #5
0
def _getStudentProject(entity):
    """Returns a project for a given student.
  """

    from soc.logic.models.student_project import logic as project_logic

    filter = {
        'student': entity,
        'status': 'accepted',
    }

    return project_logic.getForFields(filter=filter, unique=True)
Example #6
0
  def _getMapData(self, student_project_params, filter=None):
    """Constructs the JSON object required to generate 
       Google Maps on organization home page.

    Args:
      student_project_params: params for student project view
      filter: a dict for the properties that the entities should have

    Returns: 
      A JSON object containing map data.
    """

    from soc.logic.models.student_project import logic as student_project_logic

    people = {}

    # get all the student_project entities for this organization
    student_project_entities = student_project_logic.getForFields(filter=filter)

    # construct a dictionary of mentors. For each mentor construct a
    # list of 3-tuple containing student, project title and url. This is
    # mainly done to track and pair all students and mentors who
    # have allowed to publish their locations.
    for entity in student_project_entities:

      if entity.mentor.publish_location and (entity.mentor.key().name() not in people.keys()):
        # if mentor has allowed to publish his location add it to the 
        # mentors dictionary
        people[entity.mentor.key().name()] = {
            'type': 'mentor',
            'name': entity.mentor.name(),
            'city': entity.mentor.res_city,
            'ccTLD': entity.mentor.ccTld(),
            'students': []
            }

      if entity.student.publish_location:
        if entity.mentor.publish_location:
          people[entity.mentor.key().name()]['students'].append(entity.student.key().name())
        people[entity.student.key().name()] = {
          'type': 'student',
          'name': entity.student.name(),
          'city': entity.student.res_city,
          'ccTLD': entity.student.ccTld(),
          'summary': entity.title,
          'url': redirects.getPublicRedirect(entity, student_project_params),
          'mentor': entity.mentor.name()
          }

    return simplejson.dumps(people)
Example #7
0
def spawnRemindersForProjectSurvey(request, *args, **kwargs):
  """Spawns tasks for each StudentProject in the given Program.

  Expects the following to be present in the POST dict:
    program_key: Specifies the program key name for which to loop over all the
                 StudentProjects for
    survey_key: specifies the key name for the ProjectSurvey to send reminders
                for
    survey_type: either project or grading depending on the type of Survey
    project_key: optional to specify which project was the last for which a
                 task was spawn

  Args:
    request: Django Request object
  """
  
  from google.appengine.ext import db

  from soc.logic.models.program import logic as program_logic
  from soc.logic.models.student_project import logic as student_project_logic

  # set default batch size
  batch_size = 10

  post_dict = request.POST

  # retrieve the program_key and survey_key from POST data
  program_key = post_dict.get('program_key')
  survey_key = post_dict.get('survey_key')
  survey_type = post_dict.get('survey_type')

  if not (program_key and survey_key and survey_type):
    # invalid task data, log and return OK
    return error_handler.logErrorAndReturnOK(
        'Invalid sendRemindersForProjectSurvey data: %s' % post_dict)

  # get the program for the given keyname
  program_entity = program_logic.getFromKeyName(program_key)

  if not program_entity:
    # invalid program specified, log and return OK
    return error_handler.logErrorAndReturnOK(
        'Invalid program specified: %s' % program_key)

  # check and retrieve the project_key that has been done last
  if 'project_key' in post_dict:
    project_start_key = post_dict['project_key']
  else:
    project_start_key = None

  # get all valid StudentProjects from starting key
  fields = {'program': program_entity,
            'status': 'accepted'}

  if project_start_key:
    # retrieve the last project that was done
    project_start = student_project_logic.getFromKeyName(project_start_key)

    if not project_start:
      # invalid starting project key specified, log and return OK
      return error_handler.logErrorAndReturnOK(
          'Invalid Student Project Key specified: %s' %(project_start_key))

    fields['__key__ >'] = project_start.key()

  project_entities = student_project_logic.getForFields(fields,
                                                        limit=batch_size)

  for project_entity in project_entities:
    # pass along these params as POST to the new task
    task_params = {'survey_key': survey_key,
                   'survey_type': survey_type,
                   'project_key': project_entity.key().id_or_name()}
    task_url = '/tasks/surveys/projects/send_reminder/send'

    new_task = taskqueue.Task(params=task_params, url=task_url)
    new_task.add('mail')

  if len(project_entities) == batch_size:
    # spawn new task starting from the last
    new_project_start = project_entities[batch_size-1].key().id_or_name()

    # pass along these params as POST to the new task
    task_params = {'program_key': program_key,
                   'survey_key': survey_key,
                   'survey_type': survey_type,
                   'project_key': new_project_start}
    task_url = '/tasks/surveys/projects/send_reminder/spawn'

    new_task = taskqueue.Task(params=task_params, url=task_url)
    new_task.add()

  # return OK
  return http.HttpResponse()
Example #8
0
def updateOrCreateRecordsForSurveyGroup(request, *args, **kwargs):
  """Updates or creates GradingRecords for the given GradingSurveyGroup.

  Expects the following to be present in the POST dict:
    group_key: Specifies the GradingSurveyGroup key name.
    project_key: optional to specify which project was the last for which this
                 task was run
  Args:
    request: Django Request object
  """

  from soc.logic.models.grading_record import logic as grading_record_logic
  from soc.logic.models.grading_survey_group import logic as survey_group_logic
  from soc.logic.models.student_project import logic as student_project_logic

  post_dict = request.POST

  group_key = post_dict.get('group_key')

  if not group_key:
    # invalid task data, log and return OK
    return error_handler.logErrorAndReturnOK(
        'Invalid updateRecordForSurveyGroup data: %s' % post_dict)

  # get the GradingSurveyGroup for the given keyname
  survey_group_entity = survey_group_logic.getFromKeyName(group_key)

  if not survey_group_entity:
    # invalid GradingSurveyGroup specified, log and return OK
    return error_handler.logErrorAndReturnOK(
        'Invalid GradingSurveyGroup specified: %s' % group_key)

  # check and retrieve the project_key that has been done last
  if 'project_key' in post_dict:
    project_start_key = post_dict['project_key']
  else:
    project_start_key = None

  # get all valid StudentProjects from starting key
  fields = {'program': survey_group_entity.scope,
            'status': ['accepted', 'failed', 'completed']}

  if project_start_key:
    # retrieve the last project that was done
    project_start = student_project_logic.getFromKeyName(project_start_key)

    if not project_start:
      # invalid starting project key specified, log and return OK
      return error_handler.logErrorAndReturnOK(
          'Invalid Student Project Key specified: %s' %(project_start_key))

    fields['__key__ >'] = project_start.key()

  # get the first batch_size number of StudentProjects
  project_entities = student_project_logic.getForFields(fields,
                                                        limit=DEF_BATCH_SIZE)

  # update/create and batch put the new GradingRecords
  grading_record_logic.updateOrCreateRecordsFor(survey_group_entity,
                                                project_entities)

  if len(project_entities) == DEF_BATCH_SIZE:
    # spawn new task starting from the last
    new_project_start = project_entities[DEF_BATCH_SIZE-1].key().id_or_name()

    # pass along these params as POST to the new task
    task_params = {'group_key': group_key,
                   'project_key': new_project_start}
    task_url = '/tasks/grading_survey_group/update_records'

    new_task = taskqueue.Task(params=task_params, url=task_url)
    new_task.add()
  else:
    # task completed, update timestamp for last update complete
    fields = {'last_update_complete': datetime.datetime.now()}
    survey_group_logic.updateEntityProperties(survey_group_entity, fields)

  # task completed, return OK
  return http.HttpResponse('OK')
Example #9
0
  def _getMapData(self, student_project_params, filter=None):
    """Constructs the JSON object required to generate a 
      Google map on organization home page.

    Args:
      student_project_params: params for student project view
      filter: a dict for the properties that the entities should have

    Returns: 
      A JSON object containing map data with the following structure.
      [
        {
          'type': 'mentor',
          'name': MentorName,
          'city': CityName,
          'ccTLD': ccTLD,
          'students': [{'name': Name, 'city': CityName, 'ccTLD': ccTLD,
                      'summary': Summary, 'url': URL}, 
                   {'name': Name, 'city': CityName, 'ccTLD': ccTLD,
                      'summary': Summary, 'url': URL}, ]
        },
        {
          'type': 'none',
          'name': MentorName,
          'student': {'name': Name, 'city': CityName, 'ccTLD': ccTLD, 
                      'summary': Summary, 'url': URL}
        }
      ]
    """

    from soc.logic.models.student_project import logic as student_project_logic

    map_data = []
    mentors = {}
    student_only = []

    # get all the student_project entities for this organization
    student_project_entities = student_project_logic.getForFields(filter=filter)

    # construct a dictionary of mentors. For each mentor construct a
    # list of 3-tuple containing student, project title and url. This is
    # mainly done to track and pair all students and mentors who
    # have allowed to publish their locations.
    for entity in student_project_entities:

      if entity.mentor.publish_location and entity.mentor not in mentors:
        # if mentor has allowed to publish his location add it to the 
        # mentors dictionary
        mentors[entity.mentor] = []

      if entity.student.publish_location:
        # if student has allowed to publish his location, add it to the
        # corresponding mentor list, otherwise add it to the 'none' list
        if entity.mentor in mentors:
          mentors[entity.mentor].append(
              (entity.student, entity.title,
              redirects.getPublicRedirect(entity, student_project_params)))
        else:
          student_only.append(
              (entity.student, entity.title,
               redirects.getPublicRedirect(entity, student_project_params),
               entity.mentor))

    # from the index built in the form of mentors dictionary we now build
    # the map_data for all the mentors who have allowed to publish their
    # location
    for mentor in mentors:
      mentor_map = {
          'type': 'mentor',
          'name': mentor.name(),
          'city': mentor.res_city,
          'ccTLD': mentor.ccTld(),
          'students': []
          }

      for student, summary, url in mentors[mentor]:
        student_map = {
           'name': student.name(),
           'city': student.res_city,
           'ccTLD': student.ccTld(),
           'summary': summary,
           'url': url
           }

        mentor_map['students'].append(student_map)

      map_data.append(mentor_map)

    # construct the 'type': 'none' dictionary for those students, whose
    # mentors haven't allowed to publish location
    for student, summary, url, mentor in student_only:
      nomentor_map = {
          'type': 'none',
          'name': mentor.name(), 
          'student': {
              'name': student.name(),
              'city': student.res_city,
              'ccTLD': student.ccTld(),
              'summary': summary,
              'url': url
              }
          }

      map_data.append(nomentor_map)

    return simplejson.dumps(map_data)
Example #10
0
  def _getMapData(self, filter=None):
    """Constructs the JSON object required to generate 
       Google Maps on organization home page.

    Args:
      filter: a dict for the properties that the entities should have

    Returns: 
      A JSON object containing map data.
    """

    from soc.logic.models.student_project import logic as student_project_logic
    from soc.views.models import student_project as student_project_view

    sp_params = student_project_view.view.getParams().copy()

    people = {}
    projects = {}

    # get all the student_project entities for the given filter
    student_project_entities = student_project_logic.getForFields(
        filter=filter)

    # Construct a dictionary of mentors and students. For each mentor construct
    # a list of 3-tuples containing student name, project title and url.
    # And for each student a list of 3 tuples containing mentor name, project
    # title and url. Only students and mentors who have agreed to publish their
    # locations will be in the dictionary.
    for entity in student_project_entities:

      project_key_name = entity.key().id_or_name()
      project_redirect = redirects.getPublicRedirect(entity, sp_params)

      student_entity = entity.student
      student_key_name = student_entity.key().id_or_name()

      mentor_entity = entity.mentor
      mentor_key_name = mentor_entity.key().id_or_name()

      # store the project data in the projects dictionary
      projects[project_key_name] = {'title': entity.title,
                                    'redirect': project_redirect,
                                    'student_key': student_key_name,
                                    'student_name': student_entity.name(),
                                    'mentor_key': mentor_key_name,
                                    'mentor_name': mentor_entity.name()}

      if mentor_entity.publish_location:
        if mentor_key_name not in people:
          # we have not stored the information of this mentor yet
          people[mentor_key_name] = {
              'type': 'mentor',
              'name': mentor_entity.name(),
              'lat': mentor_entity.latitude,
              'long': mentor_entity.longitude,
              'projects': []
              }

        # add this project to the mentor's list
        people[mentor_key_name]['projects'].append(project_key_name)

      if student_entity.publish_location:
        if student_key_name not in people:
          # new student, store the name and location
          people[student_key_name] = {
              'type': 'student',
              'name': student_entity.name(),
              'lat': student_entity.latitude,
              'long': student_entity.longitude,
              'projects': [],
              }

        # append the current project to the known student's list of projects
        people[student_key_name]['projects'].append(project_key_name)

    # combine the people and projects data into one JSON object
    data = {}
    # TODO: to enable map data uncomment the piece of code below
    #data = {'people': people,
    #        'projects': projects}

    return simplejson.dumps(data)
Example #11
0
def spawnRemindersForProjectSurvey(request, *args, **kwargs):
  """Spawns tasks for each StudentProject in the given Program.

  Expects the following to be present in the POST dict:
    program_key: Specifies the program key name for which to loop over all the
                 StudentProjects for
    survey_key: specifies the key name for the ProjectSurvey to send reminders
                for
    survey_type: either project or grading depending on the type of Survey
    project_key: optional to specify which project was the last for which a
                 task was spawn

  Args:
    request: Django Request object
  """
  
  from google.appengine.ext import db

  from soc.logic.models.program import logic as program_logic
  from soc.logic.models.student_project import logic as student_project_logic

  # set default batch size
  batch_size = 10

  post_dict = request.POST

  # retrieve the program_key and survey_key from POST data
  program_key = post_dict.get('program_key')
  survey_key = post_dict.get('survey_key')
  survey_type = post_dict.get('survey_type')

  if not (program_key and survey_key and survey_type):
    # invalid task data, log and return OK
    return error_handler.logErrorAndReturnOK(
        'Invalid sendRemindersForProjectSurvey data: %s' % post_dict)

  # get the program for the given keyname
  program_entity = program_logic.getFromKeyName(program_key)

  if not program_entity:
    # invalid program specified, log and return OK
    return error_handler.logErrorAndReturnOK(
        'Invalid program specified: %s' % program_key)

  # check and retrieve the project_key that has been done last
  if 'project_key' in post_dict:
    project_start_key = post_dict['project_key']
  else:
    project_start_key = None

  # get all valid StudentProjects from starting key
  fields = {'program': program_entity,
            'status': 'accepted'}

  if project_start_key:
    # retrieve the last project that was done
    project_start = student_project_logic.getFromKeyName(project_start_key)

    if not project_start:
      # invalid starting project key specified, log and return OK
      return error_handler.logErrorAndReturnOK(
          'Invalid Student Project Key specified: %s' %(project_start_key))

    fields['__key__ >'] = project_start.key()

  project_entities = student_project_logic.getForFields(fields,
                                                        limit=batch_size)

  for project_entity in project_entities:
    # pass along these params as POST to the new task
    task_params = {'survey_key': survey_key,
                   'survey_type': survey_type,
                   'project_key': project_entity.key().id_or_name()}
    task_url = '/tasks/surveys/projects/send_reminder/send'

    new_task = taskqueue.Task(params=task_params, url=task_url)
    new_task.add('mail')

  if len(project_entities) == batch_size:
    # spawn new task starting from the last
    new_project_start = project_entities[batch_size-1].key().id_or_name()

    # pass along these params as POST to the new task
    task_params = {'program_key': program_key,
                   'survey_key': survey_key,
                   'survey_type': survey_type,
                   'project_key': new_project_start}
    task_url = '/tasks/surveys/projects/send_reminder/spawn'

    new_task = taskqueue.Task(params=task_params, url=task_url)
    new_task.add()

  # return OK
  return http.HttpResponse()