예제 #1
0
def sendDailyUpdates(lastLoginInterval=None, lastEmailedInterval=None, testing=False):
    '''Run once a day to send updates.  Pass arguments for testing.'''
    # get all members to loop through
    memberParticipants = Participant.objects.all().filter(type="member")
    print memberParticipants.count()
    for participant in memberParticipants:
        now = timezone.now()
        if lastLoginInterval:
            lastLogin = timezone.now() - timedelta(days=lastLoginInterval)
        else:
            lastLogin = participant.user.last_login
        if lastEmailedInterval:
            lastEmailed = timezone.now() - timedelta(days=lastEmailedInterval)
        else:
            lastEmailed = participant.member.last_emailed
        if abs((now.date() - lastLogin.date()).days) < 1:         # is this rounded integer or float?
            print participant.get_name() + ': lastlogin, lastemailed, action'
            print(str(lastLogin) + ':' + str(lastEmailed) + ':no email-rencently logged in')
            continue
        if lastLogin > lastEmailed:
            sinceDate = lastLogin
        else:
            sinceDate = lastEmailed
        requests = getRequestsOfFriends(participant, sinceDate)
        requestComments = getRelevantRequestComments(participant, sinceDate)
        items = getItemsForParticipant(participant)
        items = items.filter(share_date__gte=sinceDate)
        boolSendEmail = False
        # send if there are any new requests from friends
        if requests.count() > 0:
            boolSendEmail = True
        # send if there are any new request comments and it has been more than 2 days
        if len(requestComments) > 0 and abs((now.date() - sinceDate.date()).days) >= 2:
            boolSendEmail = True
        # send if there are any new items from friends and it has been more than 7 days
        if items.count() > 0 and abs((now.date() - sinceDate.date()).days) >= 7:
            boolSendEmail = True
        if not boolSendEmail:
            print participant.get_name() + ': lastlogin, lastemailed, req, comment, items, action'
            print(str(lastLogin) + ':' 
                  + str(lastEmailed) + ':'
                  + str(requests.count()) + ':'
                  + str(len(requestComments)) + ':'
                  + str(items.count())  + ':'
                  + ':no email-nothing to show')
            continue
        # we're ready to send that email
        email = participant.user.username
        context = {
            'name' : participant.get_name(),
            'requests' : requests,
            'requestComments' : requestComments,
            'items' : items,
        }
        body = render_to_string('email/plain_text/daily_update.txt', context)
        htmlBody = render_to_string('email/html/daily_update.html', context)
        print participant.get_name() + ': lastlogin, lastemailed, req, comment, items, action'
        print(str(lastLogin) + ':' 
              + str(lastEmailed) + ':'
              + str(requests.count()) + ':'
              + str(len(requestComments)) + ':'
              + str(items.count())  + ':'
              + ':emailed')
        if not testing:
            sendMail(
                'Recent activity update', 
                body,
                htmlBody,
                '*****@*****.**', 
                [email], 
            )
            member = participant.member
            member.last_emailed = timezone.now()
            member.save()
        time.sleep(1)
        
        
            
        
        
예제 #2
0
 def handle(self, *args, **options):
     memberParticipants = Participant.objects.all().filter(type="member")
     self.stdout.write('total people: ' + str(memberParticipants.count()))
     if options['testing']:
         self.stdout.write("Just testing")
     for participant in memberParticipants:
         lastLogin = participant.user.last_login
         lastEmailed = participant.member.last_emailed
         cutoffDate = max([lastLogin, lastEmailed])
         data = {
             'friend_invites' : {},
             'friend_confirmations' : {},
             'messages' : {},
             'requests' : {},
             'request_comments' : {},
             'items' : {},
         }
         mem = participant.member
         data['friend_invites']['pref'] = mem.email_friend_requests == mem.EMAIL_DAILY_DIGEST
         data['friend_confirmations']['pref'] = mem.email_friend_requests == mem.EMAIL_DAILY_DIGEST
         data['messages']['pref'] = mem.email_pm == mem.EMAIL_DAILY_DIGEST
         data['requests']['pref'] = mem.email_requests == mem.EMAIL_DAILY_DIGEST
         data['request_comments']['pref'] = mem.email_request_comments == mem.EMAIL_DAILY_DIGEST
         data['items']['pref'] = mem.email_shared_items == mem.EMAIL_DAILY_DIGEST
         # new requests for participant
         if data['requests']['pref']:
             data['requests']['coll'] = getRequestsOfFriends(participant, cutoffDate)
             data['requests']['count'] = data['requests']['coll'].count()
         # new request comments for participant
         if data['request_comments']['pref']:
             data['request_comments']['coll'] = getRelevantRequestComments(participant, cutoffDate)
             data['request_comments']['count'] = len(data['request_comments']['coll'])
         # new shared items for participant
         if data['items']['pref']:
             data['items']['coll'] = getItemsForParticipant(participant).filter(share_date__gte=cutoffDate)
             data['items']['count'] = data['items']['coll'].count()
         # new friend requests for participant
         if data['friend_invites']['pref']:
             data['friend_invites']['coll'] = getFriendInvites(participant, cutoffDate)
             data['friend_invites']['count'] = data['friend_invites']['coll'].count()
         # new friend confirmations for participant
         if data['friend_confirmations']['pref']:
             data['friend_confirmations']['coll'] = getFriendConfirmations(participant, cutoffDate)
             data['friend_confirmations']['count'] = data['friend_confirmations']['coll'].count()
         # new pm's for participant
         if data['messages']['pref']:
             data['messages']['coll'] = Message.objects.filter(recipient=participant)
             data['messages']['coll'] = data['messages']['coll'].filter(sent_on__gte=cutoffDate)
             data['messages']['count'] = data['messages']['coll'].count()
             
         self.stdout.write('----------------------------------------------') 
         self.stdout.write(str(participant.id) + ": " + participant.get_name() + " <" + participant.user.username + ">")
         self.stdout.write('login: '******'pref']:
                 self.stdout.write(key + ": " + str(data[key]['count']))
         
         # determine if there is anything to send
         counts = []
         for key in data:
             if data[key]['pref']:
                 counts.append(data[key]['count'])
                         
         if max(counts) > 0:
             context = {
                 'email_title' : 'Recent Activity on VillageBuilder',
                 'name' : participant.get_name(),
                 'data' : data,
             }
             body = render_to_string('email/plain_text/daily_update.txt', context)
             htmlBody = render_to_string('email/html/daily_update.html', context)
             # self.stdout.write('--------') 
             # self.stdout.write(htmlBody)
             if not options['testing']:
                 email = participant.user.username
                 sendMail(
                     'Recent activity update', 
                     body,
                     htmlBody,
                     '*****@*****.**', 
                     [email], 
                 )
                 member = participant.member
                 member.last_emailed = timezone.now()
                 member.save()
                 time.sleep(1)