Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

fire a `file_uploaded` signal on successful uploads #41

Merged
merged 2 commits into from 4 months ago

2 participants

Flavio Curella Steven Skoczen
Flavio Curella

No description provided.

Steven Skoczen skoczen merged commit b96a627 into from
Steven Skoczen skoczen closed this
Steven Skoczen
Owner

Merged in, looks great - thanks for the additions!

This will go out in 0.3.5 this afternoon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jan 25, 2013
Flavio Curella fcurella fire a `file_uploaded` signal on successful uploads 1486c0c
Apr 10, 2013
Flavio Curella fcurella Fixed Markdown syntax b96a627
This page is out of date. Refresh to see the latest.
25 README.md
Source Rendered
@@ -135,6 +135,31 @@ This sample is included in the templates directory, but at the minimum, you need
135 135 </html>
136 136 ```
137 137
  138 +Signals
  139 +=======
  140 +
  141 +The signal `ajaxuploader.signals.file_uploaded` will be fired after a file has been sucessfully uploaded.
  142 +
  143 +Listener methods receives two arguments: the backend that stored the file, and the upload's request.
  144 +
  145 +```python
  146 + from django.db import models
  147 + from django.dispatch import receiver
  148 +
  149 + from ajaxuploader.views import AjaxFileUploader
  150 + from ajaxuploader.signals import file_uploaded
  151 +
  152 +
  153 + class MyModel(models.Model):
  154 + user = models.ForeignKey('auth.User')
  155 + document = models.FileField(upload_to='attachments/%Y/%m/%d')
  156 +
  157 +
  158 + @receiver(file_uploaded, sender=AjaxFileUploader)
  159 + def create_on_upload(sender, backend, request, **kwargs):
  160 + MyModel.objects.create(user=request.user, document=backend.path)
  161 +```
  162 +
138 163 Backends
139 164 ========
140 165
4 ajaxuploader/signals.py
... ... @@ -0,0 +1,4 @@
  1 +from django.dispatch import Signal
  2 +
  3 +
  4 +file_uploaded = Signal(providing_args=['backend', 'request'])
7 ajaxuploader/views.py
... ... @@ -1,9 +1,12 @@
1 1 from django.utils import simplejson as json
  2 +from django.core.files.base import File
2 3 from django.core.serializers.json import DjangoJSONEncoder
3 4
4 5 from django.http import HttpResponse, HttpResponseBadRequest, Http404, HttpResponseNotAllowed
5 6
6 7 from ajaxuploader.backends.local import LocalUploadBackend
  8 +from ajaxuploader.signals import file_uploaded
  9 +
7 10
8 11 class AjaxFileUploader(object):
9 12 def __init__(self, backend=None, **kwargs):
@@ -51,6 +54,10 @@ def _ajax_upload(self, request, *args, **kwargs):
51 54 # save the file
52 55 backend.setup(filename, *args, **kwargs)
53 56 success = backend.upload(upload, filename, is_raw, *args, **kwargs)
  57 +
  58 + if success:
  59 + file_uploaded.send(sender=self.__class__, backend=backend, request=request)
  60 +
54 61 # callback
55 62 extra_context = backend.upload_complete(request, filename, *args, **kwargs)
56 63

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.