Ejemplo n.º 1
0
def add(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            # Check to see if the file is already in the system
            if Document.find_user_file(
                    request.FILES['doc_file']).first() == None:
                new_doc = form.save()
                #Handle zip files
                if is_zipfile(request.FILES['doc_file']):
                    url_subpath = os.path.dirname(new_doc.doc_file.url)
                    num_inner_files = handleZipFile(request.FILES['doc_file'],
                                                    url_subpath)
                    if num_inner_files > 0:
                        messages.add_message(
                            request, messages.INFO,
                            "Note: %d files inside the zip file were also saved as separate documents."
                            % num_inner_files)
                return HttpResponseRedirect('/admin/authapp/document/' +
                                            str(new_doc.id))
            else:
                messages.add_message(
                    request, messages.INFO,
                    "Note: This document is already in the system")
    form = DocumentForm()
    return render(request, 'authentication/add.html', {'form': form})
Ejemplo n.º 2
0
def upload(request):
    post = False
    form = None
    document = None
    validation = None

    if request.method == 'POST':
        post = True
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            documents = Document.find_user_file(request.FILES['user_file'])
            if documents:
                document = documents.first()
                validation = document.test_user_file(request.FILES['user_file'])
                return HttpResponse(json.dumps({
                                      'document': {"name":unicode(document), "sha512":document.sha512, "uploaded":str(document.uploaded)},
                                      'validation': {"is_valid":validation.valid, "fingerprint":validation.fingerprint}}), 
                                    content_type = "application/json")
            else:
                return HttpResponse(json.dumps({'document': None,'validation': None}), content_type = "application/json")
    else:
        form = UploadForm()

    return render(request, 'authentication/upload.html', {
        'post': post,
        'form': form,
        'document': document,
        'validation': validation
    })
Ejemplo n.º 3
0
def handleZipFile(input_file, subpath):
    # keep track of # of inner files we have (so we can report this
    # back to the user later)
    num_files = 0

    # we'll unzip the zip file to a temporary directory and *then*
    # pass the files into Django.
    tempdir = mkdtemp()

    #Extract all files from zip
    z = ZipFile(input_file)
    z.extractall(tempdir)

    for name in z.namelist():
        # the full path to file, inside temp directory.
        file_loc = os.path.join(tempdir, name)

        # skip if we don't want it in our db
        if name.startswith('__MACOSX/') or os.path.isdir(file_loc):
            continue

        # now create a Document and send this file into Django's
        # magic file handling.
        # "/document/2014/06/<filename>"
        save_name = os.path.join(subpath, name)

        if Document.find_user_file(open(file_loc, 'r')).first() == None:

            new_doc = Document()
            with open(file_loc, 'rb') as f:
                new_doc.doc_file.save(save_name, File(f))
            new_doc.save()
            num_files += 1

            #If this file was another zip file, unzip it too.
            if is_zipfile(file_loc):
                new_subpath = os.path.dirname(new_doc.doc_file.url)
                num_files += handleZipFile(file_loc, new_subpath)

    # Clear temp
    rmtree(tempdir)

    return num_files
Ejemplo n.º 4
0
def handleZipFile(input_file, subpath):
  # keep track of # of inner files we have (so we can report this
  # back to the user later)
  num_files = 0

  # we'll unzip the zip file to a temporary directory and *then*
  # pass the files into Django.
  tempdir = mkdtemp()

  #Extract all files from zip
  z = ZipFile(input_file)
  z.extractall(tempdir)

  for name in z.namelist():
    # the full path to file, inside temp directory.
    file_loc = os.path.join(tempdir, name)

    # skip if we don't want it in our db
    if name.startswith('__MACOSX/') or os.path.isdir(file_loc):
      continue

    # now create a Document and send this file into Django's
    # magic file handling.
    # "/document/2014/06/<filename>"
    save_name = os.path.join(subpath, name)

    if Document.find_user_file(open(file_loc, 'r')).first() == None:

      new_doc = Document()
      with open(file_loc, 'rb') as f:
        new_doc.doc_file.save(save_name, File(f))
      new_doc.save()
      num_files += 1

      #If this file was another zip file, unzip it too.
      if is_zipfile(file_loc):
        new_subpath = os.path.dirname(new_doc.doc_file.url)
        num_files += handleZipFile(file_loc, new_subpath)

  # Clear temp
  rmtree(tempdir)

  return num_files
Ejemplo n.º 5
0
def upload(request):
    post = False
    form = None
    document = None
    validation = None

    if request.method == 'POST':
        post = True
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            documents = Document.find_user_file(request.FILES['user_file'])
            if documents:
                document = documents.first()
                validation = document.test_user_file(
                    request.FILES['user_file'])
                return HttpResponse(json.dumps({
                    'document': {
                        "name": unicode(document),
                        "sha512": document.sha512,
                        "uploaded": str(document.uploaded)
                    },
                    'validation': {
                        "is_valid": validation.valid,
                        "fingerprint": validation.fingerprint
                    }
                }),
                                    content_type="application/json")
            else:
                return HttpResponse(json.dumps({
                    'document': None,
                    'validation': None
                }),
                                    content_type="application/json")
    else:
        form = UploadForm()

    return render(request, 'authentication/upload.html', {
        'post': post,
        'form': form,
        'document': document,
        'validation': validation
    })
Ejemplo n.º 6
0
def add(request):
  if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES)
    if form.is_valid():
      # Check to see if the file is already in the system
      if Document.find_user_file(request.FILES['doc_file']).first() == None:
        new_doc = form.save()
        #Handle zip files
        if is_zipfile(request.FILES['doc_file']):
          url_subpath = os.path.dirname(new_doc.doc_file.url)
          num_inner_files = handleZipFile(request.FILES['doc_file'], url_subpath)
          if num_inner_files > 0:
            messages.add_message(request, messages.INFO, "Note: %d files inside the zip file were also saved as separate documents." % num_inner_files)
        return HttpResponseRedirect('/admin/authapp/document/'+str(new_doc.id))
      else:
        messages.add_message(request, messages.INFO, "Note: This document is already in the system")
  form = DocumentForm()
  return render(request, 'authentication/add.html', {
                 'form': form
               })