Skip to content

Commit

Permalink
Last email fix
Browse files Browse the repository at this point in the history
  • Loading branch information
asennoussi committed Oct 17, 2023
1 parent f7585e8 commit 5e77bf5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
5 changes: 1 addition & 4 deletions contacts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ class InviteContactsRedirectView(RedirectView):

def get(self, request):
try:
linked_accounts = LinkedAccounts.objects.filter(
owner=request.user, active=True)
for la in linked_accounts:
send_invite_emails(la.associated_email)
send_invite_emails(request.user)
# Here, pull the contacts, then create chunks of emails to be scheduled.
messages.success(
request, 'Invitations sent to your contact list.')
Expand Down
47 changes: 26 additions & 21 deletions emailguru/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


import django_rq
from accounts.models import LinkedAccounts
from accounts.models import CustomUser, LinkedAccounts
from contacts.models import Contact
from dashboard.forms import PaymentButtonForm
from dashboard.models import EmailDebugInfo, FilteredEmails, Jobs
Expand Down Expand Up @@ -307,30 +307,35 @@ def update_contacts(associated_email):
return


def send_invite_emails(associaed_email):
def send_invite_emails(user):
queue = get_queue('default')
queue.enqueue(schedule_chunk_emails, associaed_email)


def schedule_chunk_emails(associated_email, chunk_size=100):
la = LinkedAccounts.objects.get(associated_email=associated_email)
credentials_dict = signer.unsign_object(la.credentials)
credentials = google.oauth2.credentials.Credentials(
credentials_dict["token"],
refresh_token=credentials_dict["refresh_token"],
token_uri=credentials_dict["token_uri"],
client_id=credentials_dict["client_id"],
client_secret=credentials_dict["client_secret"],
scopes=credentials_dict["scopes"])

contacts = get_contacts(credentials, False)
other_contacts = get_other_contacts(credentials, False)
all_contacts = contacts + other_contacts
queue.enqueue(schedule_chunk_emails, user)


def schedule_chunk_emails(user, chunk_size=100):
linked_accounts = LinkedAccounts.objects.filter(
owner=user, active=True)
registered_emails = set(CustomUser.objects.values_list('email', flat=True))
all_contacts = set() # Initialize as an empty set
for la in linked_accounts:
credentials_dict = signer.unsign_object(la.credentials)
credentials = google.oauth2.credentials.Credentials(
credentials_dict["token"],
refresh_token=credentials_dict["refresh_token"],
token_uri=credentials_dict["token_uri"],
client_id=credentials_dict["client_id"],
client_secret=credentials_dict["client_secret"],
scopes=credentials_dict["scopes"])

contacts = get_contacts(credentials, False)
other_contacts = get_other_contacts(credentials, False)
# Use set's update method to add multiple elements
all_contacts.update(contacts + other_contacts)

all_contacts = all_contacts - registered_emails
gmail_contacts = [
email for email in all_contacts if email.endswith('@gmail.com')]

gmail_contacts = ['[email protected]', '[email protected]']

queue = get_queue('default')

for i in range(0, len(gmail_contacts), chunk_size):
Expand Down

0 comments on commit 5e77bf5

Please sign in to comment.