def save(self, *args, **kwargs): """ Due to cross import, can't directly import Utilisateur """ # if self.send_to_all or self.send_to_inactive: # or self.send_to_active authusers = User.objects.filter(is_superuser=False).only('email') users = [] if self.send_to_inactive: for u in authusers: try: if u.profile.get_picture_count() == 0: users.append(u) except ObjectDoesNotExist: pass else: users = authusers from notification import Notification notif = Notification(debug=settings.IS_LOCAL) notif.set_content(self.subject, self.content) for u in users: notif.push(u.email) notif.send(debug=settings.IS_LOCAL) super(MailingList, self).save(*args, **kwargs)
def postmessage(request): """ Post a message as AJAX """ remote_addr = request.META.get('REMOTE_ADDR') http_referer = request.META.get('HTTP_REFERER') L.info("Post a message from {remote} for the page {referer}".format(remote=remote_addr, referer=http_referer)) if request.method == 'POST': if CommentPref.objects.get_preferences().only_registred and not request.user.is_authenticated(): L.error('ucomment.views.postmessage: Try to post when not authenticated. ') return HttpResponseBadRequest('') parent = int(request.POST.get('parent')) parent = Comment.objects.get(pk=parent) if parent != 0 else None content = request.POST['content'] onwallurl = request.POST.get('onwallurl') if content: referer = request.POST.get('url') if not referer: referer = "/" if not onwallurl and referer == '/' and onwallurl != '/': referer = http_referer.replace('http://%s' % Site.objects.get_current().domain, '') # comment = Comment.objects.create( comment = Comment.objects.post_comment( url=referer, message=content, raw_html=False, user=request.user, ip=remote_addr, parent=parent ) # Prepare JSON data = { 'username': request.user.username, 'submission_date': convert_date(timezone.now()), 'knowuser': True, 'avatar': request.user.profile.avatar_or_default(), 'userid': request.user.id, 'commentcount': Comment.objects.filter(user=request.user, visible=True, moderate=False).only('id').count(), 'pigstiescount': Picture.objects.filter(user=request.user, trash=False).only('id').count(), 'content': comment.content, 'commentid': comment.id, 'user_authenticated': request.user.is_authenticated(), 'csrf_token': get_token(request), } if parent is not None: data['parentid'] = parent.id else: data['parentid'] = comment.id # Send a email to all users if parent is not None: comments = list(Comment.objects.filter((Q(parent=parent) & ~Q(user=request.user))).only('user__email')) mails = {} for comment in comments: mails[comment.user.email] = '' req = RequestContext(request, { 'username': request.user.username, 'message': comment.content, 'url': comment.url }) if not settings.IS_LOCAL: notif = Notification(settings.BANDCOCHON_CONFIG.EmailTemplates.user_comment) for mail in mails.keys(): notif.push(mail, req) notif.send() # Send Json return JsonResponse(data) else: L.error("ucomment.views.postmessage : Don't have a content message") else: L.error("ucomment.views.postmessage: Not a POST call :") return HttpResponseBadRequest('')