views.py 4.21 KB
Newer Older
1 2 3
"""
This file contains view functions for wrapping the django-wiki.
"""
4
import cgi
5 6
import logging
import re
7

8
from django.conf import settings
9
from django.shortcuts import redirect
10
from django.utils.translation import ugettext as _
11
from opaque_keys.edx.keys import CourseKey
12
from wiki.core.exceptions import NoRootURL
13
from wiki.models import Article, URLPath
14

15
from course_wiki.utils import course_wiki_slug
16
from courseware.courses import get_course_by_id
17
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
18
from openedx.features.enterprise_support.api import data_sharing_consent_required
19

20 21
log = logging.getLogger(__name__)

Calen Pennington committed
22

23
def root_create(request):  # pylint: disable=unused-argument
24 25 26 27 28 29 30 31
    """
    In the edX wiki, we don't show the root_create view. Instead, we
    just create the root automatically if it doesn't exist.
    """
    root = get_or_create_root()
    return redirect('wiki:get', path=root.path)


32 33
@data_sharing_consent_required
def course_wiki_redirect(request, course_id, wiki_path=""):  # pylint: disable=unused-argument
34 35 36 37 38
    """
    This redirects to whatever page on the wiki that the course designates
    as it's home page. A course's wiki must be an article on the root (for
    example, "/6.002x") to keep things simple.
    """
39
    course = get_course_by_id(CourseKey.from_string(course_id))
40
    course_slug = course_wiki_slug(course)
Calen Pennington committed
41

42 43 44 45
    valid_slug = True
    if not course_slug:
        log.exception("This course is improperly configured. The slug cannot be empty.")
        valid_slug = False
46
    if re.match(r'^[-\w\.]+$', course_slug) is None:
47 48
        log.exception("This course is improperly configured. The slug can only contain letters, numbers, periods or hyphens.")
        valid_slug = False
49

50 51
    if not valid_slug:
        return redirect("wiki:get", path="")
Calen Pennington committed
52

53
    try:
54
        urlpath = URLPath.get_by_path(wiki_path or course_slug, select_related=True)
Calen Pennington committed
55 56

        results = list(Article.objects.filter(id=urlpath.article.id))
57 58 59 60
        if results:
            article = results[0]
        else:
            article = None
Calen Pennington committed
61

62 63 64 65
    except (NoRootURL, URLPath.DoesNotExist):
        # We will create it in the next block
        urlpath = None
        article = None
Calen Pennington committed
66

67 68 69
    if not article:
        # create it
        root = get_or_create_root()
Calen Pennington committed
70

71 72 73 74
        if urlpath:
            # Somehow we got a urlpath without an article. Just delete it and
            # recerate it.
            urlpath.delete()
Calen Pennington committed
75

76 77 78 79
        content = cgi.escape(
            # Translators: this string includes wiki markup.  Leave the ** and the _ alone.
            _("This is the wiki for **{organization}**'s _{course_name}_.").format(
                organization=course.display_org_with_default,
80
                course_name=course.display_name_with_default_escaped,
81 82
            )
        )
83 84
        urlpath = URLPath.create_article(
            root,
85
            course_slug,
86
            title=course_slug,
87 88
            content=content,
            user_message=_("Course page automatically created."),
89 90 91 92 93 94 95 96 97
            user=None,
            ip_address=None,
            article_kwargs={'owner': None,
                            'group': None,
                            'group_read': True,
                            'group_write': True,
                            'other_read': True,
                            'other_write': True,
                            })
Calen Pennington committed
98

99
    return redirect("wiki:get", path=urlpath.path)
Calen Pennington committed
100

101 102 103 104 105 106 107 108 109 110 111 112 113

def get_or_create_root():
    """
    Returns the root article, or creates it if it doesn't exist.
    """
    try:
        root = URLPath.root()
        if not root.article:
            root.delete()
            raise NoRootURL
        return root
    except NoRootURL:
        pass
Calen Pennington committed
114

115
    starting_content = "\n".join((
116 117 118
        _("Welcome to the {platform_name} Wiki").format(
            platform_name=configuration_helpers.get_value('PLATFORM_NAME', settings.PLATFORM_NAME),
        ),
119 120 121
        "===",
        _("Visit a course wiki to add an article."),
    ))
Calen Pennington committed
122

123
    root = URLPath.create_root(title=_("Wiki"), content=starting_content)
124 125 126 127 128 129 130
    article = root.article
    article.group = None
    article.group_read = True
    article.group_write = False
    article.other_read = True
    article.other_write = False
    article.save()
Calen Pennington committed
131

132
    return root