def post(self, request, *args, **kwargs): # get post data num_events = int(request.POST.get('events', 0)) num_comments = int(request.POST.get('comments', 0)) unread_notifications = int(request.POST.get('unread_notifications', 0)) unread_messages = int(request.POST.get('unread_messages', 0)) # generate data users = User.generate(20) events = Event.generate(num_events) comments = Comment.generate(num_comments, users) newsletter = Newsletter.get_newsletter() # send_email if 'send_email' in request.POST: newsletter.send_newsletter( users=[request.user], store=False, events=events, comments=comments, notifications=unread_notifications, messages=unread_messages, ) return redirect(reverse('newsletter_scenario')) # render email template else: current_site = get_current_site(request) site_name = current_site.name domain = current_site.domain protocol = 'https' if request.is_secure() else 'http' context = { 'next_events': events, 'comments': comments, 'unread_notifications': unread_notifications, 'unread_messages': unread_messages, 'user': request.user, 'event_colors': Event.get_dict_event_colors(), 'site_name': site_name, 'domain': domain, 'protocol': protocol, } return render(request, 'emails/newsletter.html', context=context)
def get_context_data(self, **kwargs): context = super(NewsletterDetailView, self).get_context_data(**kwargs) context['next_events'] = Event.objects.incomming()[:4] if self.object.comments.exists(): context['comments'] = self.object.comments.all() else: context['comments'] = Comment.objects.unsent_highlighted() # add site and domain vars current_site = Site.objects.get_current() site_name = current_site.name domain = current_site.domain protocol = 'https' if self.request.is_secure() else 'http' context['site_name'] = site_name context['domain'] = domain context['protocol'] = protocol context['event_colors'] = Event.get_dict_event_colors() return context
def send_newsletter( self, users=None, now=None, store=True, comments=None, events=None, notifications=None, messages=None, use_https=False, ): now = now if now else timezone.now() context = {} newsletter = self # set comments comments = newsletter.comments.all() if comments is None else comments # get active users users = User.objects.members().exclude( newsletters__newsletter__in=[newsletter], )[:5] if users is None else users # check if exists newsletter and users if newsletter and users: # set data context for emails context['comments'] = comments # next_events next_events = Event.objects.between_dates( now, now + timezone.timedelta(days=14) ) if events is None else events for user in users: # set user to the context context['user'] = user # set next events to the context context['next_events'] = ( next_events if user.is_experimenta() else next_events.exclude( activity_type=Event.ACTIVITY_TYPES.EXPERIMENTA ) ) if events is None else events context['event_colors'] = Event.get_dict_event_colors() # set unread notifications to the context context['unread_notifications'] = ( user.notification_set.filter( read=False, ).count() ) if notifications is None else notifications # set unread messages to the context context['unread_messages'] = ( user.received_email_messages.filter( read=False, ).count() ) if messages is None else messages # add urls vars current_site = Site.objects.get_current() site_name = current_site.name domain = current_site.domain protocol = 'https' if use_https else 'http' context['site_name'] = site_name context['domain'] = domain context['protocol'] = protocol # send email email_manager.send_emails( emails=(user.email,), template_name='newsletter', subject=newsletter.get_subject(), context=context, fail_silently=False, ) # update user's newsletter flag if store: if not SentNewsletter.objects.filter( user=user, newsletter=newsletter, ).exists(): # add log SentNewsletter.objects.create( user=user, newsletter=newsletter, )