Login

Tag "file"

Snippet List

Pre-delete signal function for deleting files a model

This snippit is meant to be used with the pre_delete signal to delete any files associated with a model instance before the instance is deleted. It will search the model instance for fields that are subclasses of FieldFile, and then delete the corresponding files. As such, it will work with any model field that is a subclass of FileField.

  • remove
  • delete
  • signals
  • file
Read More

Improve ajax progress bar by jquery extensions

This snippet in the one to improve the method described in [http://djangosnippets.org/snippets/679/](http://djangosnippets.org/snippets/679/). It uses some jquery extensions to make the task more easier: **jquery.timers.js**, **jquery.progressbar.js**, **jquery.form.js**.

  • ajax
  • javascript
  • jquery
  • upload
  • file
  • progress
  • jquery extension
Read More

Custom FileField with content type and size validation

Usage described in this blog post: [Django: FileField with ContentType and File Size Validation](http://nemesisdesign.net/blog/coding/django-filefield-content-type-size-validation/) Snippet inspired by: [Validate by file content type and size](http://djangosnippets.org/snippets/1303/)

  • forms
  • validation
  • upload
  • filefield
  • file
  • content-type
  • max_upload_size
Read More

Overwriting file storage

The title says it all — a subclass of FileSystemStorage which will overwrite files. Note that saves which fail part way though will leave the original file intact (see `test_upload_fails`). Based roughly on http://djangosnippets.org/snippets/2044/ .

  • file
  • storage
  • filestorage
  • file-storage
Read More

Reliably create a Django File using the contents of a URL

There's no direct way to save the contents of a URL to a Django File field: you're required to use a File instance but those can only safely wrap normal files, not the file-like object returned by urllib2.urlopen. Several examples online use urllib.urlretrieve() which creates a temporary file but performs no error handling without writing a ton of hackish code. This demonstrates how to create a NamedTemporaryFile, fill it with the URL contents and save it, all using APIs which raise exceptions on errors.

  • url
  • filefield
  • file
  • urlopen
Read More

GzipFileSystemStorage

`GzipFileSystemStorage` is a `FileSystemStorage` subclass that transparently compresses files. [More Info](http://theidioteque.net/blog/2009/9/29/gzipfilesystemstorage/)

  • compression
  • file
  • gzip
  • storage
  • compress
Read More

Validate by file content type and size

A simple way to handle file validation by checking for a proper content_type and by making sure that the file does not go over its limit upload size limit. In the example I am checking to make sure the file is an image or a video file then I check to make sure that the file is below the max upload limit. if conditions are not met, then a validation error is raised

  • forms
  • validation
  • filefield
  • file
Read More

Fake File Uploads

In-browser testing frameworks (I'm using [Windmill](http://www.getwindmill.com/)) have trouble testing file uploads because javascript's security policy prevents them from setting the value of file input fields. Instead the tests must issue some sort of "fake" file upload request, but implementing this on an ad-hoc basis quickly gets ugly. This middleware is designed to support fake file uploads as transparently and as thoroughly as possible. For example, it is careful to properly trigger any file upload handlers so that things like upload progress reporting will work correctly. It can also simulate a slow file upload by sleeping between reads from the file. From the client-side point of view, each input field of type "file" has a similarly-named hidden field automatically prepended. Test scripts can simply set the value of this hidden field to trigger a fake upload, rather than having to set the value of the file input field itself.

  • upload
  • testing
  • file
Read More
Author: rfk
  • 1
  • 3

Easy file upload handler

This function emulates the file upload behaviour of django's admin, but can be used in any view. It takes a list of POST keys that represent uploaded files, and saves the files into a date-formatted directory in the same manner as a `FileField`'s `upload_to` argument.

  • image
  • forms
  • view
  • upload
  • imagefield
  • filefield
  • file
Read More

Admin Image Widget

A FileField Widget that displays an image instead of a file path if the current file is an image. Could also be used with sorl.thumbnail to generate thumbnail images. **Example** class FileUploadForm(forms.ModelForm): upload = forms.FileField(widget=AdminThumbnailWidget) class Meta: model = FileUpload class FileUploadAdmin(admin.ModelAdmin): form = FileUploadForm admin.site.register(FileUpload, FileUploadAdmin)

  • image
  • newforms-admin
  • widget
  • file
  • nfa
Read More

20 snippets posted so far.