blob: 90b7090d7500af78d6ccfb318d4766aa4a9443fd [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
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
"""This module contains the view for the site menus."""
import datetime
from google.appengine.ext import ndb
from django.core.urlresolvers import reverse
from django.utils import translation
from codein.views.helper import urls
from melange.logic import timeline as timeline_logic
from melange.models import profile as profile_model
from melange.request import links
from soc.logic import accounts
from soc.views.template import Template
from soc.modules.gsoc.logic.program import getMostRecentProgram
from soc.modules.gci.logic import conversation as gciconversation_logic
from soc.modules.gci.models.task import ACTIVE_CLAIMED_TASK
from soc.modules.gci.models.task import GCITask
from soc.modules.gci.views.helper import url_names
def siteMenuContext(data):
"""Generates URL links for the hard-coded GCI site menu items.
redirect = data.redirect
program = data.program
from soc.modules.gci.models.program import GCIProgram
about_page = GCIProgram.about_page.get_value_for_datastore(program)
connect = GCIProgram.connect_with_us_page.get_value_for_datastore(program)
help_page = GCIProgram.help_page.get_value_for_datastore(program)
terms = GCIProgram.terms_and_conditions.get_value_for_datastore(program)
if program.getting_started_url:
getting_started_url = program.getting_started_url
getting_started_url = None
context = {
'about_link': redirect.document(about_page).url(),
'terms_link': redirect.document(terms).url(),
'events_link': links.LINKER.program(data.program, 'gci_events'),
'connect_link': redirect.document(connect).url(),
'help_link': redirect.document(help_page).url(),
'getting_started_link': getting_started_url,
# TODO(nathaniel): make this .program() call unnecessary.
# For now the call to .program() is very important which is why it is explicitly
# put here outside the dictionary data initialization above.
context['static_content_list_link'] = redirect.urlOf(url_names.GCI_CONTENT_LIST)
if data.ndb_profile:
context['dashboard_link'] = links.LINKER.program(
data.program, 'gci_dashboard')
if data.program.messaging_enabled and data.ndb_user:
context['messages_link'] = redirect.urlOf(url_names.GCI_CONVERSATIONS)
context['num_unread_messages'] = (
ndb.Key.from_old_key(data.program.key()), data.ndb_user.key))
if data.timeline.tasksPubliclyVisible():
# TODO(nathaniel): make this .program() call unnecessary.
context['tasks_link'] = redirect.urlOf('gci_list_tasks')
if not data.ndb_user:
context['register_as_student_link'] = links.ABSOLUTE_LINKER.program(
data.program, urls.UrlNames.PROFILE_REGISTER_AS_STUDENT, secure=True)
return context
class Header(Template):
"""MainMenu template.
def __init__(self, data): = data
def templatePath(self):
return "modules/gci/_header.html"
def context(self):
# Need this import to make sponsor visible for sponsor link_id
from soc.models.sponsor import Sponsor # pylint: disable=unused-import
gsoc_link = ''
key_name = getMostRecentProgram(
if key_name:
sponsor, program = key_name.split('/')
gsoc_kwargs = {
'sponsor': sponsor,
'program': program,
# We have to use reverse method instead of the redirect helper
# because we have to get the URL for a program of the other module
# that is not part of the request data. So we cannot directly use
# the redirect helper, since a module's redirect helper doesn't
# resolve to the correct module URL prefix.
gsoc_link = reverse('gsoc_homepage', kwargs=gsoc_kwargs)
return {
'home_link': links.LINKER.program(, 'gci_homepage'),
'gsoc_link': gsoc_link,
class MainMenu(Template):
"""MainMenu template."""
def __init__(self, data): = data
def context(self):
context = siteMenuContext(
'home_link': links.LINKER.program(, 'gci_homepage'),
if (
and == profile_model.Status.ACTIVE):
if timeline_logic.isProgramActive(
context['profile_link'] = links.ABSOLUTE_LINKER.program(, urls.UrlNames.PROFILE_EDIT, secure=True)
context['profile_link'] =
url_names.GCI_PROFILE_SHOW, secure=True)
context['admin_link'] ='gci_admin_dashboard')
return context
def templatePath(self):
return "modules/gci/_mainmenu.html"
class Footer(Template):
"""Footer template.
def __init__(self, data): = data
def context(self):
context = siteMenuContext(
program =
'privacy_policy_link': program.privacy_policy_url,
'blogger_link': program.blogger,
'irc_link': program.irc,
return context
def templatePath(self):
return "modules/gci/_footer.html"
class Status(Template):
"""Template to render the status block.
def __init__(self, data): = data
def getTimeLeftForTask(self, task):
"""Time left to complete the task in human readable format.
if not task.deadline:
return ""
time_now = datetime.datetime.utcnow()
time_left = task.deadline - time_now
days_left = time_left.days
hours_left = time_left.seconds / 3600
minutes_left = (time_left.seconds / 60) % 60
return "%s days %s hrs %s min" % (days_left, hours_left, minutes_left)
def context(self):
context = {
'user_email': accounts.denormalizeAccount(,
'logout_link': links.LINKER.logout(,
'dashboard_link': links.LINKER.program(, 'gci_dashboard')
# TODO(daniel): simplify this if statement
if (
and == profile_model.Status.ACTIVE):
q = GCITask.all()
q.filter('status IN', ACTIVE_CLAIMED_TASK)
task = q.get()
if task:
context['task'] = task
context['time_left'] = self.getTimeLeftForTask(task)
task_url =
context['task_url'] = task_url
return context
def templatePath(self):
return "modules/gci/_status_block.html"
LOGIN_LINK_LABEL = translation.ugettext('Login')
LOGOUT_LINK_LABEL = translation.ugettext('Logout')
NOT_LOGGED_IN = translation.ugettext('Not Logged In')
class LoggedInAs(Template):
"""LoggedInAs template."""
def context(self):
"""See template.Template.context for specification."""
context = {}
context['logged_in_as'] =
context['link_url'] = links.LINKER.logout(
context['link_label'] = LOGOUT_LINK_LABEL
context['username'] =
context['logged_in_as'] = NOT_LOGGED_IN
context['link_url'] = links.LINKER.login(
context['link_label'] = LOGIN_LINK_LABEL
return context
def templatePath(self):
"""See template.Template.template_path for specification."""
return 'modules/gci/_logged_in_as.html'