Beispiel #1
0
def process_course_media(request, media_element, course, user):
    for file_element in media_element.findall('file'):
        media = Media()
        media.course = course
        media.filename = file_element.get("filename")
        url = file_element.get("download_url")
        media.digest = file_element.get("digest")

        if len(url) > Media.URL_MAX_LENGTH:
            msg_text = _(u'File %(filename)s has a download URL larger \
                        than the maximum length permitted. The media file \
                        has not been registered, so it won\'t be tracked. \
                        Please, fix this issue and upload the course \
                        again.') % {'filename': media.filename}
            messages.info(request, msg_text)
            CoursePublishingLog(course=course,
                                user=user,
                                action="media_url_too_long",
                                data=msg_text).save()
        else:
            media.download_url = url
            # get any optional attributes
            for attr_name, attr_value in file_element.attrib.items():
                if attr_name == "length":
                    media.media_length = attr_value
                if attr_name == "filesize":
                    media.filesize = attr_value

            media.save()
            # save gamification events
            gamification = file_element.find('gamification')
            events = parse_gamification_events(gamification)

            process_course_media_events(request, media, events, course, user)
def parse_course_contents(req, xml_doc, course, user, new_course, process_quizzes_locally):
    # add in any baseline activities
    for meta in xml_doc.getElementsByTagName("meta")[:1]:
        if meta.getElementsByTagName("activity").length > 0:
            section = Section(
                course=course,
                title='{"en": "Baseline"}',
                order=0
            )
            section.save()
            for act in meta.getElementsByTagName("activity"):
                parse_and_save_activity(req, user, section, act, new_course, process_quizzes_locally, is_baseline=True)

    # add all the sections and activities
    for structure in xml_doc.getElementsByTagName("structure")[:1]:

        if structure.getElementsByTagName("section").length == 0:
            course.delete()
            messages.info(req, _("There don't appear to be any activities in this upload file."))
            return

        for idx, s in enumerate(structure.getElementsByTagName("section")):

            # Check if the section contains any activity (to avoid saving an empty one)
            activities = s.getElementsByTagName("activities")[:1]
            if not activities or activities[0].getElementsByTagName("activity").length == 0:
                messages.info(req, _("Section ") + str(idx + 1) + _(" does not contain any activities."))
                continue

            title = {}
            for t in s.childNodes:
                if t.nodeName == 'title':
                    title[t.getAttribute('lang')] = t.firstChild.nodeValue
            section = Section(
                course=course,
                title=json.dumps(title),
                order=s.getAttribute("order")
            )
            section.save()

            for activities in s.getElementsByTagName("activities")[:1]:
                for act in activities.getElementsByTagName("activity"):
                    parse_and_save_activity(req, user, section, act, new_course, process_quizzes_locally)

    media_element_list = [node for node in xml_doc.firstChild.childNodes if node.nodeName == 'media']
    media_element = None
    if len(media_element_list) > 0:
        media_element = media_element_list[0]

    if media_element is not None:
        for file_element in media_element.childNodes:
            if file_element.nodeName == 'file':
                media = Media()
                media.course = course
                media.filename = file_element.getAttribute("filename")
                url = file_element.getAttribute("download_url")
                media.digest = file_element.getAttribute("digest")

                if len(url) > Media.URL_MAX_LENGTH:
                    print(url)
                    messages.info(req, _('File %(filename)s has a download URL larger than the maximum length permitted. The media file has not been registered, so it won\'t be tracked. Please, fix this issue and upload the course again.') % {'filename': media.filename})
                else:
                    media.download_url = url
                    # get any optional attributes
                    for attr_name, attr_value in file_element.attributes.items():
                        if attr_name == "length":
                            media.media_length = attr_value
                        if attr_name == "filesize":
                            media.filesize = attr_value

                    media.save()
                    # save gamification events
                    if file_element.getElementsByTagName('gamification')[:1]:
                        events = parse_gamification_events(file_element.getElementsByTagName('gamification')[0])
                        # remove anything existing for this course
                        MediaGamificationEvent.objects.filter(media=media).delete()
                        # add new
                        for event in events:
                            e = MediaGamificationEvent(user=user, media=media, event=event['name'], points=event['points'])
                            e.save()
Beispiel #3
0
def handle_uploaded_file(f, extract_path, request, user):
    zipfilepath = settings.COURSE_UPLOAD_DIR + f.name

    with open(zipfilepath, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

    zip = zipfile.ZipFile(zipfilepath)
    zip.extractall(path=extract_path)

    mod_name = ''
    for dir in os.listdir(extract_path)[:1]:
        mod_name = dir

    # check there is at least a sub dir
    if mod_name == '':
        messages.info(request, _("Invalid course zip file"))
        return False

    # check that the
    if not os.path.isfile(os.path.join(extract_path, mod_name, "module.xml")):
        messages.info(request,
                      _("Zip file does not contain a module.xml file"))
        return False

    # parse the module.xml file
    print extract_path
    print mod_name

    doc = xml.dom.minidom.parse(
        os.path.join(extract_path, mod_name, "module.xml"))
    for meta in doc.getElementsByTagName("meta")[:1]:
        versionid = 0
        for v in meta.getElementsByTagName("versionid")[:1]:
            versionid = int(v.firstChild.nodeValue)
        temp_title = {}
        for t in meta.childNodes:
            if t.nodeName == "title":
                temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
        title = json.dumps(temp_title)

        temp_description = {}
        for t in meta.childNodes:
            if t.nodeName == "description":
                if t.firstChild is not None:
                    temp_description[t.getAttribute(
                        'lang')] = t.firstChild.nodeValue
                else:
                    temp_description[t.getAttribute('lang')] = None
        description = json.dumps(temp_description)

        shortname = ''
        for sn in meta.getElementsByTagName("shortname")[:1]:
            shortname = sn.firstChild.nodeValue

    old_course_filename = None
    # Find if course already exists
    try:

        print shortname

        course = Course.objects.get(shortname=shortname)
        old_course_filename = course.filename

        # check that the current user is allowed to wipe out the other course
        if course.user != user:
            messages.info(
                request,
                _("Sorry, only the original owner may update this course"))
            return False

        # check if course version is older
        if course.version > versionid:
            messages.info(request,
                          _("A newer version of this course already exists"))
            return False
        # wipe out the old sections/activities/media
        oldsections = Section.objects.filter(course=course)
        oldsections.delete()
        oldmedia = Media.objects.filter(course=course)
        oldmedia.delete()

        course.shortname = shortname
        course.title = title
        course.description = description
        course.version = versionid
        course.user = user
        course.filename = f.name
        course.lastupdated_date = timezone.now()
        course.save()
    except Course.DoesNotExist:
        course = Course()
        course.shortname = shortname
        course.title = title
        course.description = description
        course.version = versionid
        course.user = user
        course.filename = f.name
        course.is_draft = True
        course.save()

    # add in any baseline activities
    for meta in doc.getElementsByTagName("meta")[:1]:
        if meta.getElementsByTagName("activity").length > 0:
            section = Section()
            section.course = course
            section.title = '{"en": "Baseline"}'
            section.order = 0
            section.save()
            for a in meta.getElementsByTagName("activity"):
                parse_and_save_activity(section, a, True)

    # add all the sections
    for structure in doc.getElementsByTagName("structure")[:1]:

        if structure.getElementsByTagName("section").length == 0:
            messages.info(
                request,
                _("There don't appear to be any activities in this upload file."
                  ))
            course.delete()
            return False

        for s in structure.getElementsByTagName("section"):
            temp_title = {}
            for t in s.childNodes:
                if t.nodeName == 'title':
                    temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
            title = json.dumps(temp_title)
            section = Section()
            section.course = course
            section.title = title
            section.order = s.getAttribute("order")
            section.save()

            # add all the activities
            for activities in s.getElementsByTagName("activities")[:1]:
                for a in activities.getElementsByTagName("activity"):
                    parse_and_save_activity(section, a, False)

    # add all the media
    for file in doc.lastChild.lastChild.childNodes:
        if file.nodeName == 'file':
            media = Media()
            media.course = course
            media.filename = file.getAttribute("filename")
            media.download_url = file.getAttribute("download_url")
            media.digest = file.getAttribute("digest")

            # get any optional attributes
            for attrName, attrValue in file.attributes.items():
                if attrName == "length":
                    media.media_length = attrValue
                if attrName == "filesize":
                    media.filesize = attrValue

            media.save()

    if old_course_filename is not None and old_course_filename != course.filename:
        try:
            os.remove(settings.COURSE_UPLOAD_DIR + old_course_filename)
        except OSError:
            pass

    # Extract the final file into the courses area for preview
    zipfilepath = settings.COURSE_UPLOAD_DIR + f.name

    with open(zipfilepath, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

    zip = zipfile.ZipFile(zipfilepath)
    course_preview_path = settings.MEDIA_ROOT + "courses/"
    zip.extractall(path=course_preview_path)

    # remove the temp upload files
    shutil.rmtree(extract_path, ignore_errors=True)

    return course
def handle_uploaded_file(f, extract_path, request):
    zipfilepath = settings.COURSE_UPLOAD_DIR + f.name
    
    with open(zipfilepath, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)
            
    zip = zipfile.ZipFile(zipfilepath)
    zip.extractall(path=extract_path)      
    
    mod_name = ''
    for dir in os.listdir(extract_path)[:1]:
        mod_name = dir
       
    # check there is at least a sub dir 
    if mod_name == '':
        messages.info(request,_("Invalid course zip file"))
        return False
    
    # check that the 
    if not os.path.isfile(extract_path + mod_name + "/module.xml"):
        messages.info(request,_("Zip file does not contain a module.xml file"))
        return False
      
    # parse the module.xml file
    doc = xml.dom.minidom.parse(extract_path + mod_name + "/module.xml") 
    for meta in doc.getElementsByTagName("meta")[:1]:
        versionid = 0
        for v in meta.getElementsByTagName("versionid")[:1]:
            versionid = int(v.firstChild.nodeValue)
        temp_title = {}
        for t in meta.childNodes:
            if t.nodeName == "title":
                temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
        title = json.dumps(temp_title)
        
        temp_description = {}
        for t in meta.childNodes:
            if t.nodeName == "description":
                if t.firstChild is not None:
                    temp_description[t.getAttribute('lang')] = t.firstChild.nodeValue
                else:
                    temp_description[t.getAttribute('lang')] = None
        description = json.dumps(temp_description)
        
        shortname = ''
        for sn in meta.getElementsByTagName("shortname")[:1]:
            shortname = sn.firstChild.nodeValue
    
    old_course_filename = None
    # Find if course already exists
    try: 
        course = Course.objects.get(shortname = shortname)
        old_course_filename = course.filename
        old_course_version = course.version
         
        # check that the current user is allowed to wipe out the other course
        if course.user != request.user:
            messages.info(request,_("Sorry, only the original owner may update this course"))
            return False
        
        # check if course version is older
        if course.version > versionid:
            messages.info(request,_("A newer version of this course already exists"))
            return False
        # wipe out the old sections/activities/media
        oldsections = Section.objects.filter(course = course)
        oldsections.delete()
        oldmedia = Media.objects.filter(course = course)
        oldmedia.delete()
        
        course.shortname = shortname
        course.title = title
        course.description = description
        course.version = versionid
        course.user = request.user
        course.filename = f.name
        course.lastupdated_date = datetime.datetime.now()
        course.save()
    except Course.DoesNotExist:
        course = Course()
        course.shortname = shortname
        course.title = title
        course.description = description
        course.version = versionid
        course.user = request.user
        course.filename = f.name
        course.is_draft = True
        course.save()
    
    
            
    # add in any baseline activities
    for meta in doc.getElementsByTagName("meta")[:1]:
        if meta.getElementsByTagName("activity").length > 0:
            section = Section()
            section.course = course
            section.title = '{"en": "Baseline"}'
            section.order = 0
            section.save()
            for a in meta.getElementsByTagName("activity"):
                activity_create(section, a,True)
            
       
    # add all the sections
    for structure in doc.getElementsByTagName("structure")[:1]:
        
        if structure.getElementsByTagName("section").length == 0:
            messages.info(request,_("There don't appear to be any activities in this upload file."))
            course.delete()
            return False
        
        for s in structure.getElementsByTagName("section"):
            temp_title = {}
            for t in s.childNodes:
                if t.nodeName == 'title':
                    temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
            title = json.dumps(temp_title)
            section = Section()
            section.course = course
            section.title = title
            section.order = s.getAttribute("order")
            section.save()
            
            # add all the activities
            for activities in s.getElementsByTagName("activities")[:1]:
                for a in activities.getElementsByTagName("activity"):
                    activity_create(section, a,False)
                    
    # add all the media
    for file in doc.lastChild.lastChild.childNodes:
        if file.nodeName == 'file':
            media = Media()
            media.course = course
            media.filename = file.getAttribute("filename")
            media.download_url = file.getAttribute("download_url")
            media.digest = file.getAttribute("digest")
            
            # get any optional attributes
            for attrName, attrValue in file.attributes.items():
                if attrName == "length":
                    media.media_length = attrValue
                if attrName == "filesize":
                    media.filesize = attrValue

            media.save()
    
    if old_course_filename is not None and old_course_filename != course.filename:
        os.remove(settings.COURSE_UPLOAD_DIR + old_course_filename)
    
    #Extract the final file into the courses area for preview
    zipfilepath = settings.COURSE_UPLOAD_DIR + f.name
    
    with open(zipfilepath, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)
            
    zip = zipfile.ZipFile(zipfilepath)
    course_preview_path = settings.MEDIA_ROOT + "courses/"
    zip.extractall(path=course_preview_path)      
    
        
    return course       
Beispiel #5
0
def parse_course_contents(req, xml_doc, course, user, new_course,
                          process_quizzes_locally):
    # add in any baseline activities
    for meta in xml_doc.findall('meta')[:1]:
        activities = meta.findall("activity")
        if len(activities) > 0:
            section = Section(course=course,
                              title='{"en": "Baseline"}',
                              order=0)
            section.save()
            for act in activities:
                parse_and_save_activity(req,
                                        user,
                                        section,
                                        act,
                                        new_course,
                                        process_quizzes_locally,
                                        is_baseline=True)

    # add all the sections and activities
    structure = xml_doc.find("structure")
    if len(structure.findall("section")) == 0:
        course.delete()
        messages.info(
            req,
            _("There don't appear to be any activities in this upload file."))
        return

    for idx, s in enumerate(structure.findall("section")):

        activities = s.find('activities')
        # Check if the section contains any activity (to avoid saving an empty one)
        if activities is None or len(activities.findall('activity')) == 0:
            messages.info(
                req,
                _("Section ") + str(idx + 1) +
                _(" does not contain any activities."))
            continue

        title = {}
        for t in s.findall('title'):
            title[t.get('lang')] = t.text

        section = Section(course=course,
                          title=json.dumps(title),
                          order=s.get('order'))
        section.save()

        for act in activities.findall("activity"):
            parse_and_save_activity(req, user, section, act, new_course,
                                    process_quizzes_locally)

    media_element = xml_doc.find('media')
    if media_element is not None:
        for file_element in media_element.findall('file'):
            media = Media()
            media.course = course
            media.filename = file_element.get("filename")
            url = file_element.get("download_url")
            media.digest = file_element.get("digest")

            if len(url) > Media.URL_MAX_LENGTH:
                messages.info(
                    req,
                    _('File %(filename)s has a download URL larger than the maximum length permitted. The media file has not been registered, so it won\'t be tracked. Please, fix this issue and upload the course again.'
                      ) % {'filename': media.filename})
            else:
                media.download_url = url
                # get any optional attributes
                for attr_name, attr_value in file_element.attrib.items():
                    if attr_name == "length":
                        media.media_length = attr_value
                    if attr_name == "filesize":
                        media.filesize = attr_value

                media.save()
                # save gamification events
                gamification = file_element.find('gamification')
                events = parse_gamification_events(gamification)

                for event in events:
                    # Only add events if the didn't exist previously
                    e, created = MediaGamificationEvent.objects.get_or_create(
                        media=media,
                        event=event['name'],
                        defaults={
                            'points': event['points'],
                            'user': req.user
                        })

                    if created:
                        messages.info(
                            req,
                            _('Gamification for "%(event)s" at course level added'
                              ) % {'event': e.event})
Beispiel #6
0
def handle_uploaded_file(f, extract_path, request):
    zipfilepath = settings.COURSE_UPLOAD_DIR + f.name

    with open(zipfilepath, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

    zip = zipfile.ZipFile(zipfilepath)
    zip.extractall(path=extract_path)

    mod_name = ''
    for dir in os.listdir(extract_path)[:1]:
        mod_name = dir

    # check there is at least a sub dir
    if mod_name == '':
        messages.info(request, _("Invalid course zip file"))
        return False

    # check that the
    if not os.path.isfile(extract_path + mod_name + "/module.xml"):
        messages.info(request,
                      _("Zip file does not contain a module.xml file"))
        return False

    # parse the module.xml file
    doc = xml.dom.minidom.parse(extract_path + mod_name + "/module.xml")
    for meta in doc.getElementsByTagName("meta")[:1]:
        versionid = 0
        for v in meta.getElementsByTagName("versionid")[:1]:
            versionid = int(v.firstChild.nodeValue)
        temp_title = {}
        for t in meta.childNodes:
            if t.nodeName == "title":
                temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
        title = json.dumps(temp_title)
        shortname = ''
        for sn in meta.getElementsByTagName("shortname")[:1]:
            shortname = sn.firstChild.nodeValue

    old_course_filename = None
    # Find if course already exists
    try:
        course = Course.objects.get(shortname=shortname)
        old_course_filename = course.filename

        # check that the current user is allowed to wipe out the other course
        if course.user != request.user:
            messages.info(
                request,
                _("Sorry, only the original owner may update this course"))
            return False

        # check if course version is older
        if course.version > versionid:
            messages.info(request,
                          _("A newer version of this course already exists"))
            return False
        # wipe out the old sections/activities/media
        oldsections = Section.objects.filter(course=course)
        oldsections.delete()
        oldmedia = Media.objects.filter(course=course)
        oldmedia.delete()

        course.shortname = shortname
        course.title = title
        course.version = versionid
        course.user = request.user
        course.filename = f.name
        course.lastupdated_date = datetime.datetime.now()
        course.save()
    except Course.DoesNotExist:
        course = Course()
        course.shortname = shortname
        course.title = title
        course.version = versionid
        course.user = request.user
        course.filename = f.name
        course.save()

    # add all the sections
    for structure in doc.getElementsByTagName("structure")[:1]:

        if structure.getElementsByTagName("section").length == 0:
            messages.info(
                request,
                _("There don't appear to be any activities in this upload file."
                  ))
            course.delete()
            return False

        for s in structure.getElementsByTagName("section"):
            temp_title = {}
            for t in s.childNodes:
                if t.nodeName == 'title':
                    temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
            title = json.dumps(temp_title)
            section = Section()
            section.course = course
            section.title = title
            section.order = s.getAttribute("order")
            section.save()

            # add all the activities
            for activities in s.getElementsByTagName("activities")[:1]:
                for a in activities.getElementsByTagName("activity"):
                    temp_title = {}
                    for t in a.getElementsByTagName("title"):
                        temp_title[t.getAttribute(
                            'lang')] = t.firstChild.nodeValue
                    title = json.dumps(temp_title)
                    activity = Activity()
                    activity.section = section
                    activity.order = a.getAttribute("order")
                    activity.title = title
                    activity.type = a.getAttribute("type")
                    activity.digest = a.getAttribute("digest")
                    activity.save()

    # add all the media
    for files in doc.lastChild.lastChild.childNodes:
        if files.nodeName == 'file':
            media = Media()
            media.course = course
            media.filename = files.getAttribute("filename")
            media.download_url = files.getAttribute("download_url")
            media.digest = files.getAttribute("digest")
            media.save()

    if old_course_filename is not None and course.version != versionid:
        os.remove(settings.COURSE_UPLOAD_DIR + old_course_filename)
    return course