Django upload and download files in google drive using googledriveapi - python

I am new to django and trying to create a web app to upload and download files to the google drive using gdriveapi.
I have gone through the python quickstart for Google drive and tried the code in manage.py where it is working.
How to write the same code to obtain credentials and upload and download in the django app?
How to write the view and model in django to obtain credentials and upload files to gdrive?

for my model field I use file = models.FileField(upload_to='todo', storage=gd_storage, blank=True, null=True), where todo is the app name. For the view I simply used the generic ModelViewSet, where you can just serializer.save(file=self.request.data['file'])

Related

Cannot locate local path of uploaded file to Heroku from django using ephemeral file system( Web based Application)

I have recently deployed my first django project to Heroku. My project is a Web Based Application that allows users to upload a file to the server and the server will export a product after processing it.
The views.py processing the upload process is as follows
if form.is_valid():
task= form.save(commit=False)
task.task_created_by = request.user
task.save()
The model is as follows
class db(models.Model):
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
description = models.CharField(max_length=255, blank=True)
excel = models.FileField(upload_to='task/excel/')
userinput = models.FileField(upload_to='task/userinput/')
output= models.FileField(blank=True,null=True)
uploaded_at = models.DateTimeField(auto_now_add=True)
settings.py
#Media path setting
MEDIA_URL = '/uploaded/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded')
Therefore, a part of the server involves accessing the user uploaded file and thus processing it.
I did not set up any external file storage service(e.g. aws s3) and is only using the default ephemeral file system.
when I first run the application the server response this error
No such file or directory: '/app/uploaded/task\\excel\\abc.xlsx'
However, as I am using the django model file field, when I go to the django admin panel, it shows the file is uploaded and can be downloaded from the admin panel...
Therefore, the question is where did Heroku saved the uploaded file locally...
I have tried the heroku run bash method to search for the file but with no luck...
Any help would be appreciated, many thanks!!

automate file upload in django

I want to monitor a folder (which receives new csv file weekly)and add that file to my django model . The purpose simply is to automize upload procedure. Even if there is a way with selenium to upload each new file (whenever arrived in the folder) that would be fine too my upload page is the default django admin page , any help would be appreciated
from django.db import models
class FilesAdmin(models.Model):
adminupload=models.FileField(upload_to='media')
title=models.CharField(max_length=50)
def __str__(self):
return self.title

django-tenants: access tenant media files in script

I am running an app built with djang-tenants. The app asks the user (tenant) to upload some data. I want the data to be segregated in sub directories for each tenant.
Acccording to the doc (https://django-tenants.readthedocs.io/en/latest/files.html), here is how media root is configured:
settings.py
MEDIA_ROOT = "/Users/murcielago/desktop/simulation_application/data"
MULTITENANT_RELATIVE_MEDIA_ROOT = "%s"
On the upload everything is great.
Now, I can't find a way to retrieve the file being uploaded within the app. Basically I need the app to serve the file corresponding to which tenant is requesting it.
Here is how I thought this would work:
from django.conf import settings
media_file_dir = settings.MULTITENANT_RELATIVE_MEDIA_ROOT
df = pd.read_csv(media_file_dir+'/uploads/sample_orders_data.csv')
but this does not work.
I have made it work so far grabbing the tenant name from the url and passing it to the app using pickle but this is not right in terms of security and won't scale.
Would someone has a clue on the best way to handle the lecture of tenant specific files?

Django MEDIA_ROOT pointed to network drive

I'm trying to get files uploaded through the Django admin site to be placed on a network drive. Lets say the path to this drive is '\\FILESERVER\Django'.
My initial thought was to just set my media root to the same path I'd use to access the drive via File Explorer:
#settings.py
MEDIA_ROOT = r'\\FILESERVER\Django'
An Example Model:
#models.py
class Article(models.Model):
title = models.CharField(max_length=128)
pdf = models.FileField(upload_to='articles', blank=True, null=True)
def __str__(self):
return self.title
But when I upload a file, It just creates the folder on my local C: drive.
(e.g. C:\FILESERVER\Django\articles). Is there a way I can tell Django that this is supposed to be a path to a network drive?
Note: This is a Django 2.0.4 app running on a windows machine.
I believe what you're looking for is in pathlib. Have a look here:
https://docs.python.org/3/library/pathlib.html#methods-and-properties
Something like PureWindowsPath('//FILESERVER/Django').drive should do the trick for MEDIA_ROOT, IIRC.
If you use a network file server,you may need a customized django file storage class.

Using OAuth for django, how to save username from google account to database

I have managed to use Google authentication in my django site. The problem is when I go to user profile which is: http://127.0.0.1:8000/user/username
for example if my google username is Chazefate then when I go to Profile then this site is not avaible, because it didn't save the user to database, I think.
http://127.0.0.1:8000/user/Chazefate
I am using django python social auth.
So the main question is how can I save this google user in my database.

Categories