views.py 3.92 KB
Newer Older
1 2
import logging
import re
3

4 5 6
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.exceptions import ImproperlyConfigured
7
from django.shortcuts import redirect
8 9 10
from wiki.core.exceptions import NoRootURL
from wiki.models import URLPath, Article

11
from courseware.courses import get_course_by_id
12

13 14
log = logging.getLogger(__name__)

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
def root_create(request):
    """
    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)


def course_wiki_redirect(request, course_id):
    """
    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.
    """
30
    course = get_course_by_id(course_id)
31
    
32
    course_slug = course.wiki_slug
33
    
34 35 36 37 38 39 40
    valid_slug = True
    if not course_slug:
        log.exception("This course is improperly configured. The slug cannot be empty.")
        valid_slug = False
    if re.match('^[-\w\.]+$', course_slug) == None:
        log.exception("This course is improperly configured. The slug can only contain letters, numbers, periods or hyphens.")
        valid_slug = False
41

42 43
    if not valid_slug:
        return redirect("wiki:get", path="")
44 45 46 47 48 49 50 51 52 53 54 55 56
    
    
    # The wiki needs a Site object created. We make sure it exists here
    try:
        site = Site.objects.get_current()
    except Site.DoesNotExist:
        new_site = Site()
        new_site.domain = settings.SITE_NAME
        new_site.name = "edX"
        new_site.save()
        if str(new_site.id) != str(settings.SITE_ID):
            raise ImproperlyConfigured("No site object was created and the SITE_ID doesn't match the newly created one. " + str(new_site.id) + "!=" + str(settings.SITE_ID))
    
57
    try:
58
        urlpath = URLPath.get_by_path(course_slug, select_related=True)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        
        results = list( Article.objects.filter( id = urlpath.article.id ) )
        if results:
            article = results[0]
        else:
            article = None
    
    except (NoRootURL, URLPath.DoesNotExist):
        # We will create it in the next block
        urlpath = None
        article = None
        
    if not article:
        # create it
        root = get_or_create_root()
        
        if urlpath:
            # Somehow we got a urlpath without an article. Just delete it and
            # recerate it.
            urlpath.delete()
        
        urlpath = URLPath.create_article(
            root,
82
            course_slug,
83
            title=course_slug,
84
            content="This is the wiki for **{0}**'s _{1}_.".format(course.org, course.title),
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            user_message="Course page automatically created.",
            user=None,
            ip_address=None,
            article_kwargs={'owner': None,
                            'group': None,
                            'group_read': True,
                            'group_write': True,
                            'other_read': True,
                            'other_write': True,
                            })
        
    return redirect("wiki:get", path=urlpath.path)
    

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
    
    starting_content = "\n".join((
    "Welcome to the edX Wiki",
    "===",
    "Visit a course wiki to add an article."))
    
117
    root = URLPath.create_root(title="Wiki",
118
                        content=starting_content)
119 120 121 122 123 124 125 126
    article = root.article
    article.group = None
    article.group_read = True
    article.group_write = False
    article.other_read = True
    article.other_write = False
    article.save()
    
127 128
    return root