View Cleanup

This commit is contained in:
Martin Mahner 2018-06-22 12:55:56 +02:00
parent 57bd424723
commit 048c6281a7
3 changed files with 58 additions and 50 deletions

View file

@ -0,0 +1,19 @@
# Generated by Django 2.0.6 on 2018-06-22 10:51
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('dpaste', '0005_remove_snippet_highlighted'),
]
operations = [
migrations.AlterField(
model_name='snippet',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='dpaste.Snippet', verbose_name='Parent Snippet'),
),
]

View file

@ -1,17 +1,24 @@
from django.conf import settings
from django.conf.urls import url
from django.views.generic import TemplateView
from .. import views
L = getattr(settings, 'DPASTE_SLUG_LENGTH', 4)
urlpatterns = [
url(r'^$', views.SnippetView.as_view(), name='snippet_new'),
url(r'^about/$', views.AboutView.as_view(), name='dpaste_about'),
url(r'^history/$', views.SnippetHistory.as_view(), name='snippet_history'),
url(r'^$',
views.SnippetView.as_view(), name='snippet_new'),
url(r'^about/$',
TemplateView.as_view(template_name='dpaste/about.html'), name='dpaste_about'),
url(r'^history/$',
views.SnippetHistory.as_view(), name='snippet_history'),
url(r'^(?P<snippet_id>[a-zA-Z0-9]{%d,})/?$' % L,
views.SnippetDetailView.as_view(), name='snippet_details'),
url(r'^(?P<snippet_id>[a-zA-Z0-9]{%d,})/raw/?$' % L,
views.SnippetRawView.as_view(), name='snippet_details_raw'),
]

View file

@ -176,58 +176,40 @@ class SnippetHistory(TemplateView):
return ctx
# -----------------------------------------------------------------------------
# Static pages
# -----------------------------------------------------------------------------
class AboutView(TemplateView):
"""
A rather static page, we need a view just to display a couple of
statistics.
"""
template_name = 'dpaste/about.html'
def get_context_data(self, **kwargs):
ctx = super(AboutView, self).get_context_data(**kwargs)
ctx.update({
'total': Snippet.objects.count(),
'stats': Snippet.objects.values('lexer').annotate(
count=Count('lexer')).order_by('-count')[:5],
})
return ctx
# -----------------------------------------------------------------------------
# API Handling
# -----------------------------------------------------------------------------
def _format_default(s):
"""The default response is the snippet URL wrapped in quotes."""
return '"%s%s"' % (config.BASE_URL, s.get_absolute_url())
def _format_url(s):
"""The `url` format returns the snippet URL, no quotes, but a linebreak after."""
return '%s%s\n' % (config.BASE_URL, s.get_absolute_url())
def _format_json(s):
"""The `json` format export."""
return json.dumps({
'url': '%s%s' % (config.BASE_URL, s.get_absolute_url()),
'content': s.content,
'lexer': s.lexer,
})
FORMAT_MAPPING = {
'default': _format_default,
'url': _format_url,
'json': _format_json,
}
class APIView(View):
"""
API View
"""
def _format_default(self, s):
"""
The default response is the snippet URL wrapped in quotes.
"""
return '"{url}{path}"'.format(url=config.BASE_URL,
path=s.get_absolute_url())
def _format_url(self, s):
"""
The `url` format returns the snippet URL, no quotes, but a linebreak after.
"""
return '{url}{path}\n'.format(url=config.BASE_URL,
path=s.get_absolute_url())
def _format_json(self, s):
"""
The `json` format export.
"""
return json.dumps({
'url': '{url}{path}'.format(url=config.BASE_URL,
path=s.get_absolute_url()),
'content': s.content,
'lexer': s.lexer,
})
def post(self, request, *args, **kwargs):
content = request.POST.get('content', '')
lexer = request.POST.get('lexer', highlight.LEXER_DEFAULT).strip()
@ -276,11 +258,11 @@ class APIView(View):
)
s.save()
if not response_format in FORMAT_MAPPING:
response = _format_default(s)
formatter = getattr(self, '_format_{0}'.format(response_format), None)
if not formatter:
response = self._format_default(s)
else:
response = FORMAT_MAPPING[response_format](s)
response = formatter(s)
return HttpResponse(response)