Commit b603f40f authored by stef's avatar stef
Browse files

[fix] #20 attachments are have proper names and mimetypes

parent 4fba0bf3
...@@ -31,7 +31,7 @@ COUNTRIES = ( ...@@ -31,7 +31,7 @@ COUNTRIES = (
('SK', _('Slovakia')), ('SK', _('Slovakia')),
('FI', _('Finland')), ('FI', _('Finland')),
('SE', _('Sweden')), ('SE', _('Sweden')),
('UK', _('United Kingdom ')), ('UK', _('United Kingdom')),
) )
RESOURCES = ( RESOURCES = (
...@@ -91,6 +91,7 @@ class Comment(models.Model): ...@@ -91,6 +91,7 @@ class Comment(models.Model):
class Attachment(models.Model): class Attachment(models.Model):
storage = models.FileField(upload_to='static') storage = models.FileField(upload_to='static')
name= models.CharField(max_length=512) name= models.CharField(max_length=512)
type= models.CharField(max_length=512)
comment = models.ForeignKey(Comment) comment = models.ForeignKey(Comment)
class Admin: class Admin:
......
...@@ -3,6 +3,7 @@ from django.http import HttpResponse, HttpResponseRedirect, Http404 ...@@ -3,6 +3,7 @@ from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404 from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext from django.template import RequestContext
from django.core.files import File from django.core.files import File
from django.core.servers.basehttp import FileWrapper
from django.conf import settings from django.conf import settings
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
...@@ -163,7 +164,7 @@ def add(request): ...@@ -163,7 +164,7 @@ def add(request):
) )
c.save() c.save()
for f in request.FILES.getlist('attachments[]'): for f in request.FILES.getlist('attachments[]'):
a=Attachment(comment=c, name=f.name) a=Attachment(comment=c, name=f.name, type=f.content_type)
m = hashlib.sha256() m = hashlib.sha256()
for chunk in f.chunks(): for chunk in f.chunks():
m.update(chunk) m.update(chunk)
...@@ -229,6 +230,14 @@ def view(request,id): ...@@ -229,6 +230,14 @@ def view(request,id):
raise Http404 raise Http404
return render_to_response('view.html', { 'v': v, },context_instance=RequestContext(request)) return render_to_response('view.html', { 'v': v, },context_instance=RequestContext(request))
def get_attach(request,id):
f = get_object_or_404(Attachment, pk=id)
wrapper = FileWrapper(f.storage)
response=HttpResponse(wrapper, mimetype=f.type)
response['Content-Disposition'] = 'attachment; filename=%s' % f.name
response['Content-Length'] = f.storage.size
return response
def lookup(request): def lookup(request):
if request.method == 'GET': if request.method == 'GET':
form = SearchViolation(request.GET) form = SearchViolation(request.GET)
......
...@@ -99,7 +99,7 @@ $(document).ready(function() { ...@@ -99,7 +99,7 @@ $(document).ready(function() {
{% trans "Attachments" %} {% trans "Attachments" %}
<ul> <ul>
{%for a in c.attachment_set.all%} {%for a in c.attachment_set.all%}
<li><a href="{%media_url%}/{{a.storage.url}}">{{a.name}}</a></li> <li><a href="/attach/{{a.id}}">{{a.name}}</a></li>
{%endfor%} {%endfor%}
</ul> </ul>
</div> </div>
......
...@@ -15,6 +15,7 @@ urlpatterns = patterns('', ...@@ -15,6 +15,7 @@ urlpatterns = patterns('',
(r'^ajax/(?P<country>[^/]*)(/(?P<operator>[^/]*))?$', bt.ajax), (r'^ajax/(?P<country>[^/]*)(/(?P<operator>[^/]*))?$', bt.ajax),
(r'^add/$', bt.add), (r'^add/$', bt.add),
(r'^view/(?P<id>[0-9]*)$', bt.view), (r'^view/(?P<id>[0-9]*)$', bt.view),
(r'^attach/(?P<id>[0-9]*)$', bt.get_attach),
(r'^(?P<id>[0-9]*)$', bt.view), (r'^(?P<id>[0-9]*)$', bt.view),
(r'^rss/$', RssSiteNewsFeed()), (r'^rss/$', RssSiteNewsFeed()),
(r'^atom/$', AtomSiteNewsFeed()), (r'^atom/$', AtomSiteNewsFeed()),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment