Download snipt as file functionality. Closes #83.

master
Nick Sergeant 2013-05-19 13:33:44 -04:00
parent 1bfa657957
commit 31d96761fe
8 changed files with 42 additions and 6 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1075,12 +1075,15 @@ article.snipt {
-moz-transform: rotate(-45deg);
}
}
&.embed {
background-image: url('../img/embed-icon.png');
}
&.copy {
background-image: url('../img/copy-icon.png');
}
&.download {
background-image: url('../img/download-icon.png');
}
&.embed {
background-image: url('../img/embed-icon.png');
}
&.favorite {
background-image: url('../img/favorite-icon.png');
}

BIN
media/img/download-icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -10,6 +10,7 @@ from taggit.utils import edit_string_for_tags
from markdown_deux import markdown
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.util import ClassNotFound
from pygments.formatters import HtmlFormatter
from snipts.utils import slugify_uniquely
@ -176,6 +177,28 @@ class Snipt(models.Model):
else:
return '{}/{}/{}/?key={}'.format(root, self.user.username, self.slug, self.key)
def get_download_url(self):
try:
lexer_obj = get_lexer_by_name(self.lexer)
except ClassNotFound:
lexer_obj = None
if lexer_obj and lexer_obj.filenames:
filename = lexer_obj.filenames[0].replace('*', self.slug)
else:
if self.lexer == 'markdown':
filename = '{}.md'.format(self.slug)
else:
filename = '{}.txt'.format(self.slug)
if settings.DEBUG:
root = 'http://snipt.localhost'
else:
root = 'https://snipt.net'
return '{}/download/{}/{}'.format(root, self.key, filename)
def get_embed_url(self):
if settings.DEBUG:

View File

@ -121,6 +121,11 @@
{% endif %}
</li>
{% endif %}
{% if detail %}
<li>
<a class="download" href="{{ snipt.get_download_url }}">Download</a>
</li>
{% endif %}
</ul>
{% if snipt.tags.all %}
<section class="meta tags">

View File

@ -13,6 +13,7 @@ urlpatterns = patterns('',
url(r'^public/$', views.list_public, name='list-public'),
url(r'^public/tag/(?P<tag_slug>[^/]+)/$', views.list_public, name='list-public-tag'),
url(r'^download/(?P<snipt_key>[^/]+).*$', views.download, name='download'),
url(r'^embed/(?P<snipt_key>[^/]+)/$', views.embed, name='embed'),
url(r'^raw/(?P<snipt_key>[^/]+)/(?P<lexer>[^\?]+)?$', views.raw, name='raw'),
url(r'^(?P<username_or_custom_slug>[^/]+)/$', views.list_user, name='list-user'),

View File

@ -1,4 +1,4 @@
from django.http import Http404, HttpResponseRedirect
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render_to_response
from django.core.paginator import Paginator, InvalidPage
from annoying.functions import get_object_or_None
@ -61,6 +61,10 @@ def detail(request, username, snipt_slug):
'user': user,
}
def download(request, snipt_key):
snipt = get_object_or_404(Snipt, key=snipt_key)
return HttpResponse(snipt.code, content_type='application/x-download')
def embed(request, snipt_key):
snipt = get_object_or_404(Snipt, key=snipt_key)