mirror of
https://github.com/DarrenOfficial/dpaste.git
synced 2024-11-15 16:12:51 +11:00
Use Python's json now.
This commit is contained in:
parent
8e727cb940
commit
6f563ee4ea
1 changed files with 11 additions and 12 deletions
|
@ -1,16 +1,16 @@
|
||||||
import datetime
|
import datetime
|
||||||
import difflib
|
import difflib
|
||||||
import requests
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
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.context import RequestContext
|
from django.template.context import RequestContext
|
||||||
from django.http import (Http404, HttpResponseRedirect, HttpResponseBadRequest,
|
from django.http import (Http404, HttpResponseRedirect, HttpResponseBadRequest,
|
||||||
HttpResponse)
|
HttpResponse)
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.utils import simplejson
|
|
||||||
from django.db.models import Count
|
from django.db.models import Count
|
||||||
from django.views.defaults import (page_not_found as django_page_not_found,
|
from django.views.defaults import (page_not_found as django_page_not_found,
|
||||||
server_error as django_server_error)
|
server_error as django_server_error)
|
||||||
|
@ -186,7 +186,7 @@ def snippet_diff(request, template_name='dpaste/snippet_diff.html'):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def snippet_gist(request, snippet_id):
|
def snippet_gist(request, snippet_id): # pragma: no cover
|
||||||
"""
|
"""
|
||||||
Put a snippet on Github Gist.
|
Put a snippet on Github Gist.
|
||||||
"""
|
"""
|
||||||
|
@ -202,9 +202,9 @@ def snippet_gist(request, snippet_id):
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
payload = simplejson.dumps(data)
|
payload = json.dumps(data)
|
||||||
response = requests.post('https://api.github.com/gists', data=payload)
|
response = requests.post('https://api.github.com/gists', data=payload)
|
||||||
response_dict = simplejson.loads(response.content)
|
response_dict = json.loads(response.content)
|
||||||
gist_url = response_dict.get('html_url')
|
gist_url = response_dict.get('html_url')
|
||||||
|
|
||||||
# Github could be down, could return invalid JSON, it's rare
|
# Github could be down, could return invalid JSON, it's rare
|
||||||
|
@ -240,8 +240,6 @@ def about(request, template_name='dpaste/about.html'):
|
||||||
# API Handling
|
# API Handling
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
BASE_URL = getattr(settings, 'DPASTE_BASE_URL', 'https://dpaste.de')
|
|
||||||
|
|
||||||
def _format_default(s):
|
def _format_default(s):
|
||||||
"""The default response is the snippet URL wrapped in quotes."""
|
"""The default response is the snippet URL wrapped in quotes."""
|
||||||
return u'"%s%s"' % (BASE_URL, s.get_absolute_url())
|
return u'"%s%s"' % (BASE_URL, s.get_absolute_url())
|
||||||
|
@ -251,19 +249,21 @@ def _format_url(s):
|
||||||
return u'%s%s\n' % (BASE_URL, s.get_absolute_url())
|
return u'%s%s\n' % (BASE_URL, s.get_absolute_url())
|
||||||
|
|
||||||
def _format_json(s):
|
def _format_json(s):
|
||||||
from json import dumps
|
|
||||||
"""The `json` format export."""
|
"""The `json` format export."""
|
||||||
return dumps({
|
return json.dumps({
|
||||||
'url': u'%s%s' % (BASE_URL, s.get_absolute_url()),
|
'url': u'%s%s' % (BASE_URL, s.get_absolute_url()),
|
||||||
'content': s.content,
|
'content': s.content,
|
||||||
'lexer': s.lexer,
|
'lexer': s.lexer,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
BASE_URL = getattr(settings, 'DPASTE_BASE_URL', 'https://dpaste.de')
|
||||||
|
|
||||||
FORMAT_MAPPING = {
|
FORMAT_MAPPING = {
|
||||||
'default': _format_default,
|
'default': _format_default,
|
||||||
'url': _format_url,
|
'url': _format_url,
|
||||||
'json': _format_json,
|
'json': _format_json,
|
||||||
}
|
}
|
||||||
|
|
||||||
def snippet_api(request):
|
def snippet_api(request):
|
||||||
content = request.POST.get('content', '').strip()
|
content = request.POST.get('content', '').strip()
|
||||||
lexer = request.POST.get('lexer', LEXER_DEFAULT).strip()
|
lexer = request.POST.get('lexer', LEXER_DEFAULT).strip()
|
||||||
|
@ -297,8 +297,7 @@ def snippet_api(request):
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
def page_not_found(request, template_name='dpaste/404.html'):
|
def page_not_found(request, template_name='dpaste/404.html'):
|
||||||
return django_page_not_found(request, template_name)
|
return django_page_not_found(request, template_name) # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
def server_error(request, template_name='dpaste/500.html'):
|
def server_error(request, template_name='dpaste/500.html'):
|
||||||
return django_server_error(request, template_name)
|
return django_server_error(request, template_name) # pragma: no cover
|
||||||
|
|
Loading…
Reference in a new issue