def handle (self, *args, **options): translation.activate(settings.LANGUAGE_CODE) now = datetime.now() self.stdout.write("> sending updates at %s" % now) if len(args): qs = Profile.on_site.filter(user__email=args) else: qs = Profile.on_site.all() # TODO: get only new questions and questions with new answers context = {'questions': Question.on_site.all().order_by('-updated_at'), 'header': "Hello there!", 'ROOT_URL': get_root_url(), } for profile in qs: user = profile.user last_sent = profile.last_email_update try: freq = self.diffs[user.profile.email_notification] except KeyError: continue if not user.is_active: ''' send an invitation email ''' reg_profile = user.registrationprofile_set.all()[0] if reg_profile.activation_key_expired() and last_sent==NEVER_SENT: key = reg_profile.activation_key context['key'] = key context['activation_url'] = root_url + reverse('accept-invitation', args=(key,)) # reset the key duration, giving the user more time user.date_joined = now user.save() else: continue elif now-last_sent < freq: # don't exceed the frequency! continue context['is_active'] = user.is_active context['last_sent'] = last_sent context['header'] = "email.update_header" context['footer'] = 'email.footer' html_content = render_to_string("qa/email_update.html", context) ''' send the email only when there's fresh content ''' fresh_content = self.fresh_content_re.search(html_content) if not fresh_content: self.stdout.write("--- nothing fresh for %(username)s at %(email)s is_active=%(is_active)s" % user.__dict__) continue subject = '%s | %s' % (site.name, FlatBlock.objects.get(slug="email.update_header").header.rstrip()) # TODO: create a link for the update and send it to shaib text_content = 'Sorry, we only support html based email' # create the email, and attach the HTML version as well. msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [user.email]) msg.attach_alternative(html_content, "text/html") msg.send() self.stdout.write(">>> sent update to %(username)s at %(email)s is_active=%(is_active)s" % user.__dict__) profile.last_email_update = now profile.save()
def update_user(self, user): if not user.is_active: ''' inactive users get nothing! ''' return profile = user.profile last_sent = profile.last_email_update try: freq = self.diffs[user.profile.email_notification] except KeyError: return if self.start-last_sent < freq: ''' don't exceed the frequency! ''' return context = { 'ROOT_URL': get_root_url(), 'last_sent': last_sent, 'is_active': user.is_active, 'user': user, } if profile.is_candidate: ''' handle andidates ''' local_qs = Question.objects.filter(is_deleted=False, entity=profile.locality).\ exclude(answers__author=user) context['new_questions'] = local_qs.filter(created_at__gte=last_sent).order_by('updated_at') context['old_questions'] = local_qs.filter(created_at__lt=last_sent).order_by('-rating') html_content = render_to_string("email/candidate_update.html", context) else: ''' handle voters ''' # TODO: Develop a template for new upvotes # context['new_upvotes'] = QuestionUpvote.objects.filter(created_at__gte=last_sent, question__author=user).\ # order_by('question') context['new_answers'] = Answer.objects.filter(created_at__gte=last_sent, question__in = [x.actor for x in \ Follow.objects.filter(user=user, content_type=self.q_ct)]).\ order_by('question') context['new_questions'] = Question.objects.exclude(author=user).\ filter(entity=user.profile.locality, created_at__gte=last_sent).order_by('author') html_content = render_to_string("email/voter_update.html", context) subject = FlatBlock.objects.get(slug="candidate_update_email.subject").content # TODO: create a link for the update and send it to shaib text_content = 'Sorry, we only support html based email' # create the email, and attach the HTML version as well. try: msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [user.email]) msg.attach_alternative(html_content, "text/html") msg.send() except: ''' probably a bad email ''' return self.stdout.write(">>> sent update to %(username)s at %(email)s is_active=%(is_active)s" % user.__dict__) if not settings.DEBUG: profile.last_email_update = self.start profile.save()
def update_user(self, user): profile = user.profile last_sent = profile.last_email_update try: freq = self.diffs[user.profile.email_notification] except KeyError: return if self.start-last_sent < freq: ''' don't exceed the frequency! ''' return context = { 'ROOT_URL': get_root_url(), 'last_sent': last_sent, 'is_active': user.is_active, 'user': user, } if not user.is_active: ''' send an invitation email to inactive users''' return reg_profile = user.registrationprofile_set.all()[0] key = reg_profile.activation_key context['key'] = key context['activation_url'] = root_url + reverse('accept-invitation', args=(key,)) if reg_profile.activation_key_expired() and last_sent==NEVER_SENT: # reset the key duration, giving the user more time user.date_joined = self.start user.save() if profile.is_candidate: ''' handle andidates ''' local_qs = Question.objects.filter(is_deleted=False, entity=profile.locality).\ exclude(answers__author=user) context['new_questions'] = local_qs.filter(created_at__gte=last_sent).order_by('updated_at') context['old_questions'] = local_qs.filter(created_at__lt=last_sent).order_by('-rating') html_content = render_to_string("qa/candidate_email_update.html", context) else: ''' handle voters ''' return subject = FlatBlock.objects.get(slug="candidate_update_email.subject").content # TODO: create a link for the update and send it to shaib text_content = 'Sorry, we only support html based email' # create the email, and attach the HTML version as well. msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [user.email]) msg.attach_alternative(html_content, "text/html") msg.send() self.stdout.write(">>> sent update to %(username)s at %(email)s is_active=%(is_active)s" % user.__dict__) if not settings.DEBUG: profile.last_email_update = self.start profile.save()
def new_flag(sender, created, instance, **kwargs): if created: editors = User.objects.filter(profile__locality=instance.question.entity, profile__is_editor=True).values_list('email', flat=True) html_content = render_to_string("user/emails/editors_question_flagged.html", {'question': instance.question, 'reoprter': instance.reporter, 'ROOT_URL': get_root_url(), }) text_content = 'Sorry, we only support html based email' msg = EmailMultiAlternatives(_("A question has been flagged"), text_content, settings.DEFAULT_FROM_EMAIL, editors) msg.attach_alternative(html_content, "text/html") msg.send()
def new_candidate(sender, instance, **kwargs): if instance.is_candidate and instance.verification == '0': editors = User.objects.filter(profile__locality=instance.locality, profile__is_editor=True).values_list('email', flat=True) html_content = render_to_string("user/emails/editors_new_candidate.html", {'candidate': instance, 'ROOT_URL': get_root_url(), }) text_content = 'Sorry, we only support html based email' msg = EmailMultiAlternatives(_("A new candidate registered"), text_content, settings.DEFAULT_FROM_EMAIL, editors) msg.attach_alternative(html_content, "text/html") msg.send() instance.verification='S'
def new_candidate(sender, instance, **kwargs): if instance.is_candidate==True and instance.verification=='0': editors = User.objects.filter(profile__locality=instance.locality, profile__is_editor=True).values_list('email', flat=True) html_content = render_to_string("user/emails/editors_new_candidate.html", {'candidate': instance, 'ROOT_URL': get_root_url(), }) text_content = 'Sorry, we only support html based email' msg = EmailMultiAlternatives(_("A new candidate registered"), text_content, settings.DEFAULT_FROM_EMAIL, editors) msg.attach_alternative(html_content, "text/html") msg.send() instance.verification='S'
@task() def publish_answer(answer): logger.info("publishing answer %s" % unicode(answer)) question = answer.question # publish to facebook graph = get_graph_api(answer.author) if graph: answer_url = get_full_url(answer.get_absolute_url()) try: graph.post(path="me/localshot:answer", question=answer_url) except Exception, exc: logger.warn("-- Failed to publish answer to facebook") # send an email to interesed users editors = User.objects.filter(profile__locality=question.entity, profile__is_editor=True).values_list('email', flat=True) content_type = ContentType.objects.get_for_model(question) followers = Follow.objects.filter(content_type=content_type, object_id=question.id).values_list('user__email', flat=True) html_content = render_to_string("email/new_answer.html", {'answer': answer, 'ROOT_URL': get_root_url(), }) text_content = 'Sorry, we only support html based email' msg = EmailMultiAlternatives(_("A new answer for your question"), text_content, settings.DEFAULT_FROM_EMAIL, bcc=list(editors)+list(followers)) msg.attach_alternative(html_content, "text/html") msg.send()
def update_user(self, user): if not user.is_active or not user.email: ''' inactive users get nothing! ''' return profile = user.profile last_sent = profile.last_email_update try: freq = self.diffs[user.profile.email_notification] except KeyError: return if self.start - last_sent < freq: ''' don't exceed the frequency! ''' return context = { 'ROOT_URL': get_root_url(), 'last_sent': last_sent, 'is_active': user.is_active, 'user': user, } if profile.is_candidate: ''' handle andidates ''' local_qs = Question.objects.filter(is_deleted=False, entity=profile.locality).\ exclude(answers__author=user) context['new_questions'] = local_qs.filter( created_at__gte=last_sent).order_by('updated_at') context['old_questions'] = local_qs.filter( created_at__lt=last_sent).order_by('-rating') html_content = render_to_string("email/candidate_update.html", context) else: ''' handle voters ''' # TODO: Develop a template for new upvotes # context['new_upvotes'] = QuestionUpvote.objects.filter(created_at__gte=last_sent, question__author=user).\ # order_by('question') context['new_answers'] = Answer.objects.filter(created_at__gte=last_sent, is_deleted=False, question__in = [x.actor for x in \ Follow.objects.filter(user=user, content_type=self.q_ct)]).\ order_by('question') context['new_questions'] = Question.objects.exclude(author=user).\ filter(entity=user.profile.locality, is_deleted=False, created_at__gte=last_sent).order_by('author') html_content = render_to_string("email/voter_update.html", context) subject = FlatBlock.objects.get( slug="candidate_update_email.subject").content # TODO: create a link for the update and send it to shaib text_content = 'Sorry, we only support html based email' # create the email, and attach the HTML version as well. try: msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [user.email]) msg.attach_alternative(html_content, "text/html") msg.send() except: ''' probably a bad email ''' return self.stdout.write( ">>> sent update to %(username)s at %(email)s is_active=%(is_active)s" % user.__dict__) if not settings.DEBUG: profile.last_email_update = self.start profile.save()
def handle(self, *args, **options): translation.activate(settings.LANGUAGE_CODE) now = datetime.now() self.stdout.write("> sending updates at %s" % now) if len(args): qs = Profile.on_site.filter(user__email=args) else: qs = Profile.on_site.all() # TODO: get only new questions and questions with new answers context = { 'questions': Question.on_site.all().order_by('-updated_at'), 'header': "Hello there!", 'ROOT_URL': get_root_url(), } for profile in qs: user = profile.user last_sent = profile.last_email_update try: freq = self.diffs[user.profile.email_notification] except KeyError: continue if not user.is_active: ''' send an invitation email ''' reg_profile = user.registrationprofile_set.all()[0] if reg_profile.activation_key_expired( ) and last_sent == NEVER_SENT: key = reg_profile.activation_key context['key'] = key context['activation_url'] = root_url + reverse( 'accept-invitation', args=(key, )) # reset the key duration, giving the user more time user.date_joined = now user.save() else: continue elif now - last_sent < freq: # don't exceed the frequency! continue context['is_active'] = user.is_active context['last_sent'] = last_sent context['header'] = "email.update_header" context['footer'] = 'email.footer' html_content = render_to_string("qa/email_update.html", context) ''' send the email only when there's fresh content ''' fresh_content = self.fresh_content_re.search(html_content) if not fresh_content: self.stdout.write( "--- nothing fresh for %(username)s at %(email)s is_active=%(is_active)s" % user.__dict__) continue subject = '%s | %s' % ( site.name, FlatBlock.objects.get( slug="email.update_header").header.rstrip()) # TODO: create a link for the update and send it to shaib text_content = 'Sorry, we only support html based email' # create the email, and attach the HTML version as well. msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [user.email]) msg.attach_alternative(html_content, "text/html") msg.send() self.stdout.write( ">>> sent update to %(username)s at %(email)s is_active=%(is_active)s" % user.__dict__) profile.last_email_update = now profile.save()
logger.info("publishing answer %s" % unicode(answer)) question = answer.question # publish to facebook graph = get_graph_api(answer.author) if graph: answer_url = get_full_url(answer.get_absolute_url()) try: graph.post(path="me/localshot:answer", question=answer_url) except Exception, exc: logger.warn("-- Failed to publish answer to facebook") if send_email: # send an email to interesed users editors = User.objects.filter(profile__locality=question.entity, profile__is_editor=True).values_list( 'email', flat=True) content_type = ContentType.objects.get_for_model(question) followers = Follow.objects.filter(content_type=content_type, object_id=question.id).values_list( 'user__email', flat=True) html_content = render_to_string("email/new_answer.html", { 'answer': answer, 'ROOT_URL': get_root_url(), }) text_content = 'Sorry, we only support html based email' msg = EmailMultiAlternatives(_("A new answer for your question"), text_content, settings.DEFAULT_FROM_EMAIL, bcc=list(editors) + list(followers)) msg.attach_alternative(html_content, "text/html") msg.send()