Add extremely primitve post structure

master
Nick Krichevsky 2023-07-10 19:07:33 -04:00
parent 9db9d26c74
commit abb1dc17fd
16 changed files with 154 additions and 4 deletions

View File

6
printpub/post/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class PostConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "printpub.post"

View File

@ -0,0 +1,36 @@
# Generated by Django 4.2.3 on 2023-07-10 19:32
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
("user", "0004_alter_localuser_poster"),
]
operations = [
migrations.CreateModel(
name="Post",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("content", models.TextField()),
(
"author",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="user.poster"
),
),
],
),
]

View File

View File

@ -0,0 +1 @@
from printpub.post.models.post import Post

View File

@ -0,0 +1,9 @@
from django.db import models
import printpub.user.models
class Post(models.Model):
author = models.ForeignKey(printpub.user.models.Poster, on_delete=models.CASCADE)
# TODO: We will want a lot more than text content very quickly...
content = models.TextField()

View File

@ -0,0 +1 @@
from printpub.post.serializers.json.post import PostSerializer

View File

@ -0,0 +1,20 @@
import rest_framework.serializers
import printpub.post.models
class _AuthorSerializer(rest_framework.serializers.Serializer):
display_name = rest_framework.serializers.CharField(
source="local_user.display_name", read_only=True
)
username = rest_framework.serializers.CharField(
source="local_user.username", read_only=True
)
class PostSerializer(rest_framework.serializers.ModelSerializer):
author = _AuthorSerializer()
class Meta:
model = printpub.post.models.Post
fields = ["content", "author"]

5
printpub/post/urls.py Normal file
View File

@ -0,0 +1,5 @@
from django.urls import path
import printpub.post.views.post
urlpatterns = [path("post/<int:pk>", printpub.post.views.post.PostView.as_view())]

View File

@ -0,0 +1 @@
from printpub.post.views.post import PostView

View File

@ -0,0 +1,10 @@
import rest_framework
import rest_framework.generics
import printpub.post.models
import printpub.post.serializers.json
class PostView(rest_framework.generics.RetrieveAPIView):
queryset = printpub.post.models.Post.objects.all()
serializer_class = printpub.post.serializers.json.PostSerializer

View File

@ -43,6 +43,7 @@ INSTALLED_APPS = [
"django.contrib.sites",
"rest_framework",
"printpub.user.apps.UserConfig",
"printpub.post.apps.PostConfig",
]
AUTH_USER_MODEL = "user.LocalUser"

View File

@ -17,9 +17,8 @@ Including another URLconf
from django.contrib import admin
from django.urls import include, path
import printpub.user.urls
urlpatterns = [
path("", include("printpub.user.urls")),
path("", include("printpub.post.urls")),
path("admin/", admin.site.urls),
]

View File

@ -0,0 +1,28 @@
import json
import printpub.post.models
import printpub.post.serializers.json
import printpub.user.models
def test_serializes_post():
poster = printpub.user.models.Poster()
poster.save()
user = printpub.user.models.LocalUser(
username="wint", password="hunter2", display_name="dril", poster=poster
)
user.save()
post = printpub.post.models.Post(
author=poster,
content='"im not owned! im not owned!!", i continue to insist as i slowly shrink and transform into a corn cob',
)
post_serializer = printpub.post.serializers.json.PostSerializer(post)
assert post_serializer.data == {
"content": '"im not owned! im not owned!!", i continue to insist as i slowly shrink and transform into a corn cob',
"author": {
"display_name": "dril",
"username": "wint",
# TODO: work domain into here somehow
},
}

View File

@ -0,0 +1,33 @@
import rest_framework
import printpub.post.models
import printpub.user.models
class TestPostGet:
def test_getting_nonexistent_post_returns_404(self):
client = rest_framework.test.APIClient()
res = client.get("/post/1234")
assert res.status_code == 404
def test_get_200_with_existing_post(self):
poster = printpub.user.models.Poster()
poster.save()
user = printpub.user.models.LocalUser(
username="wint", password="hunter2", display_name="dril", poster=poster
)
user.save()
post = printpub.post.models.Post(
author=poster,
content='"im not owned! im not owned!!", i continue to insist as i slowly shrink and transform into a corn cob',
)
post.save()
client = rest_framework.test.APIClient()
res = client.get(f"/post/{post.pk}")
assert res.status_code == 200
# More data is validated by the serializer, this is just a sanity check
assert (
res.data["content"]
== '"im not owned! im not owned!!", i continue to insist as i slowly shrink and transform into a corn cob'
)

View File

@ -11,12 +11,12 @@ class TestWebfingerGet:
def test_request_with_no_resource_gives_400(self):
client = rest_framework.test.APIClient()
res = client.get("/.well-known/webfinger")
assert res.status_code == 400 # type: ignore
assert res.status_code == 400
def test_request_with_unknown_user_returns_404(self):
client = rest_framework.test.APIClient()
res = client.get("/.well-known/webfinger?resource=acct:wint@my.website")
assert res.status_code == 404 # type: ignore
assert res.status_code == 404
def test_known_user_returns_serializer_data(self):
client = rest_framework.test.APIClient()