blob: ec8f97c606ec10253525b3201ef17fdfdafdb1a0 [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 GSoC home page."""
import itertools
import json
from django.conf.urls import url as django_url
from melange.logic import organization as org_logic
from melange.logic import timeline as timeline_logic
from melange.request import links
from melange.models import profile as profile_model
from melange.templates import actions_required
from soc.views import base_templates
from soc.views.helper import url_patterns
from soc.views.template import Template
from soc.modules.gsoc.views import base
from soc.modules.gsoc.views import dashboard as dashboard_view
from soc.modules.gsoc.views.helper.url_patterns import url
from summerofcode.logic import timeline as soc_timeline_logic
from summerofcode.logic import project as project_logic
from summerofcode.logic import proposal as proposal_logic
from summerofcode.models import proposal as proposal_model
from summerofcode.views.helper import urls
_ACTIONS_REQUIRED_TEMPLATE_PATH = 'modules/gsoc/homepage/_action_required.html'
def _getActionsRequired(data, url_names):
"""Returns a template to display required actions for the current user.
Args:
data: request_data.RequestData for the current request.
url_names: Instance of url_names.UrlNames.
Returns:
Instance of actions_required.ActionsRequired or None, if no actions are
required at this time.
"""
if not data.ndb_profile:
# no actions are ever required from users without profiles
return None
elif not timeline_logic.isProgramActive(data.program_timeline):
# no actions are ever required when a program is not active
return None
else:
items = list(itertools.chain(
actions_required.createAppResponseNeededActionItems(data, url_names),
actions_required.createBackupAdminNeededActionItem(data, url_names),
actions_required.createLogoNeededActionItem(data, url_names)))
return (
actions_required.ActionsRequired(
data, items, _ACTIONS_REQUIRED_TEMPLATE_PATH)
if items else None)
class Timeline(Template):
"""Timeline template."""
def __init__(self, data):
self.data = data
self.json_data = json.dumps(
soc_timeline_logic.createTimelineDict(data.timeline))
def context(self):
next_deadline_msg, next_deadline_datetime = (
self.data.timeline.nextDeadline())
if next_deadline_msg and next_deadline_datetime:
return {
'next_deadline_msg': next_deadline_msg,
'next_deadline_datetime': next_deadline_datetime,
}
else:
return {}
def templatePath(self):
return "modules/gsoc/homepage/_timeline.html"
class ProposalWidget(Template):
"""Proposals widget template."""
def __init__(self, data):
self.data = data
def templatePath(self):
return "modules/gsoc/homepage/_proposal_widget.html"
def proposal_context_data(self, proposal):
"""Returns a dictionary for the given proposal in the context data.
Args:
proposal: A proposal entity.
Returns:
A dictionary with the proposal's data for the view context.
"""
organization = proposal.organization.get()
organization_context = {
'name': organization.name,
'logo_url': organization.logo_url,
}
public_comments = proposal_logic.getComments(
proposal.key, exclude_private=True)
proposal_url = links.LINKER.userId(
proposal.key.parent(), proposal.key.id(),
urls.UrlNames.PROPOSAL_REVIEW_AS_STUDENT)
return {
'title': proposal.title,
'accepted': proposal.status == proposal_model.Status.ACCEPTED,
'withdrawn': proposal.status == proposal_model.Status.WITHDRAWN,
'num_comments': len(public_comments),
'num_unread_comments': 0, # TODO(drewgottlieb): Implement unread
# comments for proposals.
'proposal_url': proposal_url,
'organization': organization_context,
}
def context(self):
accepted_proposals = []
active_proposals = []
inactive_proposals = []
max_proposals = self.data.program.apps_tasks_limit
num_active_proposals = 0
reached_max_proposals = False
proposal_submission_allowed = (
timeline_logic.isActiveStudentSignup(self.data.program_timeline))
can_submit_proposals = False
if self.data.ndb_profile:
proposals = proposal_logic.queryProposalsForStudent(
self.data.ndb_profile.key).fetch(50)
accepted_proposals = filter(
lambda proposal: (
proposal.status == proposal_model.Status.ACCEPTED and
proposal.status != proposal_model.Status.WITHDRAWN),
proposals)
active_proposals = filter(
lambda proposal: (
proposal.status != proposal_model.Status.ACCEPTED and
proposal.status != proposal_model.Status.WITHDRAWN),
proposals)
inactive_proposals = filter(
lambda proposal: (
proposal.status != proposal_model.Status.ACCEPTED and
proposal.status == proposal_model.Status.WITHDRAWN),
proposals)
num_active_proposals = len(accepted_proposals + active_proposals)
reached_max_proposals = proposal_logic.isProposalLimitReached(
self.data.ndb_profile, self.data.program)
can_submit_proposals = (
proposal_submission_allowed and not reached_max_proposals)
return {
'max_proposals': max_proposals,
'active_proposals': num_active_proposals,
'proposal_submission_allowed': proposal_submission_allowed,
'can_submit_proposals': can_submit_proposals,
'max_proposals_submitted': reached_max_proposals,
'new_proposal_url': links.LINKER.program(
self.data.program, urls.UrlNames.PROPOSAL_PICK_ORG),
'proposals': map(
self.proposal_context_data,
accepted_proposals + active_proposals + inactive_proposals),
}
# TODO(daniel): split this template into a few smaller templates so that
# the logic is not ~150 lines long
class Apply(Template):
"""Apply template."""
def __init__(self, data):
self.data = data
def context(self):
context = {}
accepted_orgs = None
redirector = self.data.redirect
redirector.program()
if self.data.timeline.orgsAnnounced():
# accepted orgs block
accepted_orgs_link = links.LINKER.program(
self.data.program, urls.UrlNames.ORG_PUBLIC_LIST)
nr_orgs = self.data.program.nr_accepted_orgs
context['nr_accepted_orgs'] = nr_orgs if nr_orgs else ""
context['accepted_orgs_link'] = accepted_orgs_link
participating_orgs = []
current_orgs = org_logic.getAcceptedOrganizationsWithLogoImageUrl(
self.data.program.key(), models=self.data.models)
for org in current_orgs:
link = links.LINKER.organization(org.key, urls.UrlNames.ORG_HOME)
participating_orgs.append({
'link': link,
'logo': org.logo_image_url_sized(size=64),
'name': org.name,
})
context['participating_orgs'] = participating_orgs
context['program_active'] = (
timeline_logic.isProgramActive(self.data.program_timeline))
is_active_student_signup = (
timeline_logic.isActiveStudentSignup(self.data.program_timeline))
context['org_signup'] = self.data.timeline.orgSignup()
if context['org_signup']:
if self.data.ndb_profile:
context['has_profile'] = True
context['is_admin'] = self.data.ndb_profile.is_admin
else:
context['has_profile'] = False
context['is_admin'] = False
context['org_member_profile_link'] = links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_ORG_MEMBER,
secure=True)
context['org_apply_link'] = links.LINKER.program(
self.data.program, urls.UrlNames.ORG_PROFILE_CREATE)
elif not self.data.timeline.beforeOrgSignupEnd():
context['after_org_signup'] = True
if self.data.ndb_profile:
context['has_profile'] = True
context['start_connection_url'] = links.LINKER.program(
self.data.program, urls.UrlNames.CONNECTION_PICK_ORG)
context['proposal_url'] = links.LINKER.program(
self.data.program, urls.UrlNames.PROPOSAL_PICK_ORG)
context['is_student'] = self.data.ndb_profile.is_student
if context['is_student']:
context['enrollment_form_submitted'] = bool(
self.data.ndb_profile.student_data.enrollment_form)
if not context['enrollment_form_submitted']:
context['enrollment_form_url'] = links.ABSOLUTE_LINKER.program(
self.data.program, 'gsoc_enrollment_form', secure=True)
context['no_role'] = not self.data.ndb_profile.is_mentor
else:
context['has_profile'] = False
context['org_member_profile_url'] = links.LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_ORG_MEMBER)
context['is_student'] = False
context['before_student_signup'] = (
timeline_logic.isBeforeStudentSignup(self.data.program_timeline))
if context['before_student_signup']:
context['student_signup_start'] = (
self.data.program_timeline.student_signup_start)
context['active_student_signup'] = is_active_student_signup
if context['active_student_signup']:
context['student_profile_url'] = links.LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_STUDENT)
# TODO(daniel): check if the form is submitted
context['has_form'] = True
else:
context['has_form'] = False
context['student_signup'] = is_active_student_signup
context['mentor_signup'] = self.data.timeline.mentorSignup()
is_active_student = (self.data.ndb_profile and
self.data.ndb_profile.student_data)
signup_active = (
self.data.timeline.orgSignup() or
is_active_student_signup or
(self.data.timeline.mentorSignup() and not
is_active_student)
)
# signup block
if signup_active and not self.data.gae_user:
# Show a login link
context['login_link'] = links.LINKER.login(self.data.request)
# TODO(daniel): links to new profile registration pages must be provided!!
if signup_active and not self.data.ndb_profile:
# Show a registration link for a relevant profile type.
context['mentor_profile_link'] = links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_ORG_MEMBER,
secure=True)
context['org_admin_profile_link'] = links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_ORG_MEMBER,
secure=True)
context['student_profile_link'] = links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_STUDENT,
secure=True)
context['show_profile_link'] = False
if self.data.timeline.orgSignup():
context['show_org_admin_link'] = True
context['show_profile_link'] = True
elif is_active_student_signup:
context['show_student_link'] = True
context['show_profile_link'] = True
elif self.data.timeline.mentorSignup():
context['show_mentor_link'] = True
context['show_student_link'] = True
context['show_profile_link'] = True
if self.data.timeline.orgSignup() and self.data.ndb_profile:
context['org_apply_link'] = links.LINKER.program(
self.data.program, urls.UrlNames.ORG_PROFILE_CREATE)
context['managed_orgs_link'] = (
links.LINKER.program(self.data.program, 'gsoc_dashboard') +
'#' + dashboard_view.MANAGED_ORGANIZATIONS_COMPONENT_ID)
if ((is_active_student_signup or
self.data.timeline.mentorSignup()) and
self.data.ndb_profile):
context['apply_link'] = accepted_orgs
if self.data.ndb_profile:
if self.data.ndb_profile.student_data:
context['profile_role'] = 'student'
else:
context['profile_role'] = 'mentor'
context['signup_active'] = signup_active
return context
def templatePath(self):
return "modules/gsoc/homepage/_apply.html"
class FeaturedProject(Template):
"""Template to render a featured project for the program."""
def __init__(self, data, project):
"""Initializes a new instance of the template for the specified featured
project.
Args:
data: request_data.RequestData object for the current request.
project: project_model.Project entity.
"""
super(FeaturedProject, self).__init__(data)
self.project = project
self.template_path = 'modules/gsoc/homepage/_featured_project.html'
def context(self):
"""See template.Template.context for specification."""
details_url = links.LINKER.userId(
self.project.key.parent(), self.project.key.id(),
urls.UrlNames.PROJECT_DETAILS)
return {
'featured_project': self.project,
'featured_project_url': details_url,
}
class ConnectWithUs(Template):
"""Connect with us template.
"""
def __init__(self, data):
self.data = data
def context(self):
return {
'blogger_link': self.data.program.blogger,
'email': self.data.program.email,
'irc_channel_link': self.data.program.irc,
'google_plus_link': self.data.program.gplus,
}
def templatePath(self):
return "modules/gsoc/_connect_with_us.html"
class Homepage(base.GSoCRequestHandler):
"""Encapsulate all the methods required to generate GSoC Home page."""
def templatePath(self):
return 'modules/gsoc/homepage/base.html'
def djangoURLPatterns(self):
"""Returns the list of tuples for containing URL to view method mapping.
"""
return [
url(r'homepage/%s$' % url_patterns.PROGRAM, self,
name='gsoc_homepage'),
url(r'program/home/%s$' % url_patterns.PROGRAM, self),
django_url(r'^program/home/%s$' % url_patterns.PROGRAM, self),
]
def checkAccess(self, data, check, mutator):
"""Access checks for GSoC Home page."""
check.isProgramVisible()
def context(self, data, check, mutator):
"""Handler to for GSoC Home page HTTP get request."""
if (data.program.enable_homepage_widgets == True and data.ndb_profile and
data.ndb_profile.is_student and
data.ndb_profile.status == profile_model.Status.ACTIVE):
show_how_it_works = False
proposal_widget = ProposalWidget(data)
else:
show_how_it_works = True
proposal_widget = None
context = {
'actions_required': _getActionsRequired(data, urls.UrlNames),
'timeline': Timeline(data),
'apply': Apply(data),
'connect_with_us': ConnectWithUs(data),
'page_name': '%s - Home page' % (data.program.name),
'program': data.program,
'show_how_it_works': show_how_it_works,
'proposal_widget': proposal_widget,
'program_select': base_templates.ProgramSelect(
'modules/gsoc/homepage/_program_select.html', data,
'gsoc_homepage'),
}
featured_project = project_logic.getFeaturedProject(data.program.key())
if featured_project:
context['featured_project'] = FeaturedProject(data, featured_project)
return context