mirror of
https://github.com/DarrenOfficial/dpaste.git
synced 2024-11-23 11:56:36 +11:00
View Cleanup
This commit is contained in:
parent
57bd424723
commit
048c6281a7
3 changed files with 58 additions and 50 deletions
19
dpaste/migrations/0006_auto_20180622_1051.py
Normal file
19
dpaste/migrations/0006_auto_20180622_1051.py
Normal 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'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,17 +1,24 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
from .. import views
|
from .. import views
|
||||||
|
|
||||||
L = getattr(settings, 'DPASTE_SLUG_LENGTH', 4)
|
L = getattr(settings, 'DPASTE_SLUG_LENGTH', 4)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', views.SnippetView.as_view(), name='snippet_new'),
|
url(r'^$',
|
||||||
url(r'^about/$', views.AboutView.as_view(), name='dpaste_about'),
|
views.SnippetView.as_view(), name='snippet_new'),
|
||||||
url(r'^history/$', views.SnippetHistory.as_view(), name='snippet_history'),
|
|
||||||
|
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,
|
url(r'^(?P<snippet_id>[a-zA-Z0-9]{%d,})/?$' % L,
|
||||||
views.SnippetDetailView.as_view(), name='snippet_details'),
|
views.SnippetDetailView.as_view(), name='snippet_details'),
|
||||||
|
|
||||||
url(r'^(?P<snippet_id>[a-zA-Z0-9]{%d,})/raw/?$' % L,
|
url(r'^(?P<snippet_id>[a-zA-Z0-9]{%d,})/raw/?$' % L,
|
||||||
views.SnippetRawView.as_view(), name='snippet_details_raw'),
|
views.SnippetRawView.as_view(), name='snippet_details_raw'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -176,58 +176,40 @@ class SnippetHistory(TemplateView):
|
||||||
return ctx
|
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
|
# 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):
|
class APIView(View):
|
||||||
"""
|
"""
|
||||||
API 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):
|
def post(self, request, *args, **kwargs):
|
||||||
content = request.POST.get('content', '')
|
content = request.POST.get('content', '')
|
||||||
lexer = request.POST.get('lexer', highlight.LEXER_DEFAULT).strip()
|
lexer = request.POST.get('lexer', highlight.LEXER_DEFAULT).strip()
|
||||||
|
@ -276,11 +258,11 @@ class APIView(View):
|
||||||
)
|
)
|
||||||
s.save()
|
s.save()
|
||||||
|
|
||||||
if not response_format in FORMAT_MAPPING:
|
formatter = getattr(self, '_format_{0}'.format(response_format), None)
|
||||||
response = _format_default(s)
|
if not formatter:
|
||||||
|
response = self._format_default(s)
|
||||||
else:
|
else:
|
||||||
response = FORMAT_MAPPING[response_format](s)
|
response = formatter(s)
|
||||||
|
|
||||||
return HttpResponse(response)
|
return HttpResponse(response)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue