def test_add_doc(self): # Add a document to the repo doc = Document.objects.create(docid='mydoc', repo=self.repo) # Query repo qds = self.repo.docs.filter(docid='mydoc') # Remove document from repo Document.delete(qds.first())
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 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 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 __process_Document(entry, obj, g): name = __valid_document_entry(entry) if not name: return None file_name = 'gedcom/%s' % name known = Document.objects.filter(docfile=file_name) if len(known) > 0: m = known[0] else: kind = __child_value_by_tags(entry, 'TYPE') m = Document(gedcom=g, kind=kind) m.docfile.name = file_name if kind == 'PHOTO': try: make_thumbnail(name) thumb = path.join('default/thumbs', name) except: print ' Warning: failed to make or find thumbnail: %s' % name return None # Bail on document creation if thumb fails else: thumb = None if thumb is not None: m.thumb.name = thumb 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 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 cargar_mod(request): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(docfile=request.FILES['docfile']) newdoc.save() #print(newdoc.docfile.name) manejo_mods.insmod('/home/pi/SO2TP3PerezFederico/PaginaTP3/media/'+newdoc.docfile.name) archivo = open('salida.txt','r') lista = [] for linea in archivo: lista.append(linea) return render_to_response( 'list.html', {'documents': lista}, context_instance=RequestContext(request)) # Redirect to the document list after POST # return HttpResponseRedirect(reverse('cargar_mod')) else: form = DocumentForm() # A empty, unbound form # Render list page with the documents and the form return render_to_response( 'list.html', {'form': form}, context_instance=RequestContext(request) )
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 investing(request): # Handle file upload file_form = FileForm(request.POST) form = DocumentForm(request.POST, request.FILES) if request.method == 'POST' and "uploadFile" in request.POST: # Upload if form.is_valid(): newdoc = Document(docfile = request.FILES['docfile']) newdoc.save() # Redirect to the document list after POST return HttpResponseRedirect(reverse('dataloader.views.investing')) elif request.method == 'POST' and "invest" in request.POST: # Make Investments if file_form.is_valid(): file_name = file_form.cleaned_data['file_input'] print "### FILE FOR INVESTMENTS: " + file_name file_path = settings.MEDIA_ROOT + "/" + file_name print file_path cr = csv.reader(open(file_path)) # Starting from second row for row in cr: if row[0] == 'fund' or row[0] == 'individual': investment_manager.fund(row) elif row[0] == 'buy': investment_manager.buy(row) elif row[0] == 'sell': investment_manager.sell(row) elif row[0] == 'sellbuy': investment_manager.sellbuy(row) return HttpResponseRedirect(reverse('dataloader.views.investing')) elif request.method == 'POST' and "clear" in request.POST: # Clear Investments print "### This should clear everything" Activity.objects.all().delete() StakeHold.objects.all().delete() Port_Indi.objects.all().delete() print "### OK check porfolio page" else: form = DocumentForm() # A empty, unbound form file_form = FileForm() # Load documents for the list page documents = Document.objects.all() return render_to_response( 'nav/investing.html', {'documents': documents, 'form': form, 'file_form':file_form}, context_instance=RequestContext(request) )
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 testAccount(self): Doc.objects(name=DOC_NAME)[0].delete( ) #strange bug happens when full collection from db is deleted :| logger.debug('Start TestUI::testAccount.') USER_NAME = 'testLogin' driver = self.driver driver.get(self.base_url) time.sleep(1) driver.find_element_by_css_selector("i.fa.fa-sign-in").click() time.sleep(1) driver.find_element_by_id("id_username").clear() driver.find_element_by_id("id_username").send_keys(USER_NAME) driver.find_element_by_id("id_password").clear() driver.find_element_by_id("id_password").send_keys(USER_NAME) driver.find_element_by_css_selector("input[type=\"submit\"]").click() time.sleep(1) self.assertEqual( "Your username and password didn't match. Please try again.", driver.find_element_by_css_selector("p").text) User.create_user(USER_NAME, USER_NAME) time.sleep(1) driver.get(self.base_url) time.sleep(1) driver.find_element_by_css_selector("i.fa.fa-sign-in").click() time.sleep(1) driver.find_element_by_id("id_username").clear() driver.find_element_by_id("id_username").send_keys(USER_NAME) driver.find_element_by_id("id_password").clear() driver.find_element_by_id("id_password").send_keys(USER_NAME) driver.find_element_by_css_selector("input[type=\"submit\"]").click() time.sleep(1) self.assertEqual("Welcome, " + USER_NAME + ".", driver.find_element_by_css_selector("p").text) PRIVATE_DOC_NAME = "prywatny" driver.find_element_by_class_name("has-menu").click() driver.find_element_by_id("newDocumentMenu").click() time.sleep(1) driver.find_element_by_id("documentNameAtStart").clear() driver.find_element_by_id("documentNameAtStart").send_keys( PRIVATE_DOC_NAME) driver.find_element_by_id("privateFlagAtStart").click() driver.find_element_by_id("saveDocumentButtonAtStart").click() time.sleep(10) logger.debug('Critical point of TestUI::testAccount.') doc = Doc.objects(name=PRIVATE_DOC_NAME)[0] self.assertEqual(doc.name, PRIVATE_DOC_NAME) self.assertTrue(doc.priv) driver.get(self.base_url) time.sleep(1) driver.find_element_by_class_name("has-menu").click() driver.find_element_by_id("documentListButton").click() time.sleep(1) self.assertEqual(PRIVATE_DOC_NAME, driver.find_element_by_css_selector("td").text) driver.find_element_by_id('accounts').click() driver.find_element_by_id("logout").click() time.sleep(1) with self.assertRaises(NoSuchElementException): driver.find_element_by_css_selector("td") logger.debug('End TestUI::testAccount.')
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 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 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 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 list(request): # Handle file upload if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(pk=1) f = request.FILES['docfile'] newdoc.docfile = f newdoc.name = f.name newdoc.save() # Redirect to the document list after POST #return HttpResponseRedirect(reverse('myproject.myapp.views.list')) else: form = DocumentForm() # A empty, unbound form # Load documents for the list page try: document = Document.objects.get(pk=1) except Document.DoesNotExist: document = None #update game if gameHeart: gameHeart.restart() # Render list page with the documents and the form return render_to_response( 'list.html', {'document': document, 'form': form}, context_instance=RequestContext(request) )
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 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 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 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 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 upload_document_with_type(request, document_type_id, multiple=True): check_permissions(request.user, "documents", [PERMISSION_DOCUMENT_CREATE]) document_type = get_object_or_404(DocumentType, pk=document_type_id) local_form = DocumentForm(prefix="local", initial={"document_type": document_type}) if USE_STAGING_DIRECTORY: staging_form = StagingDocumentForm(prefix="staging", initial={"document_type": document_type}) if request.method == "POST": if "local-submit" in request.POST.keys(): local_form = DocumentForm( request.POST, request.FILES, prefix="local", initial={"document_type": document_type} ) if local_form.is_valid(): try: if (not UNCOMPRESS_COMPRESSED_LOCAL_FILES) or ( UNCOMPRESS_COMPRESSED_LOCAL_FILES and not _handle_zip_file(request, request.FILES["local-file"], document_type) ): instance = local_form.save() _handle_save_document(request, instance, local_form) messages.success(request, _(u"Document uploaded successfully.")) except Exception, e: messages.error(request, e) if multiple: return HttpResponseRedirect(request.get_full_path()) else: return HttpResponseRedirect(reverse("document_list")) elif "staging-submit" in request.POST.keys() and USE_STAGING_DIRECTORY: staging_form = StagingDocumentForm( request.POST, request.FILES, prefix="staging", initial={"document_type": document_type} ) if staging_form.is_valid(): try: staging_file = StagingFile.get(staging_form.cleaned_data["staging_file_id"]) if (not UNCOMPRESS_COMPRESSED_STAGING_FILES) or ( UNCOMPRESS_COMPRESSED_STAGING_FILES and not _handle_zip_file(request, staging_file.upload(), document_type) ): document = Document(file=staging_file.upload(), document_type=document_type) document.save() _handle_save_document(request, document, staging_form) messages.success( request, _(u"Staging file: %s, uploaded successfully.") % staging_file.filename ) if DELETE_STAGING_FILE_AFTER_UPLOAD: staging_file.delete() messages.success(request, _(u"Staging file: %s, deleted successfully.") % staging_file.filename) except Exception, e: messages.error(request, e) if multiple: return HttpResponseRedirect(request.META["HTTP_REFERER"]) else: return HttpResponseRedirect(reverse("document_list"))
def create_document(request): now = datetime.datetime.now() document = Document() document['title'] = 'New document for %s'%now.strftime('%d/%m/%Y at %H:%M') document['content'] = 'This is a new document created to test our persistence framework' document.save() return HttpResponseRedirect('/my-documents/')
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 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 testCreateDocument(self): create_document({}, DOC_NAME, '') doc = Doc.objects(name=DOC_NAME)[0] self.assertTrue(doc.text == '') self.assertTrue(doc.name == DOC_NAME) Doc.objects.delete() create_document({}, DOC_NAME, 'test') doc = Doc.objects(name=DOC_NAME)[0] self.assertTrue(doc.text == 'test') self.assertTrue(doc.name == DOC_NAME)
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 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 testdoc(): #Check username against cache my_new_user = Document() my_new_user.barcode = '123' my_new_user.image = '/static/images/demo.png' my_new_user.doc_type = 1 db_session.add(my_new_user) db_session.commit() # Check permissions flash('new document added') return render_template('index.html')
def new(request): if request.method == 'POST': title = request.POST.get('title') body = request.POST.get('body') document = Document(title=title, body=body) document.save() return redirect('reading:index') return render(request, 'reading_assist/new.html', {})
def document(request, document_id): if request.method == 'GET': try: doc = Document.objects.get(eid=document_id) except Document.DoesNotExist: doc = Document(eid=document_id) resp = JsonResponse(doc.export()) resp['Cache-Control'] = 'no-store, must-revalidate' return resp else: return HttpResponseNotAllowed(['GET'])
def upload_file(request): if not request.user.is_authenticated(): return HttpResponse('fail unlogin') try: upload_file = request.FILES['file'] doc = Document(owner = request.user, public = False, doc_name = upload_file.name, doc_file = upload_file) doc.save() except: return HttpResponse('fail upload fail') else: return HttpResponseRedirect('/homepage/')
def suggestions(request, sample_id=1): try: suggestions_doc = Document.objects.get(title="Feature Suggestions") except Document.DoesNotExist: suggestions_doc = Document() suggestions_doc.title = "Feature Suggestions" suggestions_doc.save() page_context = { } page_context["object"] = suggestions_doc return render(request, 'suggestions.html', page_context)
def suggestions(request, sample_id=1): try: suggestions_doc = Document.objects.get(title="Feature Suggestions") except Document.DoesNotExist: suggestions_doc = Document() suggestions_doc.title = "Feature Suggestions" suggestions_doc.save() page_context = {} page_context["object"] = suggestions_doc return render(request, 'suggestions.html', page_context)
def testHandleCreateDocument(self): msg = {'name': DOC_NAME, 'text': EMPTY_DOC_STRING, 'priv': False} handle_create_document(msg, {}) id = msg['id'] created_document = Doc.objects(id=id)[0] self.assertTrue(created_document['name'] == DOC_NAME) self.assertTrue(created_document['text'] == EMPTY_DOC_STRING) msg = {'name': DOC_NAME, 'text': SAMPLE_TEXT, 'priv': False} handle_create_document(msg, {}) id = msg['id'] created_document = Doc.objects(id=id)[0] self.assertTrue(created_document['name'] == DOC_NAME) self.assertTrue(created_document['text'] == SAMPLE_TEXT)
def save(self): doc = Document() unique_filename = str(uuid.uuid4()) extension = self.filename.data.filename.split('.')[-1] doc.type = extension doc.file = os.path.join(app.config['UPLOAD_FOLDER'], 'uploaded', unique_filename + '.' + extension) doc.filename = self.filename.data.filename db.session.add(doc) try: self.filename.data.save(doc.file) db.session.commit() except Exception as e: db.session.rollback()
def upload_file(request): documents = Document.objects.all() if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(name = request.POST['name'], label = request.POST['label'], docfile = request.FILES['docfile']) newdoc.save() return redirect('/docs') else: form = DocumentForm() documents = Document.objects.all() return render(request, 'network/docs.html', {'documents': documents, 'form': form})
def add_clue(request): if request.method == 'POST': newdoc = Document(docfile=request.FILES.get('file', False)) if newdoc.docfile: newdoc.id = str(request.FILES.get('file').name) newdoc.save() request.session['file'] = newdoc return render_to_response( 'clue.html', {'image': request.session.get('file')}, 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 build_scenario(project_name, translate=False): text_path, my_map, root = build_paths(project_name) with open(my_map, 'r') as file: dico = json.load(file) dico = {**dico, **{k.replace('€', '§'): '§' + v for k, v in dico.items()}} document = Document(text_path, project_name, dico=dico, translate=translate) tex_filename = '{}.tex'.format(project_name) pdf_filename = '{}.pdf'.format(project_name) document.save(tex_filename) os.system("pdflatex {}".format(tex_filename)) os.system("mv {} {}".format(pdf_filename, os.path.join(root))) os.system("rm *.log *.aux *.tex")
def testSaveAs(self): Doc(name=DOC_NAME + '1', text=LOREM_IPSUM).save() logger.debug('TestUI::testSaveAs documents: ' + str(Doc.objects())) driver = self.driver time.sleep(1) driver.get(self.base_url) time.sleep(1) driver.find_element_by_css_selector("td").click() time.sleep(1) driver.find_element_by_id("saveDocument").click() time.sleep(1) driver.find_element_by_id("documentName").clear() driver.find_element_by_id("documentName").send_keys(DOC_NAME) driver.find_element_by_id("saveDocumentButton").click() self.assertTrue(Doc.objects(name=DOC_NAME)[0]['text'] == LOREM_IPSUM)
def decode(request): if request.method == 'POST' and request.FILES['image']: form = Document() form.Image = request.FILES['image'] form.save() img = Image.open(settings.MEDIA_ROOT + '/' + form.Image.name) hidden_text = decode_image(img) messages.info(request, 'The Secret Message is:' + hidden_text) print hidden_text msg = 'The Secret Message is:' + hidden_text return render(request, 'retrieve.html', {"message": msg}) else: print "FORM FAIL" return render(request, 'retrieve.html', {"message": ''})
def parse_bookxcess_html(document, headers, filename=None): """Parses Bookxcess book listings page """ soup = BeautifulSoup(document.contents) links = soup.findAll(['a', 'area'], href=True) parsers = { '.htm': parse_bookxcess_html, '.html': parse_bookxcess_html, '.pdf': parse_bookxcess_pdf } urls = {} for link in links: url = link['href'].strip() if not url.startswith('http://'): url = BOOKXCESS + url urlp = urlsplit(url) path = urlp.path.lower() args = { "filename": basename(path) } ext = splitext(path)[1] if ext in parsers: parser = parsers[ext] urls[url] = (parser, args) for url, (parser, args) in urls.items(): task_name = 'download-%s' % Document.hash_url(url) logging.info('parse_bookxcess_html: downloading %s in task %s' % (url, task_name)) try: deferred.defer(download_page, url, callback=parser, args=args, _name=task_name, _queue='downloader') except (taskqueue.TaskAlreadyExistsError, taskqueue.TombstonedTaskError): pass
def tag_page(request, tag, page): """ Lists documents with a certain tag. """ # Check if the page is cached cache_key = 'tag_page:%s-%s' % (tag, page) cached_response = get_cached_response(request, cache_key) if cached_response: return cached_response # Get the data tag = Tag.get_by_key_name(tag) if not tag: raise Http404 object_list = Document.all().filter('tags =', tag.key()).filter("is_published =", True) paginator = Paginator(object_list, 10) # Limit it to thise page try: page = paginator.page(page) except (EmptyPage, InvalidPage): raise Http404 # Create a response and pass it back context = { 'headline': 'Documents tagged ‘%s’' % tag.title, 'object_list': page.object_list, 'page_number': page.number, 'has_next': page.has_next(), 'next_page_number': page.next_page_number(), 'next_page_url': '/tag/%s/page/%s/' % (tag.title, page.next_page_number()) } return direct_to_template(request, 'document_list.html', context)
def document_list(request, page=1): """ Displays document lists, 10 at a time. """ cache_key = 'document_page:%s' % page cached_response = get_cached_response(request, cache_key) if cached_response: return cached_response else: # Pull the documents object_list = Document.all().filter( "is_published =", True ).order("-publication_date") # Cut it first 10 paginator = Paginator(object_list, 10) try: page = paginator.page(page) except (EmptyPage, InvalidPage): raise Http404 # Create the response context = { 'headline': 'Latest Documents', 'object_list': page.object_list, 'page_number': page.number, 'has_next': page.has_next(), 'next_page_number': page.next_page_number(), 'next_page_url': '/page/%s/' % (page.next_page_number()) } response = direct_to_template(request, 'document_list.html', context) # Add it to the cache memcache.add(cache_key, response, 60) # Pass it back return response
def viewDocs(request): params = {} user = users.get_current_user() # Synchronise with Google Docs Feed if users.IsCurrentUserAdmin(): authsub_url, session_token, client = check_auth(request, user) docs_url = 'http://docs.google.com/feeds/documents/private/full' import gdata.docs.service client = gdata.docs.service.DocsService() gdata.alt.appengine.run_on_appengine(client) getFeed(client, category='document') getFeed(client, category='spreadsheet') getFeed(client, category='presentation') getFeed(client, category='pdf') documents = Document.all() if not users.IsCurrentUserAdmin(): documents.filter('status = ', 'Public') documents.order('-status') params['documents'] = documents return respond(request, 'list.html', params)
def list(request): # Handle file upload # del request.session['file'] # Document.objects.all().delete() if request.method == 'POST': # request.session['file'] = request.FILES.get('file', False) newdoc = Document(docfile=request.FILES.get('file', False)) if newdoc.docfile: newdoc.id = str(request.FILES.get('file').name) newdoc.save() request.session['file'] = newdoc # # Redirect to the document list after POST matches = get_matches(str(request.FILES.get('file').name)) if len(matches)>20: matches = matches[:20] matched_images = [] matched_image_names = [] for im_name in matches: matched_images.append(Image.objects.get(pk=im_name)) matched_image_names.append(im_name) request.session['matches'] = matched_images store_location_information(request, matched_image_names) return HttpResponseRedirect(reverse('contextslices.photo.views.search')) if request.method == 'GET': matched_images = [] if request.session.get('file'): matches = get_matches(request.session.get('file').id) if len(matches) > 20: matches = matches[:20] matched_images = [] matched_image_names = [] for im_name in matches: matched_images.append(Image.objects.get(pk=im_name)) matched_image_names.append(im_name) request.session['matches'] = matched_images store_location_information(request, matched_image_names) return render_to_response( 'results.html', {'file': request.session.get('file'), 'matches': matched_images, 'num_results': len(matched_images)}, context_instance=RequestContext(request) )
def _handle_zip_file(request, uploaded_file, document_type): filename = getattr(uploaded_file, "filename", getattr(uploaded_file, "name", "")) if filename.lower().endswith("zip"): zfobj = zipfile.ZipFile(uploaded_file) for filename in zfobj.namelist(): if not filename.endswith("/"): zip_document = Document( file=SimpleUploadedFile(name=filename, content=zfobj.read(filename)), document_type=document_type ) zip_document.save() _handle_save_document(request, zip_document) messages.success(request, _(u"Extracted file: %s, uploaded successfully.") % filename) # Signal that uploaded file was a zip file return True else: # Otherwise tell parent to handle file return False
def deleteDoc(request, key=None): params = {} document = db.get(key) document.delete() documents = Document.all() params['documents'] = documents return viewDocs(request)
def list(request): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(docfile = request.FILES['docfile']) newdoc.save() return HttpResponseRedirect(reverse('list')) else: form = DocumentForm() documents = Document.objects.all() return render_to_response( 'uploader/list.html', {'documents': documents, 'form': form}, context_instance=RequestContext(request) )
def test_transmute(self): doctype = DocumentType.objects.create(name='transmuted') doc = Document( document_type = doctype, ) doc.save() uf = UploadedFile.transmute_document( doc, uploader=self.test_user, ) uf.save() self.assertEqual(uf.document_ptr_id, doc.id) self.assertEqual(uf.document_ptr, doc) self.assertEqual(uf.document_type, doc.document_type) self.assertEqual(doc.uploadedfile, uf) self.assertRaises(ValueError, UploadedFile.transmute_document, doc, uploader=self.test_user)
def cupload(request): print "1" from pdfviewer.models import Document from django.core.files import File print "2" import glob files= glob.glob("/home/esam/Dropbox/Public/*/*/*/*.pdf") print "3" print files try: for pdf in files: w=Document() print "pdf ",pdf #w.pdf_file=open("/home/esam/Downloads/79e4151366c15de4b2 (1).pdf","r") w.pdf_file=File(open(pdf)) w.save() except: print "error at save file",pdf print "saved",pdf