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
piks3l
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 = (
(
'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
:
...
...
bt/views.py
View file @
b603f40f
...
...
@@ -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
)
...
...
templates/view.html
View file @
b603f40f
...
...
@@ -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>
...
...
urls.py
View file @
b603f40f
...
...
@@ -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
()),
...
...
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