#!/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__ = [
    '"Selwyn Jacob" <selwynjacob90@gmail.com>',
  ]


from django.utils.translation import ugettext

from soc.logic.exceptions import AccessViolation
from soc.logic.exceptions import NotFound
from soc.views.helper import access_checker

from soc.modules.gci.models.task import GCITask


class Mutator(access_checker.Mutator):

  def unsetAll(self):
    self.data.task = unset
    super(access_checker.Mutator, self).unsetAll()

  
  def getOrgKeyForKeyName(key_name):
     return db.Key.from_path('GCIOrganization', key_name)
  
  def taskFromKwargs(self):
    """Sets the task entity in RequestData object.
    """
    self.profileFromKwargs()
    assert isSet(self.data.url_profile)

    # kwargs which defines a task
    fields = ['sponsor', 'program', 'organization', 'task_link_id']

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

    if not self.data.task:
      error_msg = access.checker.DEF_KEYNAME_BASED_ENTITY_NOT_EXISTS_MSG_FMT % {
          'model': 'GCITask',
          'key_name': key_name
          }
      raise NotFound(error_msg)


class DeveloperMutator(access_checker.DeveloperMutator, Mutator):
  pass


class AccessChecker(access_checker.AccessChecker):
  
  def isTaskInURLValid(self):
    """Checks if the task in URL exists.
    """
    assert isSet(self.data.task)

    fields = ['sponsor', 'program', 'organization', 'task_link_id']
    key_name = '/'.join(self.data.kwargs[field] for field in fields)
    if not self.data.task:
      error_msg = DEF_KEYNAME_BASED_ENTITY_NOT_EXISTS_MSG_FMT % {
          'model': 'GCITask',
          'key_name': key_name
          }
      raise AccessViolation(error_msg)

    invalid_status = ['Invalid', 'Unpublished', 'Unapproved']
    if self.data.task.status in invalid_status:
      error_msg = DEF_KEYNAME_BASED_ENTITY_INVALID_MSG_FMT % {
          'model': 'GCITask',
          'key_name': key_name,
          }
      raise AccessViolation(error_msg)

class DeveloperAccessChecker(access_checker.DeveloperAccessChecker, AccessChecker):
  pass
@@ -778,54 +778,43 @@
  %5D%0A%0A
-%0A
 from 
-django.utils.translation import ugettext
+google.appengine.ext import db
 %0A%0Afr
@@ -1080,16 +1080,31 @@
 .task = 
+access_checker.
 unset%0A  
@@ -1111,31 +1111,16 @@
   super(
-access_checker.
 Mutator,
@@ -1138,19 +1138,16 @@
 tAll()%0A%0A
-  %0A
   def ge
@@ -1160,24 +1160,30 @@
 yForKeyName(
+self, 
 key_name):%0A 
@@ -1181,17 +1181,16 @@
 _name):%0A
- 
     retu
@@ -1238,18 +1238,16 @@
 y_name)%0A
-  
 %0A  def t
@@ -1358,32 +1358,47 @@
 gs()%0A    assert 
+access_checker.
 isSet(self.data.
@@ -1690,17 +1690,17 @@
 = access
-.
+_
 checker.
@@ -1909,16 +1909,39 @@
 Mutator,
+%0A                      
  Mutator
@@ -2096,16 +2096,31 @@
  assert 
+access_checker.
 isSet(se
@@ -2308,32 +2308,47 @@
     error_msg = 
+access_checker.
 DEF_KEYNAME_BASE
@@ -2620,16 +2620,31 @@
 r_msg = 
+access_checker.
 DEF_KEYN
@@ -2861,16 +2861,45 @@
 Checker,
+%0A                            
  AccessC
@@ -2905,16 +2905,17 @@
 Checker):%0A  pass
+%0A

Fix errors in the GCI access checker module.