Add circles, views to modify them and ability to add them to posts
This commit is contained in:
parent
ac962034df
commit
2cb910cc30
12 changed files with 134 additions and 16 deletions
|
|
@ -1,3 +1,7 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from .models import Circle
|
||||
|
||||
# Register your models here.
|
||||
|
||||
admin.site.register(Circle)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
from django.db import models
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
|
|
@ -14,3 +13,6 @@ class Circle(models.Model):
|
|||
owner = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="owned_circles"
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"Circle: {self.name} of user {self.owner_id}"
|
||||
|
|
|
|||
10
flangr/circles/urls.py
Normal file
10
flangr/circles/urls.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "circles"
|
||||
urlpatterns = [
|
||||
path("", views.CircleListView.as_view(), name="circle_list"),
|
||||
path("circle/<int:pk>", views.CircleUpdateView.as_view(), name="circle_update"),
|
||||
path("add", views.CircleCreateView.as_view(), name="circle_create"),
|
||||
]
|
||||
|
|
@ -1,3 +1,41 @@
|
|||
from django.shortcuts import render
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.urls import reverse
|
||||
from django.views.generic import CreateView
|
||||
from django.views.generic import ListView
|
||||
from django.views.generic import UpdateView
|
||||
|
||||
from .models import Circle
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
class CircleListView(LoginRequiredMixin, ListView):
|
||||
def get_queryset(self):
|
||||
return Circle.objects.filter(owner=self.request.user)
|
||||
|
||||
|
||||
class CircleUpdateView(LoginRequiredMixin, UpdateView):
|
||||
model = Circle
|
||||
fields = ["name", "description", "members"]
|
||||
|
||||
def get_queryset(self):
|
||||
return Circle.objects.filter(owner=self.request.user)
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse("circles:circle_list")
|
||||
|
||||
|
||||
class CircleCreateView(LoginRequiredMixin, CreateView):
|
||||
model = Circle
|
||||
fields = ["name", "description", "members"]
|
||||
|
||||
def form_valid(self, form):
|
||||
circle = form.save(commit=False)
|
||||
circle.owner = self.request.user
|
||||
circle.save()
|
||||
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse("circles:circle_list")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
# Generated by Django 4.0.5 on 2022-07-09 13:27
|
||||
|
||||
from django.db import migrations
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("circles", "0001_initial"),
|
||||
("posts", "0003_alter_post_body_alter_post_title"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name="post",
|
||||
name="public",
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="post",
|
||||
name="shared_with",
|
||||
field=models.ManyToManyField(to="circles.circle"),
|
||||
),
|
||||
]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.0.5 on 2022-07-09 13:28
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("posts", "0004_remove_post_public_post_shared_with"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name="post",
|
||||
old_name="shared_with",
|
||||
new_name="circles",
|
||||
),
|
||||
]
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
import uuid
|
||||
|
||||
from django.db import models
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
|
|
@ -15,13 +14,14 @@ def get_img_location(instance, filename):
|
|||
class Post(models.Model):
|
||||
img = models.ImageField(upload_to=get_img_location)
|
||||
posted = models.DateTimeField(auto_now_add=True)
|
||||
public = models.BooleanField(default=False)
|
||||
|
||||
title = models.CharField(max_length=255, blank=True, default="")
|
||||
body = models.TextField(blank=True, default="")
|
||||
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
|
||||
circles = models.ManyToManyField("circles.Circle")
|
||||
|
||||
def __str__(self):
|
||||
return f"Post: {self.title} at {self.posted.strftime('%Y-%m-%d %H:%S')}"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,16 @@
|
|||
from django.views.generic import DetailView, CreateView
|
||||
from django.views.generic.edit import ModelFormMixin
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.forms import modelform_factory
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.urls import reverse
|
||||
from django.views.generic import CreateView
|
||||
from django.views.generic import DetailView
|
||||
from django.views.generic.edit import ModelFormMixin
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from .models import Comment
|
||||
from .models import Post
|
||||
|
||||
# Create your views here.
|
||||
|
||||
from .models import Post, Comment
|
||||
|
||||
|
||||
class PostDetailView(LoginRequiredMixin, DetailView, ModelFormMixin):
|
||||
model = Post
|
||||
|
|
@ -21,6 +22,7 @@ class PostDetailView(LoginRequiredMixin, DetailView, ModelFormMixin):
|
|||
comment.post = self.object
|
||||
comment.user = self.request.user
|
||||
comment.save()
|
||||
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
def get_queryset(self):
|
||||
|
|
@ -37,7 +39,7 @@ class PostDetailView(LoginRequiredMixin, DetailView, ModelFormMixin):
|
|||
|
||||
class PostCreateView(LoginRequiredMixin, CreateView):
|
||||
model = Post
|
||||
fields = ("title", "body", "img")
|
||||
fields = ("title", "body", "img", "circles")
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse("posts:post_detail", kwargs={"pk": self.object.pk})
|
||||
|
|
@ -46,4 +48,5 @@ class PostCreateView(LoginRequiredMixin, CreateView):
|
|||
self.object = form.save(commit=False)
|
||||
self.object.user = self.request.user
|
||||
self.object.save()
|
||||
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
|
|
|||
9
flangr/templates/circles/circle_form.html
Normal file
9
flangr/templates/circles/circle_form.html
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h1>Circle {{ object.name }}</h1>
|
||||
<form action="#" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
<input type="submit" value="Post">
|
||||
</form>
|
||||
{% endblock content %}
|
||||
9
flangr/templates/circles/circle_list.html
Normal file
9
flangr/templates/circles/circle_list.html
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h1>Your Circles</h1>
|
||||
<ul>
|
||||
{% for circle in object_list %}
|
||||
<li><a href="{% url "circles:circle_update" circle.pk %}">{{ circle.name }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock content %}
|
||||
|
|
@ -5,6 +5,6 @@
|
|||
<form enctype="multipart/form-data" action="" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
<input type="submit", value="Post">
|
||||
<input type="submit" value="Post">
|
||||
</form>
|
||||
{% endblock content %}
|
||||
|
|
|
|||
|
|
@ -13,16 +13,17 @@ Including another URLconf
|
|||
1. Import the include() function: from django.urls import include, path
|
||||
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.contrib import admin
|
||||
from django.urls import include
|
||||
from django.urls import path
|
||||
|
||||
urlpatterns = [
|
||||
path("admin/", admin.site.urls),
|
||||
path("accounts/", include("django.contrib.auth.urls")),
|
||||
path("posts/", include("flangr.posts.urls")),
|
||||
path("circles/", include("flangr.circles.urls")),
|
||||
]
|
||||
|
||||
if settings.DEBUG:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue