<commit_before>#!/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
#
#   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.

"""Views for creating/editing GCI Tasks.
"""

__authors__ = [
  '"Madhusudan.C.S" <<EMAIL>>',
  '"<NAME>" <<EMAIL>>',
  ]


from django import forms as django_forms
from django.utils.translation import ugettext

from soc.logic import cleaning
from soc.views import forms
from soc.views.helper import url_patterns

from soc.modules.gci.models import task
from soc.modules.gci.models.profile import GCIProfile
from soc.modules.gci.views import forms as gci_forms
from soc.modules.gci.views.base import RequestHandler
from soc.modules.gci.views.base_templates import LoggedInMsg
from soc.modules.gci.views.helper.url_patterns import url


class TaskCreateForm(gci_forms.GCIModelForm):
  """Django form for the task creation page.
  """

  tags = django_forms.CharField(
      label=ugettext('Tags'),
      help_text=ugettext('Describe this task with tags (comma separated). '
                         'Ex: Linux, Apache, C++, GUI'))

  time_to_complete_days = django_forms.IntegerField(
      label=ugettext('Time to complete'), min_value=0,
      error_messages={'min_value': ugettext('Days cannot be negative.')})

  time_to_complete_hours = django_forms.IntegerField(
      label=ugettext('Time to complete'), min_value=0,
      error_messages={'min_value': ugettext('Hours cannot be negative.')})

  def __init__(self, data, *args, **kwargs):
    super(TaskCreateForm, self).__init__(*args, **kwargs)

    self.request_data = data

    # get a list difficulty levels stored for the program entity
    difficulties = task.TaskDifficultyTag.get_by_scope(data.program)

    task_difficulties = []
    for difficulty in difficulties:
      task_difficulties.append((difficulty.tag, difficulty.tag))

    self.fields['difficulty'] = django_forms.ChoiceField(
        label=ugettext('Difficulty'), choices=task_difficulties)

    if self.instance:
      self.fields['difficulty'].initial = self.instance.difficulty

    # get a list of task type tags stored for the program entity
    type_tags = task.TaskTypeTag.get_by_scope(data.program)

    task_type_tags = []
    for type_tag in type_tags:
      task_type_tags.append((type_tag.tag, type_tag.tag))

    self.fields['task_type'] = django_forms.MultipleChoiceField(
        label=ugettext('Type'), choices=task_type_tags,
        widget=forms.CheckboxSelectMultiple)

    org_choices = []
    for org in set(data.org_admin_for + data.mentor_for):
      org_choices.append((org.key(), org.name))

    self.fields['organization'] = django_forms.ChoiceField(
        label=ugettext('Organization'), choices=org_choices)

#    self.fields['mentors'] = django_forms.ChoiceField(
#        widget=django_forms.MultipleHiddenInput(), choices=)

    # Bind all the fields here to boundclass since we do not iterate
    # over the fields using iterator for this form.
    self.bound_fields = {}
    for name, field in self.fields.items():
      self.bound_fields[name] = gci_forms.GCIBoundField(self, field, name)

  class Meta:
    model = task.GCITask
    css_prefix = 'gci-task'
    fields = ['title', 'description', 'difficulty' , 'task_type', 'arbit_tag']

  clean_description = cleaning.clean_html_content('description')

  def clean(self):
    super(TaskCreateForm, self).clean()

    cleaned_data = self.cleaned_data
    ttc_days = cleaned_data.get("time_to_complete_days")
    ttc_hours = cleaned_data.get("time_to_complete_hours")

    cleaned_data['time_to_complete'] = ttc_days * 24 + ttc_hours

    cleaned_data['program'] = self.request_data.program

    return cleaned_data

  def clean_mentors(self):<commit_msg>Initialize the edit page's dynamic fields values from the entity.<commit_after>#!/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
#
#   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.

"""Views for creating/editing GCI Tasks.
"""

__authors__ = [
  '"Madhusudan.C.S" <<EMAIL>>',
  '"<NAME>" <<EMAIL>>',
  ]


import datetime

from django import forms as django_forms
from django.utils.translation import ugettext

from soc.logic import cleaning
from soc.views import forms
from soc.views.helper import url_patterns

from soc.modules.gci.models import task
from soc.modules.gci.models.profile import GCIProfile
from soc.modules.gci.views import forms as gci_forms
from soc.modules.gci.views.base import RequestHandler
from soc.modules.gci.views.base_templates import LoggedInMsg
from soc.modules.gci.views.helper.url_patterns import url


class TaskCreateForm(gci_forms.GCIModelForm):
  """Django form for the task creation page.
  """

  tags = django_forms.CharField(
      label=ugettext('Tags'),
      help_text=ugettext('Describe this task with tags (comma separated). '
                         'Ex: Linux, Apache, C++, GUI'))

  time_to_complete_days = django_forms.IntegerField(
      label=ugettext('Time to complete'), min_value=0,
      error_messages={'min_value': ugettext('Days cannot be negative.')})

  time_to_complete_hours = django_forms.IntegerField(
      label=ugettext('Time to complete'), min_value=0,
      error_messages={'min_value': ugettext('Hours cannot be negative.')})

  def __init__(self, data, *args, **kwargs):
    super(TaskCreateForm, self).__init__(*args, **kwargs)

    self.request_data = data

    # get a list difficulty levels stored for the program entity
    difficulties = task.TaskDifficultyTag.get_by_scope(data.program)

    task_difficulties = []
    for difficulty in difficulties:
      task_difficulties.append((difficulty.tag, difficulty.tag))

    self.fields['difficulty'] = django_forms.ChoiceField(
        label=ugettext('Difficulty'), choices=task_difficulties)

    # get a list of task type tags stored for the program entity
    type_tags = task.TaskTypeTag.get_by_scope(data.program)

    task_type_tags = []
    for type_tag in type_tags:
      task_type_tags.append((type_tag.tag, type_tag.tag))

    self.fields['task_type'] = django_forms.MultipleChoiceField(
        label=ugettext('Type'), choices=task_type_tags,
        widget=forms.CheckboxSelectMultiple)

    org_choices = []
    for org in set(data.org_admin_for + data.mentor_for):
      org_choices.append((org.key(), org.name))

    self.fields['organization'] = django_forms.ChoiceField(
        label=ugettext('Organization'), choices=org_choices)

    if self.instance:
      difficulties = self.instance.difficulty
      if difficulties:
        self.fields['difficulty'].initial = difficulties[0].tag

      task_types = self.instance.task_type
      if task_types:
        self.fields['task_type'].initial = [t.tag for t in task_types]

      self.fields['organization'].initial = self.instance.org.key()

      self.fields['tags'].initial = self.instance.tags_string(
          self.instance.arbit_tag)

      ttc = datetime.timedelta(hours=self.instance.time_to_complete)
      self.fields['time_to_complete_days'].initial = ttc.days
      self.fields['time_to_complete_hours'].initial = ttc.seconds / 3600

#    self.fields['mentors'] = django_forms.ChoiceField(
#        widget=django_forms.MultipleHiddenInput(), choices=)

    # Bind all the fields here to boundclass since we do not iterate
    # over the fields using iterator for this form.
    self.bound_fields = {}
    for name, field in self.fields.items():
      self.bound_fields[name] = gci_forms.GCIBoundField(self, field, name)

  class Meta:
    model = task.GCITask
    css_prefix = 'gci-task'
    fields = ['title', 'description', 'difficulty' , 'task_type', 'arbit_tag']

  clean_description = cleaning.clean_html_content('description')

  def clean(self):
    super(TaskCreateForm, self).clean()

    cleaned_data = self.cleaned_data
    ttc_days = cleaned_data.get("time_to_complete_days")
    ttc_hours = cleaned_data.get("time_to_complete_hours")

    cleaned_data['time_to_complete'] = ttc_days * 24 + ttc_hours

    cleaned_data['program'] = self.request_data.program

    return cleaned_data

  def clean_mentors(self):
{'fraction_non_alphanumeric': 0.10564784053156147, 'fraction_numerical': 0.003654485049833887, 'mean_word_length': 4.090755355129651, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '354949', 'n_tokens_mistral': 2806, 'n_tokens_neox': 2697, 'n_words': 704}