def worker(self, *args, **kwargs): while True: try: location_post_id = self.work_queue.get_nowait() location_post = LocationPost.objects.get(id=location_post_id) logger.info("Starting water_quality_notification_email_immediate processing for LocationPost id: %d" % location_post.id) except Exception, e: time.sleep(20) continue if location_post.chlorine_level >= Decimal('0.50') and location_post.chlorine_level < Decimal('2.00'): safe_water = True else: safe_water = False try: location_subscriptions = LocationSubscription.objects.select_related().filter( location=location_post.location, email_subscription=LocationSubscription.EMAIL_IMMEDIATE_FREQ, user__is_active=True, ).exclude(user__email='') logger.info("Found %d immediate email subscriptions for %s location" % (location_subscriptions.count(), location_post.location)) for location_subscription in location_subscriptions: # Have we already sent a notification??? try: existing_log = LocationPostNotificationLog.objects.get( user=location_subscription.user, location_post=location_post, notification_type=LocationPostNotificationLog.EMAIL_IMMEDIATE, ) logger.info("Already sent immediate email notification to %s for LocationPost id %d" % (location_subscription.user, location_post.id)) continue except: pass profile = location_subscription.user.get_profile() language = profile.language translation.activate(language) email_subject = render_to_string('water_quality_notification/email/immediate-subject.txt', { 'location': location_post.location, 'language': language }) email_body = render_to_string('water_quality_notification/email/immediate.txt', { 'location': location_post.location, 'location_post': location_post, 'safe_water': safe_water, 'language': language, 'current_site': self.current_site}) email_result = send_mail( smart_str(email_subject), smart_str(email_body), settings.SERVER_EMAIL, [location_subscription.user.email], ) if email_result: location_post_notification_log = LocationPostNotificationLog( user=location_subscription.user, location_post=location_post, notification_type=LocationPostNotificationLog.EMAIL_IMMEDIATE, ) location_post_notification_log.save() logger.info("Successfully sent immediate email notification for LocationPost id %d to %s" % (location_post.id, location_subscription.user)) translation.deactivate() location_post.water_quality_notification_email_immediate_status = LocationPost.COMPLETE_STATUS location_post.save() except Exception, e: logger.error("Error processing water_quality_notification_email_immediate for LocationPost id %d: %s" % (location_post.id, e))
def worker(self, *args, **kwargs): while True: try: location_post_id = self.work_queue.get_nowait() location_post = LocationPost.objects.get(id=location_post_id) logger.info("Starting water_quality_notification_mobile processing for LocationPost id: %d" % location_post.id) except Exception, e: time.sleep(20) continue if location_post.chlorine_level >= Decimal('0.50') and location_post.chlorine_level < Decimal('2.00'): safe_water = True else: safe_water = False try: location_subscriptions = LocationSubscription.objects.select_related().filter( location=location_post.location, phone_subscription=True, user__is_active=True, user__profile__id__isnull=False, ).exclude(user__profile__mobile='') logger.info("Found %d mobile subscriptions for %s location" % (location_subscriptions.count(), location_post.location)) for location_subscription in location_subscriptions: # Have we already sent a notification??? try: existing_log = LocationPostNotificationLog.objects.get( user=location_subscription.user, location_post=location_post, notification_type=LocationPostNotificationLog.MOBILE, ) logger.info("Already sent mobile notification to %s for LocationPost id %d" % (location_subscription.user, location_post.id)) continue except: pass profile = location_subscription.user.get_profile() language = profile.language translation.activate(language) reply_message = render_to_string('water_quality_notification/sms/mobile.txt', { 'location': location_post.location, 'location_post': location_post, 'safe_water': safe_water, 'language': language}) sms_result = send_message(smart_str(reply_message), [profile.mobile]) if sms_result: location_post_notification_log = LocationPostNotificationLog( user=location_subscription.user, location_post=location_post, notification_type=LocationPostNotificationLog.MOBILE, ) location_post_notification_log.save() logger.info("Successfully sent mobile notification for LocationPost id %d to %s" % (location_post.id, location_subscription.user)) translation.deactivate() location_post.water_quality_notification_mobile_status = LocationPost.COMPLETE_STATUS location_post.save() except Exception, e: logger.error("Error processing water_quality_notification_mobile for LocationPost id %d: %s" % (location_post.id, e))