ReactNative, Apollo and Django: upload files - django

I decided to learn such a technology as GraphQL. For this, I came up with an application (I develop it on ReactNative), a python backend (django-graphene). How the client chose Apollo. I set everything up, everything works, I even did a part already. And here I needed to upload a profile photo. The problem is that I haven't found a way to do this. I used to use the REST API and the client was axios. So I just took the FormData and passed it. Accordingly, the backend accepted and saved as usual. And here I don't even know where to start. I found the apollo-upload-client package, but I didn't find how to integrate it into ReactNative (more precisely, there is a description there, but I didn't understand how to use it correctly). I do it like this:
const fileSubstitute = {
uri: uriFromCameraRoll,
name: "a.jpg",
type: "image/jpeg",
};
I found graphene-file-upload for the backend, it seems to see my files, but I don't know how to save them (by the way, I can transfer the file without apollo-upload-client).
I really like GraphQL, but uploading files is a big deal. If I don't solve it, I will be forced abandon this technology.
P.S. I didn't find any information on how to upload files via GraphQL at all. Maybe it is necessary to work with files in a different way? I will be grateful for any help

Related

Authenticating with Adwords Googleads without local storage

So having trouble finding documentation or tutorials on this. We're at the starts of a project using Python (Django) and are putting together a couple of proof-of-concept functions.
I'm trying to use the googleads python library to authenticate with the adwords api. Currently though, I can only find examples that pull the adwords credentials from local storage:
adwords.AdWordsClient.LoadFromStorage()
The problem is, we'll be storing api keys and the likes in the database as multiple adwords managers may be utilizing the tool. As such, it's not ideal to store the credentials in a static file (it looks like the credentials for loadfromstorage are stored in a yaml file, though I'm having a bit of trouble finding details even on that).
Does anyone have a good example of setting up an adwords client "on the fly", knowing the api key and generating other items as needed.
Let me know y'alls thoughts.
Thanks!
the long version is here here
tl;dr
#in case you need proxy to access the internet
prxx = common.ProxyConfig.Proxy(YOUR_PROXY_HOST, YOUR_PROXY_PORT )
proxy_config = common.ProxyConfig(http_proxy=prxx,https_proxy=prxx,disable_certificate_validation=True)
#init oauth refresh token client
oauth_client = oauth2.GoogleRefreshTokenClient(client_id=YOUR_GOOGLE_CLIENT_ID, client_secret=YOUR_GOOGLE_CLIENT_SECRET,refresh_token=YOUR_GOOGLE_REFRESH_TOKEN, proxy_config=proxy_config )
#adwords client
adwords_client = adwords.AdWordsClient(YOUR_DEVELOPER_TOKEN,oauth_client, user_agent=YOUR_USER_AGENT, client_customer_id=YOUR_CLIENT_CUSTOMER_ID, proxy_config=proxy_config)

How can I modify django-postman to allow sending of attachments?

It doesn't seem as if django-postman supports attachments so I'm trying to add attachment support. I'm thinking of doing it by creating another set of models that will refer to a postman message and then update the views/templates accordingly but it will be a fair amount of work.
Django-postman isn't exactly an SMTP based messaging system so attachments would need to implemented through a different module. I think you should check some of the django file management projects
https://www.djangopackages.com/grids/g/file-managers/
One of the simplest idea I can think of is to save files in some kind of hashed name and associate these names with the postman message .
I think this would be a good addition to postman itself.
So I ended up figuring out how to do this on my own but it could use some work. The additional constraint we had to work with was that we were already using jQuery File Upload to upload files via AJAX so we needed a way to integrate the two.
Our solution was to create an app that contained a new model and a custom reply form that made it relatively easy to link the two together.
I wrote it up at http://dangoldin.com/2013/05/17/adding-attachments-to-django-postman/ and hope it helps others.

Need help setting up django-filetransfers

My setup is: Django 1.3/Python 2.7.2/Win Server 2008 R2/IIS 7.5/MS SQL Server 2008 R2. I am developing an application whose main function is to analyze uploaded files and produce a report.
Reading over the documentation for django-filetransfers, I believe this is a solution to a problem I've been trying to solve for a while (i.e. form-based file uploads completely block all Django responses until the file-transfer finishes...horror for even moderate-sized files).
The documentation talks about piping uploads to S3 or Blobstore, and that might be what I end up doing eventually, but during development I thought maybe I could just set up my own "poor-man's S3" on a server that I control. This would basically just be another Django instance (or possibly a simple ASP.NET app) whose sole purpose is to receive uploaded files. This sounds like it should be possible with django-filetransfers and would solve the problem of Django responsiveness (???).
But I am missing some bits of understanding how this works in general, as well as some specifics. Maybe an example will help: let's say I have MyMainDjangoServer and MyFileUploadServer. MyMainDjangoServer will serve the views, including the upload form. MyFileUploadServer will "catch" the uploaded files. My questions/confusion are as follows:
My upload form will contain additional fields beyond just the file(s)...do I understand correctly that MyMainDjangoServer will somehow still get that form data, minus the file data (basically: request.POST), and the file data gets shunted over to MyFileUploadServer? How does this work? Will MyMainDjangoServer still block during the upload to MyFileUploadServer?
I assume that what I would need to do on MyFileUploadServer is have a view/URL that handles the form request and sucks out the request.FILES data. What else needs to happen? What happens to the rest of the form data?
How would I set up my settings.py for this scenario? The django-filetransfers examples seem to assume either S3 or GAE/Blobstore but maybe I am missing some basics.
Any advice/answers appreciated...this is a confusing and frustrating area of Django for me.
"MyMainDjangoServer will somehow still get that form data, minus the file data (basically: request.POST), and the file data gets shunted over to MyFileUploadServer? How does this work? Will MyMainDjangoServer still block during the upload to MyFileUploadServer?"
I know the GAE Blobstore, presumably S3 as well, handles this by requiring you to give it a success_url. In your case that would be the url on MyMainDjangoServer where your file receiving view on MyFileUploadServer would re-post the non-files form data to once the upload is complete.
Have a look at the create_upload_url method here: https://developers.google.com/appengine/docs/python/blobstore/functions
You need to recreate this functionality in some form (see below).
"How would I set up my settings.py for this scenario?"
You'd need to create your own filetransfers backend which would be a file with a prepare_upload function in it.
You can see the App Engine one here:
https://github.com/django-nonrel/djangoappengine/blob/develop/storage.py
The prepare_upload method just wraps the GAE create_upload_url method mentioned above.
So in your settings.py you'd have something like:
PREPARE_UPLOAD_BACKEND = 'myapp.filetransfers_backend.prepare_upload'
(i.e. the import path to your prepare_upload function)
For the rest you can start with the ones provided by filetransfers already:
SERVE_FILE_BACKEND = 'filetransfers.backends.url.serve_file'
# if you need it:
PUBLIC_DOWNLOAD_URL_BACKEND = 'filetransfers.backends.url.public_download_url'
These rely on the file_field.url being set (see Django docs) and since your files will be on a separate server you probably need to look into writing a custom storage backend for Django too. (the S3 and GAE cases assume you're using the custom Django storage backends from here)

How to upload a movie file in Django?

I am sorry if this question is already posted somewhere.
I am very new to Django. I have been googling around for quite a long time for sample codes to upload large files using Django, but goes in vain.
Can anyone help me with some sample code for uploading large files in Django or provide relavant reliable link(s)?
Thanks in advance.
Django itself handles large files uploads quite well (the whole file is not loaded to memory since 1.0, see http://code.djangoproject.com/ticket/2070).
But django usually sits behind web server and there is often a limit over request body. So Web server config should be probably adjusted (if you are using apache, look at http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestbody ).
There is this called django-video. I haven't tried it though. I have used django-basic-apps
which can handle video, music and image uploads.

best way to deal with JSON in django

I am getting a JSON feed from a server and today I convert it to python object and thus to django view. We are now making an update of our site. whereby
the browser client should parse json using jQuery
also we will have a adobe-air app which will consume JSON directly
However I am not so keen on exposing my back-end server directly to browser/adobe client. How best way to go via django? any existing django-app?
regards
django-newbie
You can use certain built-in elements of Django but I've always found that SimpleJSON makes things so much easier.
Why? With straight serialisation, you don't want to show everything. So with the built-in methods, you have to cut a lot out. With SimpleJSON, you built a dict, fill it with only what you want shown and pump it through the SimpleJSON lib. I find inclusion a lot more secure than exclusion when it comes to exposing APIs.
It's also a lot more versatile for consuming data as your client isn't going to be a django site, it's an AIR app with its own ideas about how to format data (even within a spec like JSON there can and probably will be differences).
Oh and remember that there isn't a date type in JSON. (I only mention it because it caused me pain in the past)
Edit: (Thanks Cide) Django ships SimpleJSON in django.utils.simplejson but it might not be there forever. Regardless, you can download it separately from Pypi