# 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

[((1277, 1356), 'models.roles.Roles.is_user_allowed', 'roles.Roles.is_user_allowed', (['app_context', 'custom_module', 'All_LOCALES_PERMISSION'], {}), '(app_context, custom_module, All_LOCALES_PERMISSION)\n', (1304, 1356), False, 'from models import roles\n'), ((1412, 1490), 'models.roles.Roles.is_user_allowed', 'roles.Roles.is_user_allowed', (['app_context', 'custom_module', 'SEE_DRAFTS_PERMISSION'], {}), '(app_context, custom_module, SEE_DRAFTS_PERMISSION)\n', (1439, 1490), False, 'from models import roles\n'), ((3396, 3547), 'models.custom_modules.Module', 'custom_modules.Module', (['"""Course"""', '"""A set of pages for delivering an online course."""', '[]', 'courses_routes'], {'notify_module_enabled': 'on_module_enabled'}), "('Course',\n    'A set of pages for delivering an online course.', [], courses_routes,\n    notify_module_enabled=on_module_enabled)\n", (3417, 3547), False, 'from models import custom_modules\n'), ((1612, 1681), 'models.roles.Roles.register_permissions', 'roles.Roles.register_permissions', (['custom_module', 'permissions_callback'], {}), '(custom_module, permissions_callback)\n', (1644, 1681), False, 'from models import roles\n'), ((1690, 1758), 'common.resource.Registry.register', 'resource.Registry.register', (['resources_display.ResourceCourseSettings'], {}), '(resources_display.ResourceCourseSettings)\n', (1716, 1758), False, 'from common import resource\n'), ((1767, 1825), 'common.resource.Registry.register', 'resource.Registry.register', (['resources_display.ResourceUnit'], {}), '(resources_display.ResourceUnit)\n', (1793, 1825), False, 'from common import resource\n'), ((1834, 1898), 'common.resource.Registry.register', 'resource.Registry.register', (['resources_display.ResourceAssessment'], {}), '(resources_display.ResourceAssessment)\n', (1860, 1898), False, 'from common import resource\n'), ((1907, 1965), 'common.resource.Registry.register', 'resource.Registry.register', (['resources_display.ResourceLink'], {}), '(resources_display.ResourceLink)\n', (1933, 1965), False, 'from common import resource\n'), ((1974, 2034), 'common.resource.Registry.register', 'resource.Registry.register', (['resources_display.ResourceLesson'], {}), '(resources_display.ResourceLesson)\n', (2000, 2034), False, 'from common import resource\n'), ((2043, 2093), 'common.resource.Registry.register', 'resource.Registry.register', (['utils.ResourceHtmlHook'], {}), '(utils.ResourceHtmlHook)\n', (2069, 2093), False, 'from common import resource\n'), ((2174, 2239), 'models.roles.Permission', 'roles.Permission', (['All_LOCALES_PERMISSION', 'All_LOCALES_DESCRIPTION'], {}), '(All_LOCALES_PERMISSION, All_LOCALES_DESCRIPTION)\n', (2190, 2239), False, 'from models import roles\n'), ((2253, 2316), 'models.roles.Permission', 'roles.Permission', (['SEE_DRAFTS_PERMISSION', 'SEE_DRAFTS_DESCRIPTION'], {}), '(SEE_DRAFTS_PERMISSION, SEE_DRAFTS_DESCRIPTION)\n', (2269, 2316), False, 'from models import roles\n')]