Is it possible to get the absolute URL of a file just uploaded with Django Filer?
My problem is this:
I'm using Django Filer to upload music. I then want to submit this file to Django Celery to upload to Mixcloud using their API. But..
I can't figure out how to get URL of the file. I'm not using views, I want to just use the admin, grab the file just uploaded and send to Celery to upload to Mixcloud using their API. But i can only import the object from the model to celery.
My Model:
class MixcloudUpload(models.Model):
files = FilerFileField(null=True, blank=True)
data = {'name': 'API Test'}
def save(self, *args, **kwargs):
super(MixcloudUpload, self).save(*args, **kwargs)
from .tasks import uploadtask
uploadtask.delay(self.id)
My Task:
@celery_app.task()
def uploadtask(request):
sleep(5)
# Update the state. The meta data is available in task.info dicttionary
# The meta data is useful to store relevant information to the task
# Here we are storing the upload progress in the meta.
post_url = 'https://api.mixcloud.com/upload/?access_token=ugY'
from .models import MixcloudUpload
mp3 = MixcloudUpload.files
data = MixcloudUpload.data
files = ({'mp3': open(mp3)})
headers = {'enctype': 'multipart/form-data'}
r = requests.post(post_url, headers=headers, files=files, data=data, verify=True)
return r, r.json(), request
Django Filer sets the uploaded_at
(datetime field) model attribute on upload. You can filter on this to get new uploads to forward to Mixcloud.
As for getting the absolute url. The filer documentation is a bit tricky in this respect. But there are two attributes which you can use:
File.objects.all()[0].path
, is the path to the file on the filesystem.File.objects.all()[0].url
, the url path after the domain, e.g.: /media/filer_public/23/23/34565b67-e1de-41da-3132-1403c335fdd4/test_img.png/
. You need to prepend your domain yourself. So the full path would be something like: "http" + urlencode(site_obj.domain) + filer_obj.url
.