Compare commits

..

No commits in common. "f267d9d2e789da0f4d86bf326bd104b432ba7ac1" and "da7075a904b471a6fc2670ebf5e10fbc596e2c19" have entirely different histories.

11 changed files with 21 additions and 216 deletions

View file

@ -53,7 +53,7 @@ ROOT_URLCONF = "flangr.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ["flangr/templates"],
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
@ -111,5 +111,3 @@ STATIC_URL = "static/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
MEDIA_URL = "media/"
AUTH_USER_MODEL = "auth.User"

View file

@ -1,9 +1,3 @@
from django.contrib import admin
from .models import Post, Collection, Comment
# Register your models here.
admin.site.register(Post)
admin.site.register(Collection)
admin.site.register(Comment)

View file

@ -1,3 +0,0 @@
from django.forms import ModelForm
from .models import Comment

View file

@ -1,17 +1,13 @@
# Generated by Django 4.0.5 on 2022-07-04 20:55
# Generated by Django 4.0.5 on 2022-07-03 21:35
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
dependencies = []
operations = [
migrations.CreateModel(
@ -29,71 +25,8 @@ class Migration(migrations.Migration):
("img", models.ImageField(upload_to="posts/%Y/%m")),
("posted", models.DateTimeField(auto_now_add=True)),
("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)),
("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,
),
),
("body", models.TextField()),
],
),
]

View file

@ -1,29 +0,0 @@
# 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),
),
]

View file

@ -1,49 +1,15 @@
import uuid
from django.db import models
from django.conf import settings
# 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):
img = models.ImageField(upload_to=get_img_location)
img = models.ImageField(upload_to="posts/%Y/%m")
posted = models.DateTimeField(auto_now_add=True)
public = models.BooleanField(default=False)
title = models.CharField(max_length=255, null=True)
body = models.TextField(null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
body = models.TextField()
def __str__(self):
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}"

View file

@ -3,29 +3,24 @@ import os
from django.test import TestCase
from django.core.files import File
from .models import Post, Collection
from .models import Post
# Create your tests here.
class TestModelTests(TestCase):
def setUp(self):
class TestPostModelTests(TestCase):
def test_can_create_model(self):
with open(
os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"),
mode="rb",
) as f:
self.post = Post.objects.create(
img=File(f, "somefile.png"),
title="Foobar",
body="Some file",
)
try:
p = Post.objects.create(
img=File(f, "somefile.png"),
title="Foobar",
body="Some file",
)
def tearDown(self):
self.post.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))
self.assertIn("Foobar", str(p))
finally:
p.img.delete()

View file

@ -1,8 +0,0 @@
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()),
]

View file

@ -1,17 +1,3 @@
from django.views.generic import DetailView
from django.forms import modelform_factory
from django.shortcuts import render
# 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)

View file

@ -1,20 +0,0 @@
<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>

View file

@ -14,15 +14,8 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
urlpatterns = [
path("admin/", admin.site.urls),
path("posts/", include("flangr.posts.urls")),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)