Beispiel #1
0
def upload_multiple_files(request, slug):
    course = get_object_or_404(Course, slug=slug)

    if request.method == 'POST':
        if settings.READ_ONLY:
            return HttpResponse('Upload is disabled for a few hours', status=401)

        form = MultipleUploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            for attachment in form.cleaned_data['files']:
                name, extension = os.path.splitext(attachment.name)
                name = logic.clean_filename(name)

                document = logic.add_file_to_course(
                    file=attachment,
                    name=name,
                    extension=extension,
                    course=course,
                    tags=[],
                    user=request.user
                )
                document.add_to_queue()

            return HttpResponseRedirect(reverse('course_show', args=[course.slug]))
    return HttpResponseRedirect(reverse('document_put', args=(course.slug,)))
Beispiel #2
0
def upload_file(request, slug):
    group = get_object_or_404(Group, slug=slug)

    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            if 'file' in request.FILES:
                file = request.FILES['file']
            else:
                filename = "New Document.md"
                open(filename, 'w').close()
                file = open(filename, 'r')
                # Django needs a size attribute to save the file
                file.size = 0


            name, extension = os.path.splitext(file.name)
            name = logic.clean_filename(name)

            if form.cleaned_data['name']:
                name = form.cleaned_data['name']

            document = logic.add_file_to_group(
                file=file,
                name=name,
                extension=extension,
                group=group,
                tags=form.cleaned_data['tags'],
                user=request.user
            )

            document.description = form.cleaned_data['description']
            document.save()

            document.add_to_queue()

            # Delete temp file
            try:
                os.remove(filename)
            except:
                pass

            return HttpResponseRedirect(reverse('group_show', args=[group.slug]))

    else:
        form = UploadFileForm()

    multiform = MultipleUploadFileForm()

    return render(request, 'documents/document_upload.html', {
        'form': form,
        'multiform': multiform,
        'group': group,
    })
Beispiel #3
0
def upload_file(request, slug):
    course = get_object_or_404(Course, slug=slug)

    if request.method == "POST":
        if settings.READ_ONLY:
            return HttpResponse("Upload is disabled for a few hours", status=401)

        form = UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            file = request.FILES["file"]

            name, extension = os.path.splitext(file.name)
            name = logic.clean_filename(name)

            if form.cleaned_data["name"]:
                name = form.cleaned_data["name"]

            document = logic.add_file_to_course(
                file=file,
                name=name,
                extension=extension,
                course=course,
                tags=form.cleaned_data["tags"],
                user=request.user,
            )

            document.description = form.cleaned_data["description"]
            document.save()

            document.add_to_queue()

            return HttpResponseRedirect(
                reverse("catalog:course_show", args=[course.slug])
            )

    else:
        form = UploadFileForm()

    multiform = MultipleUploadFileForm()

    return render(
        request,
        "documents/document_upload.html",
        {
            "form": form,
            "multiform": multiform,
            "course": course,
        },
    )
Beispiel #4
0
def import_document_from_path(doc_path, group, user):
    filename = os.path.split(doc_path)[1]
    tags, filename = extract_tags(filename)
    name, extension = os.path.splitext(filename)

    name = logic.clean_filename(name)

    document = logic.add_file_to_group(file=File(open(doc_path, 'rb')),
                                       name=name,
                                       extension=extension,
                                       group=group,
                                       tags=tags,
                                       user=user)

    document.add_to_queue()
Beispiel #5
0
    def create_documents(self, courses, documents):
        for doc in documents:
            if not doc['download_id']:
                if self.verbose:
                    self.stdout.write(
                        self.style.WARNING('SKIP: not downloaded yet: %s' %
                                           doc))
                continue

            course = courses[doc['course_id']]
            pattern = path.join(self.dump_dir, str(doc['download_id'])) + '.*'
            matching_files = glob(pattern)
            if len(matching_files) != 1:
                if self.verbose:
                    self.stdout.write(
                        self.style.WARNING('SKIP: no single file with id=%d' %
                                           doc['download_id']))
                continue

            srckey = self.source + '?document_id=%d' % doc['document_id']
            filename = matching_files[0]
            _, extension = os.path.splitext(filename)

            if extension in settings.REJECTED_FILE_FORMATS:
                if self.verbose:
                    self.stdout.write(
                        self.style.WARNING(
                            'REJECT: %s has a wrong format (%s)' %
                            (doc['download_id'], extension)))
                continue

            name = logic.clean_filename(doc['name'])

            document = logic.add_file_to_course(
                file=File(open(filename, 'rb')),
                name=name,
                extension=extension,
                course=course,
                tags=[],
                user=self.user,
                import_source=srckey,
            )
            if document:
                document.add_to_queue()
                self.stdout.write(
                    self.style.SUCCESS('Enqueued "%s" (pk %s) for processing' %
                                       (document, document.id)))
def import_document_from_path(doc_path, course, user):
    filename = os.path.split(doc_path)[1]
    tags, filename = extract_tags(filename)
    name, extension = os.path.splitext(filename)

    name = logic.clean_filename(name)

    document = logic.add_file_to_course(
        file=File(open(doc_path)),
        name=name,
        extension=extension,
        course=course,
        tags=tags,
        user=user
    )

    document.add_to_queue()
    def create_documents(self, courses, documents):
        for doc in documents:
            if not doc['download_id']:
                if self.verbose:
                    self.stdout.write(self.style.WARNING(
                        'SKIP: not downloaded yet: %s' % doc
                    ))
                continue

            course = courses[doc['course_id']]
            pattern = path.join(self.dump_dir, str(doc['download_id'])) + '.*'
            matching_files = glob(pattern)
            if len(matching_files) != 1:
                if self.verbose:
                    self.stdout.write(self.style.WARNING(
                        'SKIP: no single file with id=%d' % doc['download_id']
                    ))
                continue

            srckey = self.source + '?document_id=%d' % doc['document_id']
            filename = matching_files[0]
            _, extension = os.path.splitext(filename)

            if extension in settings.REJECTED_FILE_FORMATS:
                if self.verbose:
                    self.stdout.write(self.style.WARNING(
                        'REJECT: %s has a wrong format (%s)' % (doc['download_id'], extension)
                    ))
                continue

            name = logic.clean_filename(doc['name'])

            document = logic.add_file_to_course(
                file=File(open(filename, 'rb')),
                name=name,
                extension=extension,
                course=course,
                tags=[],
                user=self.user,
                import_source=srckey,
            )
            if document:
                document.add_to_queue()
                self.stdout.write(self.style.SUCCESS(
                    'Enqueued "%s" (pk %s) for processing' % (document, document.id)
                ))
Beispiel #8
0
def upload_multiple_files(request, slug):
    course = get_object_or_404(Course, slug=slug)

    if request.method == "POST":
        form = MultipleUploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            for attachment in form.cleaned_data["files"]:
                name, extension = os.path.splitext(attachment.name)
                name = logic.clean_filename(name)

                document = logic.add_file_to_course(
                    file=attachment, name=name, extension=extension, course=course, tags=[], user=request.user
                )
                document.add_to_queue()

            return HttpResponseRedirect(reverse("course_show", args=[course.slug]))
    return HttpResponseRedirect(reverse("document_put", args=(course.slug,)))
Beispiel #9
0
def import_document_from_path(doc_path, course, user):
    filename = os.path.split(doc_path)[1]
    tags, filename = extract_tags(filename)
    name, extension = os.path.splitext(filename)

    name = logic.clean_filename(name)

    with open(doc_path, "rb") as fd:
        document = logic.add_file_to_course(
            file=File(fd),
            name=name,
            extension=extension,
            course=course,
            tags=tags,
            user=user,
        )

    document.add_to_queue()
Beispiel #10
0
def upload_file(request, slug):
    course = get_object_or_404(Course, slug=slug)

    if request.method == 'POST':
        if settings.READ_ONLY:
            return HttpResponse('Upload is disabled for a few hours', status=401)

        form = UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            file = request.FILES['file']

            name, extension = os.path.splitext(file.name)
            name = logic.clean_filename(name)

            if form.cleaned_data['name']:
                name = form.cleaned_data['name']

            document = logic.add_file_to_course(
                file=file,
                name=name,
                extension=extension,
                course=course,
                tags=form.cleaned_data['tags'],
                user=request.user
            )

            document.description = form.cleaned_data['description']
            document.save()

            document.add_to_queue()

            return HttpResponseRedirect(reverse('course_show', args=[course.slug]))

    else:
        form = UploadFileForm()

    multiform = MultipleUploadFileForm()

    return render(request, 'documents/document_upload.html', {
        'form': form,
        'multiform': multiform,
        'course': course,
    })
Beispiel #11
0
def upload_file(request, slug):
    course = get_object_or_404(Course, slug=slug)

    if request.method == 'POST':
        if settings.READ_ONLY:
            return HttpResponse('Upload is disabled for a few hours',
                                status=401)

        form = UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            file = request.FILES['file']

            name, extension = os.path.splitext(file.name)
            name = logic.clean_filename(name)

            if form.cleaned_data['name']:
                name = form.cleaned_data['name']

            document = logic.add_file_to_course(file=file,
                                                name=name,
                                                extension=extension,
                                                course=course,
                                                tags=form.cleaned_data['tags'],
                                                user=request.user)

            document.description = form.cleaned_data['description']
            document.save()

            document.add_to_queue()

            return HttpResponseRedirect(
                reverse('course_show', args=[course.slug]))

    else:
        form = UploadFileForm()

    multiform = MultipleUploadFileForm()

    return render(request, 'documents/document_upload.html', {
        'form': form,
        'multiform': multiform,
        'course': course,
    })
Beispiel #12
0
    def create(self, validated_data):
        file = validated_data['file']
        name, extension = os.path.splitext(file.name)
        name = logic.clean_filename(name)

        if validated_data['name']:
            name = validated_data['name']

        document = logic.add_file_to_course(
            file=file,
            name=name,
            extension=extension,
            course=validated_data['course'],
            tags=validated_data['tags'],
            user=self.context['request'].user
        )

        document.description = validated_data['description']
        document.save()

        document.add_to_queue()
        return document
Beispiel #13
0
def upload_multiple_files(request, slug):
    group = get_object_or_404(Group, slug=slug)

    if request.method == 'POST':
        form = MultipleUploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            for attachment in form.cleaned_data['files']:
                name, extension = os.path.splitext(attachment.name)
                name = logic.clean_filename(name)

                document = logic.add_file_to_group(
                    file=attachment,
                    name=name,
                    extension=extension,
                    group=group,
                    tags=[],
                    user=request.user
                )
                document.add_to_queue()

            return HttpResponseRedirect(reverse('group_show', args=[group.slug]))
    return HttpResponseRedirect(reverse('document_put', args=(group.slug,)))
Beispiel #14
0
def upload_file(request, slug):
    course = get_object_or_404(Course, slug=slug)

    if request.method == "POST":
        form = UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            file = request.FILES["file"]

            name, extension = os.path.splitext(file.name)
            name = logic.clean_filename(name)

            if form.cleaned_data["name"]:
                name = form.cleaned_data["name"]

            document = logic.add_file_to_course(
                file=file,
                name=name,
                extension=extension,
                course=course,
                tags=form.cleaned_data["tags"],
                user=request.user,
            )

            document.description = form.cleaned_data["description"]
            document.save()

            document.add_to_queue()

            return HttpResponseRedirect(reverse("course_show", args=[course.slug]))

    else:
        form = UploadFileForm()

    multiform = MultipleUploadFileForm()

    return render(request, "documents/document_upload.html", {"form": form, "multiform": multiform, "course": course})
Beispiel #15
0
    def create(self, validated_data) -> Document | None:
        file = validated_data["file"]
        name, extension = os.path.splitext(file.name)
        name = logic.clean_filename(name)

        if validated_data["name"]:
            name = validated_data["name"]

        document = logic.add_file_to_course(
            file=file,
            name=name,
            extension=extension,
            course=validated_data["course"],
            tags=validated_data["tags"],
            user=self.context["request"].user,
        )

        if document:
            document.description = validated_data["description"]
            document.save()

            document.add_to_queue()

        return document