Example #1
0
    def download_course(self, request, **kwargs):
        course = self.get_course(request, **kwargs)

        file_to_download = course.getAbsPath()
        has_completed_trackers = Tracker.has_completed_trackers(
            course, request.user)

        try:
            if has_completed_trackers:
                file_to_download = os.path.join(
                    settings.COURSE_UPLOAD_DIR, "temp",
                    str(request.user.id) + "-" + course.filename)
                shutil.copy2(course.getAbsPath(), file_to_download)
                zip = zipfile.ZipFile(file_to_download, 'a')
                if has_completed_trackers:
                    zip.writestr(course.shortname + "/tracker.xml",
                                 Tracker.to_xml_string(course, request.user))
                zip.close()

            binary_file = open(file_to_download, 'rb')
            response = HttpResponse(binary_file.read(),
                                    content_type='application/zip')
            binary_file.close()
            response['Content-Length'] = os.path.getsize(file_to_download)
            response['Content-Disposition'] = \
                'attachment; filename="%s"' % (course.filename)
        except IOError:
            raise Http404(self.STR_COURSE_NOT_FOUND)

        course_downloaded.send(sender=self, course=course, request=request)

        return response
Example #2
0
    def download_course(self, request, **kwargs):
        self.is_authenticated(request)
        self.throttle_check(request)
        
        pk = kwargs.pop('pk', None)
        try:
            if request.user.is_staff:
                course = self._meta.queryset.get(pk = pk,is_archived=False)
            else:
                course = self._meta.queryset.get(pk = pk, is_archived=False,is_draft=False)
        except Course.DoesNotExist:
            raise Http404(_(u"Course not found"))
        except ValueError:
            try:
                if request.user.is_staff:
                    course = self._meta.queryset.get(shortname = pk,is_archived=False)
                else:
                    course = self._meta.queryset.get(shortname = pk, is_archived=False,is_draft=False)
            except Course.DoesNotExist:
                raise Http404(_(u"Course not found"))
         
        file_to_download = course.getAbsPath();
        schedule = course.get_default_schedule()
        has_completed_trackers = Tracker.has_completed_trackers(course,request.user)
        cohort = Cohort.member_now(course,request.user)
        if cohort:
            if cohort.schedule:
                schedule = cohort.schedule
        
        # add scheduling XML file     
        if schedule or has_completed_trackers:
            file_to_download = settings.COURSE_UPLOAD_DIR +"temp/"+ str(request.user.id) + "-" + course.filename
            shutil.copy2(course.getAbsPath(), file_to_download)
            zip = zipfile.ZipFile(file_to_download,'a')
            if schedule:
                zip.writestr(course.shortname +"/schedule.xml",schedule.to_xml_string())
            if has_completed_trackers:
                zip.writestr(course.shortname +"/tracker.xml",Tracker.to_xml_string(course,request.user))
            zip.close()

        wrapper = FileWrapper(file(file_to_download))
        response = HttpResponse(wrapper, content_type='application/zip')
        response['Content-Length'] = os.path.getsize(file_to_download)
        response['Content-Disposition'] = 'attachment; filename="%s"' %(course.filename)
        
        # Add to tracker
        tracker = Tracker()
        tracker.user = request.user
        tracker.course = course
        tracker.type = 'download'
        tracker.data = json.dumps({'version':course.version })
        tracker.ip = request.META.get('REMOTE_ADDR','0.0.0.0')
        tracker.agent = request.META.get('HTTP_USER_AGENT','unknown')
        tracker.save()
                
        course_downloaded.send(sender=self, course=course, user=request.user)
        
        return response
Example #3
0
    def download_detail(self, request, **kwargs):
        self.is_authenticated(request)
        self.throttle_check(request)

        pk = kwargs.pop('pk', None)
        try:
            course = self._meta.queryset.get(pk=pk)
        except Course.DoesNotExist:
            raise NotFound(_(u'Course download not found'))

        file_to_download = course.getAbsPath()
        schedule = course.get_default_schedule()
        has_completed_trackers = Tracker.has_completed_trackers(
            course, request.user)
        cohort = Cohort.member_now(course, request.user)
        if cohort:
            if cohort.schedule:
                schedule = cohort.schedule

        # add scheduling XML file
        if schedule or has_completed_trackers:
            file_to_download = settings.COURSE_UPLOAD_DIR + "temp/" + str(
                request.user.id) + "-" + course.filename
            shutil.copy2(course.getAbsPath(), file_to_download)
            zip = zipfile.ZipFile(file_to_download, 'a')
            if schedule:
                zip.writestr(course.shortname + "/schedule.xml",
                             schedule.to_xml_string())
            if has_completed_trackers:
                zip.writestr(course.shortname + "/tracker.xml",
                             Tracker.to_xml_string(course, request.user))
            zip.close()

        wrapper = FileWrapper(file(file_to_download))
        response = HttpResponse(wrapper, content_type='application/zip')
        response['Content-Length'] = os.path.getsize(file_to_download)
        response['Content-Disposition'] = 'attachment; filename="%s"' % (
            course.filename)

        md = CourseDownload()
        md.user = request.user
        md.course = course
        md.course_version = course.version
        md.ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
        md.agent = request.META.get('HTTP_USER_AGENT', 'unknown')
        md.save()

        course_downloaded.send(sender=self, course=course, user=request.user)

        return response
Example #4
0
    def download_detail(self, request, **kwargs):
        self.is_authenticated(request)
        self.throttle_check(request)

        pk = kwargs.pop("pk", None)
        try:
            course = self._meta.queryset.get(pk=pk)
        except Course.DoesNotExist:
            raise NotFound(_(u"Course download not found"))

        file_to_download = course.getAbsPath()
        schedule = course.get_default_schedule()
        has_completed_trackers = Tracker.has_completed_trackers(course, request.user)
        cohort = Cohort.member_now(course, request.user)
        if cohort:
            if cohort.schedule:
                schedule = cohort.schedule

        # add scheduling XML file
        if schedule or has_completed_trackers:
            file_to_download = settings.COURSE_UPLOAD_DIR + "temp/" + str(request.user.id) + "-" + course.filename
            shutil.copy2(course.getAbsPath(), file_to_download)
            zip = zipfile.ZipFile(file_to_download, "a")
            if schedule:
                zip.writestr(course.shortname + "/schedule.xml", schedule.to_xml_string())
            if has_completed_trackers:
                zip.writestr(course.shortname + "/tracker.xml", Tracker.to_xml_string(course, request.user))
            zip.close()

        wrapper = FileWrapper(file(file_to_download))
        response = HttpResponse(wrapper, content_type="application/zip")
        response["Content-Length"] = os.path.getsize(file_to_download)
        response["Content-Disposition"] = 'attachment; filename="%s"' % (course.filename)

        md = CourseDownload()
        md.user = request.user
        md.course = course
        md.course_version = course.version
        md.ip = request.META.get("REMOTE_ADDR", "0.0.0.0")
        md.agent = request.META.get("HTTP_USER_AGENT", "unknown")
        md.save()

        course_downloaded.send(sender=self, course=course, user=request.user)

        return response