X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/b57fc50443cd2f26c47267d2556b79805b41f8b8..5adae760fc382cd1f3fdc7ee02f2b8cc0dc5cfa6:/philo/models/nodes.py diff --git a/philo/models/nodes.py b/philo/models/nodes.py index 5de7338..0c89095 100644 --- a/philo/models/nodes.py +++ b/philo/models/nodes.py @@ -1,4 +1,5 @@ from inspect import getargspec +import mimetypes from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType @@ -370,11 +371,19 @@ class Redirect(TargetURLModel, View): class File(View): """Stores an arbitrary file.""" - #: Defines the mimetype of the uploaded file. This will not be validated. - mimetype = models.CharField(max_length=255) + #: The name of the uploaded file. This is meant for finding the file again later, not for display. + name = models.CharField(max_length=255) + #: Defines the mimetype of the uploaded file. This will not be validated. If no mimetype is provided, it will be automatically generated based on the filename. + mimetype = models.CharField(max_length=255, blank=True) #: Contains the uploaded file. Files are uploaded to ``philo/files/%Y/%m/%d``. file = models.FileField(upload_to='philo/files/%Y/%m/%d') + def clean(self): + if not self.mimetype: + self.mimetype = mimetypes.guess_type(self.file.name, strict=False)[0] + if self.mimetype is None: + raise ValidationError("Unknown file type.") + def actually_render_to_response(self, request, extra_context=None): wrapper = FileWrapper(self.file) response = HttpResponse(wrapper, content_type=self.mimetype) @@ -385,8 +394,8 @@ class File(View): app_label = 'philo' def __unicode__(self): - """Returns the path of the uploaded file.""" - return self.file.name + """Returns the value of :attr:`File.name`.""" + return self.name register_value_model(Node) \ No newline at end of file