Exemple #1
0
 def post(self, request, *args, **kwargs):
     if request.FILES:
         file_form = UploadFileForm(request.POST, request.FILES)
         if file_form.is_valid():
             with request.FILES['file'] as bib_file:
                 Bibtex().do_import(bib_file)
     return super(DocumentCreateView, self).post(request, *args, **kwargs)
Exemple #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,
    })
Exemple #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,
        },
    )
Exemple #4
0
def upload_file(request, parent_id):
    parentNode = get_object_or_404(Node, id=parent_id)
    if not isinstance(parentNode, Course):
        raise NotImplementedError("Not a course")

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

        if form.is_valid():
            if len(form.cleaned_data['name']) > 0:
                name = form.cleaned_data['name']
            else:
                name, _ = os.path.splitext(request.FILES['file'].name)
                name = name.lower()

            extension = os.path.splitext(request.FILES['file'].name)[1].lower()
            description = form.cleaned_data['description']
            course = parentNode

            doc = Document.objects.create(user=request.user,
                                          name=name,
                                          description=description,
                                          state="PREPARING",
                                          file_type=extension)
            doc.original.save(
                str(uuid.uuid4()) + extension, request.FILES['file'])
            doc.save()

            course.add_child(doc)

            doc.add_keywords(*form.cleaned_data['tags'])
            doc.year = form.cleaned_data['year']

            doc.state = 'READY_TO_QUEUE'
            doc.save()

            add_document_to_queue(doc)

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

    else:
        form = UploadFileForm()

    return render(request, 'document_upload.html', {
        'form': form,
        'parent': parentNode,
    })
Exemple #5
0
def upload_file(request, parent_id):
    parentNode = get_object_or_404(Node, id=parent_id)
    if not isinstance(parentNode, Course):
        raise NotImplementedError("Not a course")

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

        if form.is_valid():
            if len(form.cleaned_data['name']) > 0:
                name = form.cleaned_data['name']
            else:
                name, _ = os.path.splitext(request.FILES['file'].name)
                name = name.lower()

            extension = os.path.splitext(request.FILES['file'].name)[1][1:].lower()
            description = form.cleaned_data['description']
            course = parentNode

            doc = Document.objects.create(user=request.user,
                                          name=name, description=description, state="pending")
            course.add_child(doc)

            doc.add_keywords(*form.cleaned_data['tags'])
            doc.year = form.cleaned_data['year']

            if not os.path.exists(settings.TMP_UPLOAD_DIR):
                os.makedirs(settings.TMP_UPLOAD_DIR)

            tmp_file = os.path.join(settings.TMP_UPLOAD_DIR, "{}.{}".format(doc.id, extension))
            source = 'file://' + tmp_file
            doc.source = source

            tmp_doc = open(tmp_file, 'w')
            tmp_doc.write(request.FILES['file'].read())
            tmp_doc.close()

            doc.save() # Save document after copy to avoid corrupted state if copy failed

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

    else:
        form = UploadFileForm()

    return render(request, 'document_upload.html', {
        'form': form,
        'parent': parentNode,
    })
Exemple #6
0
def upload_file(request, parent_id):
    parentNode = get_object_or_404(Node, id=parent_id)
    if not isinstance(parentNode, Course):
        raise NotImplementedError("Not a course")

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

        if form.is_valid():
            if len(form.cleaned_data['name']) > 0:
                name = form.cleaned_data['name']
            else:
                name, _ = os.path.splitext(request.FILES['file'].name)
                name = name.lower()

            extension = os.path.splitext(request.FILES['file'].name)[1].lower()
            description = form.cleaned_data['description']
            course = parentNode

            doc = Document.objects.create(
                user=request.user,
                name=name,
                description=description,
                state="PREPARING",
                file_type=extension
            )
            doc.original.save(str(uuid.uuid4()) + extension, request.FILES['file'])
            doc.save()

            course.add_child(doc)

            doc.add_keywords(*form.cleaned_data['tags'])
            doc.year = form.cleaned_data['year']

            doc.state = 'READY_TO_QUEUE'
            doc.save()

            add_document_to_queue(doc)

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

    else:
        form = UploadFileForm()

    return render(request, 'document_upload.html', {
        'form': form,
        'parent': parentNode,
    })
Exemple #7
0
def upload_file(request):
    form = UploadFileForm(request.POST, request.FILES)

    if form.is_valid() and match(r".*\.[pP][dD][fF]$", request.FILES["file"].name):
        name = sub(r"\.[Pp][Dd][Ff]$", "", request.FILES["file"].name)
        # name = escape(name.lower().replace(' ', '_'))
        name = escape(name.lower())
        description = escape(form.cleaned_data["description"])
        course = form.cleaned_data["course"]
        doc = Document.objects.create(user=request.user.get_profile(), name=name, description=description)
        course.add_child(doc)
        url = "/tmp/TMP402_%d.pdf" % doc.id
        tmp_doc = open(url, "w")
        tmp_doc.write(request.FILES["file"].read())
        tmp_doc.close()
        PendingDocument.objects.create(document=doc, state="queued", url="file://" + url)
        return HttpResponseRedirect(reverse("course_show", args=[course.slug]))
    return HttpResponse("form invalid", "text/html")
Exemple #8
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,
    })
Exemple #9
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,
    })
Exemple #10
0
def upload_file(request, slug):
    form = UploadFileForm(request.POST, request.FILES)

    if form.is_valid() and match(r'.*\.[pP][dD][fF]$',
                                 request.FILES['file'].name):
        name = sub(r'\.[Pp][Dd][Ff]$', '', request.FILES['file'].name)
        name = escape(name.lower().replace(' ', '_'))
        description = escape(form.cleaned_data['description'])
        course = get_object_or_404(Course, slug=slug)
        doc = Document.objects.create(user=request.user.get_profile(),
                                     reference=course, name=name, 
                                     description=description)

        url = '/tmp/TMP402_%d.pdf' % doc.id
        tmp_doc = open(url, 'w')
        tmp_doc.write(request.FILES['file'].read())
        tmp_doc.close()
        PendingDocument.objects.create(document=doc, state="queued",
                                       url='file://' + url)
        return HttpResponseRedirect(reverse('course_show', args=[slug]))
    return HttpResponse('form invalid', 'text/html')
Exemple #11
0
def upload_file(request):
    form = UploadFileForm(request.POST, request.FILES)

    if form.is_valid() and match(r'.*\.[pP][dD][fF]$',
                                 request.FILES['file'].name):
        name = sub(r'\.[Pp][Dd][Ff]$', '', request.FILES['file'].name)
        #name = escape(name.lower().replace(' ', '_'))
        name = escape(name.lower())
        description = escape(form.cleaned_data['description'])
        course = form.cleaned_data['course']
        doc = Document.objects.create(user=request.user.get_profile(),
                                      name=name,
                                      description=description)
        course.add_child(doc)
        url = '/tmp/TMP402_%d.pdf' % doc.id
        tmp_doc = open(url, 'w')
        tmp_doc.write(request.FILES['file'].read())
        tmp_doc.close()
        PendingDocument.objects.create(document=doc,
                                       state="queued",
                                       url='file://' + url)
        return HttpResponseRedirect(reverse('course_show', args=[course.slug]))
    return HttpResponse('form invalid', 'text/html')
Exemple #12
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})
Exemple #13
0
def upload_file(request):
    form = UploadFileForm(request.POST, request.FILES)

    if not form.is_valid():
        return HttpResponse('form invalid'+str(form.errors), 'text/html')

    if len(form.cleaned_data['name'])>0:
        name = escape(form.cleaned_data['name'])
    else:
        name = escape(request.FILES['file'].name[:-4].lower())

    description = escape(form.cleaned_data['description'])
    course = form.cleaned_data['course']
    doc = Document.objects.create(user=request.user.get_profile(),
                                  name=name, description=description)
    course.add_child(doc)
    url = '/tmp/TMP402_%d.pdf' % doc.id
    tmp_doc = open(url, 'w')
    tmp_doc.write(request.FILES['file'].read())
    tmp_doc.close()
    PendingDocument.objects.create(document=doc, state="queued",
                                   url='file://' + url)

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