1

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
phyichai
  • 45
  • 7

1 Answers1

0

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.
rrmoelker
  • 152
  • 10
  • Is this the correct way? mp3 = MixcloudUpload.files.objects.all()[0].path When I try this I get object has no attribute 'objects' – phyichai Oct 27 '14 at 11:17
  • The example `File.objects.all()[0]` was just so you could try it out. I get it your model is named MixcloudUpload and probably contains a FilerFileField? You probably want to filter by `MixcloudUpload.objects.filter(filefield__uploaded_at__gt=last_celery)`, where `last_celery` is a datetime object and `filefield` is the name of the FilerFileField. For each of those objects you can get the path. – rrmoelker Oct 27 '14 at 11:24
  • Thanks, I've just updated the post so you can see a bit more clearly what im trying to achieve – phyichai Oct 27 '14 at 11:28
  • Thanks for your help so far. When I try that I get `TypeError: coercing to Unicode: need string or buffer, QuerySet found` I used 'files' as the field and 'now' as the Datetime object im not sure if this is right – phyichai Oct 27 '14 at 11:50
  • I've not worked with Celery. But it seems the `request` parameter in your `uploadtask` function is actually the id of a `MixcloudUpload` instance. To get the path to the file you would then do `MixcloudUpload.objects.get(id=request).files.path`. However renaming the `request` variable is a good idea if this is the case. – rrmoelker Oct 27 '14 at 12:56