forked from extern/django-helpdesk
Fix issue #37 - file uploading was not working as it should.
File uploading now works correctly from both the staff interface and from emails.
This commit is contained in:
parent
a20ab36452
commit
c8ce68e7b8
@ -276,7 +276,7 @@ def ticket_from_message(message, queue):
|
||||
mime_type=file['type'],
|
||||
size=len(file['content']),
|
||||
)
|
||||
a.file.save(file['filename'], ContentFile(file['content']))
|
||||
a.file.save(file['filename'], ContentFile(file['content']), save=False)
|
||||
a.save()
|
||||
print " - %s" % file['filename']
|
||||
|
||||
|
41
models.py
41
models.py
@ -543,31 +543,22 @@ class TicketChange(models.Model):
|
||||
return str
|
||||
|
||||
|
||||
class DynamicFileField(models.FileField):
|
||||
def attachment_path(instance, filename):
|
||||
"""
|
||||
Allows model instance to specify upload_to dynamically.
|
||||
|
||||
Model class should have a method like:
|
||||
|
||||
def get_upload_to(self, attname):
|
||||
return 'path/to/%d' % self.parent.id
|
||||
|
||||
Based on: http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/
|
||||
Provide a file path that will help prevent files being overwritten, by
|
||||
putting attachments in a folder off attachments for ticket/followup_id/.
|
||||
"""
|
||||
|
||||
def contribute_to_class(self, cls, name):
|
||||
"""Hook up events so we can access the instance."""
|
||||
super(DynamicFileField, self).contribute_to_class(cls, name)
|
||||
models.signals.post_init.connect(self._post_init, sender=cls)
|
||||
|
||||
def _post_init(self, instance=None, **kwargs):
|
||||
"""Get dynamic upload_to value from the model instance."""
|
||||
if hasattr(instance, 'get_upload_to'):
|
||||
self.upload_to = instance.get_upload_to(self.attname)
|
||||
|
||||
def db_type(self):
|
||||
"""Required by Django for ORM."""
|
||||
return 'varchar(100)'
|
||||
import os
|
||||
from django.conf import settings
|
||||
os.umask(0)
|
||||
path = 'helpdesk/attachments/%s/%s' % (instance.followup.ticket.ticket_for_url, instance.followup.id )
|
||||
try:
|
||||
os.makedirs(os.path.join(settings.MEDIA_ROOT, path), 0777)
|
||||
except OSError,e:
|
||||
if e[0] != 17:
|
||||
# 17 = 'File exists'
|
||||
raise e
|
||||
return os.path.join(path, filename)
|
||||
|
||||
|
||||
class Attachment(models.Model):
|
||||
@ -578,9 +569,9 @@ class Attachment(models.Model):
|
||||
|
||||
followup = models.ForeignKey(FollowUp)
|
||||
|
||||
file = DynamicFileField(
|
||||
file = models.FileField(
|
||||
_('File'),
|
||||
upload_to='helpdesk/attachments',
|
||||
upload_to=attachment_path,
|
||||
)
|
||||
|
||||
filename = models.CharField(
|
||||
|
@ -97,7 +97,7 @@
|
||||
{% if forloop.last %}</div></ul>{% endif %}
|
||||
{% endfor %}
|
||||
{% for attachment in followup.attachment_set.all %}{% if forloop.first %}<div class='attachments'><ul>{% endif %}
|
||||
<li><a href='{{ attachment.get_file_url }}'>{{ attachment.filename }}</a> ({{ attachment.mime_type }}, {{ attachment.size|filesizeformat }})</li>
|
||||
<li><a href='{{ attachment.file.url }}'>{{ attachment.filename }}</a> ({{ attachment.mime_type }}, {{ attachment.size|filesizeformat }})</li>
|
||||
{% if forloop.last %}</ul></div>{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
@ -267,15 +267,16 @@ def update_ticket(request, ticket_id):
|
||||
)
|
||||
|
||||
if request.FILES:
|
||||
import mimetypes
|
||||
for file in request.FILES.getlist('attachment'):
|
||||
filename = file['filename'].replace(' ', '_')
|
||||
filename = file.name.replace(' ', '_')
|
||||
a = Attachment(
|
||||
followup=f,
|
||||
filename=filename,
|
||||
mime_type=file['content-type'],
|
||||
size=len(file['content']),
|
||||
mime_type=mimetypes.guess_type(filename)[0] or 'application/octet-stream',
|
||||
size=file.size,
|
||||
)
|
||||
a.file.save(file['filename'], ContentFile(file['content']))
|
||||
a.file.save(file.name, file, save=False)
|
||||
a.save()
|
||||
|
||||
ticket.save()
|
||||
|
Loading…
Reference in New Issue
Block a user