Get creation of a text file working
authorDavid Kerkeslager <kerkeslager@gmail.com>
Wed, 1 Jul 2020 03:25:56 +0000 (23:25 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Wed, 1 Jul 2020 03:25:56 +0000 (23:25 -0400)
txt_house/migrations/0001_initial.py [new file with mode: 0644]
txt_house/migrations/__init__.py [new file with mode: 0644]
txt_house/models.py [new file with mode: 0644]
txt_house/settings.py
txt_house/templates/index.html [new file with mode: 0644]
txt_house/urls.py
txt_house/views.py [new file with mode: 0644]

diff --git a/txt_house/migrations/0001_initial.py b/txt_house/migrations/0001_initial.py
new file mode 100644 (file)
index 0000000..c761dfb
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
diff --git a/txt_house/models.py b/txt_house/models.py
new file mode 100644 (file)
index 0000000..2f57ca0
--- /dev/null
@@ -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)
index cbe5164..4744d07 100644 (file)
@@ -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 (file)
index 0000000..8523976
--- /dev/null
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8"/>
+    <meta name="viewport" content="width=device-width, initial-scale=1"/>
+    <title>txt.house</title>
+  </head>
+
+  <body>
+    <form action='create' method='post'>
+      {% csrf_token %}
+      <textarea name='text'></textarea>
+      <input type='submit'></input>
+    </form>
+  </body>
+</html>
index ecefff9..a58345d 100644 (file)
@@ -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/<str:pk>', views.text_file, name='text-file'),
 ]
diff --git a/txt_house/views.py b/txt_house/views.py
new file mode 100644 (file)
index 0000000..5a8f922
--- /dev/null
@@ -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)