Commit b603f40f authored by stef's avatar stef

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

parent 4fba0bf3
......@@ -31,7 +31,7 @@ COUNTRIES = (
('SK', _('Slovakia')),
('FI', _('Finland')),
('SE', _('Sweden')),
('UK', _('United Kingdom ')),
('UK', _('United Kingdom')),
)
RESOURCES = (
......@@ -91,6 +91,7 @@ class Comment(models.Model):
class Attachment(models.Model):
storage = models.FileField(upload_to='static')
name= models.CharField(max_length=512)
type= models.CharField(max_length=512)
comment = models.ForeignKey(Comment)
class Admin:
......
......@@ -3,6 +3,7 @@ from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.core.files import File
from django.core.servers.basehttp import FileWrapper
from django.conf import settings
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.exceptions import ObjectDoesNotExist
......@@ -163,7 +164,7 @@ def add(request):
)
c.save()
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()
for chunk in f.chunks():
m.update(chunk)
......@@ -229,6 +230,14 @@ def view(request,id):
raise Http404
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):
if request.method == 'GET':
form = SearchViolation(request.GET)
......
......@@ -99,7 +99,7 @@ $(document).ready(function() {
{% trans "Attachments" %}
<ul>
{%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%}
</ul>
</div>
......
......@@ -15,6 +15,7 @@ urlpatterns = patterns('',
(r'^ajax/(?P<country>[^/]*)(/(?P<operator>[^/]*))?$', bt.ajax),
(r'^add/$', bt.add),
(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'^rss/$', RssSiteNewsFeed()),
(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