Skip to content

Commit

Permalink
Change parent class name to mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebamus committed Sep 26, 2024
1 parent c554200 commit 6b0f3e0
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 84 deletions.
4 changes: 2 additions & 2 deletions blog/admin/project_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rangefilter.filters import DateRangeFilter

from blog.admin.common_admin import blog_admin_site
from blog.models.blog import ProjectPost
from blog.models.blog import ProjectPostMixin
from blog.models.blog_reply import ProjectPostReply
from common.mixin.admin.redis_cache_handler import (
AdminCacheCleanPostMixin,
Expand Down Expand Up @@ -65,5 +65,5 @@ class ProjectPostReplyAdmin(CustomActionsAdminMixin, TrimHtmlTagsAdminMixin, Adm
]


blog_admin_site.register(ProjectPost, ProjectPostAdmin)
blog_admin_site.register(ProjectPostMixin, ProjectPostAdmin)
blog_admin_site.register(ProjectPostReply, ProjectPostReplyAdmin)
48 changes: 24 additions & 24 deletions blog/models/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_data(self):
return self.get_queryset().data()


class Post(models.Model):
class PostMixin(models.Model):
class Difficulty(models.TextChoices):
BEGINNER = "0", _("Beginner")
INTERMEDIATE = "1", _("Intermediate")
Expand Down Expand Up @@ -132,11 +132,11 @@ def tag_save(self, content):
self.tag_set.add(tag)


class ProjectPost(Post):
class ProjectPost(PostMixin):
role = models.CharField(
max_length=15,
choices=Post.ProjectRole.choices,
default=Post.ProjectRole.OWNER,
choices=PostMixin.ProjectRole.choices,
default=PostMixin.ProjectRole.OWNER,
verbose_name=_("Project Role"),
)
dev_lang = models.CharField(
Expand All @@ -145,8 +145,8 @@ class ProjectPost(Post):
version = models.CharField(max_length=10, blank=False, verbose_name=_("Version"))
branch = models.CharField(
max_length=15,
choices=Post.Branch.choices,
default=Post.Branch.BACKEND,
choices=PostMixin.Branch.choices,
default=PostMixin.Branch.BACKEND,
verbose_name=_("Project Branch"),
)
repository = models.URLField(blank=True, verbose_name=_("Repository"))
Expand All @@ -168,20 +168,20 @@ def get_absolute_url(self):
return reverse("blog:project_detail", kwargs={"pk": self.pk})


class OnlineStudyPost(Post):
class OnlineStudyPost(PostMixin):
dev_lang = models.CharField(
max_length=20, blank=False, verbose_name=_("Development Language")
)
branch = models.CharField(
max_length=15,
choices=Post.Branch.choices,
default=Post.Branch.BACKEND,
choices=PostMixin.Branch.choices,
default=PostMixin.Branch.BACKEND,
verbose_name=_("Project Branch"),
)
difficulty_level = models.CharField(
max_length=15,
choices=Post.Difficulty.choices,
default=Post.Difficulty.BEGINNER,
choices=PostMixin.Difficulty.choices,
default=PostMixin.Difficulty.BEGINNER,
verbose_name=_("Difficulty Level"),
)
like_user_set = models.ManyToManyField(
Expand All @@ -202,7 +202,7 @@ def get_absolute_url(self):
return reverse("blog:online_study_detail", kwargs={"pk": self.pk})


class BlogPost(Post):
class BlogPost(PostMixin):
like_user_set = models.ManyToManyField(
settings.AUTH_USER_MODEL,
blank=True,
Expand All @@ -221,27 +221,27 @@ def get_absolute_url(self):
return reverse("blog:blog_detail", kwargs={"pk": self.pk})


class OpenSourcePost(Post):
class OpenSourcePost(PostMixin):
role = models.CharField(
max_length=15,
choices=Post.ProjectRole.choices,
default=Post.ProjectRole.OWNER,
choices=PostMixin.ProjectRole.choices,
default=PostMixin.ProjectRole.OWNER,
verbose_name=_("Project Role"),
)
dev_lang = models.CharField(
max_length=20, blank=False, verbose_name=_("Development Language")
)
branch = models.CharField(
max_length=15,
choices=Post.Branch.choices,
default=Post.Branch.BACKEND,
choices=PostMixin.Branch.choices,
default=PostMixin.Branch.BACKEND,
verbose_name=_("Project Branch"),
)
repository = models.URLField(blank=True, verbose_name=_("Repository"))
difficulty_level = models.CharField(
max_length=15,
choices=Post.Difficulty.choices,
default=Post.Difficulty.BEGINNER,
choices=PostMixin.Difficulty.choices,
default=PostMixin.Difficulty.BEGINNER,
verbose_name=_("Difficulty Level"),
)
like_user_set = models.ManyToManyField(
Expand All @@ -262,20 +262,20 @@ def get_absolute_url(self):
return reverse("blog:opensource_detail", kwargs={"pk": self.pk})


class BooksPost(Post):
class BooksPost(PostMixin):
dev_lang = models.CharField(
max_length=20, blank=False, verbose_name=_("Development Language")
)
branch = models.CharField(
max_length=15,
choices=Post.Branch.choices,
default=Post.Branch.BACKEND,
choices=PostMixin.Branch.choices,
default=PostMixin.Branch.BACKEND,
verbose_name=_("Project Branch"),
)
difficulty_level = models.CharField(
max_length=15,
choices=Post.Difficulty.choices,
default=Post.Difficulty.BEGINNER,
choices=PostMixin.Difficulty.choices,
default=PostMixin.Difficulty.BEGINNER,
verbose_name=_("Difficulty Level"),
)
like_user_set = models.ManyToManyField(
Expand Down
12 changes: 6 additions & 6 deletions blog/models/blog_reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
logger = logging.getLogger(getattr(settings, "BLOG_LOGGER", "django"))


class BlogReply(models.Model):
class BlogReplyMixin(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
comment = models.TextField(default="", blank=False, verbose_name=_("Comment"))
depth = models.SmallIntegerField(default=0, verbose_name=_("Reply depth"))
Expand All @@ -35,7 +35,7 @@ def __str__(self):
return self.comment


class ProjectPostReply(BlogReply):
class ProjectPostReply(BlogReplyMixin):
post = models.ForeignKey("blog.ProjectPost", on_delete=models.CASCADE)

class Meta:
Expand All @@ -47,7 +47,7 @@ class Meta:
]


class OnlineStudyPostReply(BlogReply):
class OnlineStudyPostReply(BlogReplyMixin):
post = models.ForeignKey("blog.OnlineStudyPost", on_delete=models.CASCADE)

class Meta:
Expand All @@ -59,7 +59,7 @@ class Meta:
]


class BlogPostReply(BlogReply):
class BlogPostReply(BlogReplyMixin):
post = models.ForeignKey("blog.BlogPost", on_delete=models.CASCADE)

class Meta:
Expand All @@ -71,7 +71,7 @@ class Meta:
]


class OpenSourcePostReply(BlogReply):
class OpenSourcePostReply(BlogReplyMixin):
post = models.ForeignKey("blog.OpenSourcePost", on_delete=models.CASCADE)

class Meta:
Expand All @@ -83,7 +83,7 @@ class Meta:
]


class BooksPostReply(BlogReply):
class BooksPostReply(BlogReplyMixin):
post = models.ForeignKey("blog.BooksPost", on_delete=models.CASCADE)

class Meta:
Expand Down
4 changes: 2 additions & 2 deletions blog/sitemaps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from .models import ProjectPost, OnlineStudyPost, BlogPost, OpenSourcePost, BooksPost
from .models import ProjectPostMixin, OnlineStudyPost, BlogPost, OpenSourcePost, BooksPost


class ProjectPostListSitemap(Sitemap):
Expand All @@ -21,7 +21,7 @@ class ProjectPostSitemap(Sitemap):
priority = 0.5

def items(self):
return ProjectPost.objects.all()
return ProjectPostMixin.objects.all()

def location(self, obj):
return reverse(
Expand Down
12 changes: 6 additions & 6 deletions blog/tests/test_blog_html.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from django.urls import reverse

from blog.models.blog import ProjectPost
from blog.models.blog import ProjectPostMixin
from users.models import User

headers = {
Expand Down Expand Up @@ -49,7 +49,7 @@ def test_blog_project_create(staff):
**headers,
)
assert response.status_code == 302
queryset = ProjectPost.objects.filter(
queryset = ProjectPostMixin.objects.filter(
title="test",
content="<p>hello<br></p>",
)
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_blog_project_update(staff):
**headers,
)
assert response.status_code == 302
queryset = ProjectPost.objects.filter(
queryset = ProjectPostMixin.objects.filter(
title="test",
)
assert "hello" in queryset.values()[0]["content"]
Expand All @@ -95,7 +95,7 @@ def test_blog_project_update(staff):
},
**headers,
)
queryset = ProjectPost.objects.filter(
queryset = ProjectPostMixin.objects.filter(
title="test",
)
assert "bye" in queryset.values()[0]["content"]
Expand All @@ -119,7 +119,7 @@ def test_blog_project_delete(staff):
**headers,
)
assert response.status_code == 302
queryset = ProjectPost.objects.filter(
queryset = ProjectPostMixin.objects.filter(
title="test",
)
assert queryset.exists()
Expand All @@ -128,7 +128,7 @@ def test_blog_project_delete(staff):
path=url,
**headers,
)
queryset = ProjectPost.objects.filter(
queryset = ProjectPostMixin.objects.filter(
title="test",
)
assert queryset[0].is_deleted is True
30 changes: 15 additions & 15 deletions blog/views/project/project_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@
)
from common.decorators.cache import index_cache_clean

from ...models.blog import ProjectPost
from ...models.blog import ProjectPostMixin
from .project_forms import ProjectForm

logger = logging.getLogger(getattr(settings, "BLOG_LOGGER", "django"))


class ProjectListView(ListView):
model = ProjectPost
model = ProjectPostMixin
template_name = "blog/blog_list.html"
paginate_by = 10
paginate_orphans = 1 # if last page has 1 item, it will add in last page.
context_object_name = "board"

def get_queryset(self):
return ProjectPost.activate_objects.get_data()
return ProjectPostMixin.activate_objects.get_data()


class ProjectDetailView(DetailView):
model = ProjectPost
model = ProjectPostMixin
template_name = "blog/project/project_detail.html"
context_object_name = "board"
cache_prefix = "blog:Project"
Expand Down Expand Up @@ -96,12 +96,12 @@ def get_context_data(self, **kwargs):
context.update(queryset)
else:
pre_temp_queryset = (
ProjectPost.objects.filter(pk__lt=context["board"].pk)
ProjectPostMixin.objects.filter(pk__lt=context["board"].pk)
.order_by("-pk")
.first()
)
next_temp_queryset = (
ProjectPost.objects.filter(pk__gt=context["board"].pk)
ProjectPostMixin.objects.filter(pk__gt=context["board"].pk)
.order_by("pk")
.first()
)
Expand All @@ -117,7 +117,7 @@ def get_context_data(self, **kwargs):
context["next_board"] = next_temp_queryset

context["like_state"] = (
ProjectPost.objects.filter(pk=self.kwargs.get("pk"))
ProjectPostMixin.objects.filter(pk=self.kwargs.get("pk"))
.first()
.like_user_set.filter(pk=self.request.user.pk)
.exists()
Expand Down Expand Up @@ -148,7 +148,7 @@ def get_context_data(self, **kwargs):

@method_decorator(index_cache_clean, name="dispatch")
class ProjectCreateView(LoginRequiredMixin, CreateView):
model = ProjectPost
model = ProjectPostMixin
template_name = "blog/project/project_edit.html"
success_url = reverse_lazy("blog:project_list")
form_class = ProjectForm
Expand All @@ -167,7 +167,7 @@ def form_valid(self, form):

@method_decorator(index_cache_clean, name="dispatch")
class ProjectUpdateView(LoginRequiredMixin, UpdateView):
model = ProjectPost
model = ProjectPostMixin
pk_url_kwarg = "pk"
form_class = ProjectForm
template_name = "blog/project/project_update.html"
Expand Down Expand Up @@ -196,7 +196,7 @@ def form_valid(self, form):

@method_decorator(index_cache_clean, name="dispatch")
class ProjectDeleteView(LoginRequiredMixin, View):
model = ProjectPost
model = ProjectPostMixin
pk_url_kwarg = "pk"
success_url = reverse_lazy("blog:project_list")
login_url = reverse_lazy("users:login")
Expand Down Expand Up @@ -226,7 +226,7 @@ def post(self, request, *args, **kwargs):

class ProjectLikeJsonView(LoginRequiredMixin, View):
def get(self, request, pk):
post = get_object_or_404(ProjectPost, pk=pk)
post = get_object_or_404(ProjectPostMixin, pk=pk)

with transaction.atomic():
post_like = post.like_user_set.select_for_update().filter(
Expand All @@ -236,16 +236,16 @@ def get(self, request, pk):
if post_like: # there are field data already, not created
message = "Like canceled"
if post.like_count != 0:
ProjectPost.objects.filter(pk=pk).update(
ProjectPostMixin.objects.filter(pk=pk).update(
like_count=F("like_count") - 1
)
post.like_user_set.remove(self.request.user)
else:
message = "Like"
ProjectPost.objects.filter(pk=pk).update(like_count=F("like_count") + 1)
ProjectPostMixin.objects.filter(pk=pk).update(like_count=F("like_count") + 1)
post.like_user_set.add(self.request.user)

post = ProjectPost.objects.get(pk=pk) # get latest post information
post = ProjectPostMixin.objects.get(pk=pk) # get latest post information

context = {"like_count": post.like_count, "message": message}

Expand All @@ -255,7 +255,7 @@ def get(self, request, pk):
class ProjectVisitJsonView(View):
def get(self, request, pk):
with transaction.atomic():
ProjectPost.objects.filter(pk=pk).update(visit_count=F("visit_count") + 1)
ProjectPostMixin.objects.filter(pk=pk).update(visit_count=F("visit_count") + 1)
message = "visit count updated"

context = {"message": message}
Expand Down
Loading

0 comments on commit 6b0f3e0

Please sign in to comment.