export_git.py 1.65 KB
Newer Older
Carson Gee committed
1 2 3 4 5
"""
This views handles exporting the course xml to a git repository if
the giturl attribute is set.
"""

6 7 8
import logging

from django.contrib.auth.decorators import login_required
Carson Gee committed
9 10 11
from django.core.exceptions import PermissionDenied
from django_future.csrf import ensure_csrf_cookie
from django.utils.translation import ugettext as _
12

13 14
from .access import has_course_access
import contentstore.git_export_utils as git_export_utils
Carson Gee committed
15
from edxmako.shortcuts import render_to_response
16
from xmodule.modulestore.django import modulestore
17
from opaque_keys.edx.keys import CourseKey
18 19 20

log = logging.getLogger(__name__)

Carson Gee committed
21

22 23
@ensure_csrf_cookie
@login_required
24
def export_git(request, course_key_string):
25
    """
26
    This method serves up the 'Export to Git' page
27
    """
28 29
    course_key = CourseKey.from_string(course_key_string)
    if not has_course_access(request.user, course_key):
Carson Gee committed
30
        raise PermissionDenied()
31 32

    course_module = modulestore().get_course(course_key)
Carson Gee committed
33
    failed = False
34

35
    log.debug('export_git course_module=%s', course_module)
36 37

    msg = ""
Carson Gee committed
38 39 40
    if 'action' in request.GET and course_module.giturl:
        if request.GET['action'] == 'push':
            try:
41
                git_export_utils.export_to_git(
Carson Gee committed
42 43 44 45 46
                    course_module.id,
                    course_module.giturl,
                    request.user,
                )
                msg = _('Course successfully exported to git repository')
47
            except git_export_utils.GitExportError as ex:
Carson Gee committed
48
                failed = True
49
                msg = unicode(ex)
50

51
    return render_to_response('export_git.html', {
52 53
        'context_course': course_module,
        'msg': msg,
Carson Gee committed
54
        'failed': failed,
55
    })