Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################################################################
- # models.py
- class Post(models.Model):
- author = models.ForeignKey(CustomUserModel)
- title = models.CharField(max_length=200)
- # TextField for lots of text.
- # yeah, max len is used only for input widget
- text = models.TextField(max_length=15000)
- is_published = models.BooleanField(default=False) # maybe add default?
- ################################################################
- # views.py
- # function view :(
- def unpublish_posts(request):
- ids = request.POST.get('ids').split(',')
- # dunno if django is ok with filter list containing unstripped int-strings
- ids = [int(id_) for id_ in ids]
- unpublish_qs = Post.objects.filter(pk__in=ids, is_published=True)
- if not unpublish_qs.count():
- return HttpResponse()
- unpublish_qs.update(is_published=False)
- # not tested but something like that should work
- notify_emails = (
- unpublish_qs.distinct('author__email')
- .values_list('author__email', flat=True)
- )
- send_email(
- 'Notification',
- f'Your one or more articles has been unpublished!',
- settings.EMAIL_DEFAULT_SENDER,
- notify_emails,
- fail_silently=False,
- )
- # probably we shoud return any resp, not None
- return HttpResponse()
- # WTF
- # else:
- # post.is_published = True
- # post.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement