Add extremely primitve post structure
parent
9db9d26c74
commit
abb1dc17fd
|
@ -0,0 +1,6 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class PostConfig(AppConfig):
|
||||||
|
default_auto_field = "django.db.models.BigAutoField"
|
||||||
|
name = "printpub.post"
|
|
@ -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"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1 @@
|
||||||
|
from printpub.post.models.post import Post
|
|
@ -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()
|
|
@ -0,0 +1 @@
|
||||||
|
from printpub.post.serializers.json.post import PostSerializer
|
|
@ -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"]
|
|
@ -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())]
|
|
@ -0,0 +1 @@
|
||||||
|
from printpub.post.views.post import PostView
|
|
@ -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
|
|
@ -43,6 +43,7 @@ INSTALLED_APPS = [
|
||||||
"django.contrib.sites",
|
"django.contrib.sites",
|
||||||
"rest_framework",
|
"rest_framework",
|
||||||
"printpub.user.apps.UserConfig",
|
"printpub.user.apps.UserConfig",
|
||||||
|
"printpub.post.apps.PostConfig",
|
||||||
]
|
]
|
||||||
|
|
||||||
AUTH_USER_MODEL = "user.LocalUser"
|
AUTH_USER_MODEL = "user.LocalUser"
|
||||||
|
|
|
@ -17,9 +17,8 @@ Including another URLconf
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
|
|
||||||
import printpub.user.urls
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", include("printpub.user.urls")),
|
path("", include("printpub.user.urls")),
|
||||||
|
path("", include("printpub.post.urls")),
|
||||||
path("admin/", admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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
|
||||||
|
},
|
||||||
|
}
|
|
@ -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'
|
||||||
|
)
|
|
@ -11,12 +11,12 @@ class TestWebfingerGet:
|
||||||
def test_request_with_no_resource_gives_400(self):
|
def test_request_with_no_resource_gives_400(self):
|
||||||
client = rest_framework.test.APIClient()
|
client = rest_framework.test.APIClient()
|
||||||
res = client.get("/.well-known/webfinger")
|
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):
|
def test_request_with_unknown_user_returns_404(self):
|
||||||
client = rest_framework.test.APIClient()
|
client = rest_framework.test.APIClient()
|
||||||
res = client.get("/.well-known/webfinger?resource=acct:wint@my.website")
|
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):
|
def test_known_user_returns_serializer_data(self):
|
||||||
client = rest_framework.test.APIClient()
|
client = rest_framework.test.APIClient()
|
||||||
|
|
Loading…
Reference in New Issue