Facebook
From Michał, 1 Year ago, written in Python.
Embed
Download Paste or View Raw
Hits: 84
  1. #models
  2.  
  3. from django.db import models
  4. from django.urls import reverse
  5. import misaka
  6. from taggit.managers import TaggableManager
  7.  
  8. class Post(models.Model):
  9.     titile = models.CharField(max_length=250)
  10.     created_at = models.DateTimeField(auto_now=True)
  11.     text = models.TextField()
  12.     text_html = models.TextField(editable=False)
  13.     tags = TaggableManager()
  14.  
  15.     def __str__(self):
  16.         return self.text
  17.     def save(self,*args,**kwargs):
  18.         self.text_html = misaka.html(self.text)
  19.         super().save(*args,**kwargs)
  20.     def get_absolute_url(self):
  21.         return reverse(
  22.             "posts:post_detail",
  23.             kwargs={
  24.                 "username": self.user.username,
  25.                 "pk": self.pk
  26.             }
  27.         )
  28.  
  29. class Actual(models.Model):
  30.     created_at = models.DateTimeField(auto_now=True)
  31.     text = models.TextField()
  32.     text_html = models.TextField(editable=False)
  33.  
  34.     def __str__(self):
  35.         return self.text
  36.     def save(self,*args,**kwargs):
  37.         self.text_html = misaka.html(self.text)
  38.         super().save(*args,**kwargs)
  39.     def get_absolute_url(self):
  40.         return reverse(
  41.             "posts:actual",
  42.             kwargs={
  43.                 "username": self.user.username,
  44.             }
  45.         )
  46.      
  47.      
  48. #views
  49. from django.shortcuts import render
  50. from django.contrib.auth.mixins import LoginRequiredMixin
  51. from django.urls import reverse_lazy
  52. from django.views import generic
  53. from django.http import Http404
  54. from braces.views import SelectRelatedMixin
  55. from . import models,forms
  56. from django.contrib import messages
  57. from django.contrib.auth import get_user_model
  58. from taggit.models import Tag
  59. User = get_user_model()
  60.  
  61. # Create your views here.
  62.  
  63. class PostList(SelectRelatedMixin, generic.ListView):
  64.     model = models.Post
  65.     select_related = ("user")
  66.  
  67. class Actual(generic.ListView):
  68.     models = models.Actual
  69.     template_name = 'posts/actual.html'
  70.  
  71.  
  72. class UserPosts(generic.ListView):
  73.     model = models.Post
  74.     template_name = "posts/user_post.html"
  75.  
  76.     def get_queryset(self):
  77.         try:
  78.             self.post_user = User.objects.prefetch_related("posts").get(
  79.                 username__iexact=self.kwargs.get("username")
  80.             )
  81.         except User.DoesNotExist:
  82.             raise Http404
  83.         else:
  84.             return self.post_user.posts.all()
  85.  
  86.     def get_context_data(self, **kwargs):
  87.         context = super().get_context_data(**kwargs)
  88.         context["post_user"] = self.post_user
  89.         return context
  90.  
  91.  
  92. class PostDetail(SelectRelatedMixin, generic.DetailView):
  93.     model = models.Post
  94.     select_related = ("user")
  95.  
  96.     def get_queryset(self):
  97.         queryset = super().get_queryset()
  98.         return queryset.filter(
  99.             user__username__iexact=self.kwargs.get("username")
  100.         )
  101.  
  102.  
  103. class CreatePost(LoginRequiredMixin, SelectRelatedMixin, generic.CreateView):
  104.     # form_class = forms.PostForm
  105.     form_class = forms.Post
  106.     model = models.Post
  107.  
  108.     def get_form_kwargs(self):
  109.         kwargs = super().get_form_kwargs()
  110.         kwargs.update({"user": self.request.user})
  111.         return kwargs
  112.  
  113.     def form_valid(self, form):
  114.         self.object = form.save(commit=False)
  115.         self.object.user = self.request.user
  116.         self.object.save()
  117.         return super().form_valid(form)
  118.  
  119.  
  120. class DeletePost(LoginRequiredMixin, SelectRelatedMixin, generic.DeleteView):
  121.     model = models.Post
  122.     select_related = ("user", "group")
  123.     success_url = reverse_lazy("posts:all")
  124.  
  125.     def get_queryset(self):
  126.         queryset = super().get_queryset()
  127.         return queryset.filter(user_id=self.request.user.id)
  128.  
  129.     def delete(self, *args, **kwargs):
  130.         messages.success(self.request, "Post usuniÄ™to")
  131.         return super().delete(*args, **kwargs)
  132.      
  133. #forms
  134. class ModelPost:
  135.     class Meta:
  136.         model = Post
  137.         fields = [
  138.             'titile',
  139.             'text',
  140.             'tags',
  141.         ]
  142.