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' or notice.notice_type == 'disapprove_renewal':
                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' or notice.notice_type == 'approve_renewal':
                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' or notice.notice_type == 'disapprove_renewal':
                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,
                    reminder=True)
                if get_setting('module', 'memberships',
                               'renewalreminderexcludecorpmembers'):
                    # exclude corp members
                    memberships = memberships.exclude(
                        corporate_membership_id__gt=0)

            # 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_context.update({'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 = {
                    'site_display_name': site_display_name,
                    'site_contact_name': site_contact_name,
                    'site_contact_email': site_contact_email,
                    'time_submitted': nowstr,
                    '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:
                    if notice.notice_type == 'expiration' and membership.auto_renew and membership.has_rp(
                    ):
                        # skip if auto renew is set up for this membership
                        continue

                    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 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)
                if get_setting('module', 'memberships', 'renewalreminderexcludecorpmembers'):
                    # exclude corp members
                    memberships = memberships.exclude(corporate_membership_id__gt=0)

            # 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_context.update({'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 = {'site_display_name': site_display_name,
                                  'site_contact_name': site_contact_name,
                                  'site_contact_email': site_contact_email,
                                  'time_submitted': nowstr,
                                  '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