def email_script_errors(err_msg):
     """Send error message to us in case of an error.
     """
     email = Email()
     email.sender = get_setting('site', 'global', 'siteemailnoreplyaddress')
     email.sender_display = get_setting('site', 'global', 'sitedisplayname')
     site_url = get_setting('site', 'global', 'siteurl')
 
     now = datetime.now()
     nowstr = time.strftime("%d-%b-%y %I:%M %p", now.timetuple())
     email.recipient = get_script_support_emails()
     if email.recipient:
         email.body = '%s \n\nTime Submitted: %s\n' % (err_msg, nowstr)
         email.content_type = "text"
         email.subject = 'Error Setting Up Campaign Monitor Account on New Site %s' % site_url
         
         email.send()
Beispiel #2
0
    def save(self, request):
        from django.template.loader import render_to_string
        from django.template import RequestContext
        from emails.models import Email
        from newsletters.utils import newsletter_articles_list, newsletter_news_list, \
                            newsletter_pages_list, newsletter_jobs_list
        from site_settings.utils import get_setting
        
        # converted from function newsletters_generate_processor
        opening_text = render_to_string('newsletters/opening_text.txt', 
                                        context_instance=RequestContext(request))
        simplified = self.cleaned_data['format']
        try:
            simplified = int(simplified)
        except:
            simplified = 0
        
        # articles
        art_content = ""
        if self.cleaned_data['articles']:
            articles_days = self.cleaned_data['articles_days']
            art_content = newsletter_articles_list(request, articles_days, simplified)
            
        # calendar events    
        event_content = ""
        if self.cleaned_data['events']:
            pass
        
        # news
        news_content = ""
        if self.cleaned_data['news']:
            news_days = self.cleaned_data['news_days']
            news_content = newsletter_news_list(request, news_days, simplified)
            
        
        # jobs
        job_content = ""
        if self.cleaned_data['jobs']:
            jobs_days = self.cleaned_data['jobs_days']
            job_content = newsletter_jobs_list(request, jobs_days, simplified)
                
        # pages
        page_content = ""
        if self.cleaned_data['pages']:
            pages_days = self.cleaned_data['pages_days']
            page_content = newsletter_pages_list(request, pages_days, simplified)
        
        # jumplink
        jumplink_content = ""
        if self.cleaned_data['jump_links']:
            jumplink_content = render_to_string('newsletters/jumplinks.txt', locals(), 
                                        context_instance=RequestContext(request))
            
        # login block
        login_content = ""
        if self.cleaned_data['include_login']:
            login_content = render_to_string('newsletters/login.txt',  
                                        context_instance=RequestContext(request))
            
        # rss list
        
        
        email_d = {}
        # store all content in email_d["[content]"]
        # will be used to replace [content] in the template
        email_d["[content]"] = opening_text
        
        # get the newsletter template now
        template = 'newsletters/templates/%s' % (self.cleaned_data['template'])
        email_d['template_path_name'] = template
        
        #check if we have [jumplink] in the email template, if not, 
        #include the jumplinks at the top of the newsletter
        template_content = render_to_string(template)
        if jumplink_content:
            if template_content.find("[jumplinks]") == -1:
                email_d["[content]"] += jumplink_content
                
        email_d["[content]"] += "%s%s%s%s%s%s" % (login_content, event_content, art_content,
                                news_content, job_content, page_content)
        
        
        email_d["[jumplinks]"] = jumplink_content
        email_d["[articles]"] = art_content
        email_d["[calendarevents]"] = event_content
        email_d["[events]"] = event_content
        email_d["[jobs]"] = job_content
        email_d["[contentmanagers]"] = page_content
        email_d["[pages]"] = page_content
        email_d["[releases]"] = news_content
        email_d["[news]"] = news_content
        
        email_d["[sitewebmaster]"] = get_setting('site', "global", "sitewebmaster")
        email_d["[sitedisplayname]"] = get_setting('site', "global", "sitedisplayname")
        
        today = datetime.date.today()
        email_d["[monthsubmitted]"] = today.strftime("%B") # June
        email_d["[yearsubmitted]"] = today.strftime("%Y")  # 2010
        email_d["[unsubscribeurl]"] = "[unsubscribeurl]"    
        
        email_d["[currentweekdayname]"] = today.strftime("%A")    # Wednesday
        email_d["[currentday]"] = today.strftime("%d")                       
        email_d["[currentmonthname]"] = today.strftime("%B")
        
        
        
        email = Email()
        is_valid = email.template_body(email_d)
        email.sender_display = "%s %s" % (request.user.first_name, request.user.last_name)
        email.sender = request.user.email
        email.reply_to = request.user.email
        email.recipient = request.user.email
        #email.send_to_email2 
        email.content_type = 'text/html'
        
        personalize_subject_first_name = self.cleaned_data['personalize_subject_first_name']
        personalize_subject_last_name = self.cleaned_data['personalize_subject_last_name']
        
        email.subject = self.cleaned_data['subject']
        if personalize_subject_first_name and personalize_subject_last_name:
            email.subject = "[firstname] [lastname], " + email.subject
        elif personalize_subject_first_name:
            email.subject = "[firstname], " + email.subject
        elif personalize_subject_last_name:
            email.subject = "[lastname], " + email.subject
        email.status = 1
        email.status_detail = 'active'
        email.category = 'marketing'
        
        email.save(request.user)
        
        # action object - these 3 already included on the form: member_only, group and send_to_emails
        now = datetime.datetime.now()
        self.instance.email = email
        self.instance.name = email.subject
        self.instance.type = 'Distribution E-mail'
        self.instance.name = email.subject
        self.instance.description = '%s Electronic Newsletter: generated %s' % \
                            (get_setting('site', "global", "sitedisplayname"), 
                             now.strftime('%d-%b-%y %I:%M:%S %p'))
        self.instance.category = 'marketing'
        self.instance.due_dt = now
        try:
            entity = (request.user.get_profile()).entity
        except:
            entity = None
        if entity:
            self.instance.entity = entity
        self.instance.status = 1
        self.instance.status_detail = 'open'
        self.instance.save(request.user)
        
        return self.instance

        
        
    def handle(self, *args, **options):
        verbosity = 1
        if 'verbosity' in options:
            verbosity = options['verbosity']
            
        from django.conf import settings
        from memberships.models import Notice, Membership, NoticeLog, NoticeLogRecord
        from base.utils import fieldify
        from emails.models import Email
        from profiles.models import Profile
        from site_settings.utils import get_setting
        
        site_display_name = get_setting('site', 'global', 'sitedisplayname')
        site_contact_name = get_setting('site', 'global', 'sitecontactname')
        site_contact_email = get_setting('site', 'global', 'sitecontactemail')
        site_url = get_setting('site', 'global', 'siteurl')
        
        corp_replace_str = """
                            <br /><br />
                            <font color="#FF0000">
                            Organizational Members, please contact your company Membership coordinator
                            to ensure that your membership is being renewed.
                            </font>
                            """
        
        email = Email()
        email.sender = get_setting('site', 'global', 'siteemailnoreplyaddress')
        email.sender_display = site_display_name
        email.reply_to = site_contact_email
        
        now = datetime.now()
        nowstr = time.strftime("%d-%b-%y %I:%M %p", now.timetuple())
        
        def email_admins_recap(notices, total_sent):
            """Send admins recap after the notices were processed.
            """
            email.recipient = get_admin_emails()
            if email.recipient:
                template_name = "memberships/notices/email_recap.html"
                try:
                    recap_email_content = render_to_string(template_name, {'notices':notices,
                                                                      'total_sent':total_sent,
                                                                      'site_url': site_url,
                                                                      'site_display_name': site_display_name,
                                                                      'site_contact_name': site_contact_name,
                                                                      'site_contact_email': site_contact_email})
                    email.body = recap_email_content
                    email.content_type = "html"
                    email.subject = '%s Membership Notices Distributed' % site_display_name
                    
                    email.send()
                except TemplateDoesNotExist:
                    pass
        
        def email_script_errors(err_msg):
            """Send error message to us if any.
            """
            email.recipient = get_script_support_emails()
            if email.recipient:
                email.body = '%s \n\nTime Submitted: %s\n' % (err_msg, nowstr)
                email.content_type = "text"
                email.subject = 'Error Processing Membership Notices on %s' % site_url
                
                email.send()
        
        def get_script_support_emails():
            admins = getattr(settings, 'ADMINS', None) 
            if admins:
                recipients_list = [admin[1] for admin in admins]
                return ','.join(recipients_list)
            
            return None
        
        def get_admin_emails():
            admin_emails = get_setting('module', 'memberships', 'membershiprecipients').strip()
            if admin_emails:
                admin_emails = admin_emails.split(',')
            if not admin_emails:
                admin_emails = (get_setting('site', 'global', 'admincontactemail').strip()).split(',')
                
            if admin_emails:
                admin_emails = ','.join(admin_emails)
            return admin_emails
            
            
        
        def process_notice(notice):
            notice.members_sent = []
            num_sent = 0
            if notice.notice_time == 'before':
                start_dt = now + timedelta(days=notice.num_days)
            else:
                start_dt = now - timedelta(days=notice.num_days)
            
            memberships = Membership.objects.filter(status=1)
            if notice.notice_type == 'join':
                memberships = memberships.filter(subscribe_dt__year=start_dt.year,
                                                subscribe_dt__month=start_dt.month,
                                                subscribe_dt__day=start_dt.day,
                                                renewal=False)
            elif notice.notice_type == 'renew':
                memberships = memberships.filter(subscribe_dt__year=start_dt.year,
                                                subscribe_dt__month=start_dt.month,
                                                subscribe_dt__day=start_dt.day,
                                                renewal=True)
            else: # 'expire'
                memberships = memberships.filter(expire_dt__year=start_dt.year,
                                                expire_dt__month=start_dt.month,
                                                expire_dt__day=start_dt.day)
                
            # filter by membership type
            if notice.membership_type:
                memberships = memberships.filter(membership_type=notice.membership_type)
                
            if memberships:
                email.content_type = notice.content_type
                
                # password
                passwd_str = """
                            If you've forgotten your password or need to reset the auto-generated one,
                            click <a href="%s%s">here</a> and follow the instructions on the page to 
                            reset your password.
                            """ % (site_url, reverse('auth_password_reset'))
#                notice.email_content = notice.email_content.replace("[password]", passwd_str)
                
                global_context = {'sitedisplayname': site_display_name,
                                  'sitecontactname': site_contact_name,
                                  'sitecontactemail': site_contact_email,
                                  'timesubmitted': nowstr,
                                  'password': passwd_str
                                  }
                
                
                # log notice sent
                notice_log = NoticeLog(notice=notice,
                                       num_sent=0)
                notice_log.save()
                notice.log = notice_log
                notice.err = ''
                
                memberships_count = memberships.count()
                
                for membership in memberships:
                    try:
                        email_member(notice, membership, global_context)
                        if memberships_count <= 50:
                            notice.members_sent.append(membership)
                        num_sent += 1
                        
                        # log record
                        notice_log_record = NoticeLogRecord(notice_log=notice_log,
                                                            membership=membership)
                        notice_log_record.save()
                    except:
                        # catch the exception and email to developers
                        notice.err += traceback.format_exc()
                        
                if num_sent > 0:
                    notice_log.num_sent = num_sent
                    notice_log.save()
                    
            return num_sent    
            
        def email_member(notice, membership, global_context):
            user = membership.user

            body = notice.email_content
            context = membership.entry_items
            context.update(global_context)
            
            body = body.replace("[membershiptypeid]", str(membership.membership_type.id))
            body = body.replace("[membershiplink]", '%s%s' % (site_url, membership.get_absolute_url()))

            # memberships page ------------
            memberships_page = "%s%s%s" % \
                (site_url, reverse('profile', args=[membership.user]), "#userview-memberships")
            body = body.replace("[renewlink]", memberships_page)

            if membership.expire_dt:
                body = body.replace("[expirationdatetime]", 
                                    time.strftime("%d-%b-%y %I:%M %p", membership.expire_dt.timetuple()))
            else:
                body = body.replace("[expirationdatetime]", '')
                
            # corporate member corp_replace_str
            if membership.corporate_membership_id:
                body = body.replace("<!--[corporatemembernotice]-->", corp_replace_str)
            else:
                body = body.replace("<!--[corporatemembernotice]-->", "")
                
                
            context.update({'membershiptypeid': str(membership.membership_type.id),
                            'membershiplink': '%s%s' % (site_url, membership.get_absolute_url()),
                            'renewlink': memberships_page,
                            'membernumber': membership.member_number,
                            'membershiptype': membership.membership_type.name,
                            })
            if membership.expire_dt:
                context['expirationdatetime'] = time.strftime("%d-%b-%y %I:%M %p", 
                                                    membership.expire_dt.timetuple())
                
            # corporate member corp_replace_str
            if membership.corporate_membership_id:
                context['corporatemembernotice'] =  corp_replace_str
           
            body = fieldify(body)
                
            body = '%s <br /><br />%s' % (body, get_footer())
            
            context = Context(context)
            template = Template(body)
            body = template.render(context)
            
            email.recipient = user.email
            email.subject = notice.subject.replace('(name)', user.get_full_name())
            email.body = body
            if notice.sender:
                email.sender = notice.sender
                email.reply_to = notice.sender
            if notice.sender_display:
                email.sender_display = notice.sender_display
                
            email.send()
            if verbosity > 1:
                print 'To ', email.recipient, email.subject
            
                
        def get_footer():
            return """
                    This e-mail was generated by Tendenci&reg; Software - a 
                    web based membership management software solution 
                    www.tendenci.com developed by Schipul - The Web Marketing Company
                    """   
                    
                    
        
        exception_str = ""
        
        notices = Notice.objects.filter(status=1, status_detail='active').exclude(notice_time='attimeof')
        
        if notices:
            if verbosity > 1:
                print "Start sending out notices to members:"
            total_notices = 0
            total_sent = 0
            for notice in notices:
                total_notices += 1
                total_sent += process_notice(notice)
                if hasattr(notice, 'err'):
                    exception_str += notice.err
                
            if total_sent > 0:
                processed_notices = [notice for notice in notices if hasattr(notice, 'log') and notice.log.num_sent>0]
                email_admins_recap(processed_notices, total_sent)
              
            # if there is any error, notify us  
            if exception_str:
                email_script_errors(exception_str)
                
            if verbosity > 1:
                print 'Total notice processed: %d' % (total_notices)
                print 'Total email sent: %d' % (total_sent)
                print "Done"
        else:
            if verbosity > 1:
                print "No notices on the site."