| # Copyright 2014 the Melange authors. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Module for templates with project lists.""" |
| |
| from google.appengine.ext import ndb |
| |
| from django.utils import translation |
| |
| from melange.request import links |
| |
| from soc.views.helper import lists |
| from soc.views import template |
| |
| from summerofcode.logic import project as project_logic |
| from summerofcode.views.helper import urls |
| |
| |
| _PROJECT_LIST_TITLE = translation.ugettext( |
| 'List of projects accepted into %s') |
| |
| _HOST_PROJECT_LIST_DESCRIPTION = translation.ugettext('All Projects') |
| |
| _PUBLIC_PROJECT_LIST_DESCRIPTION = translation.ugettext('Accepted Projects') |
| |
| _MANAGE_LINK = translation.ugettext('<a href="%s">Manage</a>') |
| |
| |
| class ProjectListPrefetcher(lists.ListPrefetcher): |
| """Prefetcher implementation for list of projects.""" |
| |
| def prefetch(self, entities): |
| """See lists.Prefetcher.prefetch for specification.""" |
| # cache students |
| keys = set(entity.key.parent() for entity in entities) |
| |
| # cache organizations |
| keys.update(entity.organization for entity in entities) |
| |
| # cache mentors |
| for entity in entities: |
| keys.update(entity.mentors) |
| |
| cached_entities = ndb.get_multi(keys) |
| |
| cached_data = {} |
| for cached_entity in cached_entities: |
| cached_data[cached_entity.key] = cached_entity |
| |
| return cached_data |
| |
| |
| class ProjectList(template.Template): |
| """Template for list of projects.""" |
| |
| def __init__(self, template_path, data, list_config, query, |
| description): |
| """Initializes a new instance of the list for the specified parameters. |
| |
| Args: |
| url_names: Instance of url_names.UrlNames. |
| template_path: The path of the template to be used. |
| data: request_data.RequestData for the current request. |
| list_config: List configuration object. |
| query: ndb.Query to get list data. |
| description: A string containing description of the list. |
| """ |
| super(ProjectList, self).__init__(data) |
| self.template_path = template_path |
| self._list_config = list_config |
| self._query = query |
| self._description = description |
| |
| def context(self): |
| """See template.Template.context for specification.""" |
| list_configuration_response = lists.ListConfigurationResponse( |
| self.data, self._list_config, idx=0, |
| description=self._description) |
| |
| return { |
| 'list_title': _PROJECT_LIST_TITLE % self.data.program.name, |
| 'lists': [list_configuration_response], |
| } |
| |
| def getListData(self): |
| # TODO(daniel): add missing doc string |
| idx = lists.getListIndex(self.data.request) |
| if idx != 0: |
| return None |
| |
| prefetcher = ProjectListPrefetcher() |
| |
| response_builder = lists.RawQueryContentResponseBuilder( |
| self.data.request, self._list_config, self._query, |
| lists.keyStarter, prefetcher=prefetcher) |
| |
| return response_builder.buildNDB() |
| |
| |
| def _getStudent(entity, cached_data, *args): |
| """Helper function to get value for 'Student' column.""" |
| return cached_data[entity.key.parent()].public_name |
| |
| |
| def _getOrganization(entity, cached_data, *args): |
| """Helper function to get value for 'Organization' column.""" |
| return cached_data[entity.organization].name |
| |
| |
| def _getMentors(entity, cached_data, *args): |
| """Helper function to get value for 'Mentors' column.""" |
| return ', '.join( |
| cached_data[mentor_key].public_name for mentor_key in entity.mentors) |
| |
| |
| def _getManage(entity, *args): |
| """Helper function to get value for 'Manage' column.""" |
| return _MANAGE_LINK % links.LINKER.userId( |
| entity.key.parent(), entity.key.id(), |
| urls.UrlNames.PROJECT_MANAGE_ADMIN) |
| |
| |
| def getPublicProjectList(data): |
| """Returns an instance of ProjectList class that lists projects publicly. |
| |
| Args: |
| data: request_data.RequestData for the current request. |
| |
| Returns: |
| The newly created ProjectList object. |
| """ |
| list_config = lists.ListConfiguration(add_key_column=False) |
| |
| list_config.addPlainTextColumn( |
| 'key', 'Key', |
| lambda entity, *args: entity.key.urlsafe(), |
| hidden=True) |
| list_config.addSimpleColumn('title', 'Title') |
| list_config.addHtmlColumn('student', 'Student', _getStudent) |
| list_config.addHtmlColumn('organization', 'Organization', _getOrganization) |
| list_config.addHtmlColumn('mentors', 'Mentors', _getMentors) |
| |
| list_config.setRowAction( |
| lambda entity, *args: links.LINKER.userId( |
| entity.key.parent(), entity.key.id(), |
| urls.UrlNames.PROJECT_DETAILS)) |
| |
| list_config.setDefaultPagination(False) |
| list_config.setDefaultSort('student') |
| |
| query = project_logic.queryAcceptedProjects(data.program.key()) |
| |
| return ProjectList( |
| 'summerofcode/_list_component.html', data, list_config, |
| query, _PUBLIC_PROJECT_LIST_DESCRIPTION) |
| |
| |
| def getOrganizationPublicProjectList(data): |
| """Returns an instance of ProjectList class that lists all projects which |
| are accepted for the organization specified in the URL. |
| |
| Args: |
| data: request_data.RequestData for the current request. |
| |
| Returns: |
| The newly created ProjectList object. |
| """ |
| list_config = lists.ListConfiguration(add_key_column=False) |
| |
| list_config.addPlainTextColumn( |
| 'key', 'Key', |
| lambda entity, *args: entity.key.urlsafe(), |
| hidden=True) |
| list_config.addSimpleColumn('title', 'Title') |
| list_config.addHtmlColumn('student', 'Student', _getStudent) |
| list_config.addHtmlColumn('mentors', 'Mentors', _getMentors) |
| |
| list_config.setRowAction( |
| lambda entity, *args: links.LINKER.userId( |
| entity.key.parent(), entity.key.id(), |
| urls.UrlNames.PROJECT_DETAILS)) |
| |
| list_config.setDefaultPagination(False) |
| list_config.setDefaultSort('student') |
| |
| query = project_logic.queryAcceptedProjectsForOrg(data.url_ndb_org.key) |
| |
| return ProjectList( |
| 'summerofcode/_list_component.html', data, list_config, |
| query, _PUBLIC_PROJECT_LIST_DESCRIPTION) |
| |
| |
| def getProgramHostProjectList(data): |
| """Returns an instance of ProjectList class that lists all projects for |
| the program. |
| |
| Args: |
| data: request_data.RequestData for the current request. |
| |
| Returns: |
| The newly created ProjectList object. |
| """ |
| list_config = lists.ListConfiguration(add_key_column=False) |
| |
| list_config.addPlainTextColumn( |
| 'key', 'Key', lambda entity, *args: entity.key.urlsafe(), hidden=True) |
| list_config.addSimpleColumn('title', 'Title') |
| list_config.addHtmlColumn('student', 'Student', _getStudent) |
| list_config.addHtmlColumn('organization', 'Organization', _getOrganization) |
| list_config.addSimpleColumn('status', 'Status') |
| list_config.addPlainTextColumn('mentors', 'Mentors', _getMentors) |
| list_config.addHtmlColumn('manage_link', 'Manage', _getManage) |
| |
| list_config.setRowAction( |
| lambda entity, *args: links.LINKER.userId( |
| entity.key.parent(), entity.key.id(), |
| urls.UrlNames.PROJECT_DETAILS)) |
| |
| list_config.setDefaultSort('organization') |
| |
| query = project_logic.queryAllProjects(data.program.key()) |
| |
| return ProjectList( |
| 'summerofcode/_list_component.html', data, list_config, |
| query, _HOST_PROJECT_LIST_DESCRIPTION) |
| |
| |
| def getStudentProjectList(data, student_key): |
| """Returns an instance of ProjectList class that lists all projects for |
| the specified student. |
| |
| Args: |
| data: request_data.RequestData for the current request. |
| student_key: ndb.Key of the student. |
| |
| Returns: |
| The newly created ProjectList object. |
| """ |
| list_config = lists.ListConfiguration(add_key_column=False) |
| |
| list_config.addPlainTextColumn( |
| 'key', 'Key', lambda entity, *args: entity.key.urlsafe(), hidden=True) |
| list_config.addSimpleColumn('title', 'Title') |
| list_config.addHtmlColumn('organization', 'Organization', _getOrganization) |
| list_config.addSimpleColumn('status', 'Status') |
| list_config.addHtmlColumn('mentors', 'Mentors', _getMentors) |
| |
| list_config.setRowAction( |
| lambda entity, *args: links.LINKER.userId( |
| entity.key.parent(), entity.key.id(), |
| urls.UrlNames.PROJECT_DETAILS)) |
| |
| query = project_logic.queryAllProjectsForStudent(student_key) |
| |
| return ProjectList( |
| 'summerofcode/_list_component.html', data, list_config, |
| query, _PUBLIC_PROJECT_LIST_DESCRIPTION) |
| |
| |
| def getMentoredProjectList(data, profile_key): |
| """Returns an instance of ProjectList class that lists all projects mentored |
| by the specified organization member. |
| |
| Args: |
| data: request_data.RequestData for the current request. |
| profile_key: ndb.Key of the profile. |
| |
| Returns: |
| The newly created ProjectList object. |
| """ |
| list_config = lists.ListConfiguration(add_key_column=False) |
| |
| list_config.addPlainTextColumn( |
| 'key', 'Key', lambda entity, *args: entity.key.urlsafe(), hidden=True) |
| list_config.addSimpleColumn('title', 'Title') |
| |
| # organization column is added only when the user is a mentor for |
| # more than one organization |
| if len(data.ndb_profile.mentor_for) > 1: |
| list_config.addPlainTextColumn( |
| 'organization', 'Organization', _getOrganization) |
| |
| list_config.addSimpleColumn('status', 'Status') |
| list_config.addPlainTextColumn('mentors', 'Mentors', _getMentors) |
| |
| list_config.setRowAction( |
| lambda entity, *args: links.LINKER.userId( |
| entity.key.parent(), entity.key.id(), |
| urls.UrlNames.PROJECT_DETAILS)) |
| |
| query = project_logic.queryAllProjectsForOrgMember(profile_key) |
| |
| return ProjectList( |
| 'summerofcode/_list_component.html', data, list_config, |
| query, _PUBLIC_PROJECT_LIST_DESCRIPTION) |