Create a Github gist from a snippet.

I'm not yet sure how useful it is, since its creates Gists in a anomymous space and not under your account, but if you want to save Snippets permanently, thats the way to go.
This commit is contained in:
Martin Mahner 2013-06-05 19:40:41 +02:00
parent 3c871c4b90
commit 8db8977910
4 changed files with 40 additions and 5 deletions

View file

@ -56,6 +56,8 @@
<a class="btn snippet-diff-trigger" href="#snippet-diff"><i class="icon-search"></i> {% trans "Compare Snippets" %}</a>
{% endif %}
<a class="btn" href="{% url "snippet_details_raw" snippet.secret_id %}"><i class="icon-align-left"></i> {% trans "View Raw" %}</a>
<a class="btn" href="{% url "snippet_gist" snippet.secret_id %}"
rel="nofollow" title="Create a secret Gist"><i class="icon-share"></i> {% trans "Gist" %}</a>
</div>
<!-- ======================================================================

View file

@ -7,5 +7,6 @@ urlpatterns = patterns('dpaste.views',
url(r'^history/$', 'snippet_history', name='snippet_history'),
url(r'^(?P<snippet_id>[a-zA-Z0-9]+)/$', 'snippet_details', name='snippet_details'),
url(r'^(?P<snippet_id>[a-zA-Z0-9]+)/delete/$', 'snippet_delete', name='snippet_delete'),
url(r'^(?P<snippet_id>[a-zA-Z0-9]+)/gist/$', 'snippet_gist', name='snippet_gist'),
url(r'^(?P<snippet_id>[a-zA-Z0-9]+)/raw/$', 'snippet_details', {'template_name': 'dpaste/snippet_details_raw.html', 'is_raw': True}, name='snippet_details_raw'),
)

View file

@ -1,24 +1,26 @@
import datetime
import difflib
import requests
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.template.context import RequestContext
from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest, \
HttpResponse, HttpResponseForbidden
from django.http import (Http404, HttpResponseRedirect, HttpResponseBadRequest,
HttpResponse, HttpResponseForbidden)
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.utils import simplejson
from django.db.models import Count
from django.views.defaults import page_not_found as django_page_not_found, \
server_error as django_server_error
from django.views.defaults import (page_not_found as django_page_not_found,
server_error as django_server_error)
from dpaste.forms import SnippetForm
from dpaste.models import Snippet
from dpaste.highlight import guess_code_lexer, \
LEXER_WORDWRAP, LEXER_LIST
import difflib
def about(request, template_name='dpaste/about.html'):
template_context = {
@ -197,6 +199,35 @@ def snippet_diff(request, template_name='dpaste/snippet_diff.html'):
RequestContext(request)
)
def snippet_gist(request, snippet_id):
"""
Put a snippet on Github Gist.
"""
snippet = get_object_or_404(Snippet, secret_id=snippet_id)
data = {
'description': 'the description for this gist',
'public': False,
'files': {
'dpaste.de Snippet': {
'content': snippet.content,
}
}
}
try:
payload = simplejson.dumps(data)
response = requests.post('https://api.github.com/gists', data=payload)
response_dict = simplejson.loads(response.content)
gist_url = response_dict.get('html_url')
# Github could be down, could return invalid JSON, it's rare
except:
return HttpResponse('Creating a Github Gist failed. Sorry, please go back and try again.')
return HttpResponseRedirect(gist_url)
def guess_lexer(request):
code_string = request.GET.get('codestring', False)
response = simplejson.dumps({'lexer': guess_code_lexer(code_string)})

View file

@ -2,6 +2,7 @@ django==1.5.1
django-mptt==0.4.2
pygments==1.6
south==0.7.6
requests==1.2.3
# Deployment specific
##mysql-python==1.2.4