Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ################################################################
  2. # models.py
  3.  
  4. class Post(models.Model):
  5.     author = models.ForeignKey(CustomUserModel)
  6.     title = models.CharField(max_length=200)
  7.     # TextField for lots of text.
  8.     # yeah, max len is used only for input widget
  9.     text = models.TextField(max_length=15000)
  10.     is_published = models.BooleanField(default=False)  # maybe add default?
  11.  
  12.  
  13. ################################################################
  14. # views.py
  15.  
  16. # function view :(
  17. def unpublish_posts(request):
  18.     ids = request.POST.get('ids').split(',')
  19.     # dunno if django is ok with filter list containing unstripped int-strings
  20.     ids = [int(id_) for id_ in ids]
  21.  
  22.     unpublish_qs = Post.objects.filter(pk__in=ids, is_published=True)
  23.  
  24.     if not unpublish_qs.count():
  25.         return HttpResponse()
  26.  
  27.     unpublish_qs.update(is_published=False)
  28.  
  29.     # not tested but something like that should work
  30.     notify_emails = (
  31.         unpublish_qs.distinct('author__email')
  32.         .values_list('author__email', flat=True)
  33.     )
  34.  
  35.     send_email(
  36.         'Notification',
  37.         f'Your one or more articles has been unpublished!',
  38.         settings.EMAIL_DEFAULT_SENDER,
  39.         notify_emails,
  40.         fail_silently=False,
  41.     )
  42.  
  43.     # probably we shoud return any resp, not None
  44.     return HttpResponse()
  45.  
  46.         # WTF
  47.         # else:
  48.         #     post.is_published = True
  49.         #     post.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement