Compare commits
6 commits
da7075a904
...
f267d9d2e7
| Author | SHA1 | Date | |
|---|---|---|---|
| f267d9d2e7 | |||
| 343e8b8834 | |||
| 641ace957b | |||
| 32636b15b6 | |||
| dc384710b2 | |||
| 8ba167823d |
11 changed files with 216 additions and 21 deletions
|
|
@ -53,7 +53,7 @@ ROOT_URLCONF = "flangr.urls"
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||||
"DIRS": [],
|
"DIRS": ["flangr/templates"],
|
||||||
"APP_DIRS": True,
|
"APP_DIRS": True,
|
||||||
"OPTIONS": {
|
"OPTIONS": {
|
||||||
"context_processors": [
|
"context_processors": [
|
||||||
|
|
@ -111,3 +111,5 @@ STATIC_URL = "static/"
|
||||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||||
|
|
||||||
MEDIA_URL = "media/"
|
MEDIA_URL = "media/"
|
||||||
|
|
||||||
|
AUTH_USER_MODEL = "auth.User"
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,9 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from .models import Post, Collection, Comment
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
|
||||||
|
admin.site.register(Post)
|
||||||
|
admin.site.register(Collection)
|
||||||
|
admin.site.register(Comment)
|
||||||
|
|
|
||||||
3
flangr/posts/forms.py
Normal file
3
flangr/posts/forms.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.forms import ModelForm
|
||||||
|
|
||||||
|
from .models import Comment
|
||||||
|
|
@ -1,13 +1,17 @@
|
||||||
# Generated by Django 4.0.5 on 2022-07-03 21:35
|
# Generated by Django 4.0.5 on 2022-07-04 20:55
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
initial = True
|
initial = True
|
||||||
|
|
||||||
dependencies = []
|
dependencies = [
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
|
|
@ -25,8 +29,71 @@ class Migration(migrations.Migration):
|
||||||
("img", models.ImageField(upload_to="posts/%Y/%m")),
|
("img", models.ImageField(upload_to="posts/%Y/%m")),
|
||||||
("posted", models.DateTimeField(auto_now_add=True)),
|
("posted", models.DateTimeField(auto_now_add=True)),
|
||||||
("public", models.BooleanField(default=False)),
|
("public", models.BooleanField(default=False)),
|
||||||
|
("title", models.CharField(max_length=255, null=True)),
|
||||||
|
("body", models.TextField(null=True)),
|
||||||
|
(
|
||||||
|
"user",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="Comment",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("comment", models.CharField(max_length=255)),
|
||||||
|
(
|
||||||
|
"post",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE, to="posts.post"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"user",
|
||||||
|
models.ForeignKey(
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.SET_NULL,
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="Collection",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
("title", models.CharField(max_length=255)),
|
("title", models.CharField(max_length=255)),
|
||||||
("body", models.TextField()),
|
("description", models.TextField()),
|
||||||
|
(
|
||||||
|
"posts",
|
||||||
|
models.ManyToManyField(related_name="collections", to="posts.post"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"user",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
# Generated by Django 4.0.5 on 2022-07-04 21:26
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
import flangr.posts.models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("posts", "0001_initial"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="comment",
|
||||||
|
name="post",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="comments",
|
||||||
|
to="posts.post",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="post",
|
||||||
|
name="img",
|
||||||
|
field=models.ImageField(upload_to=flangr.posts.models.get_img_location),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -1,15 +1,49 @@
|
||||||
|
import uuid
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
|
|
||||||
|
def get_img_location(instance, filename):
|
||||||
|
uid = uuid.uuid4().hex
|
||||||
|
return f"{uid[0:2]}/{uid[2:]}"
|
||||||
|
|
||||||
|
|
||||||
class Post(models.Model):
|
class Post(models.Model):
|
||||||
img = models.ImageField(upload_to="posts/%Y/%m")
|
img = models.ImageField(upload_to=get_img_location)
|
||||||
posted = models.DateTimeField(auto_now_add=True)
|
posted = models.DateTimeField(auto_now_add=True)
|
||||||
public = models.BooleanField(default=False)
|
public = models.BooleanField(default=False)
|
||||||
|
|
||||||
title = models.CharField(max_length=255)
|
title = models.CharField(max_length=255, null=True)
|
||||||
body = models.TextField()
|
body = models.TextField(null=True)
|
||||||
|
|
||||||
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Post: {self.title} at {self.posted.strftime('%Y-%m-%d %H:%S')}"
|
return f"Post: {self.title} at {self.posted.strftime('%Y-%m-%d %H:%S')}"
|
||||||
|
|
||||||
|
|
||||||
|
class Collection(models.Model):
|
||||||
|
title = models.CharField(max_length=255)
|
||||||
|
description = models.TextField()
|
||||||
|
|
||||||
|
posts = models.ManyToManyField("Post", related_name="collections")
|
||||||
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Collection: {self.title}"
|
||||||
|
|
||||||
|
|
||||||
|
class Comment(models.Model):
|
||||||
|
post = models.ForeignKey("Post", on_delete=models.CASCADE, related_name="comments")
|
||||||
|
comment = models.CharField(max_length=255)
|
||||||
|
|
||||||
|
user = models.ForeignKey(
|
||||||
|
settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Comment by {self.user} on {self.post}"
|
||||||
|
|
|
||||||
|
|
@ -3,24 +3,29 @@ import os
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.core.files import File
|
from django.core.files import File
|
||||||
|
|
||||||
from .models import Post
|
from .models import Post, Collection
|
||||||
|
|
||||||
# Create your tests here.
|
# Create your tests here.
|
||||||
|
|
||||||
|
|
||||||
class TestPostModelTests(TestCase):
|
class TestModelTests(TestCase):
|
||||||
def test_can_create_model(self):
|
def setUp(self):
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"),
|
os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"),
|
||||||
mode="rb",
|
mode="rb",
|
||||||
) as f:
|
) as f:
|
||||||
try:
|
self.post = Post.objects.create(
|
||||||
p = Post.objects.create(
|
img=File(f, "somefile.png"),
|
||||||
img=File(f, "somefile.png"),
|
title="Foobar",
|
||||||
title="Foobar",
|
body="Some file",
|
||||||
body="Some file",
|
)
|
||||||
)
|
|
||||||
|
|
||||||
self.assertIn("Foobar", str(p))
|
def tearDown(self):
|
||||||
finally:
|
self.post.img.delete()
|
||||||
p.img.delete()
|
|
||||||
|
def test_post_has_sensible_str(self):
|
||||||
|
self.assertIn("Foobar", str(self.post))
|
||||||
|
|
||||||
|
def test_collection_has_sensible_str(self):
|
||||||
|
col = Collection.objects.create(title="A collection", description="foobar")
|
||||||
|
self.assertIn("A collection", str(col))
|
||||||
|
|
|
||||||
8
flangr/posts/urls.py
Normal file
8
flangr/posts/urls.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from . import views
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("post/<int:pk>", views.PostDetailView.as_view()),
|
||||||
|
path("post/<int:pk>/comments", views.AddCommentView.as_view()),
|
||||||
|
]
|
||||||
|
|
@ -1,3 +1,17 @@
|
||||||
from django.shortcuts import render
|
from django.views.generic import DetailView
|
||||||
|
from django.forms import modelform_factory
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
|
from .models import Post, Comment
|
||||||
|
|
||||||
|
|
||||||
|
class PostDetailView(DetailView):
|
||||||
|
model = Post
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
context = {
|
||||||
|
"add_comment_form": modelform_factory(Comment, fields=("comment",)),
|
||||||
|
**kwargs,
|
||||||
|
}
|
||||||
|
return super().get_context_data(**context)
|
||||||
|
|
|
||||||
20
flangr/templates/posts/post_detail.html
Normal file
20
flangr/templates/posts/post_detail.html
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<body>
|
||||||
|
{% if object.title %}
|
||||||
|
<h1>{{ object.title }}</h1>
|
||||||
|
{% else %}
|
||||||
|
<h1>{{ object.user.username }}</h1>
|
||||||
|
{% endif %}
|
||||||
|
<img width=400 src={{ object.img.url }}/>
|
||||||
|
<h2> Comments</h2>
|
||||||
|
<ul>
|
||||||
|
{% for comment in object.comments.all %}
|
||||||
|
<li>{{ comment.user }} - {{ comment.comment }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
<form action="{% url 'posts:add_comment' object.pk%}"
|
||||||
|
method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ add_comment_form }}
|
||||||
|
<input type="submit", value="Post">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
|
@ -14,8 +14,15 @@ Including another URLconf
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path
|
from django.urls import path, include
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.conf.urls.static import static
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("admin/", admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
|
path("posts/", include("flangr.posts.urls")),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if settings.DEBUG:
|
||||||
|
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue