| # Copyright 2011 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 containing the Org Homepage view.""" |
| |
| from django.utils.translation import ugettext |
| |
| from google.appengine.ext import ndb |
| |
| from melange.request import access |
| from melange.request import exception |
| from melange.request import links |
| |
| from soc.views.helper import lists |
| from soc.views.helper import url_patterns |
| from soc.views.org_home import BanOrgPost |
| from soc.views.template import Template |
| from soc.views import toggle_button |
| |
| from soc.modules.gci.models.organization import GCIOrganization |
| from soc.modules.gci.models.task import CLAIMABLE |
| from soc.modules.gci.models.task import GCITask |
| from soc.modules.gci.views.base import GCIRequestHandler |
| from soc.modules.gci.views.helper.url_patterns import url |
| from soc.modules.gci.views.helper import url_names |
| |
| |
| _BAN_ORGANIZATION_HELP_TEST = ugettext( |
| 'When an organization is banned, students cannot work on their tasks') |
| |
| class AboutUs(Template): |
| """About us template.""" |
| |
| def context(self): |
| return { |
| 'description': self.data.url_ndb_org.description, |
| 'logo_url': self.data.url_ndb_org.logo_image_url, |
| 'homepage': self.data.url_ndb_org.contact.web_page, |
| 'name': self.data.url_ndb_org.name, |
| } |
| |
| def templatePath(self): |
| return 'modules/gci/org_home/_about_us.html' |
| |
| |
| class ContactUs(Template): |
| """Organization Contact template.""" |
| |
| def context(self): |
| return { |
| 'organization': self.data.url_ndb_org, |
| } |
| |
| def templatePath(self): |
| return "modules/gci/org_home/_contact_us.html" |
| |
| |
| class OpenTasksList(Template): |
| """List to display all the open tasks for the current organization.""" |
| |
| def __init__(self, data): |
| self.data = data |
| list_config = lists.ListConfiguration() |
| |
| list_config.addSimpleColumn('title', 'Title') |
| #list_config.addPlainTextColumn( |
| # 'task_type', 'Type', |
| # lambda entity, all_d, all_t, *args: entity.taskType(all_t)) |
| #list_config.addPlainTextColumn('arbit_tag', 'Tags', lambda entity, |
| # *args: entity.taskArbitTag()) |
| list_config.addPlainTextColumn('time_to_complete', 'Time to complete', |
| lambda entity, *args: entity.taskTimeToComplete()) |
| list_config.addPlainTextColumn('types', 'Type', |
| lambda entity, *args: ", ".join(entity.types)) |
| |
| list_config.setRowAction( |
| lambda e, *args: data.redirect.id(e.key().id()).urlOf(url_names.GCI_VIEW_TASK)) |
| |
| self.list_config = list_config |
| |
| def context(self): |
| description = 'List of all Open tasks.' |
| return { |
| 'lists': [lists.ListConfigurationResponse( |
| self.data, self.list_config, 0, description)], |
| } |
| |
| def getListData(self): |
| if lists.getListIndex(self.data.request) != 0: |
| return None |
| query = GCITask.all() |
| query.filter('org', self.data.url_ndb_org.key.to_old_key()) |
| query.filter('status IN', CLAIMABLE) |
| starter = lists.keyStarter |
| |
| response_builder = lists.RawQueryContentResponseBuilder( |
| self.data.request, self.list_config, query, starter) |
| return response_builder.build() |
| |
| def templatePath(self): |
| return 'modules/gci/org_home/_open_tasks.html' |
| |
| class CompletedTasksList(Template): |
| """List to display all the closed/completed tasks for the current organization.""" |
| |
| def __init__(self, data): |
| self.data = data |
| |
| list_config = lists.ListConfiguration() |
| |
| def _getStudent(entity, *args): |
| """Helper function to get value for student column.""" |
| student_key = ndb.Key.from_old_key( |
| GCITask.student.get_value_for_datastore(entity)) |
| return student_key.get().public_name |
| |
| list_config.addSimpleColumn('title', 'Title') |
| list_config.addPlainTextColumn('student', 'Student', _getStudent) |
| list_config.addPlainTextColumn('types', 'Type', |
| lambda entity, *args: ", ".join(entity.types)) |
| |
| list_config.setRowAction( |
| lambda e, *args: data.redirect.id(e.key().id()).urlOf( |
| url_names.GCI_VIEW_TASK)) |
| |
| self.list_config = list_config |
| |
| def context(self): |
| description = 'List of all Completed tasks.' |
| return { |
| 'lists': [lists.ListConfigurationResponse( |
| self.data, self.list_config, 1, description)], |
| } |
| |
| def getListData(self): |
| if lists.getListIndex(self.data.request) != 1: |
| return None |
| query = GCITask.all() |
| query.filter('org', self.data.url_ndb_org.key.to_old_key()) |
| query.filter('status', 'Closed') |
| starter = lists.keyStarter |
| |
| response_builder = lists.RawQueryContentResponseBuilder( |
| self.data.request, self.list_config, query, starter) |
| return response_builder.build() |
| |
| def templatePath(self): |
| return 'modules/gci/org_home/_closed_tasks.html' |
| |
| |
| class GCIBanOrgPost(BanOrgPost, GCIRequestHandler): |
| """Handles banning/unbanning of GCI organizations.""" |
| |
| def _getModulePrefix(self): |
| return 'gci' |
| |
| def _getURLPattern(self): |
| return url_patterns.ORG |
| |
| def _getURLName(self): |
| return url_names.GCI_ORG_BAN |
| |
| def _getOrgModel(self): |
| return GCIOrganization |
| |
| |
| # TODO(daniel): re-add host actions |
| class HostActions(Template): |
| """Template to render the left side host actions. |
| """ |
| |
| def __init__(self, data): |
| super(HostActions, self).__init__(data) |
| self.toggle_buttons = [] |
| |
| def context(self): |
| is_banned = self.data.organization.status == 'invalid' |
| |
| org_banned = toggle_button.ToggleButtonTemplate( |
| self.data, 'on_off', 'Banned', 'organization-banned', |
| links.LINKER.organization( |
| self.data.organization.key(), url_names.GCI_ORG_BAN), |
| checked=is_banned, |
| help_text=_BAN_ORGANIZATION_HELP_TEST, |
| labels={ |
| 'checked': 'Yes', |
| 'unchecked': 'No'}) |
| self.toggle_buttons.append(org_banned) |
| |
| context = { |
| 'title': 'Host Actions', |
| 'toggle_buttons': self.toggle_buttons, |
| } |
| |
| return context |
| |
| def templatePath(self): |
| """See template.Template.templatePath for specification.""" |
| return 'modules/gci/_user_action.html' |
| |
| |
| class OrgHomepage(GCIRequestHandler): |
| """Encapsulates all the methods required to render the org homepage.""" |
| |
| access_checker = access.ALL_ALLOWED_ACCESS_CHECKER |
| |
| def templatePath(self): |
| return 'modules/gci/org_home/base.html' |
| |
| def djangoURLPatterns(self): |
| return [ |
| url(r'org/%s$' % url_patterns.ORG, self, |
| name=url_names.GCI_ORG_HOME), |
| url(r'org/home/%s' % url_patterns.ORG, self), |
| ] |
| |
| def jsonContext(self, data, check, mutator): |
| idx = lists.getListIndex(data.request) |
| list_content = None |
| if idx == 0: |
| list_content = OpenTasksList(data).getListData() |
| elif idx == 1: |
| list_content = CompletedTasksList(data).getListData() |
| |
| if list_content: |
| return list_content.content() |
| else: |
| raise exception.Forbidden(message='You do not have access to this data') |
| |
| def context(self, data, check, mutator): |
| context = { |
| 'page_name': '%s - Home page' % data.url_ndb_org.name, |
| 'about_us': AboutUs(data), |
| 'contact_us': ContactUs(data), |
| 'feed_url': data.url_ndb_org.contact.feed_url, |
| } |
| |
| if data.timeline.tasksPubliclyVisible(): |
| context['open_tasks_list'] = OpenTasksList(data) |
| context['completed_tasks_list'] = CompletedTasksList(data) |
| |
| return context |