app/soc/views/models/organization.py
changeset 2694 b8f083157e33
parent 2573 f09f317769c4
child 2699 3b11f9c24d65
equal deleted inserted replaced
2693:7bda04a2b6c3 2694:b8f083157e33
   418     content = lists.getListContent(request, params, filter)
   418     content = lists.getListContent(request, params, filter)
   419     contents = [content]
   419     contents = [content]
   420 
   420 
   421     return self._list(request, params, contents, page_name)
   421     return self._list(request, params, contents, page_name)
   422 
   422 
   423   def _getMapData(self, student_project_params, filter=None):
   423   def _getMapData(self, filter=None):
   424     """Constructs the JSON object required to generate 
   424     """Constructs the JSON object required to generate 
   425        Google Maps on organization home page.
   425        Google Maps on organization home page.
   426 
   426 
   427     Args:
   427     Args:
   428       student_project_params: params for student project view
       
   429       filter: a dict for the properties that the entities should have
   428       filter: a dict for the properties that the entities should have
   430 
   429 
   431     Returns: 
   430     Returns: 
   432       A JSON object containing map data.
   431       A JSON object containing map data.
   433     """
   432     """
   434 
   433 
   435     from soc.logic.models.student_project import logic as student_project_logic
   434     from soc.logic.models.student_project import logic as student_project_logic
       
   435     from soc.views.models import student_project as student_project_view
       
   436 
       
   437     sp_params = student_project_view.view.getParams().copy()
   436 
   438 
   437     people = {}
   439     people = {}
   438 
   440     projects = {}
   439     # get all the student_project entities for this organization
   441 
   440     student_project_entities = student_project_logic.getForFields(filter=filter)
   442     # get all the student_project entities for the given filter
   441 
   443     student_project_entities = student_project_logic.getForFields(
   442     # construct a dictionary of mentors. For each mentor construct a
   444         filter=filter)
   443     # list of 3-tuple containing student, project title and url. This is
   445 
   444     # mainly done to track and pair all students and mentors who
   446     # Construct a dictionary of mentors and students. For each mentor construct
   445     # have allowed to publish their locations.
   447     # a list of 3-tuples containing student name, project title and url.
       
   448     # And for each student a list of 3 tuples containing mentor name, project
       
   449     # title and url. Only students and mentors who have agreed to publish their
       
   450     # locations will be in the dictionary.
   446     for entity in student_project_entities:
   451     for entity in student_project_entities:
   447 
   452 
   448       if entity.mentor.publish_location and (entity.mentor.key().name() not in people.keys()):
   453       project_key_name = entity.key().id_or_name()
   449         # if mentor has allowed to publish his location add it to the 
   454       project_redirect = redirects.getPublicRedirect(entity, sp_params)
   450         # mentors dictionary
   455 
   451         people[entity.mentor.key().name()] = {
   456       student_entity = entity.student
   452             'type': 'mentor',
   457       student_key_name = student_entity.key().id_or_name()
   453             'name': entity.mentor.name(),
   458 
   454             'city': entity.mentor.res_city,
   459       mentor_entity = entity.mentor
   455             'ccTLD': entity.mentor.ccTld(),
   460       mentor_key_name = mentor_entity.key().id_or_name()
   456             'students': []
   461 
   457             }
   462       # store the project data in the projects dictionary
   458 
   463       projects[project_key_name] = {'title': entity.title,
   459       if entity.student.publish_location:
   464                                     'redirect': project_redirect,
   460         if entity.mentor.publish_location:
   465                                     'student_key': student_key_name,
   461           people[entity.mentor.key().name()]['students'].append(entity.student.key().name())
   466                                     'student_name': student_entity.name(),
   462         people[entity.student.key().name()] = {
   467                                     'mentor_key': mentor_key_name,
   463           'type': 'student',
   468                                     'mentor_name': mentor_entity.name()}
   464           'name': entity.student.name(),
   469 
   465           'city': entity.student.res_city,
   470       if mentor_entity.publish_location:
   466           'ccTLD': entity.student.ccTld(),
   471         if mentor_key_name not in people:
   467           'summary': entity.title,
   472           # we have not stored the information of this mentor yet
   468           'url': redirects.getPublicRedirect(entity, student_project_params),
   473           people[mentor_key_name] = {
   469           'mentor': entity.mentor.name()
   474               'type': 'mentor',
   470           }
   475               'name': mentor_entity.name(),
   471 
   476               'lat': mentor_entity.latitude,
   472     return simplejson.dumps(people)
   477               'long': mentor_entity.longitude,
       
   478               'projects': []
       
   479               }
       
   480 
       
   481         # add this project to the mentor's list
       
   482         people[mentor_key_name]['projects'].append(project_key_name)
       
   483 
       
   484       if student_entity.publish_location:
       
   485         if student_key_name not in people:
       
   486           # new student, store the name and location
       
   487           people[student_key_name] = {
       
   488               'type': 'student',
       
   489               'name': student_entity.name(),
       
   490               'lat': student_entity.latitude,
       
   491               'long': student_entity.longitude,
       
   492               'projects': [],
       
   493               }
       
   494 
       
   495         # append the current project to the known student's list of projects
       
   496         people[student_key_name]['projects'].append(project_key_name)
       
   497 
       
   498     # combine the people and projects data into one JSON object
       
   499     data = {}
       
   500     # TODO: to enable map data uncomment the piece of code below
       
   501     #data = {'people': people,
       
   502     #        'projects': projects}
       
   503 
       
   504     return simplejson.dumps(data)
   473 
   505 
   474   def _public(self, request, entity, context):
   506   def _public(self, request, entity, context):
   475     """See base.View._public().
   507     """See base.View._public().
   476     """
   508     """
   477 
   509 
   480     program_entity = entity.scope
   512     program_entity = entity.scope
   481 
   513 
   482     if timeline_helper.isAfterEvent(program_entity.timeline,
   514     if timeline_helper.isAfterEvent(program_entity.timeline,
   483                                     'accepted_students_announced_deadline'):
   515                                     'accepted_students_announced_deadline'):
   484       # accepted projects
   516       # accepted projects
   485       ap_params = student_project_view.view.getParams().copy() 
   517       ap_params = student_project_view.view.getParams().copy()
   486 
   518 
   487       # define the list redirect action to show the notification
   519       # define the list redirect action to show the notification
   488       ap_params['list_action'] = (redirects.getPublicRedirect, ap_params)
   520       ap_params['list_action'] = (redirects.getPublicRedirect, ap_params)
   489       ap_params['list_description'] = self.DEF_ACCEPTED_PROJECTS_MSG_FMT % (
   521       ap_params['list_description'] = self.DEF_ACCEPTED_PROJECTS_MSG_FMT %(
   490           entity.name)
   522           entity.name)
   491       ap_params['list_heading'] = 'soc/student_project/list/heading.html'
   523       ap_params['list_heading'] = 'soc/student_project/list/heading.html'
   492       ap_params['list_row'] = 'soc/student_project/list/row.html'
   524       ap_params['list_row'] = 'soc/student_project/list/row.html'
   493 
   525 
   494       # only show projects that have not failed
   526       # only show projects that have not failed
   510 
   542 
   511       # construct the list and put it into the context
   543       # construct the list and put it into the context
   512       context['list'] = soc.logic.lists.Lists(contents)
   544       context['list'] = soc.logic.lists.Lists(contents)
   513 
   545 
   514       # obtain data to construct the organization map as json object
   546       # obtain data to construct the organization map as json object
   515       context['org_map_data'] = self._getMapData(ap_params, filter)
   547       context['org_map_data'] = self._getMapData(filter)
   516 
   548 
   517     return super(View, self)._public(request=request, entity=entity,
   549     return super(View, self)._public(request=request, entity=entity,
   518                                      context=context)
   550                                      context=context)
   519 
   551 
   520   def _getExtraMenuItems(self, role_description, params=None):
   552   def _getExtraMenuItems(self, role_description, params=None):