SHARE
TWEET

Untitled

a guest Oct 17th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. models
  2. class Post(models.Model):
  3.     created_by = models.ForeignKey(User, verbose_name="user", on_delete='CASCADE')
  4.     article_title = models.CharField(max_length=200, db_index=True)
  5.     slug = models.SlugField(max_length=150, blank=True, unique=True)
  6.     article_text = models.TextField(blank=True, db_index=True)
  7.     article_date = models.DateTimeField(auto_now_add=True)
  8.     tags = models.ManyToManyField('Tag', blank=True, related_name='posts')
  9.     picture = models.ImageField(null=True, blank=True, upload_to='image/', verbose_name='image')
  10.     comments = GenericRelation('comments')
  11.     # likes = models.PositiveIntegerField(default=0)
  12.     # dislikes = models.PositiveIntegerField(default=0)
  13.  
  14.     def get_absolute_url(self):
  15.         return reverse('post_detail_url', kwargs={'slug': self.slug})
  16.  
  17.     def get_update_url(self):
  18.         return reverse('post_update_url', kwargs={'slug': self.slug})
  19.  
  20.     def get_delete_url(self):
  21.         return reverse('post_delete_url', kwargs={'slug': self.slug})
  22.  
  23.     # def get_delete_comment_url(self):
  24.     #     return reverse('comment_delete_url', kwargs={'slug': self.slug})
  25.  
  26.  
  27.     def save(self, *args, **kwargs):
  28.         if not self.id:
  29.             self.slug = generate_slug(self.article_title)
  30.         super().save(*args, **kwargs)
  31.  
  32.     def __str__(self):
  33.         return self.article_title
  34.  
  35.     class Meta:
  36.         ordering = ['-article_date']
  37.  
  38.  
  39. class Comments(models.Model):
  40.     author = models.ForeignKey(User, verbose_name='Пользователь', on_delete='CASCADE')
  41.     post = models.ForeignKey(Post, verbose_name='Заметка', on_delete='CASCADE')
  42.     text = models.TextField()
  43.     date_comments = models.DateTimeField(auto_now_add=True)
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. views
  51. class PostDelete(ObjectDeleteMixin, View):
  52.     model = Post
  53.     template = 'network/postDelete.html'
  54.     redirect_url = 'post_list_url'
  55.  
  56. def delete_comment(request, id, slug):
  57.     try:
  58.  
  59.         comment = Comments.objects.get(id=id)
  60.         comment.delete()
  61.         return redirect('post_detail_url', slug)
  62.     except Comments.DoesNotExist:
  63.         return HttpResponseNotFound("<h2> Комментарий уже был удален. Вернитесь и обновите страницу. </h2>")
  64.  
  65.  
  66.  
  67.  
  68. forms
  69.  
  70. class PostForm(LoginRequiredMixin, forms.ModelForm, CreateView):
  71.  
  72.     class Meta:
  73.         model = Post
  74.         fields = [
  75.             'article_title',
  76.             'article_text',
  77.             'tags',
  78.             'picture',
  79.         ]
  80.  
  81.         widgets = {
  82.             'article_title': forms.TextInput(attrs={'class': 'form-control', 'size': 14, 'title': 'Enter your title'}),
  83.             'article_text': forms.Textarea(attrs={'class': 'form-control', 'size': 14, 'title': 'Enter body post'}),
  84.             'tags': forms.SelectMultiple(attrs={'class': 'form-control', 'title': 'choice tag'}),
  85.         }
  86.  
  87.     def form_valid(self, form):
  88.         form.instance.created_by = self.request.user
  89.         return super().form_valid(form)
  90.  
  91.     def clean_slug(self):
  92.         new_slug = self.cleaned_data['slug'].lower()
  93.  
  94.         if new_slug == 'create':
  95.             raise ValidationError('wrong name for "Slug"')
  96.         return new_slug
  97.  
  98.  
  99. class CommentForm(ModelForm):
  100.     class Meta:
  101.         model = Comments
  102.         fields = [
  103.             'text'
  104.         ]
  105.  
  106.  
  107.  
  108. utils
  109. class ObjectDeleteMixin:
  110.     model = None
  111.     template = None
  112.     redirect_url = None
  113.  
  114.     def get(self, request, slug):
  115.         obj = self.model.objects.get(slug__iexact=slug)
  116.         return render(request,  self.template, context={self.model.__name__.lower: obj})
  117.  
  118.     def post(self, request, slug):
  119.         obj = self.model.objects.get(slug=slug)
  120.         print(obj)
  121.         print(Post)
  122.         obj.delete()
  123.         return redirect(reverse(self.redirect_url))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top