refactoring attachments

This commit is contained in:
slav0nic 2009-05-25 13:56:21 +03:00
parent bb74c80bf7
commit b9a33e57c5
5 changed files with 9 additions and 15 deletions

View file

@ -14,5 +14,6 @@ djapian_db
*.kpf
*.autosave
static/forum/avatars
static/forum/attachments
secret.txt
django_evolution

View file

@ -59,13 +59,13 @@ class AddPostForm(forms.ModelForm):
self.forum = kwargs.pop('forum', None)
self.ip = kwargs.pop('ip', None)
super(AddPostForm, self).__init__(*args, **kwargs)
if self.topic:
self.fields['name'].widget = forms.HiddenInput()
self.fields['name'].required = False
self.fields['body'].widget = forms.Textarea(attrs={'class':'bbcode', 'rows':'20', 'cols':'95'})
if not forum_settings.ATTACHMENT_SUPPORT:
self.fields['attachment'].widget = forms.HiddenInput()
self.fields['attachment'].required = False

View file

@ -393,16 +393,6 @@ class Attachment(models.Model):
def get_absolute_url(self):
return reverse('forum_attachment', args=[self.hash])
def size_display(self):
size = self.size
if size < 1024:
return '%b' % size
elif size < 1024 * 1024:
return '%dKb' % int(size / 1024)
else:
return '%.2fMb' % (size / float(1024 * 1024))
def get_absolute_path(self):
return os.path.join(settings.MEDIA_ROOT, forum_settings.ATTACHMENT_UPLOAD_TO,
self.path)

View file

@ -264,6 +264,7 @@ def pm_unreads(user):
@register.filter
def attachment_link(attach):
from django.template.defaultfilters import filesizeformat
if attach.content_type in ['image/png', 'image/gif', 'image/jpeg']:
img = '<img src="%sforum/img/attachment/image.png" alt="attachment" >' % (settings.MEDIA_URL)
elif attach.content_type in ['application/x-tar', 'application/zip']:
@ -274,7 +275,7 @@ def attachment_link(attach):
img = '<img src="%sforum/img/attachment/doc.png" alt="attachment" >' % (settings.MEDIA_URL)
else:
img = '<img src="%sforum/img/attachment/unknown.png" alt="attachment" >' % (settings.MEDIA_URL)
attachment = '%s <a href="%s">%s</a> (%s)' % (img, attach.get_absolute_url(), attach.name, attach.size_display())
attachment = '%s <a href="%s">%s</a> (%s)' % (img, attach.get_absolute_url(), attach.name, filesizeformat(attach.size))
return mark_safe(attachment)
@register.simple_tag

View file

@ -790,7 +790,9 @@ def add_subscription(request, topic_id):
def show_attachment(request, hash):
attachment = get_object_or_404(Attachment, hash=hash)
file_obj = file(attachment.get_absolute_path())
return HttpResponse(file_obj, content_type=attachment.content_type)
response = HttpResponse(file_obj, content_type=attachment.content_type)
response['Content-Disposition'] = 'attachment; filename=%s' % attachment.name
return response
#TODO: check markup
@render_to('forum/post_preview.html')