--- /dev/null
+# 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)),
+ ],
+ ),
+ ]
--- /dev/null
+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)
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
+
+ 'txt_house',
]
MIDDLEWARE = [
--- /dev/null
+<!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>
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'),
]
--- /dev/null
+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)