blob: f6599d37fe50853adbf036e246c5dcbe19f5a1fc [file] [log] [blame]
# 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 views for GCI home page."""
from django.utils import translation
from codein.logic import timeline as ci_timeline_logic
from codein.views.helper import urls
from melange.logic import organization as org_logic
from melange.logic import timeline as timeline_logic
from melange.request import links
from soc.views.helper import url_patterns
from soc.views.template import Template
from soc.modules.gci.logic import task as task_logic
from soc.modules.gci.models import program as program_model
from soc.modules.gci.views import common_templates
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
class HowItWorks(Template):
"""How it works template."""
CONTEST_BEGINS_ON_MSG = translation.ugettext('Contest begins on %s')
CONTEST_CLOSED_ON_MSG = translation.ugettext('Contest closed on %s')
GET_STARTED_NOW_MSG = translation.ugettext('Get Started Now!')
def __init__(self, data):
self.data = data
def context(self):
program = self.data.program
from soc.modules.gci.models.program import GCIProgram
about_page = GCIProgram.about_page.get_value_for_datastore(program)
example_tasks_link = ''
all_tasks_link = ''
main_text = self._getMainText()
if self.data.timeline.orgSignup():
if not self.data.ndb_profile:
start_text = 'Register as a Mentor'
start_link = links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_ORG_MEMBER)
else:
start_text = 'Sign up as organization'
start_link = links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.ORG_PROFILE_CREATE)
if self.data.program.example_tasks:
example_tasks_link = self.data.program.example_tasks
elif (timeline_logic.isActiveStudentSignup(self.data.program_timeline) and
not self.data.ndb_profile):
start_text = 'Register as a Student'
start_link = links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_STUDENT,
secure=True)
# TODO(nathaniel): make this .program() call unnecessary.
self.data.redirect.program()
all_tasks_link = self.data.redirect.urlOf(url_names.GCI_ALL_TASKS_LIST)
elif self.data.timeline.tasksPubliclyVisible():
# TODO(nathaniel): make this .program() call unnecessary.
self.data.redirect.program()
start_text = 'Search for tasks'
start_link = self.data.redirect.urlOf('gci_list_tasks')
elif self.data.program.example_tasks:
start_text = 'See example tasks'
start_link = self.data.program.example_tasks
else:
start_text = start_link = ''
return {
'about_link': self.data.redirect.document(about_page).url(),
'start_text': start_text,
'start_link': start_link,
'example_tasks_link': example_tasks_link,
'all_tasks_link': all_tasks_link,
'main_text': main_text,
'program_id': self.data.program.link_id
}
def templatePath(self):
return "modules/gci/homepage/_how_it_works.html"
def _getMainText(self):
if timeline_logic.isBeforeStudentSignup(self.data.program_timeline):
return self.CONTEST_BEGINS_ON_MSG % (
self.data.program_timeline.student_signup_start.strftime('%b %d'),)
elif timeline_logic.isActiveStudentSignup(self.data.program_timeline):
return self.GET_STARTED_NOW_MSG
elif ci_timeline_logic.isAfterStopAllWorkDeadline(
self.data.program_timeline):
contest_closed = self.data.program_timeline.stop_all_work_deadline
return self.CONTEST_CLOSED_ON_MSG % (contest_closed.strftime('%b %d'),)
class FeaturedTask(Template):
"""Featured task template.
"""
def __init__(self, data, featured_task):
self.data = data
self.featured_task = featured_task
def context(self):
task_url = self.data.redirect.id(self.featured_task.key().id()).urlOf(
'gci_view_task')
return {
'featured_task': self.featured_task,
'featured_task_url': task_url,
}
def templatePath(self):
return "modules/gci/homepage/_featured_task.html"
class ParticipatingOrgs(Template):
"""Participating orgs template."""
# Maximum number of organizations to show in the widget.
_ORG_COUNT = 16
def __init__(self, data):
self.data = data
def context(self):
participating_orgs = []
for org in org_logic.getAcceptedOrganizationsWithLogoImageUrl(
self.data.program.key(), limit=self._ORG_COUNT):
link = links.LINKER.organization(org.key, url_names.GCI_ORG_HOME)
participating_orgs.append({
'link': link,
'logo': org.logo_image_url,
'name': org.name,
})
org_list_url = links.LINKER.program(
self.data.program, urls.UrlNames.ORG_PUBLIC_LIST)
num_orgs = len(participating_orgs)
# use "wide" mode, aka "4 org logos across" in certain cases.
# default is "5 org logos across"
if num_orgs in (6, 7, 8, 11, 12, 16):
css_extra_class = 'wide'
else:
css_extra_class = ''
context = {
'participating_orgs': participating_orgs,
'org_list_url': org_list_url,
'all_participating_orgs': (
self.data.program.nr_accepted_orgs <= num_orgs),
'css_extra_class': css_extra_class,
}
if not self.data.ndb_profile:
context['register_url'] = links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_ORG_MEMBER)
return context
def templatePath(self):
return "modules/gci/homepage/_participating_orgs.html"
class Leaderboard(Template):
"""Leaderboard template."""
def __init__(self, data):
self.data = data
def context(self):
# TODO(nathaniel): make this .program() call unnecessary.
self.data.redirect.program()
return {
'leaderboard_url': self.data.redirect.urlOf(url_names.GCI_LEADERBOARD),
}
def templatePath(self):
return "modules/gci/homepage/_leaderboard.html"
class ConnectWithUs(Template):
"""Connect with us template.
"""
def __init__(self, data):
self.data = data
def context(self):
return {
'program': self.data.program,
}
def templatePath(self):
return "modules/gci/homepage/_connect_with_us.html"
class Homepage(GCIRequestHandler):
"""Encapsulate all the methods required to generate GCI Home page."""
def templatePath(self):
return 'modules/gci/homepage/base.html'
def djangoURLPatterns(self):
return [
url(r'homepage/%s$' % url_patterns.PROGRAM, self,
name='gci_homepage'),
url(r'program/home/%s$' % url_patterns.PROGRAM, self),
]
def checkAccess(self, data, check, mutator):
check.isProgramVisible()
def context(self, data, check, mutator):
current_timeline = data.timeline.currentPeriod()
context = {
'page_name': '%s - Home page' % data.program.name,
'how_it_works': HowItWorks(data),
'participating_orgs': ParticipatingOrgs(data),
'timeline': common_templates.Timeline(data),
'complete_percentage': data.timeline.completePercentage(),
'current_timeline': current_timeline,
'connect_with_us': ConnectWithUs(data),
'program': data.program,
}
if current_timeline in ['student_signup_period',
'working_period', 'offseason']:
featured_task = task_logic.getFeaturedTask(data.program)
if featured_task:
context['featured_task'] = FeaturedTask(data, featured_task)
if data.is_host or data.timeline.winnersAnnounced():
context['winners'] = self._getWinnersTemplate(
data, data.program.winner_selection_type)
return context
def _getWinnersTemplate(self, data, winner_selection_type):
"""Factory method that returns a template to displays the Grand
Prize Winners of the program with the specified winner selection type.
Args:
data: A RequestData describing the current request.
winner_selection_type: the specified WinnerSelectionType.
Returns:
a template appropriate for the specified winner selection type.
"""
if (winner_selection_type ==
program_model.WinnerSelectionType.ORG_NOMINATED):
return common_templates.OrgNominatedWinners(data)
elif (winner_selection_type ==
program_model.WinnerSelectionType.GLOBAL_RANKING):
return common_templates.GlobalRankingWinners(data)
else:
raise ValueError(
'Invalid value of winner_selection_type %s' % winner_selection_type)