From 9b232a209ae57cea33a0156a5c4cac7e72edda8f Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Thu, 2 Jul 2020 22:27:06 -0400 Subject: [PATCH] Get editing working --- txt_house/models.py | 12 ++- txt_house/templates/base.html | 115 +++++++++++++++++++++++++++++ txt_house/templates/edit.html | 61 +++++++++++++++ txt_house/templates/index.html | 17 ++++- txt_house/templates/text_file.html | 87 ++++++++++++++++++++++ txt_house/urls.py | 1 + txt_house/views.py | 59 ++++++++++++++- 7 files changed, 346 insertions(+), 6 deletions(-) create mode 100644 txt_house/templates/edit.html create mode 100644 txt_house/templates/text_file.html diff --git a/txt_house/models.py b/txt_house/models.py index 2f57ca0..f394478 100644 --- a/txt_house/models.py +++ b/txt_house/models.py @@ -9,11 +9,19 @@ class TextFile(models.Model): text = models.TextField() edit_key = models.CharField(max_length=43) - def get_absolute_url(self): - pk = base64.urlsafe_b64encode( + def get_pk(self): + return base64.urlsafe_b64encode( self.pk.to_bytes(math.ceil(self.pk.bit_length() / 8), 'big'), ).decode('utf-8') + def get_view_url(self): + pk = self.get_pk() + return reverse('text-file', kwargs={ 'pk': pk }) + def get_edit_url(self): + pk = self.get_pk() + + return reverse('edit', kwargs={ 'pk': pk }) + admin.site.register(TextFile) diff --git a/txt_house/templates/base.html b/txt_house/templates/base.html index 1856c0d..a9fb172 100644 --- a/txt_house/templates/base.html +++ b/txt_house/templates/base.html @@ -4,6 +4,121 @@ txt.house + + diff --git a/txt_house/templates/edit.html b/txt_house/templates/edit.html new file mode 100644 index 0000000..ba59df2 --- /dev/null +++ b/txt_house/templates/edit.html @@ -0,0 +1,61 @@ +{% extends "base.html" %} + +{% block body %} + + + + + +{% if edit_key %} + + + {{ edit_key }} + + Save this key in order to edit this file in the future. +{% endif %} + +
+ {% csrf_token %} + {% if edit_key %} + + {% else %} + + + + + + {% if incorrect_key %} + You must enter the correct edit key to save your changes. + {% endif %} + {% endif %} + + +
+ +{% endblock %} diff --git a/txt_house/templates/index.html b/txt_house/templates/index.html index fcd6fe9..8d8535a 100644 --- a/txt_house/templates/index.html +++ b/txt_house/templates/index.html @@ -1,9 +1,24 @@ {% extends 'base.html' %} {% block body %} -
+ + +

txt.house

+ +

Paste or edit in text and submit to get a permanent link for it.

+ + {% csrf_token %}
+ {% endblock %} diff --git a/txt_house/templates/text_file.html b/txt_house/templates/text_file.html new file mode 100644 index 0000000..6961756 --- /dev/null +++ b/txt_house/templates/text_file.html @@ -0,0 +1,87 @@ +{% extends 'base.html' %} + +{% block body %} + + + + + + +
{{ text }}
+ +{% endblock %} diff --git a/txt_house/urls.py b/txt_house/urls.py index a58345d..1665870 100644 --- a/txt_house/urls.py +++ b/txt_house/urls.py @@ -23,4 +23,5 @@ urlpatterns = [ path('', views.index, name='index'), path('create', views.create, name='create'), path('t/', views.text_file, name='text-file'), + path('t//edit', views.edit, name='edit'), ] diff --git a/txt_house/views.py b/txt_house/views.py index 5a8f922..7a1f078 100644 --- a/txt_house/views.py +++ b/txt_house/views.py @@ -2,7 +2,7 @@ import base64 import secrets from django.http import HttpResponse, Http404 -from django.shortcuts import get_object_or_404, redirect, render +from django.shortcuts import get_object_or_404, render from . import models @@ -21,7 +21,15 @@ def create(request): tf = models.TextFile(text=text, edit_key=secrets.token_urlsafe()) tf.save() - return redirect(tf.get_absolute_url()) + return render( + request, + 'edit.html', + { + 'text_file': tf, + 'text': tf.text, + 'edit_key': tf.edit_key, + }, + ) def text_file(request, pk): if request.method != 'GET': @@ -31,4 +39,49 @@ def text_file(request, pk): tf = get_object_or_404(models.TextFile, pk=pk_int) - return HttpResponse(tf.text) + if request.GET.get('raw'): + return HttpResponse(tf.text, content_type='text/plain') + + font = request.GET.get('font', 'sans') + + return render( + request, + 'text_file.html', + { + 'text_file': tf, + 'font': font, + 'text': tf.text.strip(), + }, + ) + +def edit(request, pk): + pk_int = int.from_bytes(base64.urlsafe_b64decode(pk), 'big') + + tf = get_object_or_404(models.TextFile, pk=pk_int) + + context = { + 'text_file': tf, + } + + if request.method == 'POST': + edit_key = request.POST.get('edit_key','') + text = request.POST.get('text','') + + context['text'] = text + + if secrets.compare_digest(edit_key, tf.edit_key): + context['edit_key'] = edit_key + tf.text = text + tf.save() + + else: + context['incorrect_key'] = True + + elif request.method == 'GET': + context['text'] = tf.text + + return render( + request, + 'edit.html', + context, + ) -- 2.20.1