Ejemplo n.º 1
0
    def regenerate_both(modeladmin, request, queryset):
        for doc in queryset:
            packs = doc.packs.filter(type__in=['pdf', 'pngs'])
            if packs:
                packs[0].delete()

            create_pdf.delay(doc.id, callback=create_pngs.subtask((doc.id,)))
Ejemplo n.º 2
0
    def regenerate_pdf(modeladmin, request, queryset):
        for doc in queryset:
            packs = doc.packs.filter(type='pdf')
            if packs:
                packs[0].delete()

            create_pdf.delay(doc.id)
Ejemplo n.º 3
0
def api_document_upload_view(request):
    if request.path.startswith('/api/'):
        DigestAuthentication().is_authenticated(request)

    if request.method == 'POST':
        log.debug('Document Post')
        # Process the request
        form = ApiDocumentUploadForm(request.POST, request.FILES)
        if form.is_valid():
            new_file = form.save(commit=False)

            with statsd.timer('upload.md5_calc_time'):
                sum = md5()
                for chunk in new_file.file.chunks():
                    sum.update(chunk)

                md5_sum = sum.hexdigest()
                new_file.md5_sum = md5_sum

            new_file.title = new_file.file.name

            if not Document.objects.unique_md5(md5_sum, (DerivedFile,)):

                response_data = {
                        'result': 'failure',
                        'success': False,
                        'message': 'file already uploaded'
                    }
            else:
                new_file.save()
                # Create a PDF and pngs
                create_pdf.delay(new_file.id,
                    callback=create_pngs.subtask((new_file.id,)))
                response_data = {
                    'result': 'success',
                    'success': True,
                    'message': 'File uploaded'
                }
        else:
            response_data = {
                'result': 'failure',
                'success': False,
                'message': 'invalid form'
            }
    else:
        response_data = {
            'result': 'failure',
            'success': False,
            'message': 'you need to send me something!'
        }

    return HttpResponse(json.dumps(response_data), mimetype="application/json")
Ejemplo n.º 4
0
def get_upload_details(request):
    # Handle the file upload
    if request.method == 'POST':
        form = UploadDetailForm(request.POST)

        # Check the form is valid, and that we have a file to work with
        if form.is_valid() and request.session.get('upload_file', False):
            # Get the information that we stored on the file
            temp_file = request.session['upload_file']
            temp_file_sum = temp_file.md5_sum

            # Generate a document instance from the details the user provided
            new_document = Document(md5_sum = temp_file_sum)
            new_document.file.save(temp_file.name, temp_file.file, False)

            new_document.title = form.cleaned_data['title']

            # Save the document and clean up temp files and stuff
            new_document.save()
            log.info("Saved a new document: %s", new_document.id)
            clean_temp_file(temp_file)
            del request.session['upload_file']

            # Create a PDF and pngs
            create_pdf.delay(new_document.id,
                callback=create_pngs.subtask((new_document.id,)))

    if request.session.get('upload_file', False):
        temp_file = request.session['upload_file']

        data = {'filename': temp_file.name}

        try:
            try:
                file_data = solr.extract(temp_file.file)
            except IOError, e:
                log.warn('Solr extraction failed: %s', e)
                file_data = {'metadata': None}
            log.debug(file_data['metadata'])

            try:
                if file_data['metadata']:
                    data['title'] = file_data['metadata']['title'][0] or data['filename']
                    data['author'] = file_data['metadata']['Author'][0]
            except:
                pass

            data['title'] = data['filename']

            details_form = UploadDetailForm(data)

            # Redirect the user to the upload information page after POST
            return render_to_response('upload/upload_details.html',
                {'form': details_form, 'file_name': data['filename']},
                context_instance=RequestContext(request)
            )
        except SolrError:
            # Return an error message
            return render_to_response('upload/upload_details.html',
                {'error': 'Solr extraction error', 'file_name': data['filename']},
                context_instance=RequestContext(request)
            )