示例#1
0
def generateTestFile(id, option='csv', fname=None):
    """
    Generates a test file & uploads it to Document
    OPTIONS: (csv|not-csv|bad_data)
    :return file object
    """
    file_name = None
    output = io.BytesIO()
    wr = csv.writer(output, delimiter=",")
    if option == 'csv':
        file_name = "test1.csv"
        wr.writerow(['a', 'b', 'c', 'd', 'e'])
        for _ in range(1, 5):
            wr.writerow([1, 2, 3, 4, 5])
    elif option == 'not-csv':
        file_name = 'not-csv'
        wr.writerow('hello world\n1\n2.5\nlol\n');
    elif option == 'bad_data':
        file_name = 'bad_data.csv'
        for _ in range(1, 4):
            wr.writerow([1, 2, 3, 4, 5])
            wr.writerow([6.0, 6.0, 6.0, 6.0, 6.0])

    if fname: # overrides any default file name set
        file_name = fname

    user_file = SimpleUploadedFile(file_name, output.getvalue())
    Document.upload_file(id, file_name, user_file, FilesTest.username)
示例#2
0
    def test_read_only_file(self):

        doc = Document(title='New Doc', file_name='A File', file=self.upload,
                                                                author=self.u1)
        doc.save()

        self.assertRaises(ReadOnlyFileError, doc.file.delete)
        self.assertRaises(ReadOnlyFileError, doc.file.write, 'test')
        self.assertRaises(ReadOnlyFileError, doc.file.writelines, 'test')
示例#3
0
def upload_file(request):
	if request.method == 'POST':
		form = UploadForm(request.POST, request.FILES)
		if form.is_valid():
			newdoc = Document(filename = request.POST['filename'],docfile = request.FILES['docfile'])
			newdoc.save(form)
			return redirect("uploads")
	else:
		form = UploadForm()
		#tambien se puede utilizar render_to_response
		#return render_to_response('upload.html', {'form': form}, context_instance = RequestContext(request))
	return render(request, 'upload.html', {'form': form})
示例#4
0
def generateBadDoc(id):
    """
    Generates a 'bad' document (causes data-processing to crash)
    """
    new_files = []
    file_name = "test1"
    new_files.append(file_name)
    with open(os.path.join(settings.MEDIA_ROOT, file_name), 'wb') as csvfile:
            wr = csv.writer(csvfile, delimiter=",")
            wr.writerow(['c{}'.format(k) for k in range(4)])
            # uneven row/column lengths!
            wr.writerow([k for k in range(4)])
            wr.writerow([k for k in range(3)])
            wr.writerow([k for k in range(2)])
    Document.upload_file(id, file_name, file_name, PlotTest.username) # not sure about path correctness
    return new_files
示例#5
0
 def testRemoveAccess(self):
     # self.createPanePlot()
     Access.give_access(self.account_id_one, self.account_id_two, PermissionsTest.FILE_NAME)
     Access.remove_access(self.account_id_one, PermissionsTest.FILE_NAME)
     shared_doc = Document.select_shared_files(self.account_id_two)
     # Now file is removed from shared.
     self.assertEqual(shared_doc, [])
示例#6
0
def uploaded_new_document(file):
    """
    Sets up a new document object ready to be saved.
    Note that it won't fill in the `title` or `author` attributes,
    you have to do that.
    """
    file_type, md5_sum = identify_and_md5(file)
    try:
        blob = ParentBlob.objects.get(md5_sum=md5_sum)
    except ParentBlob.DoesNotExist:
        blob = ParentBlob.objects.create(md5_sum=md5_sum, file=file,
                                            file_type=file_type)
        blob.save()
    document = Document()
    document.file_name = os.path.basename(file.name)
    document._blob = blob

    return document
示例#7
0
    def test_auto_blob_creation(self):
        """
        Test that a blob is created when using file attriube on a document.
        """
        doc = Document(title='New Doc', file_name='A File', file=self.upload,
                                                                author=self.u1)
        doc.save()
        doc.file.seek(0)
        self.upload.seek(0)

        self.assertEqual(doc.file.read(), self.upload.read())
        self.assertEqual(ParentBlob.objects.count(), 1)

        doc.file.seek(0)
        self.upload.seek(0)

        self.assertEqual(ParentBlob.objects.all()[0].file.read(),
                                                        self.upload.read())
示例#8
0
文件: views.py 项目: noriakioji/QADDA
def get_panes_data(acct_id):
    """
    Fetches relevant data needed for template rendering on GET request
    :return: dict
    """
    documents = Document.select_user_files(acct_id)
    shared_files = Document.select_shared_files(acct_id)
    user_name = Account.objects.get(id=acct_id).username
    upload = DocumentForm()
    form = CreateForm()
    panes = compute_views.handle_get_panes(acct_id)
    return {
        "user_name": user_name,
        "documents": documents,
        "shared_documents": shared_files,
        "form": form,
        "panes": panes,
        "upload": upload,
    }
示例#9
0
 def testGiveAccess(self):
     # self.createPanePlot()
     Access.give_access(self.account_id_one, self.account_id_two, PermissionsTest.FILE_NAME)
     # Then check retrieval.
     shared_doc = Document.select_shared_files(self.account_id_two)[0]
     # User 2 has none
     result = LR.get_user_regressions(self.account_id_two)
     self.assertEqual(shared_doc.file_name, PermissionsTest.FILE_NAME)
     self.assertEqual(shared_doc.account_id, self.account_id_one)
     self.assertEqual(result, [])
示例#10
0
文件: views.py 项目: noriakioji/QADDA
def list_panes(request):
    """
    Entry handler for user's home page!
    """
    # redirect to login page if not signed in
    if not request.user.is_authenticated():
        return redirect("top")

    if request.method == "POST":
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            owner = Account.objects.get(id=request.user.id).username
            Document.upload_file(request.user.id, request.FILES["csv_file"].name, request.FILES["csv_file"], owner)
        # Redirect to the document list after POST
        return HttpResponseRedirect(reverse("app.views.list_panes"))

    elif request.method == "GET":
        return render_to_response(
            "home.html", get_panes_data(request.user.id), context_instance=RequestContext(request)
        )
示例#11
0
文件: views.py 项目: mwschouten/gps
def submit(request):

    print('REQUEST.FILES', request.FILES)
    print('REQUEST.FILES', request.FILES['docfile'].__dict__)
    print('REQUEST.POST', request.POST)
    name = request.FILES['docfile']._name
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)

        print('FORM ', form)
        print('FORM IS VALID', form.is_valid())

        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            print ('NEWDOC ',newdoc)
            print ('NEWDOC ',newdoc.__dict__)
            print ('NEWDOC ',newdoc.docfile.__dict__)
            file_stored = 'media/' + newdoc.docfile.name
            print ('FILE STORED ',file_stored)

            ok,matched = import_xlsx(filename = request.FILES['docfile']._name,
                                     file_stored = file_stored)


            # Respond ANYWAY
            return JsonResponse({
                'ok':True,
                'id':newdoc.id,
                'name':request.FILES['docfile']._name,
                'size':request.FILES['docfile']._size,
                'match':'new'}
                )
        else:
            return JsonResponse({
                'ok':False,
                'errors':form.errors}
                )
            
示例#12
0
    def test_auto_derived_blob_creation(self):
        """
        Test that a blob is created when using the file attribue on a derived
        document.
        """
        doc = Document(title='New Doc', file_name='A File', file=self.upload,
                                                                author=self.u1)
        doc.save()
        derived = DerivedDocument(derived_from=doc._blob, index=0,
                                                        file=self.upload2)
        derived.save()

        self.upload2.seek(0)
        self.upload.seek(0)

        self.assertEqual(derived.file.read(), self.upload2.read())
        self.assertEqual(DerivedBlob.objects.count(), 1)

        self.upload2.seek(0)
        derived.file.seek(0)

        self.assertEqual(DerivedBlob.objects.all()[0].file.read(),
                                                        self.upload2.read())
示例#13
0
def generateDocs(id, n, r, c, name=None):
    """
    Helper to create test docs according to the parameters below

    :param id: account id
    :param n: number of files to generate
    :param r: number of rows per file
    :param c: number of cols per file
    :return: [files names of new files generated]
    """
    new_files = []
    for i in range(1, n+1):
        if not name:
            file_name = "test{}".format(i)
        else:
            file_name = name + str(i)
        new_files.append(file_name)
        with open(os.path.join(settings.MEDIA_ROOT, file_name), 'wb') as csvfile:
            wr = csv.writer(csvfile, delimiter=",")
            wr.writerow(['c{}'.format(k) for k in range(c)])
            for j in range(r):
                wr.writerow([i*k for k in range(1, c+1)])
        Document.upload_file(id, file_name, file_name, PlotTest.username) # not sure about path correctness
    return new_files
示例#14
0
 def testFilePath(self):
     generateTestFile(self.account_id)
     selected_file = Document.select_file(self.account_id, "test1.csv")
     filePath = file_path(selected_file, "test1.csv")
     self.assertEqual(type(filePath), str)
示例#15
0
 def tearDown(self):
     Document.clear_docs()
     Document.objects.all().delete()
示例#16
0
 def testClearAccess(self):
     Access.give_access(self.account_id_one, self.account_id_two, PermissionsTest.FILE_NAME)
     Access.clear_accesses()
     shared_doc = Document.select_shared_files(self.account_id_two)
     self.assertEqual(shared_doc, [])