From: David Kerkeslager Date: Wed, 1 Jul 2020 03:25:56 +0000 (-0400) Subject: Get creation of a text file working X-Git-Url: https://code.kerkeslager.com/?a=commitdiff_plain;h=3c5fb86f6a9a6c7fccb7b2975165ce6c29836c16;p=txt.house Get creation of a text file working --- diff --git a/txt_house/migrations/0001_initial.py b/txt_house/migrations/0001_initial.py new file mode 100644 index 0000000..c761dfb --- /dev/null +++ b/txt_house/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 3.0.7 on 2020-07-01 02:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='TextFile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('text', models.TextField()), + ('edit_key', models.CharField(max_length=43)), + ], + ), + ] diff --git a/txt_house/migrations/__init__.py b/txt_house/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/txt_house/models.py b/txt_house/models.py new file mode 100644 index 0000000..2f57ca0 --- /dev/null +++ b/txt_house/models.py @@ -0,0 +1,19 @@ +import base64 +import math + +from django.contrib import admin +from django.db import models +from django.urls import reverse + +class TextFile(models.Model): + text = models.TextField() + edit_key = models.CharField(max_length=43) + + def get_absolute_url(self): + pk = base64.urlsafe_b64encode( + self.pk.to_bytes(math.ceil(self.pk.bit_length() / 8), 'big'), + ).decode('utf-8') + + return reverse('text-file', kwargs={ 'pk': pk }) + +admin.site.register(TextFile) diff --git a/txt_house/settings.py b/txt_house/settings.py index cbe5164..4744d07 100644 --- a/txt_house/settings.py +++ b/txt_house/settings.py @@ -37,6 +37,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + + 'txt_house', ] MIDDLEWARE = [ diff --git a/txt_house/templates/index.html b/txt_house/templates/index.html new file mode 100644 index 0000000..8523976 --- /dev/null +++ b/txt_house/templates/index.html @@ -0,0 +1,16 @@ + + + + + + txt.house + + + +
+ {% csrf_token %} + + +
+ + diff --git a/txt_house/urls.py b/txt_house/urls.py index ecefff9..a58345d 100644 --- a/txt_house/urls.py +++ b/txt_house/urls.py @@ -16,6 +16,11 @@ Including another URLconf from django.contrib import admin from django.urls import path +from . import views + urlpatterns = [ path('admin/', admin.site.urls), + path('', views.index, name='index'), + path('create', views.create, name='create'), + path('t/', views.text_file, name='text-file'), ] diff --git a/txt_house/views.py b/txt_house/views.py new file mode 100644 index 0000000..5a8f922 --- /dev/null +++ b/txt_house/views.py @@ -0,0 +1,34 @@ +import base64 +import secrets + +from django.http import HttpResponse, Http404 +from django.shortcuts import get_object_or_404, redirect, render + +from . import models + +def index(request): + return render( + request, + 'index.html', + ) + +def create(request): + if request.method != 'POST': + raise Http404() + + text = request.POST.get('text') + + tf = models.TextFile(text=text, edit_key=secrets.token_urlsafe()) + tf.save() + + return redirect(tf.get_absolute_url()) + +def text_file(request, pk): + if request.method != 'GET': + raise Http404() + + pk_int = int.from_bytes(base64.urlsafe_b64decode(pk), 'big') + + tf = get_object_or_404(models.TextFile, pk=pk_int) + + return HttpResponse(tf.text)