def process_course(extract_path, f, mod_name, request, user): xml_path = os.path.join(extract_path, mod_name, "module.xml") # check that the module.xml file exists if not os.path.isfile(xml_path): msg_text = _(u"Zip file does not contain a module.xml file") messages.info(request, msg_text, extra_tags="danger") CoursePublishingLog(user=user, action="no_module_xml", data=msg_text).save() return False, 400, False # parse the module.xml file doc = ET.parse(xml_path) meta_info = parse_course_meta(doc) is_new_course = False oldsections = [] old_course_filename = None # Find if course already exists try: course = Course.objects.get(shortname=meta_info['shortname']) course_manager = CoursePermissions.objects.filter( user=user, course=course, role=CoursePermissions.MANAGER).count() # check that the current user is allowed to wipe out the other course if course.user != user and course_manager == 0: msg_text = \ _(u"Sorry, you do not have permissions to update this course.") messages.info(request, msg_text) CoursePublishingLog(course=course, new_version=meta_info['versionid'], old_version=course.version, user=user, action="permissions_error", data=msg_text).save() return False, 401, is_new_course # check if course version is older if course.version > meta_info['versionid']: msg_text = _(u"A newer version of this course already exists") messages.info(request, msg_text) CoursePublishingLog(course=course, new_version=meta_info['versionid'], old_version=course.version, user=user, action="newer_version_exists", data=msg_text).save() return False, 400, is_new_course # obtain the old sections oldsections = list( Section.objects.filter(course=course).values_list('pk', flat=True)) # wipe out old media oldmedia = Media.objects.filter(course=course) oldmedia.delete() old_course_filename = course.filename course.lastupdated_date = timezone.now() except Course.DoesNotExist: course = Course() course.status = CourseStatus.DRAFT is_new_course = True old_course_version = course.version course.shortname = meta_info['shortname'] course.title = meta_info['title'] course.description = meta_info['description'] course.version = meta_info['versionid'] course.priority = int(meta_info['priority']) course.user = user course.filename = f.name course.save() if not parse_course_contents(request, doc, course, user, is_new_course): return False, 500, is_new_course clean_old_course(request, user, oldsections, old_course_filename, course) # save gamification events if 'gamification' in meta_info: events = parse_gamification_events(meta_info['gamification']) for event in events: # Only add events if the didn't exist previously e, created = CourseGamificationEvent.objects.get_or_create( course=course, event=event['name'], defaults={ 'points': event['points'], 'user': user }) if created: msg_text = \ _(u'Gamification for "%(event)s" at course level added') \ % {'event': e.event} messages.info(request, msg_text) CoursePublishingLog(course=course, new_version=meta_info['versionid'], old_version=old_course_version, user=user, action="gamification_added", data=msg_text).save() tmp_path = replace_zip_contents(xml_path, doc, mod_name, extract_path) # Extract the final file into the courses area for preview zipfilepath = os.path.join(settings.COURSE_UPLOAD_DIR, f.name) shutil.copy(tmp_path + ".zip", zipfilepath) course_preview_path = os.path.join(settings.MEDIA_ROOT, "courses") ZipFile(zipfilepath).extractall(path=course_preview_path) writer = GamificationXMLWriter(course) writer.update_gamification(request.user) return course, 200, is_new_course