<commit_before>#!/usr/bin/env python2.5
#
# Copyright 2009 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.

"""GCI specific views for Organizations.
"""

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


from django import http

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

from soc.logic import dicts
from soc.logic.helper import timeline as timeline_helper

from soc.views import helper
from soc.views.helper import decorators
from soc.views.helper import lists
from soc.views.helper import redirects
from soc.views.helper import responses
from soc.views.models import organization

import soc.logic.lists

from soc.modules.gci.logic.models import program as gci_program_logic
from soc.modules.gci.logic.models import org_admin as gci_org_admin_logic
from soc.modules.gci.logic.models import organization as gci_org_logic
from soc.modules.gci.logic.models import task as gci_task_logic
from soc.modules.gci.logic.models.org_app_survey import logic as org_app_logic
from soc.modules.gci.views.helper import access as gci_access
from soc.modules.gci.views.models import program as gci_program_view
from soc.modules.gci.views.helper import redirects as gci_redirects

import soc.modules.gci.logic.models.organization


class View(organization.View):
  """View methods for the GCI Organization model.
  """

  DEF_OPEN_PROJECTS_MSG_FMT = ugettext(
      'List of tasks published by %s that are open.')

  DEF_CLAIMED_PROJECTS_MSG_FMT = ugettext(
      'List of tasks published by %s that are claimed.')

  DEF_CLOSED_PROJECTS_MSG_FMT = ugettext(
      'List of tasks published by %s that are closed.')

  def __init__(self, params=None):
    """Defines the fields and methods required for the program View class
    to provide the user with list, public, create, edit and delete views.

    Params:
      params: a dict with params for this View
    """

    rights = gci_access.GCIChecker(params)
    rights['create'] = ['checkIsDeveloper']
    rights['edit'] = [('checkHasRoleForKeyFieldsAsScope',
                           gci_org_admin_logic.logic),
                      ('checkGroupIsActiveForLinkId', gci_org_logic.logic)]
    rights['delete'] = ['checkIsDeveloper']
    rights['home'] = ['allow']
    rights['public_list'] = ['allow']
    rights['applicant'] = [('checkIsOrgAppAccepted', org_app_logic)]
    rights['apply_mentor'] = ['checkIsUser']
    rights['list_requests'] = [('checkHasRoleForKeyFieldsAsScope',
                                gci_org_admin_logic.logic)]
    rights['list_roles'] = [('checkHasRoleForKeyFieldsAsScope',
                             gci_org_admin_logic.logic)]
    rights['request_task'] = [
        'checkIsUser',
        ('checkIsAfterEvent',
            ['tasks_publicly_visible', 'scope_path', gci_program_logic.logic]),
        ('checkIsBeforeEvent',
            ['task_claim_deadline', 'scope_path', gci_program_logic.logic]),
        ]

    new_params = {}
    new_params['logic'] = soc.modules.gci.logic.models.organization.logic
    new_params['rights'] = rights

    new_params['scope_view'] = gci_program_view

    new_params['name'] = "GCI Organization"
    new_params['module_name'] = "organization"
    new_params['sidebar_grouping'] = 'GCI Organizations'

    new_params['public_template'] = 'modules/gci/organization/public.html'
    new_params['home_template'] = 'modules/gci/organization/home.html'

    new_params['module_package'] = 'soc.modules.gci.views.models'
    new_params['url_name'] = 'gci/org'
    new_params['document_prefix'] = 'gci_org'

    new_params['extra_dynaexclude'] = ['slots', 'slots_calculated',
                                       'nr_applications', 'nr_mentors',
                                       'slots_desired', 'ideas',
                                       'task_quota_limit']

    new_params['mentor_role_name'] = 'gci_mentor'
    new_params['mentor_url_name'] = 'gci/mentor'
    new_params['org_admin_role_name'] = 'gci_org_admin'

    # TODO(ljvderijk): prefetch these entities and pass as args
    new_params['public_field_extra'] = lambda entity: {
        "open_tasks": str(len(gci_task_logic.logic.getForFields({
            'scope': entity,
            'status': ['Open', 'Reopened']
        }))),
        "claimed_tasks": str(len(gci_task_logic.logic.getForFields({
            'scope': entity,
            'status': ['ClaimRequested', 'Claimed', 'ActionNeeded',
                       'NeedsReview', 'NeedsWork'],
        }))),
        "closed_tasks": str(len(gci_task_logic.logic.getForFields({
            'scope': entity,
            'status': ['AwaitingRegistration', 'Closed'],
        }))),
        "home_page": lists.urlize(entity.home_page),
    }
    new_params['public_field_keys'] = [
        "name", "task_quota_limit", "open_tasks",
        "claimed_tasks", "closed_tasks", "home_page",
    ]
    new_params['public_field_names'] = [
        "Name", "Tasks Quota", "Open Tasks",
        "Claimed Tasks", "Closed Tasks", "Home Page",
    ]

    new_params['org_app_logic'] = org_app_logic

    patterns = []
    patterns += [
        (r'^%(url_name)s/(?P<access_type>request_task)/%(key_fields)s$',
          '%(module_package)s.%(module_name)s.request_task',
          'Request more tasks'),
        ]

    new_params['extra_django_patterns'] = patterns

    params = dicts.merge(params, new_params, sub_merge=True)

    super(View, self).__init__(params=params)

  def getListTasksData(self, request, params_collection, org_entity):
    """Returns the list data for Organization Tasks list.

    Args:<commit_msg>Fix 'list participating organizations' view<commit_after>#!/usr/bin/env python2.5
#
# Copyright 2009 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.

"""GCI specific views for Organizations.
"""

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


from django import http

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

from soc.logic import dicts
from soc.logic.helper import timeline as timeline_helper

from soc.views import helper
from soc.views.helper import decorators
from soc.views.helper import lists
from soc.views.helper import redirects
from soc.views.helper import responses
from soc.views.models import organization

import soc.logic.lists

from soc.modules.gci.logic.models import program as gci_program_logic
from soc.modules.gci.logic.models import org_admin as gci_org_admin_logic
from soc.modules.gci.logic.models import organization as gci_org_logic
from soc.modules.gci.logic.models import task as gci_task_logic
from soc.modules.gci.logic.models.org_app_survey import logic as org_app_logic
from soc.modules.gci.views.helper import access as gci_access
from soc.modules.gci.views.models import program as gci_program_view
from soc.modules.gci.views.helper import redirects as gci_redirects

import soc.modules.gci.logic.models.organization


class View(organization.View):
  """View methods for the GCI Organization model.
  """

  DEF_OPEN_PROJECTS_MSG_FMT = ugettext(
      'List of tasks published by %s that are open.')

  DEF_CLAIMED_PROJECTS_MSG_FMT = ugettext(
      'List of tasks published by %s that are claimed.')

  DEF_CLOSED_PROJECTS_MSG_FMT = ugettext(
      'List of tasks published by %s that are closed.')

  def __init__(self, params=None):
    """Defines the fields and methods required for the program View class
    to provide the user with list, public, create, edit and delete views.

    Params:
      params: a dict with params for this View
    """

    rights = gci_access.GCIChecker(params)
    rights['create'] = ['checkIsDeveloper']
    rights['edit'] = [('checkHasRoleForKeyFieldsAsScope',
                           gci_org_admin_logic.logic),
                      ('checkGroupIsActiveForLinkId', gci_org_logic.logic)]
    rights['delete'] = ['checkIsDeveloper']
    rights['home'] = ['allow']
    rights['public_list'] = ['allow']
    rights['applicant'] = [('checkIsOrgAppAccepted', org_app_logic)]
    rights['apply_mentor'] = ['checkIsUser']
    rights['list_requests'] = [('checkHasRoleForKeyFieldsAsScope',
                                gci_org_admin_logic.logic)]
    rights['list_roles'] = [('checkHasRoleForKeyFieldsAsScope',
                             gci_org_admin_logic.logic)]
    rights['request_task'] = [
        'checkIsUser',
        ('checkIsAfterEvent',
            ['tasks_publicly_visible', 'scope_path', gci_program_logic.logic]),
        ('checkIsBeforeEvent',
            ['task_claim_deadline', 'scope_path', gci_program_logic.logic]),
        ]

    new_params = {}
    new_params['logic'] = soc.modules.gci.logic.models.organization.logic
    new_params['rights'] = rights

    new_params['scope_view'] = gci_program_view

    new_params['name'] = "GCI Organization"
    new_params['module_name'] = "organization"
    new_params['sidebar_grouping'] = 'GCI Organizations'

    new_params['public_template'] = 'modules/gci/organization/public.html'
    new_params['home_template'] = 'modules/gci/organization/home.html'

    new_params['module_package'] = 'soc.modules.gci.views.models'
    new_params['url_name'] = 'gci/org'
    new_params['document_prefix'] = 'gci_org'

    new_params['extra_dynaexclude'] = ['slots', 'slots_calculated',
                                       'nr_applications', 'nr_mentors',
                                       'slots_desired', 'ideas',
                                       'task_quota_limit']

    new_params['mentor_role_name'] = 'gci_mentor'
    new_params['mentor_url_name'] = 'gci/mentor'
    new_params['org_admin_role_name'] = 'gci_org_admin'
    new_params['org_app_logic'] = org_app_logic

    patterns = []
    patterns += [
        (r'^%(url_name)s/(?P<access_type>request_task)/%(key_fields)s$',
          '%(module_package)s.%(module_name)s.request_task',
          'Request more tasks'),
        ]

    new_params['extra_django_patterns'] = patterns

    params = dicts.merge(params, new_params, sub_merge=True)

    super(View, self).__init__(params=params)

    # The organization view manually overwrites public_field_extra,
    # so we must do the same (or be overwritten by them).

    self._params['public_field_extra'] = lambda entity: {
        "home_page": lists.urlize(entity.home_page),
    }
    import settings
    self._params['public_field_keys'] = [
        "name", "short_name", "home_page",
    ]
    self._params['public_field_names'] = [
        "Name", "Short Name", "Home Page",
    ]

    if settings.GCI_TASK_QUOTA_LIMIT_ENABLED:
      self._params['public_field_keys'].append("task_quota_limit")
      self._params['public_field_names'].append("Tasks Quota")

  def getListTasksData(self, request, params_collection, org_entity):
    """Returns the list data for Organization Tasks list.

    Args:
{'fraction_non_alphanumeric': 0.1148846960167715, 'fraction_numerical': 0.0016771488469601676, 'mean_word_length': 3.475046904315197, 'pattern_counts': {'":': 5, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '363899', 'n_tokens_mistral': 3779, 'n_tokens_neox': 3604, 'n_words': 854}