blob: 68ff855c20b19a7af616b56e001cbe66aeb3a8ce [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 for the GCI view to bulk create GCITasks."""
from django import forms
from django import http
from melange.models import organization as org_model
from melange.request import access
from melange.request import links
from soc.views.helper import url_patterns
from soc.modules.gci.models.bulk_create_data import GCIBulkCreateData
from soc.modules.gci.tasks import bulk_create
from soc.modules.gci.views import forms as gci_forms
from soc.modules.gci.views.base import GCIRequestHandler
from soc.modules.gci.views.helper import url_names
from soc.modules.gci.views.helper import url_patterns as soc_url_patterns
class BulkCreateForm(gci_forms.GCIModelForm):
"""Django form for the bulk create page.
"""
class Meta:
model = GCIBulkCreateData
css_prefix = 'gci_bulk_create'
fields = ['task_data']
task_data = forms.CharField(label='Task Data', required=True,
widget=forms.Textarea)
class BulkCreate(GCIRequestHandler):
"""View for bulk creation of GCITasks.
"""
def djangoURLPatterns(self):
"""The URL pattern for the view.
"""
return [
soc_url_patterns.url(r'bulk/%s$' % url_patterns.ORG, self,
name=url_names.GCI_TASK_BULK_CREATE)]
def checkAccess(self, data, check, mutator):
"""Denies access if the currently logged user is not allowed to
bulk create tasks.
"""
check.isLoggedIn()
# tasks can be created only for accepted organizations
access.UrlOrgStatusAccessChecker(
[org_model.Status.ACCEPTED]).checkAccess(data, check)
check.canBulkCreateTask()
def templatePath(self):
"""Returns the path to the template.
"""
return 'modules/gci/bulk_create/base.html'
def context(self, data, check, mutator):
"""Handler for default HTTP GET request."""
context = {
'page_name': 'Bulk upload tasks for %s' % data.url_ndb_org.name,
'beginner_task_tag': bulk_create.BEGINNER_TASK_TAG
}
# get a list of task type tags stored for the program entity
tts = data.program.task_types
context['types'] = ', '.join([str(x) for x in tts])
if data.POST:
form = BulkCreateForm(data=data.POST)
context['form'] = form
else:
context['form'] = BulkCreateForm()
return context
def post(self, data, check, mutator):
"""Handles POST requests for the bulk create page."""
form = BulkCreateForm(data=data.POST)
if not form.is_valid():
# TODO(nathaniel): problematic self-call.
return self.get(data, check, mutator)
bulk_create.spawnBulkCreateTasks(
form.cleaned_data['task_data'], data.url_ndb_org.key,
data.ndb_profile)
url = links.LINKER.organization(
data.url_ndb_org.key, url_names.GCI_TASK_BULK_CREATE)
return http.HttpResponseRedirect(url + '?validated=true')