# Copyright 2012 Google Inc. All Rights Reserved.
#
# 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.

"""Courses module."""

__author__ = 'Pavel Simakov (psimakov@google.com)'

from common import resource
from controllers import assessments
from controllers import lessons
from controllers import utils
from models import content
from models import resources_display
from models import custom_modules
from models import roles
from tools import verify


All_LOCALES_PERMISSION = 'can_pick_all_locales'
All_LOCALES_DESCRIPTION = 'Can pick all locales, including unavailable ones.'

SEE_DRAFTS_PERMISSION = 'can_see_draft_content'
SEE_DRAFTS_DESCRIPTION = 'Can see lessons and assessments with draft status.'


custom_module = None


def can_pick_all_locales(app_context):
    return roles.Roles.is_user_allowed(
        app_context, custom_module, All_LOCALES_PERMISSION)


def can_see_drafts(app_context):
    return roles.Roles.is_user_allowed(
        app_context, custom_module, SEE_DRAFTS_PERMISSION)


def register_module():
    """Registers this module in the registry."""

    def on_module_enabled():
        roles.Roles.register_permissions(custom_module, permissions_callback)
        resource.Registry.register(resources_display.ResourceCourseSettings)
        resource.Registry.register(resources_display.ResourceUnit)
        resource.Registry.register(resources_display.ResourceAssessment)
        resource.Registry.register(resources_display.ResourceLink)
        resource.Registry.register(resources_display.ResourceLesson)
        resource.Registry.register(utils.ResourceHtmlHook)

    def permissions_callback(unused_app_context):
        return [
            roles.Permission(All_LOCALES_PERMISSION, All_LOCALES_DESCRIPTION),
            roles.Permission(SEE_DRAFTS_PERMISSION, SEE_DRAFTS_DESCRIPTION)
        ]

    # provide parser to verify
    verify.parse_content = content.parse_string_in_scope

    # setup routes
    courses_routes = [
        ('/', lessons.CourseHandler),
        ('/activity', lessons.UnitHandler),
        ('/answer', assessments.AnswerHandler),
        ('/assessment', lessons.AssessmentHandler),
        ('/course', lessons.CourseHandler),
        ('/forum', utils.ForumHandler),
        ('/preview', utils.PreviewHandler),
        ('/register', utils.RegisterHandler),
        ('/resources', utils.ResourcesHandler),
        ('/rest/locale', utils.StudentLocaleRESTHandler),
        ('/review', lessons.ReviewHandler),
        ('/reviewdashboard', lessons.ReviewDashboardHandler),
        ('/student/editstudent', utils.StudentEditStudentHandler),
        ('/student/settracks', utils.StudentSetTracksHandler),
        ('/student/home', utils.StudentProfileHandler),
        ('/student/unenroll', utils.StudentUnenrollHandler),
        ('/unit', lessons.UnitHandler)]

    global custom_module  # pylint: disable=global-statement
    custom_module = custom_modules.Module(
        'Course',
        'A set of pages for delivering an online course.',
        [], courses_routes,
        notify_module_enabled=on_module_enabled)
    return custom_module

[((41, 11, 42, 59), 'models.roles.Roles.is_user_allowed', 'roles.Roles.is_user_allowed', ({(42, 8, 42, 19): 'app_context', (42, 21, 42, 34): 'custom_module', (42, 36, 42, 58): 'All_LOCALES_PERMISSION'}, {}), '(app_context, custom_module, All_LOCALES_PERMISSION)', False, 'from models import roles\n'), ((46, 11, 47, 58), 'models.roles.Roles.is_user_allowed', 'roles.Roles.is_user_allowed', ({(47, 8, 47, 19): 'app_context', (47, 21, 47, 34): 'custom_module', (47, 36, 47, 57): 'SEE_DRAFTS_PERMISSION'}, {}), '(app_context, custom_module, SEE_DRAFTS_PERMISSION)', False, 'from models import roles\n'), ((92, 20, 96, 48), 'models.custom_modules.Module', 'custom_modules.Module', (), '', False, 'from models import custom_modules\n'), ((54, 8, 54, 77), 'models.roles.Roles.register_permissions', 'roles.Roles.register_permissions', ({(54, 41, 54, 54): 'custom_module', (54, 56, 54, 76): 'permissions_callback'}, {}), '(custom_module, permissions_callback)', False, 'from models import roles\n'), ((55, 8, 55, 76), 'common.resource.Registry.register', 'resource.Registry.register', ({(55, 35, 55, 75): 'resources_display.ResourceCourseSettings'}, {}), '(resources_display.ResourceCourseSettings)', False, 'from common import resource\n'), ((56, 8, 56, 66), 'common.resource.Registry.register', 'resource.Registry.register', ({(56, 35, 56, 65): 'resources_display.ResourceUnit'}, {}), '(resources_display.ResourceUnit)', False, 'from common import resource\n'), ((57, 8, 57, 72), 'common.resource.Registry.register', 'resource.Registry.register', ({(57, 35, 57, 71): 'resources_display.ResourceAssessment'}, {}), '(resources_display.ResourceAssessment)', False, 'from common import resource\n'), ((58, 8, 58, 66), 'common.resource.Registry.register', 'resource.Registry.register', ({(58, 35, 58, 65): 'resources_display.ResourceLink'}, {}), '(resources_display.ResourceLink)', False, 'from common import resource\n'), ((59, 8, 59, 68), 'common.resource.Registry.register', 'resource.Registry.register', ({(59, 35, 59, 67): 'resources_display.ResourceLesson'}, {}), '(resources_display.ResourceLesson)', False, 'from common import resource\n'), ((60, 8, 60, 58), 'common.resource.Registry.register', 'resource.Registry.register', ({(60, 35, 60, 57): 'utils.ResourceHtmlHook'}, {}), '(utils.ResourceHtmlHook)', False, 'from common import resource\n'), ((64, 12, 64, 77), 'models.roles.Permission', 'roles.Permission', ({(64, 29, 64, 51): 'All_LOCALES_PERMISSION', (64, 53, 64, 76): 'All_LOCALES_DESCRIPTION'}, {}), '(All_LOCALES_PERMISSION, All_LOCALES_DESCRIPTION)', False, 'from models import roles\n'), ((65, 12, 65, 75), 'models.roles.Permission', 'roles.Permission', ({(65, 29, 65, 50): 'SEE_DRAFTS_PERMISSION', (65, 52, 65, 74): 'SEE_DRAFTS_DESCRIPTION'}, {}), '(SEE_DRAFTS_PERMISSION, SEE_DRAFTS_DESCRIPTION)', False, 'from models import roles\n')]