<commit_before># 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 Organization Application."""

import logging

from django import http
from django.utils import simplejson
from django.utils.translation import ugettext

from soc.logic.exceptions import BadRequest
from soc.logic.exceptions import NotFound
from soc.mapreduce.helper import control as mapreduce_control
from soc.models.org_app_record import OrgAppRecord
from soc.views import org_app
from soc.views.helper import access_checker
from soc.views.helper import url_patterns

from soc.logic import org_app as org_app_logic
from soc.modules.gsoc.views import forms as gsoc_forms
from soc.modules.gsoc.views.base import GSoCRequestHandler
from soc.modules.gsoc.views.helper import url_names
from soc.modules.gsoc.views.helper.url_patterns import url


class GSoCOrgAppEditForm(org_app.OrgAppEditForm):
  """Form to create/edit GSoC organization application survey.
  """

  class Meta(org_app.OrgAppEditForm.Meta):
    pass

  def __init__(self, *args, **kwargs):
    super(GSoCOrgAppEditForm, self).__init__(
        gsoc_forms.GSoCBoundField, *args, **kwargs)

  def templatePath(self):
    return 'v2/modules/gsoc/_form.html'


class GSoCOrgAppTakeForm(org_app.OrgAppTakeForm):
  """Form for would-be organization admins to apply for a GSoC program.
  """

  CHECKBOX_SELECT_MULTIPLE = gsoc_forms.CheckboxSelectMultiple

  RADIO_FIELD_RENDERER = gsoc_forms.RadioFieldRenderer

  class Meta(org_app.OrgAppTakeForm.Meta):
    pass

  def __init__(self, survey, *args, **kwargs):
    super(GSoCOrgAppTakeForm, self).__init__(
        survey, gsoc_forms.GSoCBoundField, *args, **kwargs)

  def templatePath(self):
    return 'v2/modules/gsoc/_form.html'


class GSoCOrgAppEditPage(GSoCRequestHandler):
  """View for creating/editing organization application."""

  def djangoURLPatterns(self):
    return [
         url(r'org/application/edit/%s$' % url_patterns.PROGRAM,
             self, name='gsoc_edit_org_app'),
    ]

  def checkAccess(self, data, check, mutator):
    check.isHost()

  def templatePath(self):
    return 'v2/modules/gsoc/org_app/edit.html'

  def context(self, data, check, mutator):
    if data.org_app:
      form = GSoCOrgAppEditForm(data.POST or None, instance=data.org_app)
    else:
      form = GSoCOrgAppEditForm(data.POST or None)

    if data.org_app:
      page_name = ugettext('Edit - %s' % (data.org_app.title))
    else:
      page_name = 'Create new organization application'

    context = {
        'page_name': page_name,
        'post_url': self.linker.program(data.program, 'gsoc_edit_org_app'),
        'forms': [form],
        'error': bool(form.errors),
        }

    return context

  def orgAppFromForm(self, data):
    """Create/edit the organization application entity from form.

    Args:
      data: A RequestData describing the current request.

    Returns:
      a newly created or updated organization application entity or None.
    """
    if data.org_app:
      form = GSoCOrgAppEditForm(data.POST, instance=data.org_app)
    else:
      form = GSoCOrgAppEditForm(data.POST)

    if not form.is_valid():
      return None

    form.cleaned_data['modified_by'] = data.user

    if not data.org_app:
      form.cleaned_data['created_by'] = data.user
      form.cleaned_data['program'] = data.program
      key_name = 'gsoc_program/%s/orgapp' % data.program.key().name()
      entity = form.create(key_name=key_name, commit=True)
    else:
      entity = form.save(commit=True)

    return entity

  def post(self, data, check, mutator):
    org_app = self.orgAppFromForm(data)
    if org_app:
      # TODO(nathaniel): is this .program() necessary?
      data.redirect.program()

      return data.redirect.to('gsoc_edit_org_app', validated=True)
    else:
      # TODO(nathaniel): problematic self-use.
      return self.get(data, check, mutator)


class GSoCOrgAppPreviewPage(GSoCRequestHandler):
  """Organization Application preview page.

  View for Organization Administrators to preview the organization
  application for the program specified in the URL.
  """

  def djangoURLPatterns(self):
    return [
         url(r'org/application/preview/%s$' % url_patterns.PROGRAM,
             self, name='gsoc_preview_org_app'),
    ]

  def checkAccess(self, data, check, mutator):
    check.isHost()
    if not data.org_app:
      raise NotFound(access_checker.DEF_NO_ORG_APP % data.program.name)

  def templatePath(self):
    return 'v2/modules/gsoc/org_app/take.html'

  def context(self, data, check, mutator):
    form = GSoCOrgAppTakeForm(data.org_app)

    context = {
        'page_name': '%s' % (data.org_app.title),
        'forms': [form],
        'error': bool(form.errors),
        }

    return context


class GSoCOrgAppTakePage(GSoCRequestHandler):
  """View for organizations to submit their application.
  """

  def djangoURLPatterns(self):
    return [
         url(r'org/application/%s$' % url_patterns.PROGRAM,
             self, name='gsoc_take_org_app'),
         url(r'org/application/%s$' % url_patterns.ID,
             self, name='gsoc_retake_org_app'),
    ]

  def checkAccess(self, data, check, mutator):
    if not data.org_app:
      raise NotFound(access_checker.DEF_NO_ORG_APP % data.program.name)
    mutator.orgAppRecordIfIdInKwargs()
    assert access_checker.isSet(data.org_app)

    show_url = None
    if 'id' in data.kwargs:
      show_url = data.redirect.id().urlOf('gsoc_show_org_app')

    check.isSurveyActive(data.org_app, show_url)

    if data.org_app_record:
      check.canRetakeOrgApp()
    else:
      check.canTakeOrgApp()

  def templatePath(self):
    return 'v2/modules/gsoc/org_app/take.html'

  def context(self, data, check, mutator):
    if data.org_app_record:
      form = GSoCOrgAppTakeForm(
          data.org_app, data.POST or None, instance=data.org_app_record)
    else:
      form = GSoCOrgAppTakeForm(data.org_app, data.POST or None)

    context = {
        'page_name': '%s' % (data.org_app.title),
        'forms': [form],
        'error': bool(form.errors),
        }

    return context

  def recordOrgAppFromForm(self):
    """Create/edit a new student evaluation record based on the form input.

    Returns:
      a newly created or updated evaluation record entity or None
    """
    if self.data.org_app_record:
      form = GSoCOrgAppTakeForm(
          self.data.org_app, self.data.POST, instance=self.data.org_app_record)
    else:
      form = GSoCOrgAppTakeForm(self.data.org_app, self.data.POST)

    if not form.is_valid():
      return None

    if not self.data.org_app_record:
      form.cleaned_data['user'] = self.data.user
      form.cleaned_data['main_admin'] = self.data.user
      form.cleaned_data['survey'] = self.data.org_app
      form.cleaned_data['program'] = self.data.program
      entity = form.create(commit=True)
    else:
      entity = form.save(commit=True)

    return entity

  def post(self, data, check, mutator):
    org_app_record = self.recordOrgAppFromForm()
    if org_app_record:
      data.redirect.id(org_app_record.key().id())
      return data.redirect.to('gsoc_retake_org_app', validated=True)
    else:
      # TODO(nathaniel): problematic self-use.
      return self.get(data, check, mutator)
<commit_msg>Eliminate use of per-request state in GSoCOrgAppTakePage.<commit_after># 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 Organization Application."""

import logging

from django import http
from django.utils import simplejson
from django.utils.translation import ugettext

from soc.logic.exceptions import BadRequest
from soc.logic.exceptions import NotFound
from soc.mapreduce.helper import control as mapreduce_control
from soc.models.org_app_record import OrgAppRecord
from soc.views import org_app
from soc.views.helper import access_checker
from soc.views.helper import url_patterns

from soc.logic import org_app as org_app_logic
from soc.modules.gsoc.views import forms as gsoc_forms
from soc.modules.gsoc.views.base import GSoCRequestHandler
from soc.modules.gsoc.views.helper import url_names
from soc.modules.gsoc.views.helper.url_patterns import url


class GSoCOrgAppEditForm(org_app.OrgAppEditForm):
  """Form to create/edit GSoC organization application survey.
  """

  class Meta(org_app.OrgAppEditForm.Meta):
    pass

  def __init__(self, *args, **kwargs):
    super(GSoCOrgAppEditForm, self).__init__(
        gsoc_forms.GSoCBoundField, *args, **kwargs)

  def templatePath(self):
    return 'v2/modules/gsoc/_form.html'


class GSoCOrgAppTakeForm(org_app.OrgAppTakeForm):
  """Form for would-be organization admins to apply for a GSoC program.
  """

  CHECKBOX_SELECT_MULTIPLE = gsoc_forms.CheckboxSelectMultiple

  RADIO_FIELD_RENDERER = gsoc_forms.RadioFieldRenderer

  class Meta(org_app.OrgAppTakeForm.Meta):
    pass

  def __init__(self, survey, *args, **kwargs):
    super(GSoCOrgAppTakeForm, self).__init__(
        survey, gsoc_forms.GSoCBoundField, *args, **kwargs)

  def templatePath(self):
    return 'v2/modules/gsoc/_form.html'


class GSoCOrgAppEditPage(GSoCRequestHandler):
  """View for creating/editing organization application."""

  def djangoURLPatterns(self):
    return [
         url(r'org/application/edit/%s$' % url_patterns.PROGRAM,
             self, name='gsoc_edit_org_app'),
    ]

  def checkAccess(self, data, check, mutator):
    check.isHost()

  def templatePath(self):
    return 'v2/modules/gsoc/org_app/edit.html'

  def context(self, data, check, mutator):
    if data.org_app:
      form = GSoCOrgAppEditForm(data.POST or None, instance=data.org_app)
    else:
      form = GSoCOrgAppEditForm(data.POST or None)

    if data.org_app:
      page_name = ugettext('Edit - %s' % (data.org_app.title))
    else:
      page_name = 'Create new organization application'

    context = {
        'page_name': page_name,
        'post_url': self.linker.program(data.program, 'gsoc_edit_org_app'),
        'forms': [form],
        'error': bool(form.errors),
        }

    return context

  def orgAppFromForm(self, data):
    """Create/edit the organization application entity from form.

    Args:
      data: A RequestData describing the current request.

    Returns:
      a newly created or updated organization application entity or None.
    """
    if data.org_app:
      form = GSoCOrgAppEditForm(data.POST, instance=data.org_app)
    else:
      form = GSoCOrgAppEditForm(data.POST)

    if not form.is_valid():
      return None

    form.cleaned_data['modified_by'] = data.user

    if not data.org_app:
      form.cleaned_data['created_by'] = data.user
      form.cleaned_data['program'] = data.program
      key_name = 'gsoc_program/%s/orgapp' % data.program.key().name()
      entity = form.create(key_name=key_name, commit=True)
    else:
      entity = form.save(commit=True)

    return entity

  def post(self, data, check, mutator):
    org_app = self.orgAppFromForm(data)
    if org_app:
      # TODO(nathaniel): is this .program() necessary?
      data.redirect.program()

      return data.redirect.to('gsoc_edit_org_app', validated=True)
    else:
      # TODO(nathaniel): problematic self-use.
      return self.get(data, check, mutator)


class GSoCOrgAppPreviewPage(GSoCRequestHandler):
  """Organization Application preview page.

  View for Organization Administrators to preview the organization
  application for the program specified in the URL.
  """

  def djangoURLPatterns(self):
    return [
         url(r'org/application/preview/%s$' % url_patterns.PROGRAM,
             self, name='gsoc_preview_org_app'),
    ]

  def checkAccess(self, data, check, mutator):
    check.isHost()
    if not data.org_app:
      raise NotFound(access_checker.DEF_NO_ORG_APP % data.program.name)

  def templatePath(self):
    return 'v2/modules/gsoc/org_app/take.html'

  def context(self, data, check, mutator):
    form = GSoCOrgAppTakeForm(data.org_app)

    context = {
        'page_name': '%s' % (data.org_app.title),
        'forms': [form],
        'error': bool(form.errors),
        }

    return context


class GSoCOrgAppTakePage(GSoCRequestHandler):
  """View for organizations to submit their application."""

  def djangoURLPatterns(self):
    return [
         url(r'org/application/%s$' % url_patterns.PROGRAM,
             self, name='gsoc_take_org_app'),
         url(r'org/application/%s$' % url_patterns.ID,
             self, name='gsoc_retake_org_app'),
    ]

  def checkAccess(self, data, check, mutator):
    if not data.org_app:
      raise NotFound(access_checker.DEF_NO_ORG_APP % data.program.name)
    mutator.orgAppRecordIfIdInKwargs()
    assert access_checker.isSet(data.org_app)

    show_url = None
    if 'id' in data.kwargs:
      show_url = data.redirect.id().urlOf('gsoc_show_org_app')

    check.isSurveyActive(data.org_app, show_url)

    if data.org_app_record:
      check.canRetakeOrgApp()
    else:
      check.canTakeOrgApp()

  def templatePath(self):
    return 'v2/modules/gsoc/org_app/take.html'

  def context(self, data, check, mutator):
    if data.org_app_record:
      form = GSoCOrgAppTakeForm(
          data.org_app, data.POST or None, instance=data.org_app_record)
    else:
      form = GSoCOrgAppTakeForm(data.org_app, data.POST or None)

    context = {
        'page_name': '%s' % (data.org_app.title),
        'forms': [form],
        'error': bool(form.errors),
        }

    return context

  def recordOrgAppFromForm(self, data):
    """Create/edit a new student evaluation record based on the form input.

    Args:
      data: A RequestData describing the current request.

    Returns:
      a newly created or updated evaluation record entity or None
    """
    if data.org_app_record:
      form = GSoCOrgAppTakeForm(
          data.org_app, data.POST, instance=data.org_app_record)
    else:
      form = GSoCOrgAppTakeForm(data.org_app, data.POST)

    if not form.is_valid():
      return None

    if not data.org_app_record:
      form.cleaned_data['user'] = data.user
      form.cleaned_data['main_admin'] = data.user
      form.cleaned_data['survey'] = data.org_app
      form.cleaned_data['program'] = data.program
      entity = form.create(commit=True)
    else:
      entity = form.save(commit=True)

    return entity

  def post(self, data, check, mutator):
    org_app_record = self.recordOrgAppFromForm(data)
    if org_app_record:
      data.redirect.id(org_app_record.key().id())
      return data.redirect.to('gsoc_retake_org_app', validated=True)
    else:
      # TODO(nathaniel): problematic self-use.
      return self.get(data, check, mutator)

{'fraction_non_alphanumeric': 0.1031367309282942, 'fraction_numerical': 0.0016542597187758478, 'mean_word_length': 3.4476513865308434, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4025070', 'n_tokens_mistral': 5141, 'n_tokens_neox': 4995, 'n_words': 1209}