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()
Ejemplo n.º 2
0
        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()
Ejemplo n.º 3
0
    def save(self, *args, **kwargs):
        data = self.cleaned_data
        subject = ''
        subj = data.get('subject', '')
        inc_last_name = data.get('personalize_subject_last_name')
        inc_first_name = data.get('personalize_subject_first_name')

        if inc_first_name and not inc_last_name:
            subject = '[firstname] ' + subj
        elif inc_last_name and not inc_first_name:
            subject = '[lastname] ' + subj
        elif inc_first_name and inc_last_name:
            subject = '[firstname] [lastname] ' + subj
        else:
            subject = subj
        nl = super(OldGenerateForm, self).save(*args, **kwargs)
        nl.subject = subject
        nl.actionname = subject
        nl.date_created = datetime.datetime.now()
        nl.send_status = 'draft'
        if nl.default_template:
            template = render_to_string(nl.default_template,
                                        context_instance=RequestContext(
                                            self.request))
            email_content = nl.generate_newsletter(self.request, template)

            email = Email()
            email.subject = subject
            email.body = email_content
            email.sender = self.request.user.email
            email.sender_display = self.request.user.profile.get_name()
            email.reply_to = self.request.user.email
            email.creator = self.request.user
            email.creator_username = self.request.user.username
            email.owner = self.request.user
            email.owner_username = self.request.user.username
            email.save()
            nl.email = email

        nl.save()

        return nl
Ejemplo n.º 4
0
    def save(self, *args, **kwargs):
        data = self.cleaned_data
        subject = ''
        subj = data.get('subject', '')
        inc_last_name = data.get('personalize_subject_last_name')
        inc_first_name = data.get('personalize_subject_first_name')

        if inc_first_name and not inc_last_name:
            subject = '[firstname] ' + subj
        elif inc_last_name and not inc_first_name:
            subject = '[lastname] ' + subj
        elif inc_first_name and inc_last_name:
            subject = '[firstname] [lastname] ' + subj
        else:
            subject = subj
        nl = super(OldGenerateForm, self).save(*args, **kwargs)
        nl.subject = subject
        nl.actionname = subject
        nl.date_created = datetime.datetime.now()
        nl.send_status = 'draft'
        if nl.default_template:
            template = render_to_string(nl.default_template, context_instance=RequestContext(self.request))
            email_content = nl.generate_newsletter(self.request, template)

            email = Email()
            email.subject = subject
            email.body = email_content
            email.sender = self.request.user.email
            email.sender_display = self.request.user.profile.get_name()
            email.reply_to = self.request.user.email
            email.creator = self.request.user
            email.creator_username = self.request.user.username
            email.owner = self.request.user
            email.owner_username = self.request.user.username
            email.save()
            nl.email = email

        nl.save()

        return nl
Ejemplo n.º 5
0
def message(request, group_slug, template_name='user_groups/message.html'):
    """
    Send a message to the group
    """
    from tendenci.core.emails.models import Email

    group = get_object_or_404(Group, slug=group_slug)
    EventLog.objects.log(instance=group)

    members = GroupMembership.objects.filter(
        group=group,
        status=True,
        status_detail='active')

    num_members = members.count()

    form = MessageForm(request.POST or None,
        request=request,
        num_members=num_members)


    if request.method == 'POST' and form.is_valid():

        email = Email()
        email.sender_display = request.user.get_full_name()
        email.sender = get_setting('site', 'global', 'siteemailnoreplyaddress')
        email.reply_to = email.sender
        email.content_type = 'text/html'
        email.subject = form.cleaned_data['subject']
        email.body = form.cleaned_data['body']
        email.save(request.user)

        # send email to myself (testing email)
        if form.cleaned_data['is_test']:
            email.recipient = request.user.email
            email.send()

            messages.add_message(
                request,
                messages.SUCCESS,
                'Successfully sent test email to yourself')

            EventLog.objects.log(instance=email)

        else:
            # send email to members
            for member in members:
                email.recipient = member.member.email
                email.send()

            messages.add_message(
                request,
                messages.SUCCESS,
                'Successfully sent email to all %s members in this group' % num_members)

            EventLog.objects.log(instance=email)
    else:
        print 'form errors', form.errors.items()


    return render(request, template_name, {
        'group': group,
        'num_members': num_members,
        'form': form})
    def handle(self, *args, **options):
        verbosity = 1
        if 'verbosity' in options:
            verbosity = options['verbosity']

        from django.conf import settings
        from tendenci.addons.memberships.models import (Notice,
                                                        MembershipDefault,
                                                        NoticeLog,
                                                        NoticeDefaultLogRecord)
        from tendenci.core.base.utils import fieldify
        from tendenci.core.emails.models import Email
        from tendenci.core.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)

            if notice.notice_type == 'disapprove':
                status_detail_list = ['disapproved']
            else:
                status_detail_list = ['active', 'expired']
            memberships = MembershipDefault.objects.filter(
                                    status=True,
                                    status_detail__in=status_detail_list
                                    )
            if notice.notice_type == 'join':
                memberships = memberships.filter(
                                    join_dt__year=start_dt.year,
                                    join_dt__month=start_dt.month,
                                    join_dt__day=start_dt.day,
                                    renewal=False)
            elif notice.notice_type == 'renewal':
                memberships = memberships.filter(
                                    renew_dt__year=start_dt.year,
                                    renew_dt__month=start_dt.month,
                                    renew_dt__day=start_dt.day,
                                    renewal=True)
            elif notice.notice_type == 'approve':
                memberships = memberships.filter(
                                    application_approved_denied_dt__year=start_dt.year,
                                    application_approved_denied_dt__month=start_dt.month,
                                    application_approved_denied_dt__day=start_dt.day,
                                    application_approved=True)
            elif notice.notice_type == 'disapprove':
                memberships = memberships.filter(
                                    application_approved_denied_dt__year=start_dt.year,
                                    application_approved_denied_dt__month=start_dt.month,
                                    application_approved_denied_dt__day=start_dt.day,
                                    application_approved=False)
            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)

            memberships_count = memberships.count()

            if memberships_count > 0:
                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'))

                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 = ''

                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 = NoticeDefaultLogRecord(
                                                notice_log=notice_log,
                                                membership=membership)
                        notice_log_record.save()
                    except:
                        # catch the exception and email
                        notice.err += traceback.format_exc()
                        print 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.get_field_items()
            context['membership'] = membership
            context.update(global_context)

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

            body = body.replace("[membershiptypeid]",
                                str(membership.membership_type.id))
            body = body.replace("[membershiplink]",
                                '%s' % memberships_page)

            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': memberships_page,
                            '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
            subject = notice.subject.replace('(name)',
                                        user.get_full_name())
            template = Template(subject)
            subject = template.render(context)

            email.subject = subject
            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=True, 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."
Ejemplo n.º 7
0
def message(request, group_slug, template_name='user_groups/message.html'):
    """
    Send a message to the group
    """
    from tendenci.core.emails.models import Email

    group = get_object_or_404(Group, slug=group_slug)
    EventLog.objects.log(instance=group)

    members = GroupMembership.objects.filter(group=group,
                                             status=True,
                                             status_detail='active')

    num_members = members.count()

    form = MessageForm(request.POST or None,
                       request=request,
                       num_members=num_members)

    if request.method == 'POST' and form.is_valid():

        email = Email()
        email.sender_display = request.user.get_full_name()
        email.sender = get_setting('site', 'global', 'siteemailnoreplyaddress')
        email.reply_to = email.sender
        email.content_type = 'text/html'
        email.subject = form.cleaned_data['subject']
        email.body = form.cleaned_data['body']
        email.save(request.user)

        # send email to myself (testing email)
        if form.cleaned_data['is_test']:
            email.recipient = request.user.email
            email.send()

            messages.add_message(request, messages.SUCCESS,
                                 _('Successfully sent test email to yourself'))

            EventLog.objects.log(instance=email)

        else:
            # send email to members
            for member in members:
                email.recipient = member.member.email
                email.send()

            messages.add_message(
                request, messages.SUCCESS,
                _('Successfully sent email to all %(num)s members in this group'
                  % {'num': num_members}))

            EventLog.objects.log(instance=email)

        return redirect('group.detail', group_slug=group_slug)

    else:
        print 'form errors', form.errors.items()

    return render(request, template_name, {
        'group': group,
        'num_members': num_members,
        'form': form
    })
Ejemplo n.º 8
0
    def handle(self, *args, **options):
        verbosity = 1
        if 'verbosity' in options:
            verbosity = options['verbosity']
        # first test if we have notices set up
        from tendenci.addons.corporate_memberships.models import Notice
        if not Notice.objects.filter(status=True,
                                     status_detail='active'
                                    ).exclude(
                                    notice_time='attimeof'
                                    ).exists():
            if verbosity > 1:
                print('No notices set up...existing...')
            # no active notices to process. stop here
            return

        from tendenci.addons.corporate_memberships.models import (
            CorpMembership,
            NoticeLog,
            NoticeLogRecord)
        from tendenci.core.base.utils import fieldify
        from tendenci.core.emails.models import Email
        from tendenci.core.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')

        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 = "corporate_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 Corporate 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 Corporate 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', 'corporate_memberships',
                                       'corporatemembershiprecipients').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)

            if notice.notice_type == 'disapprove':
                status_detail_list = ['inactive']
            else:
                status_detail_list = ['active', 'expired']

            memberships = CorpMembership.objects.filter(
                                    status=True,
                                    status_detail__in=status_detail_list
                                    )
            if notice.notice_type in ['approve_join', 'disapprove_join'
                                      'approve_renewal', 'disapprove_renewal']:
                filters = {'approved_denied_dt__year': start_dt.year,
                           'approved_denied_dt__month': start_dt.month,
                           'approved_denied_dt__day': start_dt.day,
                           'renewal': False,
                           'approved': True
                           }
                if notice.notice_type in ['approve_renewal',
                                          'disapprove_renewal']:
                    filters.update({'renewal': True})
                if notice.notice_type in ['disapprove_join',
                                          'disapprove_renewal']:
                    filters.update({'approved': False})

                memberships = memberships.filter(**filters)
            else:  # 'expire'
                memberships = memberships.filter(
                    expiration_dt__year=start_dt.year,
                    expiration_dt__month=start_dt.month,
                    expiration_dt__day=start_dt.day)

            # filter by membership type
            if notice.corporate_membership_type:
                memberships = memberships.filter(
                                corporate_membership_type=notice.corporate_membership_type)

            memberships_count = memberships.count()

            if memberships_count > 0:
                email.content_type = notice.content_type

                global_context = {'site_display_name': site_display_name,
                                  'site_contact_name': site_contact_name,
                                  'site_contact_email': site_contact_email,
                                  'time_submitted': nowstr,
                                  }

                # log notice sent
                notice_log = NoticeLog(notice=notice,
                                       num_sent=0)
                notice_log.save()
                notice.log = notice_log
                notice.err = ''

                for membership in memberships:
                    try:
                        num_sent += email_member(notice, membership, global_context)
                        if memberships_count <= 50:
                            notice.members_sent.append(membership)

                        # log record
                        notice_log_record = NoticeLogRecord(
                            notice_log=notice_log,
                            corp_membership=membership)
                        notice_log_record.save()
                    except:
                        # catch the exception and email
                        notice.err += traceback.format_exc()
                        print 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):
            corp_profile = membership.corp_profile

            representatives = corp_profile.reps.filter(Q(is_dues_rep=True)|(Q(is_member_rep=True)))
            sent = 0

            for recipient in representatives:
                body = notice.email_content
                context = membership.get_field_items()
                context['membership'] = membership
                context.update(global_context)

                context.update({
                    'rep_first_name': recipient.user.first_name,
                })

                if membership.expiration_dt:
                    body = body.replace("[expirationdatetime]",
                                        time.strftime(
                                          "%d-%b-%y %I:%M %p",
                                          membership.expiration_dt.timetuple()))
                else:
                    body = body.replace("[expirationdatetime]", '')

                context.update({
                    'corporatemembershiptypeid': str(membership.corporate_membership_type.id),
                    'corporatemembershiptype': membership.corporate_membership_type.name,
                    'view_link': "%s%s" % (site_url, membership.get_absolute_url()),
                    'renew_link': "%s%s" % (site_url, membership.get_renewal_url()),
                    'renewed_individuals_list': render_to_string(('notification/corp_memb_notice_email/renew_list.html'),
                                                             {'corp_membership': membership, }),
                })

                body = fieldify(body)

                body = '%s <br /><br />%s' % (body, get_footer())

                context = Context(context)
                template = Template(body)
                body = template.render(context)

                email.recipient = recipient.user.email
                subject = notice.subject.replace('(name)',
                                            corp_profile.name)
                template = Template(subject)
                subject = template.render(context)

                email.subject = subject
                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()
                sent += 1
                if verbosity > 1:
                    print 'To ', email.recipient, email.subject
            return sent

        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=True, 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."
Ejemplo n.º 9
0
    def handle(self, *args, **options):
        verbosity = 1
        if 'verbosity' in options:
            verbosity = options['verbosity']
            
        from django.conf import settings
        from tendenci.addons.memberships.models import Notice, Membership, NoticeLog, NoticeLogRecord
        from tendenci.core.base.utils import fieldify
        from tendenci.core.emails.models import Email
        from tendenci.apps.profiles.models import Profile
        from tendenci.core.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 user.profile.is_superuser_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.active()
            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
                        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=True, 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."
    def handle(self, *args, **options):
        verbosity = 1
        if "verbosity" in options:
            verbosity = options["verbosity"]
        # first test if we have notices set up
        from tendenci.addons.corporate_memberships.models import Notice

        if not Notice.objects.filter(status=True, status_detail="active").exclude(notice_time="attimeof").exists():
            if verbosity > 1:
                print ("No notices set up...existing...")
            # no active notices to process. stop here
            return

        from tendenci.addons.corporate_memberships.models import CorpMembership, NoticeLog, NoticeLogRecord
        from tendenci.core.base.utils import fieldify
        from tendenci.core.emails.models import Email
        from tendenci.core.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")

        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 = "corporate_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 Corporate 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 Corporate 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", "corporate_memberships", "corporatemembershiprecipients").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 = CorpMembership.objects.filter(status=True, status_detail__in=["active", "expired"])
            if notice.notice_type in ["approve_join", "disapprove_join"]:
                memberships = memberships.filter(
                    approved_denied_dt__year=start_dt.year,
                    approved_denied_dt__month=start_dt.month,
                    approved_denied_dt__day=start_dt.day,
                    renewal=False,
                )
            elif notice.notice_type in ["approve_renewal", "disapprove_renewal"]:
                memberships = memberships.filter(
                    approved_denied_dt__year=start_dt.year,
                    approved_denied_dt__month=start_dt.month,
                    approved_denied_dt__day=start_dt.day,
                    renewal=True,
                )
            else:  # 'expire'
                memberships = memberships.filter(
                    expiration_dt__year=start_dt.year,
                    expiration_dt__month=start_dt.month,
                    expiration_dt__day=start_dt.day,
                )

            # filter by membership type
            if notice.corporate_membership_type:
                memberships = memberships.filter(corporate_membership_type=notice.corporate_membership_type)

            memberships_count = memberships.count()

            if memberships_count > 0:
                email.content_type = notice.content_type

                global_context = {
                    "site_display_name": site_display_name,
                    "site_contact_name": site_contact_name,
                    "site_contact_email": site_contact_email,
                    "time_submitted": nowstr,
                }

                # log notice sent
                notice_log = NoticeLog(notice=notice, num_sent=0)
                notice_log.save()
                notice.log = notice_log
                notice.err = ""

                for membership in memberships:
                    try:
                        num_sent += email_member(notice, membership, global_context)
                        if memberships_count <= 50:
                            notice.members_sent.append(membership)

                        # log record
                        notice_log_record = NoticeLogRecord(notice_log=notice_log, corp_membership=membership)
                        notice_log_record.save()
                    except:
                        # catch the exception and email
                        notice.err += traceback.format_exc()
                        print 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):
            corp_profile = membership.corp_profile

            representatives = corp_profile.reps.filter(Q(is_dues_rep=True) | (Q(is_member_rep=True)))
            sent = 0

            for recipient in representatives:
                body = notice.email_content
                context = membership.get_field_items()
                context["membership"] = membership
                context.update(global_context)

                context.update({"rep_first_name": recipient.user.first_name})

                if membership.expiration_dt:
                    body = body.replace(
                        "[expirationdatetime]", time.strftime("%d-%b-%y %I:%M %p", membership.expiration_dt.timetuple())
                    )
                else:
                    body = body.replace("[expirationdatetime]", "")

                context.update(
                    {
                        "corporatemembershiptypeid": str(membership.corporate_membership_type.id),
                        "corporatemembershiptype": membership.corporate_membership_type.name,
                        "view_link": "%s%s" % (site_url, membership.get_absolute_url()),
                        "renew_link": "%s%s" % (site_url, membership.get_renewal_url()),
                        "renewed_individuals_list": render_to_string(
                            ("notification/corp_memb_notice_email/renew_list.html"), {"corp_membership": membership}
                        ),
                    }
                )

                body = fieldify(body)

                body = "%s <br /><br />%s" % (body, get_footer())

                context = Context(context)
                template = Template(body)
                body = template.render(context)

                email.recipient = recipient.user.email
                email.subject = notice.subject.replace("(name)", corp_profile.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()
                sent += 1
                if verbosity > 1:
                    print "To ", email.recipient, email.subject
            return sent

        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=True, 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."