<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.

"""Module containing the AccessChecker class that contains helper functions
for checking access.
"""

__authors__ = [
    '"<NAME>" <<EMAIL>>',
  ]


from django.utils.translation import ugettext

from google.appengine.api import users

from soc.logic.models.user import logic as user_logic

from soc.logic.exceptions import LoginRequest
from soc.logic.exceptions import RedirectRequest
from soc.logic.exceptions import AccessViolation

from soc.modules.gsoc.models.organization import GSoCOrganization
from soc.modules.gsoc.models.proposal import GSoCProposal


DEF_AGREE_TO_TOS_MSG_FMT = ugettext(
    'You must agree to the <a href="%(tos_link)s">site-wide Terms of'
    ' Service</a> in your <a href="/user/edit_profile">User Profile</a>'
    ' in order to view this page.')

DEF_NOT_PARTICIPATING_MSG = ugettext(
    'You are not participating in this program and have no access.')

DEF_ALREADY_PARTICIPATING_MSG = ugettext(
    'You cannot become a Student because you are already participating '
    'in this program.')

DEF_DEV_LOGOUT_LOGIN_MSG_FMT = ugettext(
    'Please <a href="%%(sign_out)s">sign out</a>'
    ' and <a href="%%(sign_in)s">sign in</a>'
    ' again as %(role)s to view this page.')

DEF_NO_USER_LOGIN_MSG = ugettext(
    'Please create <a href="/user/create_profile">User Profile</a>'
    ' in order to view this page.')

DEF_PAGE_INACTIVE_MSG = ugettext(
    'This page is inactive at this time.')

DEF_SCOPE_INACTIVE_MSG = ugettext(
    'The scope for this request is not active.')

DEF_PROGRAM_INACTIVE_MSG_FMT = ugettext(
    'This page is inaccessible because %s is not active at this time.')

DEF_PAGE_INACTIVE_OUTSIDE_MSG_FMT = ugettext(
    'This page is inactive before %s and after %s.')

DEF_PAGE_INACTIVE_BEFORE_MSG_FMT = ugettext(
    'This page is inactive before %s')

DEF_NO_SUCH_PROGRAM_MSG = ugettext(
    'The url is wrong (no program was found).')

DEF_ROLE_INACTIVE_MSG = ugettext(
    'This page is inaccessible because you do not have a profile '
    'in the program at this time.')

DEF_IS_NOT_STUDENT_MSG = ugettext(
    'This page is inaccessible because you do not have a student role '
    'in the program.')

DEF_IS_STUDENT_MSG = ugettext(
    'This page is inaccessible because you are registered as a student.')

DEF_ORG_DOES_NOT_EXISTS_MSG_FMT = ugettext(
    'Organization, whose link_id is %(link_id)s, does not exist in '
    '%(program)s.')

DEF_ORG_NOT_ACTIVE_MSG_FMT = ugettext(
    'Organization %(name)s is not active in %(program)s.')

DEF_ID_BASED_ENTITY_NOT_EXISTS_MSG_FMT = ugettext(
    '%(model)s entity, whose id is %(id)s, does not exist.')

DEF_ID_BASED_ENTITY_INVALID_MSG_FMT = ugettext(
    '%(model)s entity, whose id is %(id)s, is invalid at this time.')

DEF_ENTITY_DOES_NOT_BELONG_TO_YOU = ugettext(
    'This %(model)s entity does not belong to you.')

DEF_NOT_HOST_MSG = ugettext(
    'You need to be a program adminstrator to access this page.')

DEF_NOT_ADMIN_MSG = ugettext(
    'You need to be a organization administrator for %s to access this page.')

DEF_NOT_MENTOR_MSG = ugettext(
    'You need to be a mentor for %s to access this page.')

DEF_ALREADY_ADMIN_MSG = ugettext(
    'You cannot be a organization administrator for %s to access this page.')

DEF_ALREADY_MENTOR_MSG = ugettext(
    'You cannot be a mentor for %s to access this page.')

DEF_NOT_DEVELOPER_MSG = ugettext(
    'You need to be a site developer to access this page.')

DEF_ALREADY_PARTICIPATING_AS_NON_STUDENT_MSG = ugettext(
    'You cannot register as a student since you are already a '
    'mentor or organization administrator in %s.')

DEF_ALREADY_PARTICIPATING_AS_STUDENT_MSG = ugettext(
    'You cannot register as a %s since you are already a '
    'student in %s.')

DEF_NOT_VALID_INVITATION_MSG = ugettext(
    'This is not a valid invitation.')

DEF_NOT_VALID_REQUEST_MSG = ugettext(
    'This is not a valid request.')

DEF_HAS_ALREADY_ROLE_FOR_ORG_MSG = ugettext(
    'You already have %(role)s role for %(org)s.')

DEF_PROPOSAL_NOT_PUBLIC_MSG = ugettext(
    'This proposal is not made public, '
    'and you are not the student who submitted the proposal, '
    'nor are you a mentor for the organization it was submitted to.')


unset = object()


def isSet(value):
  """Returns true iff value is not unset.
  """
  return value is not unset


class Mutator(object):
  """Helper class for access checking.

  Mutates the data object as requested.
  """

  def __init__(self, data):
    self.data = data
    self.unsetAll()

  def unsetAll(self):
    self.data.organization = unset
    self.data.proposal = unset
    self.data.proposer = unset
    self.data.proposer_user = unset
    self.data.can_respond = unset
    self.data.invite = unset
    self.data.invited_user = unset
    self.data.request_entity = unset
    self.data.requester = unset
    self.data.public_only = unset
    self.data.public_comments_visible = unset
    self.data.private_comments_visible = unset

  def organizationFromKwargs(self):
    # kwargs which defines an organization
    fields = ['sponsor', 'program', 'organization']

    key_name = '/'.join(self.data.kwargs[field] for field in fields)
    self.data.organization = GSoCOrganization.get_by_key_name(key_name)

  def proposalFromKwargs(self):
    id = int(self.data.kwargs['id'])
    self.data.proposal = GSoCProposal.get_by_id(id, parent=self.data.profile)

  def canRespondForUser(self):
    assert isSet(self.data.invited_user)
    assert isSet(self.data.invite)

    if self.data.invited_user.key() != self.data.user.key():
      # org admins may see the invitations and can respond to requests
      self.data.can_respond = self.data.invite.type == 'Request'
    else:
      # user that the entity refers to may only respond if it is a Request
      self.data.can_respond = self.data.invite.type == 'Invitation'

  def commentVisible(self):
    assert isSet(self.data.proposer_user)

    self.data.public_comments_visible = False
    self.data.private_comments_visible = False

    # if the proposal is public, everyone may access it
    if self.data.proposal.is_publicly_visible:<commit_msg>Sort unsetAll method<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.

"""Module containing the AccessChecker class that contains helper functions
for checking access.
"""

__authors__ = [
    '"<NAME>" <<EMAIL>>',
  ]


from django.utils.translation import ugettext

from google.appengine.api import users

from soc.logic.models.user import logic as user_logic

from soc.logic.exceptions import LoginRequest
from soc.logic.exceptions import RedirectRequest
from soc.logic.exceptions import AccessViolation

from soc.modules.gsoc.models.organization import GSoCOrganization
from soc.modules.gsoc.models.proposal import GSoCProposal


DEF_AGREE_TO_TOS_MSG_FMT = ugettext(
    'You must agree to the <a href="%(tos_link)s">site-wide Terms of'
    ' Service</a> in your <a href="/user/edit_profile">User Profile</a>'
    ' in order to view this page.')

DEF_NOT_PARTICIPATING_MSG = ugettext(
    'You are not participating in this program and have no access.')

DEF_ALREADY_PARTICIPATING_MSG = ugettext(
    'You cannot become a Student because you are already participating '
    'in this program.')

DEF_DEV_LOGOUT_LOGIN_MSG_FMT = ugettext(
    'Please <a href="%%(sign_out)s">sign out</a>'
    ' and <a href="%%(sign_in)s">sign in</a>'
    ' again as %(role)s to view this page.')

DEF_NO_USER_LOGIN_MSG = ugettext(
    'Please create <a href="/user/create_profile">User Profile</a>'
    ' in order to view this page.')

DEF_PAGE_INACTIVE_MSG = ugettext(
    'This page is inactive at this time.')

DEF_SCOPE_INACTIVE_MSG = ugettext(
    'The scope for this request is not active.')

DEF_PROGRAM_INACTIVE_MSG_FMT = ugettext(
    'This page is inaccessible because %s is not active at this time.')

DEF_PAGE_INACTIVE_OUTSIDE_MSG_FMT = ugettext(
    'This page is inactive before %s and after %s.')

DEF_PAGE_INACTIVE_BEFORE_MSG_FMT = ugettext(
    'This page is inactive before %s')

DEF_NO_SUCH_PROGRAM_MSG = ugettext(
    'The url is wrong (no program was found).')

DEF_ROLE_INACTIVE_MSG = ugettext(
    'This page is inaccessible because you do not have a profile '
    'in the program at this time.')

DEF_IS_NOT_STUDENT_MSG = ugettext(
    'This page is inaccessible because you do not have a student role '
    'in the program.')

DEF_IS_STUDENT_MSG = ugettext(
    'This page is inaccessible because you are registered as a student.')

DEF_ORG_DOES_NOT_EXISTS_MSG_FMT = ugettext(
    'Organization, whose link_id is %(link_id)s, does not exist in '
    '%(program)s.')

DEF_ORG_NOT_ACTIVE_MSG_FMT = ugettext(
    'Organization %(name)s is not active in %(program)s.')

DEF_ID_BASED_ENTITY_NOT_EXISTS_MSG_FMT = ugettext(
    '%(model)s entity, whose id is %(id)s, does not exist.')

DEF_ID_BASED_ENTITY_INVALID_MSG_FMT = ugettext(
    '%(model)s entity, whose id is %(id)s, is invalid at this time.')

DEF_ENTITY_DOES_NOT_BELONG_TO_YOU = ugettext(
    'This %(model)s entity does not belong to you.')

DEF_NOT_HOST_MSG = ugettext(
    'You need to be a program adminstrator to access this page.')

DEF_NOT_ADMIN_MSG = ugettext(
    'You need to be a organization administrator for %s to access this page.')

DEF_NOT_MENTOR_MSG = ugettext(
    'You need to be a mentor for %s to access this page.')

DEF_ALREADY_ADMIN_MSG = ugettext(
    'You cannot be a organization administrator for %s to access this page.')

DEF_ALREADY_MENTOR_MSG = ugettext(
    'You cannot be a mentor for %s to access this page.')

DEF_NOT_DEVELOPER_MSG = ugettext(
    'You need to be a site developer to access this page.')

DEF_ALREADY_PARTICIPATING_AS_NON_STUDENT_MSG = ugettext(
    'You cannot register as a student since you are already a '
    'mentor or organization administrator in %s.')

DEF_ALREADY_PARTICIPATING_AS_STUDENT_MSG = ugettext(
    'You cannot register as a %s since you are already a '
    'student in %s.')

DEF_NOT_VALID_INVITATION_MSG = ugettext(
    'This is not a valid invitation.')

DEF_NOT_VALID_REQUEST_MSG = ugettext(
    'This is not a valid request.')

DEF_HAS_ALREADY_ROLE_FOR_ORG_MSG = ugettext(
    'You already have %(role)s role for %(org)s.')

DEF_PROPOSAL_NOT_PUBLIC_MSG = ugettext(
    'This proposal is not made public, '
    'and you are not the student who submitted the proposal, '
    'nor are you a mentor for the organization it was submitted to.')


unset = object()


def isSet(value):
  """Returns true iff value is not unset.
  """
  return value is not unset


class Mutator(object):
  """Helper class for access checking.

  Mutates the data object as requested.
  """

  def __init__(self, data):
    self.data = data
    self.unsetAll()

  def unsetAll(self):
    self.data.action = unset
    self.data.can_respond = unset
    self.data.invited_user = unset
    self.data.invite = unset
    self.data.organization = unset
    self.data.private_comments_visible = unset
    self.data.proposal = unset
    self.data.proposer = unset
    self.data.proposer_user = unset
    self.data.public_comments_visible = unset
    self.data.public_only = unset
    self.data.request_entity = unset
    self.data.requester = unset

  def organizationFromKwargs(self):
    # kwargs which defines an organization
    fields = ['sponsor', 'program', 'organization']

    key_name = '/'.join(self.data.kwargs[field] for field in fields)
    self.data.organization = GSoCOrganization.get_by_key_name(key_name)

  def proposalFromKwargs(self):
    id = int(self.data.kwargs['id'])
    self.data.proposal = GSoCProposal.get_by_id(id, parent=self.data.profile)

  def canRespondForUser(self):
    assert isSet(self.data.invited_user)
    assert isSet(self.data.invite)

    if self.data.invited_user.key() != self.data.user.key():
      # org admins may see the invitations and can respond to requests
      self.data.can_respond = self.data.invite.type == 'Request'
    else:
      # user that the entity refers to may only respond if it is a Request
      self.data.can_respond = self.data.invite.type == 'Invitation'

  def commentVisible(self):
    assert isSet(self.data.proposer_user)

    self.data.public_comments_visible = False
    self.data.private_comments_visible = False

    # if the proposal is public, everyone may access it
    if self.data.proposal.is_publicly_visible:
{'fraction_non_alphanumeric': 0.08985357195681112, 'fraction_numerical': 0.0014790711433219938, 'mean_word_length': 3.9900369003690037, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '915947', 'n_tokens_mistral': 4537, 'n_tokens_neox': 4476, 'n_words': 1413}