mirror of
https://github.com/django-helpdesk/django-helpdesk.git
synced 2024-12-13 18:31:10 +01:00
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'],
|
mime_type=file['type'],
|
||||||
size=len(file['content']),
|
size=len(file['content']),
|
||||||
)
|
)
|
||||||
a.file.save(file['filename'], ContentFile(file['content']))
|
a.file.save(file['filename'], ContentFile(file['content']), save=False)
|
||||||
a.save()
|
a.save()
|
||||||
print " - %s" % file['filename']
|
print " - %s" % file['filename']
|
||||||
|
|
||||||
|
41
models.py
41
models.py
@ -543,31 +543,22 @@ class TicketChange(models.Model):
|
|||||||
return str
|
return str
|
||||||
|
|
||||||
|
|
||||||
class DynamicFileField(models.FileField):
|
def attachment_path(instance, filename):
|
||||||
"""
|
"""
|
||||||
Allows model instance to specify upload_to dynamically.
|
Provide a file path that will help prevent files being overwritten, by
|
||||||
|
putting attachments in a folder off attachments for ticket/followup_id/.
|
||||||
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/
|
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
def contribute_to_class(self, cls, name):
|
from django.conf import settings
|
||||||
"""Hook up events so we can access the instance."""
|
os.umask(0)
|
||||||
super(DynamicFileField, self).contribute_to_class(cls, name)
|
path = 'helpdesk/attachments/%s/%s' % (instance.followup.ticket.ticket_for_url, instance.followup.id )
|
||||||
models.signals.post_init.connect(self._post_init, sender=cls)
|
try:
|
||||||
|
os.makedirs(os.path.join(settings.MEDIA_ROOT, path), 0777)
|
||||||
def _post_init(self, instance=None, **kwargs):
|
except OSError,e:
|
||||||
"""Get dynamic upload_to value from the model instance."""
|
if e[0] != 17:
|
||||||
if hasattr(instance, 'get_upload_to'):
|
# 17 = 'File exists'
|
||||||
self.upload_to = instance.get_upload_to(self.attname)
|
raise e
|
||||||
|
return os.path.join(path, filename)
|
||||||
def db_type(self):
|
|
||||||
"""Required by Django for ORM."""
|
|
||||||
return 'varchar(100)'
|
|
||||||
|
|
||||||
|
|
||||||
class Attachment(models.Model):
|
class Attachment(models.Model):
|
||||||
@ -578,9 +569,9 @@ class Attachment(models.Model):
|
|||||||
|
|
||||||
followup = models.ForeignKey(FollowUp)
|
followup = models.ForeignKey(FollowUp)
|
||||||
|
|
||||||
file = DynamicFileField(
|
file = models.FileField(
|
||||||
_('File'),
|
_('File'),
|
||||||
upload_to='helpdesk/attachments',
|
upload_to=attachment_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
filename = models.CharField(
|
filename = models.CharField(
|
||||||
|
@ -97,7 +97,7 @@
|
|||||||
{% if forloop.last %}</div></ul>{% endif %}
|
{% if forloop.last %}</div></ul>{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% for attachment in followup.attachment_set.all %}{% if forloop.first %}<div class='attachments'><ul>{% endif %}
|
{% 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 %}
|
{% if forloop.last %}</ul></div>{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
@ -267,15 +267,16 @@ def update_ticket(request, ticket_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if request.FILES:
|
if request.FILES:
|
||||||
|
import mimetypes
|
||||||
for file in request.FILES.getlist('attachment'):
|
for file in request.FILES.getlist('attachment'):
|
||||||
filename = file['filename'].replace(' ', '_')
|
filename = file.name.replace(' ', '_')
|
||||||
a = Attachment(
|
a = Attachment(
|
||||||
followup=f,
|
followup=f,
|
||||||
filename=filename,
|
filename=filename,
|
||||||
mime_type=file['content-type'],
|
mime_type=mimetypes.guess_type(filename)[0] or 'application/octet-stream',
|
||||||
size=len(file['content']),
|
size=file.size,
|
||||||
)
|
)
|
||||||
a.file.save(file['filename'], ContentFile(file['content']))
|
a.file.save(file.name, file, save=False)
|
||||||
a.save()
|
a.save()
|
||||||
|
|
||||||
ticket.save()
|
ticket.save()
|
||||||
|
Loading…
Reference in New Issue
Block a user