Created
September 8, 2011 17:42
-
-
Save octaflop/1204054 to your computer and use it in GitHub Desktop.
A set of django management commands to find expired Members and send them a notification.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from memdir.models import Member | |
import datetime | |
from pprint import pprint | |
class Command(BaseCommand): | |
args = '<None>' | |
help = """Finds all the Members in the database with an expired entry and | |
tags them as expired. This command should be run right before the | |
send_notices command. | |
""" | |
def handle(self, *args, **options): | |
now = datetime.datetime.now() | |
expired_members = Member.objects.filter(renewal__lte=now) | |
for member in expired_members: | |
member.is_active=False | |
pprint("Agency %s is expired" % member.agency) | |
try: | |
member.save() | |
except: | |
pprint("ERROR: Agency %s could not be saved" % member.agency) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.management.base import BaseCommand, CommandError | |
from memdir.models import MemberUser, Member | |
import datetime | |
import notification | |
from pprint import pprint | |
def notify_member(member, notif): | |
context = { | |
'member': member, | |
} | |
for user in member.users.all(): | |
user = user.user | |
notification.send([user], notif, context) | |
pprint("User %s was notified with notification %s for agency %s" % | |
(user.username, notif, member.agency)) | |
member.rec_expiry_notice=True | |
member.save() | |
class Command(BaseCommand): | |
args = '<None>' | |
help = """Sends a notification via web and email interfaces about: | |
* expired members, | |
* expiries happening within a month, | |
""" | |
def handle(self, *args, **options): | |
next_month = datetime.datetime.now() + datetime.timedelta(365/12) | |
upcoming_members =\ | |
Member.objects.filter(is_subscribed=True).filter(is_active=True).filter(rec_upcoming_notice=False).filter(rec_expiry_notice=False).filter(next_expiry__lte=next_month) | |
expired_members =\ | |
Member.objects.filter(is_subscribed=True).filter(is_active=False).filter(rec_expiry_notice=False) | |
for member in expired_members: | |
notify_member(member, "member_expired") | |
for member in upcoming_members: | |
notify_member(member, "upcoming_expired") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment