blob: 2765d15dc8a6690874f584f1c30cc487128c8c79 [file] [log] [blame]
#!/usr/bin/env python2.5
# 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.
"""Module containing the views for listing all the projects accepted
into a GSoC program.
import logging
from google.appengine.ext import db
from django.utils import simplejson
from soc.logic.exceptions import AccessViolation
from soc.logic.exceptions import BadRequest
from soc.views.base_templates import ProgramSelect
from soc.views.helper import lists
from soc.views.helper import url_patterns
from soc.views.template import Template
from soc.modules.gsoc.logic import project as project_logic
from soc.modules.gsoc.models.organization import GSoCOrganization
from soc.modules.gsoc.models.profile import GSoCStudentInfo
from soc.modules.gsoc.models.project import GSoCProject
from soc.modules.gsoc.models.proposal import GSoCProposal
from soc.modules.gsoc.views.base import RequestHandler
from soc.modules.gsoc.views.helper.url_patterns import url
class ProjectList(Template):
"""Template for listing the student projects accepted in the program.
def __init__(self, request, data):
self.request = request = data
list_config = lists.ListConfiguration(add_key_column=False)
list_config.addColumn('key', 'Key', (lambda ent, *args: "%s/%s" % (
ent.parent().key().name(), ent.key().id())), hidden=True)
list_config.addColumn('student', 'Student',
lambda entity, *args: entity.parent().name())
list_config.addSimpleColumn('title', 'Title')
list_config.addColumn('org', 'Organization',
lambda entity, *args:
def status(project):
"""Status to show on the list with color.
if project.status == 'accepted':
return """<strong><font color="green">Accepted</font><strong>"""
elif project.status == 'withdrawn':
return """<strong><font color="red">Withdrawn</font></strong>"""
return project.status
list_config.addColumn('status', 'Status',
lambda entity, *args: status(entity))
# hidden keys
'full_project_key', 'Full project key',
(lambda ent, *args: str(ent.key())), hidden=True)
# action button
bounds = [1,'all']
keys = ['full_project_key']
list_config.addPostButton('withdraw', "Withdraw", "", bounds, keys)
list_config.addPostButton('accept', "Accept", "", bounds, keys)
self._list_config = list_config
def context(self):
list = lists.ListConfigurationResponse(, self._list_config, idx=0,
description='List of %s projects whether accepted or withdrawn' % (
return {
'lists': [list],
def post(self):
idx = lists.getListIndex(self.request)
if idx != 0:
return None
data ='data')
if not data:
raise BadRequest("Missing data")
parsed = simplejson.loads(data)
button_id ='button_id')
if not button_id:
raise BadRequest("Missing button_id")
elif button_id == 'withdraw':
return self.postHandler(parsed)
elif button_id == 'accept':
return self.postHandler(parsed, withdraw=False)
raise BadRequest("Unknown button_id")
def postHandler(self, data, withdraw=True):
program =
for properties in data:
if 'full_project_key' not in properties:
logging.warning("Missing key in '%s'" % properties)
project_key = properties['full_project_key']
project = db.get(db.Key(project_key))
if not project:
logging.warning("Project '%s' doesn't exist" % project_key)
if withdraw and project.status == 'withdrawn':
logging.warning("Project '%s' already withdrawn" % project_key)
if not withdraw and project.status == 'accepted':
logging.warning("Project '%s' already accepted" % project_key)
# key of the organization for the project
org_key =
# key of the student profile for the project
profile_key = project.parent_key()
qp = GSoCProposal.all()
qp.filter('org', org_key)
# FIXME: ??? Mentors can change overtime so how does this work???
qp.filter('mentor IN', project.mentors)
if withdraw:
qp.filter('status', 'accepted')
qp.filter('status', 'withdrawn')
proposal = qp.get()
def withdraw_or_accept_project_txn():
org = GSoCOrganization.get(org_key)
if withdraw:
new_status = 'withdrawn'
new_number = 0
org.slots -= 1
new_status = 'accepted'
new_number = 1
org.slots += 1
project.status = new_status
proposal.status = new_status
#profile.number_of_projects = new_number
student_info = GSoCStudentInfo.all().ancestor(profile_key).get()
student_info.number_of_project = new_number
db.put([proposal, project, student_info, org])
xg_on = db.create_transaction_options(xg=True)
db.run_in_transaction_options(xg_on, withdraw_or_accept_project_txn)
return True
def getListData(self):
"""Returns the list data as requested by the current request.
If the lists as requested is not supported by this component None is
idx = lists.getListIndex(self.request)
if idx == 0:
list_query = project_logic.getProjectsQuery(
starter = lists.keyStarter
prefetcher = lists.modelPrefetcher(GSoCProject, ['org'],
response_builder = lists.RawQueryContentResponseBuilder(
self.request, self._list_config, list_query,
starter, prefetcher=prefetcher)
return None
def templatePath(self):
return "v2/modules/gsoc/withdraw_projects/_project_list.html"
class WithdrawProjects(RequestHandler):
"""View methods for withdraw projects
def templatePath(self):
return 'v2/modules/gsoc/withdraw_projects/base.html'
def djangoURLPatterns(self):
"""Returns the list of tuples for containing URL to view method mapping.
return [
url(r'withdraw_projects/%s$' % url_patterns.PROGRAM, self,
def checkAccess(self):
"""Access checks for the view.
def jsonContext(self):
"""Handler for JSON requests.
list_content = ProjectList(self.request,
if not list_content:
raise AccessViolation(
'You do not have access to this data')
return list_content.content()
def post(self):
list_content = ProjectList(self.request,
if not
raise AccessViolation(
'You cannot change this data')
def context(self):
"""Handler for GSoC Accepted Projects List page HTTP get request.
program =
return {
'page_name': '%s - Projects' % program.short_name,
'project_list': ProjectList(self.request,,
'program_select': ProgramSelect(, 'gsoc_withdraw_projects'),