I'm developing my Django website since about 2 months and I begin to get a good global result with my own functions.
But, now I have to start a very hard part (to my mind) and I need some advices, ideas before to do that.
My Django website creates some PDF files from HTML templates with Django variables. Up to now, I'm saving PDF files directly on my Desktop (in a specific folder) but it's completely unsecured.
So, I installed another web application which is named LogicalDoc in order to save PDF file directly on this application. PDF files are created and sent to LogicalDoc.
LogicalDoc owns 2 API : SOAP and REST (http://wiki.logicaldoc.com/rest/#/) and I know that Django could communicate with REST method.
I'm reading this part of Django documentation too in order to understand How I can process : https://docs.djangoproject.com/en/dev/topics/http/file-uploads/
I made a scheme in order to understand what I'm exposing :
Then, I write a script which makes some things :
When the PDF file is created, I create a folder inside LogicalDoc which takes for example the following name : lastname_firstname_birthday
Two possibilities : If the folder exists,I don't create a new folder, else I create it.
Once it's done, I send the PDF file directly inside the folder by comparing PDF name with folder name to do that
I have some questions about this process :
Firstly, is it possible to make this kind of things ?
Is it hard to do that ?
What kind of advices could you give me ?
Thank you so much !
PS : If you need some part of my script, mainly PDF creating part, I can post it just after my question ;)
An idea is pretty simple, however it always requires some practice.
I strongly advice you to use REST api and forget about SOAP as the only thing it can bring to you - is 'pain' :)
If we check documentation, document/create it gives next information.
Endpoint we have to communicate with.
[protocol]://[server]:[port]/document/create
HTTP method to use - POST
List of parameters to provide with your request: body,
document, content
Even more, you can test API by clicking on "Try it out" button and check requests in "Network" tab of your browser (if you open Developer Tools)
I am not sure what kind of metadata do you have to provide in 'document' parameter but what I know you can easy get an idea of what should be done by testing it and putting XML or JSON data into 'document' parameter.
Content is an array of bytes transferred to the server (which would be your file).
To sum up, a request to 'document/create' uri will be simple
body = { 'headers': {},'object': {},}
document = "<note>data</note>"
content=open('report.xls', 'rb') #r - reading, b - binary
r = requests.post('http://logicaldoc/document/create', body=body, document=document, content=content)
Please keep in mind that file transferring requests take time and sometimes you may get timeout exception. Your code will stop and will be waiting for response, so it may be a good idea to get some practice with asyncio or celery. Just keep in mind those kind of possible issues.
Related
Goal: I want to send the result of a <script> tag in HTML to a server of any kind.
I am working with the Spotify authorization API for a project and one of the required keys is contained in a query string at the end of my app’s redirect URI. I need to get this key.
My solution is to set the redirect URI to a redirect page. Then, on the page, automatically run a script that gets the current URL and send it to my Python script for use. If there’s another way, please tell me, because I’m pretty stuck.
However, to send variables between HTML and Python, I have found I need to use a simple server. Setting this up is the hard part. I’ve made Java, Node.js, and Python servers, but nothing seems to want to work with the <string> tag, and I’m doubtful something like this would even get the actual output of the script.
Is there a way to do this? This is a pretty long question and I apologize, and it should probably be noted I’m a beginner so an explanation and code examples would be nice. Thank you to anyone who reads this!
I am working on application where I am giving a functionality to user where they can signin from dropbox by using my application but when I am trying to get the file from Dropbox I am unable to do it. When user choose the desired file from dropbox and click on choose and then nothing happen. Can anyone please help out either is it possible to do it or not? If yes, how can we do it? I am at beginner level. Please help me out and explain the whole process to do it in detail.
Are you using public Dropbox files?
Then you just need to fetch the item by URL and download it. If this happens in a browser tool, you'll need JavaScript and not Python to download it.
Or you leave out JS and just use Python to render an HTML page where a button is for a Dropbox file and clicking the button triggers a download of the file. That is a generic HTML task you can search for.
If you need access to sign in Dropbox and view private files, consider using a Python library built around Dropbox.
See the Python Dropbox guide. Are you using a library like that? Please share as your question was vague.
https://www.dropbox.com/developers/documentation/python#
Also please share an explanation of what your logic is or a small code snippet. I can't see what you are doing yet so I don't know where you are missing something or making a mistake.
From the screenshot, I see you're using the Dropbox Chooser. That's a pre-built way to let your end-users select files from their Dropbox accounts and give them to your app.
Make sure you implement the success and cancel callback methods as documented there for the Chooser.
In the success callback, you'll get the information for the selected file(s). That occurs in JavaScript in the browser though, so if you need that on your server, you'll need to write some JavaScript to send that up to your server, e.g., via an AJAX call or a form or whatever means you use in your app.
So I had a number of amino acid sequence strings that I wanted to use as input into a tool that studies its interactions with certain components of the human immune system (http://www.cbs.dtu.dk/services/NetMHCcons/).
I wanted to ask what, if any, would be a way of accessing, inputting data and getting the output, via a script (R or python preferably). My main issue was I had a lot of sequences that need to be queried separately so wanted to automate the whole thing. The website has one field that reads "Submission" which takes in the string input. There is another field "select species/loci" which gives a drop down menu from which an option needs to be selected. Lastly there's a "submit" button. The output simply loads on the page after hitting submit.
I've tentatively poked around with RSelenium and Rcurl but wanted to ask if there was a more efficient method.
I took a look at what it'd take to send a POST request to this service from Python, and it looks possible:
this form takes in "multipart/form-data" (see: How to send a "multipart/form-data" with requests in python?), you'll need to send your data in this format. You could inspect a request from the browser (using the dev tools) and copy the fields from there as a starting point.
once the form is submitted, it doesn't give you the result right away. You'd need to get your job ID from the response, and then poll the URL: http://www.cbs.dtu.dk/cgi-bin/webface2.fcgi?jobid={your_job_id}&wait=20 until it gives you the result
the result will then need to be downloaded and parsed
This tool is however available as a portable version for linux/mac: https://services.healthtech.dtu.dk/software.php
Perhaps downloading this version would make it easier?
Try this :
Submitting to a web form using python
This link is an answer to how to send web forms in python, using urllib. Check your source code and extract the necessary data using re module from the source code of the link you have put up, and send the request.
save the HTML source code of http://www.cbs.dtu.dk/services/NetMHCcons/ in the python file as
source_code = '''...'''
The HTML can be found by using CTRL+U in firefox.
My problem is: I need to generate a csv file from the data I have in the database and then download it. Seems easy, but I cannot find a solution that will allow me to do it in the background so the user won't be redirected anywhere.
What I need is user clicking a button and the download starting immediately with the user staying on the same page. Something like a simple html download attribute, but with actually generating a file first. I cannot generate file in advance as the data changes all the time and I need to output the most recent version of it, also, user may specify some filters for the data, so I need to take it into account.
I use Django with Nginx as a production server. Django should always return a response, so I'm not sure I would be able to do what I want at all. Is there any different way to do it then?
I've searched for a long time, but unable to find anything similar to my request. Thanks everyone!
This problem is just a more in depth explanation of the other problem.
I put the code on github because it's quite a fair bit. I'm sorry for the inconvenience
https://github.com/lonehangman/mccdropdav/blob/master/views.py
Line 100 onwards is where the problems start. For the past week I've tried and failed to upload a file from pages (on iPad) to dropbox through a webdav set up on Google App Engine (GAE).
I can view the contents of my dropbox but when I download or upload from my computer or iPad it always gives an error. I checked the logs and saw that there was a key error for line 110, so I tried to fix this by printing meta_entry, which I then realised was a non existent resource.
(Hence the # make a fake Resource to ease our exporting. On line 106).
This problem left me befuddled for days, but then my friend told me to remove line 152 root.append(self.export_meta_entry(metadata,href=self.request.path)) # first response's href contains exactly what you asked for (relative path)
Doing this got rid of the key error but still wouldn't let me upload to dropbox. The iPad tells me it's uploading but I check my dropbox but no new files are to be seen. Nor can I download files.
The logs don't seem to be picking it up.
Can anyone please explain and try help (If you're not going to help don't bother writing stuff like 'go somewhere else' or 'learn python noob'.)
If there is any more information needed just ask.
I'm quite tired at the time of writing this so it does seemed a bit rushed.
Not looking your entire app, but only your put method (calling Dropbox put_file)
def put(self):
path = '/' + self.request_path
self.client.put_file(ROOT, os.path.dirname(path), self.request.body_file, file_name=os.path.basename(path))
self.response.set_status(201,'Created')
Dropbox API tutorial example of put_file
f = open('working-draft.txt')
response = client.put_file('/magnum-opus.txt', f)
print "uploaded:", response
as well as put_file documentation say the first argument of put_file is the complete path of the file in dropbox tree (including the name of the file), and the second argument is a file-like object.
So something like that may be better
self.client.put_file(path, self.request.body_file)
As you can see, you may look at the return value of put_file to get further information on what happened in your put_filecall. You may adjust your response code according to the response from Dropbox.