def test_file_on_server_method(self): document = Document(owner=self.dummy_user, external_url=EXTERNAL_PDF_URL) self.assertEqual(document.url, "/proxy?origurl=" + EXTERNAL_PDF_URL) self.assertEqual(document.file_on_server, False) # test if no input given, char field will be set to empty string document2 = Document(owner=self.dummy_user) self.assertEqual(document2.external_url, "")
def save_document(request_file, content_subdir, related_obj, ashash=True): uploadedfile = UploadedFile(request_file) file_content = uploadedfile.read() doc_obj = Document() doc_obj.filehash = md5(file_content).hexdigest() doc_obj.urlencfilename = quote(uploadedfile.name) doc_obj.filename = uploadedfile.name doc_obj.content_type = uploadedfile.file.content_type if ashash: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filehash else: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filename if related_obj.__class__.__name__.lower() == "queryset": if len(related_obj) == 1: setattr(doc_obj, related_obj[0].__class__.__name__.lower(), related_obj[0]) else: print "ERROR: The queryset object had %s elements to it" % str( len(related_obj)) else: setattr(doc_obj, related_obj.__class__.__name__.lower(), related_obj) doc_obj.save() wfile = open(doc_obj.filepath, "w") wfile.write(file_content) wfile.close()
def post(self, request, format=None): my_file = request.FILES['file'] file_name = str(uuid.uuid1()) + '.dcm' print(file_name) with open(file_name, 'wb+') as temp_file: temp_file.write(my_file.read()) print(file_name) ds = dicom.read_file(file_name) document = Document() document.date_time = datetime.datetime.now() document.doc_file = request.FILES['file'] document.save() print(document.doc_id) for ke in ds.dir(): if ke == 'PixelData': continue prop = DICOMProperty() prop.prop_key = ke prop.prop_value = str(getattr(ds, ke, '')) prop.doc_id = document print(prop) prop.save() print(Document.objects.all()) documents = Document.objects.all() serializer = DocumentListSerializer(documents) print(serializer.data) #return Response(status=status.HTTP_200_OK, data=serializer.data) return HttpResponseRedirect("/static/header.html")
def index(request): if request.method == "POST": form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(docfile=request.FILES['docfile']) newdoc.save() question = form.cleaned_data['question'] renderthis = process(newdoc.docfile.name, question) imageurlcontainer = renderthis[0] anstemp = renderthis[1] anstemp = anstemp.split("\n") print anstemp ans = anstemp[-2].strip() return render( request, 'polls/showimg.html', { "imageurlcontainer": imageurlcontainer, "ans": ans, "question": question }) else: return HttpResponse("Error brah") else: system("pwd") form = DocumentForm() documents = Document.objects.all() return render(request, 'polls/index.html', {"form": form})
def upload_file(): if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) save_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) doc = Document( document_name=filename, path_to_file=save_path ) database.session.add(doc) database.session.commit() doc_id = doc.get_id() # print(doc_id) rel = Relation( user_id=current_user.id_user, document_id=doc_id, type_relation='loader' ) database.session.add(rel) database.session.commit() file.save(save_path) return redirect(url_for('administrator_pages.upload_file', filename=filename)) return render_template( 'admin_pages/load_files.html', title='Страница загрузки файлов' )
def create_link(self): self.status = "unsigned" self.view_count = 0 self.signed_count = 0 self.version = "0" if self.doc_id is not None: document = Document.Document() thisdocument = document.find_by_doc_id(self.doc_id) new_link_count = getattr(thisdocument, "link_count", 0) new_link_count += 1 self.link = self.doc_id + "_" + str(new_link_count) thisdocument.link_count = new_link_count thisdocument.update() if thisdocument.render == "latex": '''If the document has a repo then we can name the version after the last commit''' with tempfile.TemporaryDirectory() as tmpdir: clone = 'git clone ' + thisdocument.doc_url subprocess.check_output(clone, shell=True, cwd=tmpdir) repo_name = os.listdir(tmpdir)[0] filesdir = os.path.join(tmpdir, repo_name) myoutput = subprocess.check_output("git rev-parse HEAD", shell=True, cwd=filesdir) self.version = myoutput.decode(encoding="ascii", errors="ignore") self.version = self.version.rstrip() result = self.create() else: return False return self
def create_document(args, user): """ Create a new document, store and indexing the data :param args: document data :param user: current user :return: create True if the document is created successfull or False if not """ try: validate_file_type(args['document'], 'document') file_size = get_file_size(args['document']) filename = document_file.save(args['document']) document = Document(filename=filename, size=file_size) # : define document's parser parser = PDFParser(document.file, max_pages=app.config['MAX_PAGES']) # : extracting metadata metadata = parser.read_metadata() # : generating thumbnail thumb_name = generate_thumbnail(parser) document.thumbnail = thumb_name # : saving the document save_document(document, metadata, user) # :indexing document index_document(document, metadata) return {'create': True, 'message': _('Success creation')} except UploadNotAllowed: return {'create': False, 'message': _('Name of document has errors')} except FileTypeError as ex: return {'create': False, 'message': ex.message}
def post(self, userid): try: json_data = json.loads(self.request.body.decode('utf-8')) # TODO convert this to a validator function if not json_data.get("doc_url"): self.write(json.dumps({"response": "Error, White paper url not found"})) if not json_data.get("doc_name"): self.write(json.dumps({"response": "Error, White paper name not found"})) if not json_data.get("doc_main_tex"): json_data["main_tex"] = "main.tex" if not json_data.get("contract_url"): json_data["contract_url"] = "" if not json_data.get("email_body_html"): json_data["email_body_html"] = "" if not json_data.get("email_body_txt"): json_data["email_body_txt"] = "" if not json_data.get("render"): json_data["render"] = "google" if not json_data.get("type"): json_data["type"] = conf.CONTRACT if not json_data.get("doc_description"): json_data["doc_description"] = " It is required to sign this before you can continue. Please\ read carefully and sign to continue." if not json_data.get("doc_getit_btn"): json_data["doc_getit_btn"] = "Sign to receive the document!" if json_data.get("type") == conf.CONTRACT and \ json_data.get("contract_url") == "" and \ json_data.get("doc_url") != "": json_data["contract_url"] = json_data.get("doc_url") if json_data.get("type") == conf.NDA and ( json_data.get("contract_url") == "" or json_data.get("doc_url") == ""): self.write(json.dumps({ "response": "Error, Couldn't create the document, " "no valid urls provided" })) if json_data.get("type") == conf.DOCUMENT and \ json_data.get("doc_url") == "": self.write(json.dumps({ "response": "Error, Couldn't create " "the document, no valid urls provided" })) doc = Document.Document() doc.__dict__ = json_data userjson = ast.literal_eval(userid) result = create_dynamic_endpoint(doc, userjson) if result is not False: self.write(json.dumps({"doc_id": result})) else: self.write(json.dumps({"response": "Error, Couldn't create the document"})) except Exception as e: logger.info("error creating endpoint" + str(e)) self.write(json.dumps({"response": "Error the parameters are incorrect please send a valid json"}))
def __process_Document(entry, obj, g): full_name = __child_value_by_tags(entry, 'FILE') name = path.basename(full_name).decode('utf-8').strip() known = Document.objects.filter(docfile=name).exists() if not known and not gedcom_storage.exists(name): return None kind = __child_value_by_tags(entry, 'TYPE') if known: m = Document.objects.filter(docfile=name).first() else: m = Document(gedcom=g, kind=kind) m.docfile.name = name if kind == 'PHOTO': try: make_thumbnail(name, 'w128h128') make_thumbnail(name, 'w640h480') except Exception as e: print e print ' Warning: failed to make or find thumbnail: %s' % name return None # Bail on document creation if thumb fails m.save() if isinstance(obj, Person) and \ not m.tagged_people.filter(pointer=obj.pointer).exists(): m.tagged_people.add(obj) elif isinstance(obj, Family) and \ not m.tagged_families.filter(pointer=obj.pointer).exists(): m.tagged_families.add(obj) return m
def uploadAFile(request): # Handle file upload if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) some_file = File(request.FILES['docfile']) # import pdb # pdb.set_trace() # print averageMinuteFileContent if form.is_valid(): path = default_storage.save('averaging.txt', ContentFile(some_file.read())) averageFileName = averaging(path, some_file.name) averageMinuteFile = averageFileName + '_minute.csv' averageHourFile = averageFileName + '_hour.csv' averageDailyFile = averageFileName + '_daily.csv' fileHandlerMinute = open(averageMinuteFile, 'rb') fileHandlerHour = open(averageHourFile, 'rb') fileHandlerDay = open(averageDailyFile, 'rb') averageMinuteFileHandle = File(fileHandlerMinute) averageHourFileHandle = File(fileHandlerHour) averageDayFileHandle = File(fileHandlerDay) # print averageMinuteFileHandle.__dict__ # print some_file.__dict__ newdoc = Document( podId=request.POST['podId'], location=request.POST['location'], startDate=request.POST['startDate'], endDate=request.POST['endDate'], podUseType=request.POST['podUseType'], pollutantOfInterest=request.POST['pollutantOfInterest'], podUseReason=request.POST['podUseReason'], projectName=request.POST['projectName'], mentorName=request.POST['mentorName'], school=request.POST['school'], userName=request.user.first_name, docfile=some_file, averageMinuteFile=averageMinuteFileHandle, averageHourFile=averageHourFileHandle, averageDayFile=averageDayFileHandle # averageMinuteFile=averageMinuteFileHandle ) # print request.FILES['docfile'] newdoc.save() os.remove(averageMinuteFile) os.remove(averageHourFile) os.remove(averageDailyFile) # Redirect to the document list after POST return HttpResponseRedirect(reverse('uploadedFiles')) else: form = DocumentForm() # A empty, unbound form # Render list page with the documents and the form return render(request, 'upload.html', {'form': form})
def upload(request): name = "" if request.method == 'POST' and request.POST.get('search', "") == "": context = {} form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newDoc = Document(docfile=request.FILES.get('docfile', "")) name = form.cleaned_data['docfile'].name newDoc.save() documents = Document.objects.all() if name != "": location = "../media/uploads/" + name s = convert_video_to_frame(cwd + "/media/uploads/" + name) sorted_inverted = model_to_inverted_index(s) print sorted_inverted convert_from_mili(sorted_inverted) val = 1 if len(Index.objects.filter()) > 0: val = len(Index.objects.filter()) + 1 b = Index(invertedIndex=json.dumps(sorted_inverted), name=name, video_id=val, images=json.dumps(s)) vid_id = b.video_id b.save() return redirect("/video/%s" % vid_id) return render(request, 'home.html', context) if request.method == 'GET': return render(request, 'upload.html')
def view_docs(): document_list = [] error = '' username = '' success = '' doc_len = 0 user = User.User() if 'user' in session: username = session['user']['username'] # we get all the user data by the username user = user.find_by_attr("username", username) else: logger.info("The user is not logued in") return redirect(url_for('login')) success = request.args.get('success', '') if request.method == 'GET' or request.method == 'POST': docs = Document.Document() docs = docs.find_by_attr("org_id", user.org_id) document_list = docs doc_len = len(document_list) return render_template('view_docs.html', error=error, document_list = document_list, doc_len=doc_len, base_url = PDF_URL, success=success)
def index(request, document_id=None): if not document_id: document_id = 'default' base_url = '{}://{}{}'.format( 'https' if request.is_secure() else 'http', request.META.get('HTTP_HOST') or 'localhost', reverse('index-default')) if base_url.endswith('/'): base_url = base_url[:-1] try: doc = Document.objects.get(eid=document_id) except Document.DoesNotExist: doc = Document(eid=document_id) context = { 'document_id': document_id, 'document_title': doc.title, 'document_content': doc.content, 'document_version': doc.version, 'base_url': base_url } resp = render(request, 'editor/index.html', context) resp['Cache-Control'] = 'no-store, must-revalidate' return resp
def save(self): doc = Document() doc.file = self.document_path doc.type = self.document_type.data doc.filename = self.document_name.data db.session.add(doc) db.session.commit()
def encode(request): if request.method == 'POST' and request.FILES['image']: form = Document() form.Image = request.FILES['image'] form.save() text = request.POST['data'] img = Image.open(settings.MEDIA_ROOT + '/' + form.Image.name) encoded_file = "enc" + form.Image.name img_encoded = encode_image(img, text) if img_encoded: img_encoded.save(encoded_file) print "Message Encoded! " os.startfile(encoded_file) response = HttpResponse(content_type='application/force-download') response[ 'Content-Disposition'] = 'attachment; filename=%s' % smart_str( encoded_file) else: print "FAIL!!!!!" return redirect('home') else: print "FORM FAIL" return render(request, 'send.html')
def create_order(): user_authenticated_id = get_jwt_identity() helper_id = request.form.get('helper_id') description = request.form.get('description') order = Order(description=description, helper_id=helper_id, status_id=1) order.save() if os.environ.get('AWS_S3_BUCKET_NAME'): files = request.files for key in files: file = files[key] if file: url_document = upload_file_to_s3( file, os.environ.get('AWS_S3_BUCKET_NAME')) if url_document: document = Document(name=file.filename, url=url_document, order=order, user_id=user_authenticated_id) document.save() else: print("Faltan las credenciales de AWS") DBManager.commitSession() orderSerialized = order.serialize() if orderSerialized: new_order_mail(order.helper, order) #Añadir los documentos al objeto orderSerialized["documents"] = list( map(lambda document: document.serialize(), order.documents)) return jsonify({"status": "ok", "order": orderSerialized})
def create_order(): user_authenticated_id = get_jwt_identity() helper_id = request.form.get('helper_id') description = request.form.get('description') long_description = request.form.get('long_description') order = Order(description=description, long_description=long_description, helper_id=helper_id, status_id=1) order.save() documentURL = request.form.get('files') if documentURL: filename = 'Documents URL' document = Document(name=filename, url=documentURL, order=order, user_id=user_authenticated_id) document.save() DBManager.commitSession() orderSerialized = order.serialize() if orderSerialized: new_order_mail(order.helper, order) # Añadir los documentos al objeto orderSerialized["documents"] = list( map(lambda document: document.serialize(), order.documents)) return jsonify({"status": "ok", "order": orderSerialized})
def save_video(id): user_authenticated_id = get_jwt_identity() order = Order.query.get(id) if os.environ.get('AWS_S3_BUCKET_NAME'): files = request.files print("files", files) for key in files: file = files[key] if file: url_document = upload_file_to_s3( file, os.environ.get('AWS_S3_BUCKET_NAME')) if url_document: document = Document(name=file.filename, url=url_document, order=order, user_id=user_authenticated_id) document.save() else: print("Faltan las credenciales de AWS") DBManager.commitSession() order_new_data_mail(order) return jsonify(order.serializeForEditView()), 200
def index(request): # Handle file upload context = {} if request.method == 'POST': form = SearchForm(request.POST, request.FILES) if form.is_valid(): try: newdoc = Document(docfile=request.FILES['docfile']) newdoc.save() query = str(newdoc.docfile) except: query = str(Document.objects.all().order_by('-id')[0].docfile) limit = form.cleaned_data['search_limit'] search_method = form.cleaned_data['search_by'] distance_model = form.cleaned_data['distance_model'] if search_method == 'sift': results = subprocess.Popen('python sift_feature/search.py -v sift_feature/vocabulary.npy -i sift_feature/index.csv -q media/%s -l %s -d %s' % (query, limit, distance_model), shell=True, stdout=subprocess.PIPE).stdout.read() # noqa else: results = subprocess.Popen('python color_feature/search.py -i color_feature/index.csv -q media/%s -l %s -d %s' % (query, limit, distance_model), shell=True, stdout=subprocess.PIPE).stdout.read() # noqa print results context['results'] = results.split('\n')[:-1] context['query'] = query else: form = SearchForm() # A empty, unbound form context['form'] = form # Render list page with the documents and the form return render_to_response( 'index.html', context, context_instance=RequestContext(request) )
def new_document(payload): body = request.get_json() # Get information from submitted lat = body.get('lat', None) lon = body.get('lon', None) category = body.get('category', None) name = body.get('name', None) description = body.get('description', None) document_id = Document.generate_document_id() try: new_document = Document(lat=lat, lon=lon, category=category, name=name, description=description, document_id=document_id) new_document.insert() # Generate QRcode using the document information document_formatted = new_document.format() Document.generate_qrcode(document_formatted) return jsonify({ 'success': True, 'new_document': document_formatted, }) except BaseException: print(sys.exc_info()) abort(400)
def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create(username=self.username) self.user.set_password(self.password) self.user.save() perspective, created = Perspective.objects.get_or_create( name='default') perspective.set_default_user() perspective.save() ModuleSetting.set('default_perspective', perspective.id) self.folder = Folder(name='test') self.folder.set_default_user() self.folder.save() self.document = Document(title='test_document', folder=self.folder) self.document.set_default_user() self.document.save() self.file = File(name='test_file', folder=self.folder) self.file.set_default_user() self.file.save() self.link = WebLink(title='test', folder=self.folder, url='test') self.link.set_default_user() self.link.save()
def document_add_typed(request, folder_id=None, response_format='html'): "Document add to preselected folder" folder = None if folder_id: folder = get_object_or_404(Folder, pk=folder_id) if not request.user.profile.has_permission(folder, mode='x'): folder = None document = Document() if request.POST: if 'cancel' not in request.POST: form = DocumentForm( request.user.profile, folder_id, request.POST, instance=document) if form.is_valid(): document = form.save() document.set_user_from_request(request) return HttpResponseRedirect(reverse('documents_document_view', args=[document.id])) else: return HttpResponseRedirect(reverse('document_index')) else: form = DocumentForm(request.user.profile, folder_id) context = _get_default_context(request) context.update({'form': form, 'folder': folder}) return render_to_response('documents/document_add_typed', context, context_instance=RequestContext(request), response_format=response_format)
def executeUpload(request): # Save the files form = DocumentForm(request.POST, request.FILES) file_new = request.FILES['docfile'] if form.is_valid(): #Save temporary file newdoc = Document(docfile=file_new) newdoc.save() fn = file_new.name fn = fn.replace(" ", "_") #Move the file to the new folder src = os.path.join(django_settings.FILE_UPLOAD_TEMP_DIR, 'uploads', fn) file_upload = src path = os.path.join(request.session['projectPath'], 'Uploads') target = os.path.join(path, fn) if os.path.exists(target): os.remove(target) shutil.move(src, path) #Delete the temporary file newdoc.delete()
def getAll(self): doc = [] for instances in self.session.query(Document).order_by(Document.id): doc.append( Document(title=instances.title, url=instances.url, words=instances.words)) return doc
def __init__(self, doc_id=None): if doc_id: try: doc = Document.Document() self.document = doc.find_by_doc_id(doc_id) user = User.User() self.user = user.find_by_attr("org_id", self.document.org_id) except Exception as e: logger.info(F"[error renderPDF init] obtaining document from docid: {str(e)}")
def test_model_document(self): """Test Document Model""" folder = Folder(name='test') folder.save() obj = Document(title='test', folder=folder) obj.save() self.assertEquals(folder, obj.folder) self.assertNotEquals(obj.id, None) obj.delete()
def fileuploader(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(docfile=request.FILES['docfile']) newdoc.save() return HttpResponseRedirect('/docViewer/confirmation/') else: form = UploadFileForm() return render_to_response('fileuploader.html', {'form': form})
def save_video(id): user_authenticated_id = get_jwt_identity() video = request.json.get('video', None) order = Order.query.get(id) document = Document(name="Video", url=video, order=order, user_id=user_authenticated_id) document.save() DBManager.commitSession() return jsonify(order.serializeForEditView()), 200
def _doc_get_or_create(eid): try: doc = Document.objects.get(eid=eid) except Document.DoesNotExist: try: doc = Document(eid=eid) doc.save() except IntegrityError: doc = Document.objects.get(eid=eid) return doc
def test_changed(self): """ Test that FileField.save_form_data, if passed a truthy value, updates its instance attribute. """ d = Document(myfile='something.txt') self.assertEqual(d.myfile, 'something.txt') field = d._meta.get_field('myfile') field.save_form_data(d, 'else.txt') self.assertEqual(d.myfile, 'else.txt')