Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
Respect My Net
Commits
b603f40f
Commit
b603f40f
authored
Nov 04, 2011
by
stef
Browse files
[fix] #20 attachments are have proper names and mimetypes
parent
4fba0bf3
Changes
4
Hide whitespace changes
Inline
Side-by-side
bt/models.py
View file @
b603f40f
...
@@ -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
:
...
...
bt/views.py
View file @
b603f40f
...
@@ -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
)
...
...
templates/view.html
View file @
b603f40f
...
@@ -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>
...
...
urls.py
View file @
b603f40f
...
@@ -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
()),
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment