Skip to content

Commit

Permalink
filter post with comment, added tags
Browse files Browse the repository at this point in the history
  • Loading branch information
walosha committed Jan 18, 2023
1 parent 7b76884 commit e49f2e4
Show file tree
Hide file tree
Showing 20 changed files with 265 additions and 29 deletions.
3 changes: 3 additions & 0 deletions account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ class CustomUser(AbstractUser):
class Meta:
verbose_name = "user"
verbose_name_plural = "users"

def __str__(self):
return f'{self.firstname} {self.lastname}'
11 changes: 10 additions & 1 deletion account/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
from .models import CustomUser


class BasicUserSerializer(serializers.ModelSerializer):
status = serializers.SerializerMethodField()

class Meta:
model = get_user_model()
fields = [
'id', 'firstname', 'lastname', 'email', 'category']


class CustomObtainTokenPairSerializer(TokenObtainPairSerializer):
serializer_class = CustomUserManager

Expand Down Expand Up @@ -43,4 +52,4 @@ def validate_current_password(self, value):
class ListUserSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
fields = ['email', "firstname", "lastname", "category"]
fields = ["id", 'email', "firstname", "lastname", "category"]
1 change: 0 additions & 1 deletion account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class AuthViewSets(viewsets.ModelViewSet):
serializer_class = CreateUserSerializer

def get_serializer_class(self):
print('get_serializer_class', self.action)
if self.action == 'list':
return ListUserSerializer
if self.action == 'create':
Expand Down
20 changes: 20 additions & 0 deletions attendance/migrations/0003_attendance_attendees.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.0.8 on 2023-01-17 22:14

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('attendance', '0002_remove_attendance_title'),
]

operations = [
migrations.AddField(
model_name='attendance',
name='attendees',
field=models.ManyToManyField(to=settings.AUTH_USER_MODEL),
),
]
20 changes: 20 additions & 0 deletions attendance/migrations/0004_alter_attendance_eventid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.0.8 on 2023-01-17 23:40

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('event', '0002_alter_event_options'),
('attendance', '0003_attendance_attendees'),
]

operations = [
migrations.AlterField(
model_name='attendance',
name='eventId',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='event_attendance', to='event.event'),
),
]
20 changes: 20 additions & 0 deletions attendance/migrations/0005_alter_attendance_eventid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.0.8 on 2023-01-17 23:42

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('event', '0002_alter_event_options'),
('attendance', '0004_alter_attendance_eventid'),
]

operations = [
migrations.AlterField(
model_name='attendance',
name='eventId',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='event_attendance', to='event.event'),
),
]
4 changes: 3 additions & 1 deletion attendance/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.db import models
from event.models import Event
from account.models import CustomUser
from core.models import AuditableModel


class Attendance (AuditableModel):
eventId = models.ForeignKey(
Event, on_delete=models.SET_NULL, null=True)
Event, on_delete=models.CASCADE, null=True, related_name='event_attendance')
attendees = models.ManyToManyField(CustomUser)
comment = models.CharField(max_length=256, blank=True)

class Meta:
Expand Down
6 changes: 5 additions & 1 deletion blog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class CommentAdmin(admin.ModelAdmin):

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ["id", "author", 'author', 'created_at', 'status']
list_display = ["id", "author", 'author', 'created_at', 'status', "tags"]
list_filter = ['status', 'created_at']
search_fields = ['author', "status", 'body']
# prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ['author']
date_hierarchy = 'publish'
ordering = ['status', 'publish']
4 changes: 2 additions & 2 deletions blog/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@


class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status=Post.Status.PUBLISHED)
def get_queryset(self, instance):
return super().get_queryset().filter(status=instance.Status.PUBLISHED)
20 changes: 20 additions & 0 deletions blog/migrations/0003_post_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.0.8 on 2023-01-18 12:12

from django.db import migrations
import taggit.managers


class Migration(migrations.Migration):

dependencies = [
('taggit', '0005_auto_20220424_2025'),
('blog', '0002_alter_post_slug'),
]

operations = [
migrations.AddField(
model_name='post',
name='tags',
field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
]
35 changes: 35 additions & 0 deletions blog/migrations/0004_uuidtaggeditem_alter_post_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.0.8 on 2023-01-18 12:34

from django.db import migrations, models
import django.db.models.deletion
import taggit.managers


class Migration(migrations.Migration):

dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('taggit', '0005_auto_20220424_2025'),
('blog', '0003_post_tags'),
]

operations = [
migrations.CreateModel(
name='UUIDTaggedItem',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('object_id', models.UUIDField(db_index=True, verbose_name='object ID')),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_tagged_items', to='contenttypes.contenttype', verbose_name='content type')),
('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_items', to='taggit.tag')),
],
options={
'verbose_name': 'Tag',
'verbose_name_plural': 'Tags',
},
),
migrations.AlterField(
model_name='post',
name='tags',
field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='blog.UUIDTaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
]
19 changes: 19 additions & 0 deletions blog/migrations/0005_alter_post_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.0.8 on 2023-01-18 12:52

import blog.models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0004_uuidtaggeditem_alter_post_tags'),
]

operations = [
migrations.AlterField(
model_name='post',
name='id',
field=models.UUIDField(default=blog.models.generateUUID, editable=False, primary_key=True, serialize=False),
),
]
26 changes: 26 additions & 0 deletions blog/migrations/0006_alter_comment_author_alter_comment_postid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.0.8 on 2023-01-18 19:17

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('blog', '0005_alter_post_id'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='author_comments', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='comment',
name='postId',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='post_comments', to='blog.post'),
),
]
25 changes: 19 additions & 6 deletions blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@
from core.models import AuditableModel
from django.utils import timezone
from account.models import CustomUser
from taggit.managers import TaggableManager
from .manager import PublishedManager
from taggit.models import GenericUUIDTaggedItemBase, TaggedItemBase
from django.utils.translation import gettext_lazy as _
from uuid import uuid4

# from .manager import PublishedManager

def generateUUID():
return str(uuid4())

class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status=Post.Status.PUBLISHED)

class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase):
class Meta:
verbose_name = _("Tag")
verbose_name_plural = _("Tags")


class Post(AuditableModel):
id = models.UUIDField(
primary_key=True, default=generateUUID, editable=False)

class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'
Expand All @@ -29,6 +40,8 @@ class Status(models.TextChoices):
objects = models.Manager() # The default manager.
published = PublishedManager() # Our custom manager.

tags = TaggableManager(through=UUIDTaggedItem)

class Meta:
ordering = ['-publish']
indexes = [
Expand All @@ -39,7 +52,7 @@ def __str__(self):
return self.title

def save(self, *args, **kwargs):
self.slug = str(f'{self.author.firstname} {self.author.lastname}').replace(
self.slug = str(f'{self.title}').replace(
" ", "-").strip().lower() + '-' + str(int(time.time()))
return super().save(*args, **kwargs)

Expand All @@ -50,7 +63,7 @@ class Comment(AuditableModel):
related_name='comments')
author = models.ForeignKey(CustomUser,
on_delete=models.CASCADE,
related_name='comments')
related_name='author')
body = models.TextField()
active = models.BooleanField(default=True)

Expand Down
21 changes: 13 additions & 8 deletions blog/serializers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# django #selenium #webscraping
from rest_framework import serializers
from django.contrib.auth.models import User, Group
from taggit.serializers import (TagListSerializerField,
TaggitSerializer)
from .models import Post, Comment


class PostSerializer(serializers.ModelSerializer):
class CommentSerializer(serializers.ModelSerializer):
author = serializers.UUIDField()

class Meta:
model = Post
model = Comment
# fields = ('id', 'title', 'description', 'completed')
# Shortcut for getting all fields
fields = '__all__'


class CommentSerializer(serializers.ModelSerializer):
class PostSerializer(TaggitSerializer, serializers.ModelSerializer):
tags = TagListSerializerField()
comments = CommentSerializer(many=True, read_only=True)
author = serializers.UUIDField()

class Meta:
model = Comment
# fields = ('id', 'title', 'description', 'completed')
# Shortcut for getting all fields
fields = '__all__'
model = Post
fields = ('title', "author", "slug", "comments", "tags", "status")
5 changes: 3 additions & 2 deletions blog/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.urls import path, include
from .views import PostListCreateAPIView, PostRetrieveAPIView, PostDestroyAPIView, PostUpdateAPIView, CommentListCreateAPIView, CommentRetrieveAPIView, CommentDestroyAPIView, CommentUpdateAPIView
from .views import PostCreateAPIView, PostListAPIView, PostRetrieveAPIView, PostDestroyAPIView, PostUpdateAPIView, CommentListCreateAPIView, CommentRetrieveAPIView, CommentDestroyAPIView, CommentUpdateAPIView

urlpatterns = [
path('posts/<uuid:pk>/', PostRetrieveAPIView.as_view(),
name="Post_detail"),
path('posts/', PostListCreateAPIView.as_view(), name="post_list"),
path('posts/create/', PostCreateAPIView.as_view(), name="post_create"),
path('posts/', PostListAPIView.as_view(), name="post_list"),
path('posts/<uuid:pk>/', PostUpdateAPIView.as_view(), name="post_update"),
path('posts/<uuid:pk>/', PostDestroyAPIView.as_view(),
name="post_delete"),
Expand Down
11 changes: 8 additions & 3 deletions blog/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from .models import Post, Comment
from .serializers import PostSerializer, CommentSerializer
from rest_framework import generics
# from django.db.models import Count


# Post serializer
class PostCreateAPIView(generics.CreateAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer

class PostListCreateAPIView(generics.ListCreateAPIView):

class PostListAPIView(generics.ListAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer


class PostRetrieveAPIView(generics.RetrieveAPIView):

queryset = Post.objects.all()
serializer_class = PostSerializer

Expand All @@ -25,7 +30,7 @@ class PostDestroyAPIView(generics.DestroyAPIView):
serializer_class = PostSerializer


# Commment serializer
# Commment
class CommentListCreateAPIView(generics.ListCreateAPIView):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
Expand Down
Loading

0 comments on commit e49f2e4

Please sign in to comment.