From 8a0994e79a61f7bb8b6b80fb45acdcd0fc87245d Mon Sep 17 00:00:00 2001 From: nikhila05 Date: Fri, 20 Sep 2019 12:00:58 +0530 Subject: [PATCH] blocked domain and email filter in sales, elasticsearch related changes (#301) * updated testcases, update code for account view page, modified code for attachments and comments for assigned user * updated testcases in events, invoices, tasks modules * added duplicate contacts tabs, fixed time format in marketing campaign list page, added test cases in marketing, added contacts list filter in contacts page * updated requirements * added email search using elasticsearch, changes for unsubscribing contacts, added blocked emails, domains in settings pages * added elasticsearch in travis file * blocked domain and email filter in sales, elasticsearch related changes --- .gitlab-ci.yml | 2 +- .travis.yml | 2 + accounts/tasks.py | 99 +-- cases/tasks.py | 38 +- common/tasks.py | 35 +- contacts/tasks.py | 38 +- crm/settings.py | 10 +- events/tasks.py | 51 +- invoices/tasks.py | 34 +- leads/tasks.py | 41 +- leads/templates/leads.html | 61 +- leads/views.py | 4 +- marketing/models.py | 4 + marketing/search_indexes.py | 87 ++- marketing/tasks.py | 81 ++- marketing/templates/search/contact_emails.txt | 13 + .../search/failed_contact_emails.txt | 18 + .../templates/search_contact_emails.html | 666 ++++++++++++++++++ .../unsubscribe_from_campaign_template.html | 30 + marketing/tests.py | 39 +- marketing/urls.py | 6 +- marketing/views.py | 70 +- opportunity/tasks.py | 38 +- requirements.txt | 3 +- tasks/celery_tasks.py | 34 +- templates/marketing/lists/detail.html | 2 +- templates/root.html | 22 +- 27 files changed, 1253 insertions(+), 275 deletions(-) create mode 100644 marketing/templates/search/failed_contact_emails.txt create mode 100644 marketing/templates/search_contact_emails.html create mode 100644 marketing/templates/unsubscribe_from_campaign_template.html diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c3ccb780..ca3c2b37 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ Python 3.5.2: - virtualenv -p python3 env - source env/bin/activate - pip install --no-cache-dir -r requirements.txt - - python manage.py test --keepdb --noinput + - python manage.py test --keepdb --parallel --noinput - coverage run --source=accounts,cases,common,contacts,leads,opportunity,tasks,invoices,events,teams,marketing manage.py test --keepdb --noinput - coverage report -m except: diff --git a/.travis.yml b/.travis.yml index df578d08..1b070da0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,8 @@ python: services: - elasticsearch - redis-server + - elasticsearch + #Django & Database addons: diff --git a/accounts/tasks.py b/accounts/tasks.py index a2c5fcbf..f6c9e2d1 100644 --- a/accounts/tasks.py +++ b/accounts/tasks.py @@ -13,39 +13,43 @@ from accounts.models import Account, Email, EmailLog from common.models import User from common.utils import convert_to_custom_timezone from contacts.models import Contact +from marketing.models import BlockedDomain, BlockedEmail @task def send_email(email_obj_id): email_obj = Email.objects.filter(id=email_obj_id).first() + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) if email_obj: from_email = email_obj.from_email contacts = email_obj.recipients.all() for contact_obj in contacts: if not EmailLog.objects.filter(email=email_obj, contact=contact_obj, is_sent=True).exists(): - html = email_obj.message_body - context_data = { - 'email': contact_obj.email if contact_obj.email else '', - 'name': contact_obj.first_name if contact_obj.first_name else '' + ' ' + contact_obj.last_name if contact_obj.last_name else '', - } - try: - html_content = Template(html).render(Context(context_data)) - subject = email_obj.message_subject - msg = EmailMessage( - subject, - html_content, - from_email=from_email, - to=[contact_obj.email, ] - ) - msg.content_subtype = "html" - res = msg.send() - if res: - email_obj.rendered_message_body = html_content - email_obj.save() - EmailLog.objects.create(email=email_obj, contact=contact_obj, is_sent=True) - except Exception as e: - print(e) - pass + if (contact_obj.email not in blocked_emails) and (contact_obj.email.split('@')[-1] not in blocked_domains): + html = email_obj.message_body + context_data = { + 'email': contact_obj.email if contact_obj.email else '', + 'name': contact_obj.first_name if contact_obj.first_name else '' + ' ' + contact_obj.last_name if contact_obj.last_name else '', + } + try: + html_content = Template(html).render(Context(context_data)) + subject = email_obj.message_subject + msg = EmailMessage( + subject, + html_content, + from_email=from_email, + to=[contact_obj.email, ] + ) + msg.content_subtype = "html" + res = msg.send() + if res: + email_obj.rendered_message_body = html_content + email_obj.save() + EmailLog.objects.create(email=email_obj, contact=contact_obj, is_sent=True) + except Exception as e: + print(e) + pass @@ -54,28 +58,33 @@ def send_email_to_assigned_user(recipients, from_email, domain='demo.django-crm. """ Send Mail To Users When they are assigned to a contact """ account = Account.objects.filter(id=from_email).first() created_by = account.created_by + + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) + for user in recipients: recipients_list = [] user = User.objects.filter(id=user, is_active=True).first() if user: - recipients_list.append(user.email) - context = {} - context["url"] = protocol + '://' + domain + \ - reverse('accounts:view_account', args=(account.id,)) - context["user"] = user - context["account"] = account - context["created_by"] = created_by - subject = 'Assigned a account for you.' - html_content = render_to_string( - 'assigned_to/account_assigned.html', context=context) + if (user.email not in blocked_emails) and (user.email.split('@')[-1] not in blocked_domains): + recipients_list.append(user.email) + context = {} + context["url"] = protocol + '://' + domain + \ + reverse('accounts:view_account', args=(account.id,)) + context["user"] = user + context["account"] = account + context["created_by"] = created_by + subject = 'Assigned a account for you.' + html_content = render_to_string( + 'assigned_to/account_assigned.html', context=context) - msg = EmailMessage( - subject, - html_content, - to=recipients_list - ) - msg.content_subtype = "html" - msg.send() + msg = EmailMessage( + subject, + html_content, + to=recipients_list + ) + msg.content_subtype = "html" + msg.send() @task @@ -92,10 +101,16 @@ def send_scheduled_emails(): sent_time = convert_to_custom_timezone( sent_time, each.timezone, to_utc=True) + # if ( + # str(each.scheduled_date_time.date()) == str(sent_time.date()) and + # str(scheduled_date_time.hour) == str(sent_time.hour) and + # (str(scheduled_date_time.minute + 5) < str(sent_time.minute) or + # str(scheduled_date_time.minute - 5) > str(sent_time.minute)) + # ): + # send_email.delay(each.id) if ( str(each.scheduled_date_time.date()) == str(sent_time.date()) and str(scheduled_date_time.hour) == str(sent_time.hour) and - (str(scheduled_date_time.minute + 5) < str(sent_time.minute) or - str(scheduled_date_time.minute - 5) > str(sent_time.minute)) + str(scheduled_date_time.minute) == str(sent_time.minute) ): send_email.delay(each.id) diff --git a/cases/tasks.py b/cases/tasks.py index 8359d007..a0ca46a3 100644 --- a/cases/tasks.py +++ b/cases/tasks.py @@ -7,6 +7,7 @@ from django.template.loader import render_to_string from accounts.models import User from cases.models import Case +from marketing.models import BlockedDomain, BlockedEmail @task @@ -14,25 +15,28 @@ def send_email_to_assigned_user(recipients, case_id, domain='demo.django-crm.io' """ Send Mail To Users When they are assigned to a case """ case = Case.objects.get(id=case_id) created_by = case.created_by + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) for user in recipients: recipients_list = [] user = User.objects.filter(id=user, is_active=True).first() if user: - recipients_list.append(user.email) - context = {} - context["url"] = protocol + '://' + domain + \ - reverse('cases:view_case', args=(case.id,)) - context["user"] = user - context["case"] = case - context["created_by"] = created_by - subject = 'Assigned to case.' - html_content = render_to_string( - 'assigned_to/cases_assigned.html', context=context) + if (user.email not in blocked_emails) and (user.email.split('@')[-1] not in blocked_domains): + recipients_list.append(user.email) + context = {} + context["url"] = protocol + '://' + domain + \ + reverse('cases:view_case', args=(case.id,)) + context["user"] = user + context["case"] = case + context["created_by"] = created_by + subject = 'Assigned to case.' + html_content = render_to_string( + 'assigned_to/cases_assigned.html', context=context) - msg = EmailMessage( - subject, - html_content, - to=recipients_list - ) - msg.content_subtype = "html" - msg.send() + msg = EmailMessage( + subject, + html_content, + to=recipients_list + ) + msg.content_subtype = "html" + msg.send() diff --git a/common/tasks.py b/common/tasks.py index 17037f87..ad2fb31c 100644 --- a/common/tasks.py +++ b/common/tasks.py @@ -5,13 +5,13 @@ from django.contrib.auth.tokens import PasswordResetTokenGenerator from django.core.mail import EmailMessage from django.shortcuts import reverse from django.template.loader import render_to_string -from django.utils import six - -from common.models import Comment, User, Profile -from django.utils import timezone -from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode +from django.utils import six, timezone from django.utils.encoding import force_bytes, force_text +from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode + +from common.models import Comment, Profile, User from common.token_generator import account_activation_token +from marketing.models import BlockedDomain, BlockedEmail @task @@ -104,19 +104,22 @@ def send_email_user_mentions(comment_id, called_from, domain='demo.django-crm.io else: context["url"] = '' # subject = 'Django CRM : comment ' + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) if recipients: for recipient in recipients: - recipients_list = [recipient, ] - context["mentioned_user"] = recipient - html_content = render_to_string('comment_email.html', context=context) - msg = EmailMessage( - subject, - html_content, - from_email=comment.commented_by.email, - to=recipients_list - ) - msg.content_subtype = "html" - msg.send() + if (recipient not in blocked_emails) and (recipient.split('@')[-1] not in blocked_domains): + recipients_list = [recipient, ] + context["mentioned_user"] = recipient + html_content = render_to_string('comment_email.html', context=context) + msg = EmailMessage( + subject, + html_content, + from_email=comment.commented_by.email, + to=recipients_list + ) + msg.content_subtype = "html" + msg.send() @task diff --git a/contacts/tasks.py b/contacts/tasks.py index 9fd12a1e..dd028fd0 100644 --- a/contacts/tasks.py +++ b/contacts/tasks.py @@ -5,32 +5,36 @@ from django.template.loader import render_to_string from common.models import User from contacts.models import Contact +from marketing.models import BlockedDomain, BlockedEmail @task def send_email_to_assigned_user(recipients, contact_id, domain='demo.django-crm.io', protocol='http'): """ Send Mail To Users When they are assigned to a contact """ contact = Contact.objects.get(id=contact_id) + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) created_by = contact.created_by for user in recipients: recipients_list = [] user = User.objects.filter(id=user, is_active=True).first() if user: - recipients_list.append(user.email) - context = {} - context["url"] = protocol + '://' + domain + \ - reverse('contacts:view_contact', args=(contact.id,)) - context["user"] = user - context["contact"] = contact - context["created_by"] = created_by - subject = 'Assigned a contact for you.' - html_content = render_to_string( - 'assigned_to/contact_assigned.html', context=context) + if (user.email not in blocked_emails) and (user.email.split('@')[-1] not in blocked_domains): + recipients_list.append(user.email) + context = {} + context["url"] = protocol + '://' + domain + \ + reverse('contacts:view_contact', args=(contact.id,)) + context["user"] = user + context["contact"] = contact + context["created_by"] = created_by + subject = 'Assigned a contact for you.' + html_content = render_to_string( + 'assigned_to/contact_assigned.html', context=context) - msg = EmailMessage( - subject, - html_content, - to=recipients_list - ) - msg.content_subtype = "html" - msg.send() + msg = EmailMessage( + subject, + html_content, + to=recipients_list + ) + msg.content_subtype = "html" + msg.send() diff --git a/crm/settings.py b/crm/settings.py index 938bbcf9..0959e4f2 100644 --- a/crm/settings.py +++ b/crm/settings.py @@ -240,7 +240,7 @@ CELERY_BEAT_SCHEDULE = { "task": "marketing.tasks.send_scheduled_campaigns", "schedule": crontab(hour='*/1') }, - "runs-scheduled-emails-for-accounts-every-five-minutes": { + "runs-scheduled-emails-for-accounts-every-one-minute": { "task": "accounts.tasks.send_scheduled_emails", "schedule": crontab(minute='*/1') } @@ -352,3 +352,11 @@ if os.path.isfile('crm/local_settings.py'): from .local_settings import * else: print("No local settings file found") + + +# CACHES = { +# 'default': { +# 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', +# 'LOCATION': '127.0.0.1:11211', +# } +# } \ No newline at end of file diff --git a/events/tasks.py b/events/tasks.py index dbc8c2a8..6012d00d 100644 --- a/events/tasks.py +++ b/events/tasks.py @@ -7,6 +7,7 @@ from django.template.loader import render_to_string from common.models import User from contacts.models import Contact from events.models import Event +from marketing.models import BlockedDomain, BlockedEmail @task @@ -21,27 +22,45 @@ def send_email(event_id, recipients, domain='demo.django-crm.io', protocol='http context["url"] = protocol + '://' + domain + \ reverse('events:detail_view', args=(event.id,)) # recipients = event.assigned_to.filter(is_active=True) - + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) for user in recipients: recipients_list = [] user = User.objects.filter(id=user, is_active=True).first() if user: - recipients_list.append(user.email) - event_members = event.assigned_to.filter(is_active=True) + if (user.email not in blocked_emails) and (user.email.split('@')[-1] not in blocked_domains): + recipients_list.append(user.email) + event_members = event.assigned_to.filter(is_active=True) - context['other_members'] = list(event_members.exclude( - id=user.id).values_list('email', flat=True)) - if len(context['other_members']) > 0: - context['other_members'] = ', '.join(context['other_members']) - else: - context['other_members'] = '' - context['user'] = user.email - html_content = render_to_string( - 'assigned_to_email_template_event.html', context=context) - msg = EmailMessage( - subject=subject, body=html_content, to=recipients_list) - msg.content_subtype = "html" - msg.send() + context['other_members'] = list(event_members.exclude( + id=user.id).values_list('email', flat=True)) + if len(context['other_members']) > 0: + context['other_members'] = ', '.join(context['other_members']) + else: + context['other_members'] = '' + context['user'] = user.email + html_content = render_to_string( + 'assigned_to_email_template_event.html', context=context) + msg = EmailMessage( + subject=subject, body=html_content, to=recipients_list) + msg.content_subtype = "html" + msg.send() + + # if recipients.count() > 0: + # for recipient in recipients: + # context['other_members'] = list(recipients.exclude( + # id=recipient.id).values_list('email', flat=True)) + # if len(context['other_members']) > 0: + # context['other_members'] = ', '.join(context['other_members']) + # else: + # context['other_members'] = '' + # context['user'] = recipient.email + # html_content = render_to_string( + # 'assigned_to_email_template_event.html', context=context) + # msg = EmailMessage( + # subject=subject, body=html_content, to=[recipient.email, ]) + # msg.content_subtype = "html" + # msg.send() # if recipients.count() > 0: # for recipient in recipients: diff --git a/invoices/tasks.py b/invoices/tasks.py index 65832fec..bdcb8ea6 100644 --- a/invoices/tasks.py +++ b/invoices/tasks.py @@ -6,32 +6,36 @@ from django.template.loader import render_to_string from common.models import User from invoices.models import Invoice, InvoiceHistory +from marketing.models import BlockedDomain, BlockedEmail @task def send_email(invoice_id, recipients, domain='demo.django-crm.io', protocol='http'): invoice = Invoice.objects.filter(id=invoice_id).first() created_by = invoice.created_by + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) for user in recipients: recipients_list = [] user = User.objects.filter(id=user, is_active=True).first() if user: - recipients_list.append(user.email) - subject = 'Shared an invoice with you.' - context = {} - context['invoice_title'] = invoice.invoice_title - context['invoice_id'] = invoice_id - context['invoice_created_by'] = invoice.created_by - context["url"] = protocol + '://' + domain + \ - reverse('invoices:invoice_details', args=(invoice.id,)) + if (user.email not in blocked_emails) and (user.email.split('@')[-1] not in blocked_domains): + recipients_list.append(user.email) + subject = 'Shared an invoice with you.' + context = {} + context['invoice_title'] = invoice.invoice_title + context['invoice_id'] = invoice_id + context['invoice_created_by'] = invoice.created_by + context["url"] = protocol + '://' + domain + \ + reverse('invoices:invoice_details', args=(invoice.id,)) - context['user'] = user - html_content = render_to_string( - 'assigned_to_email_template.html', context=context) - msg = EmailMessage( - subject=subject, body=html_content, to=recipients_list) - msg.content_subtype = "html" - msg.send() + context['user'] = user + html_content = render_to_string( + 'assigned_to_email_template.html', context=context) + msg = EmailMessage( + subject=subject, body=html_content, to=recipients_list) + msg.content_subtype = "html" + msg.send() recipients = invoice.accounts.filter(status='open') if recipients.count() > 0: subject = 'Shared an invoice with you.' diff --git a/leads/tasks.py b/leads/tasks.py index 333dddba..d215459a 100644 --- a/leads/tasks.py +++ b/leads/tasks.py @@ -9,6 +9,7 @@ from django.template.loader import render_to_string from accounts.models import User from leads.models import Lead +from marketing.models import BlockedDomain, BlockedEmail def get_rendered_html(template_name, context={}): @@ -70,28 +71,31 @@ def send_email_to_assigned_user(recipients, lead_id, domain='demo.django-crm.io' """ Send Mail To Users When they are assigned to a lead """ lead = Lead.objects.get(id=lead_id) created_by = lead.created_by + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) for user in recipients: recipients_list = [] user = User.objects.filter(id=user, is_active=True).first() if user: - recipients_list.append(user.email) - context = {} - context["url"] = protocol + '://' + domain + \ - reverse('leads:view_lead', args=(lead.id,)) - context["user"] = user - context["lead"] = lead - context["created_by"] = created_by - context["source"] = source - subject = 'Assigned a lead for you. ' - html_content = render_to_string( - 'assigned_to/leads_assigned.html', context=context) - msg = EmailMessage( - subject, - html_content, - to=recipients_list - ) - msg.content_subtype = "html" - msg.send() + if (user.email not in blocked_emails) and (user.email.split('@')[-1] not in blocked_domains): + recipients_list.append(user.email) + context = {} + context["url"] = protocol + '://' + domain + \ + reverse('leads:view_lead', args=(lead.id,)) + context["user"] = user + context["lead"] = lead + context["created_by"] = created_by + context["source"] = source + subject = 'Assigned a lead for you. ' + html_content = render_to_string( + 'assigned_to/leads_assigned.html', context=context) + msg = EmailMessage( + subject, + html_content, + to=recipients_list + ) + msg.content_subtype = "html" + msg.send() @task @@ -112,7 +116,6 @@ def create_lead_from_file(validated_rows, invalid_rows, user_id, source): lead.email = row.get('email') lead.phone = row.get('phone') lead.address_line = row.get('address') - lead.source = source # lead.city = row.get('city') # lead.state = row.get('state') # lead.postcode = row.get('postcode') diff --git a/leads/templates/leads.html b/leads/templates/leads.html index 8f55b496..3c0f82ce 100644 --- a/leads/templates/leads.html +++ b/leads/templates/leads.html @@ -2,6 +2,12 @@ {% load staticfiles %} {% load paginate %} {% load thumbnail %} + +{% block date_picker %} +{% endblock date_picker %} + +{% block quill_editor_links %} +{% endblock quill_editor_links %} {% block extralinks %} + + + +

You have successfully unsubscribed from Campaign emails from + + + {{request.get_host}} . + +

+ + + \ No newline at end of file diff --git a/marketing/tests.py b/marketing/tests.py index 4cdf328c..79f0e3e4 100644 --- a/marketing/tests.py +++ b/marketing/tests.py @@ -4,8 +4,11 @@ from datetime import datetime, timedelta import openpyxl import xlwt from django.core.files.uploadedfile import SimpleUploadedFile +from django.core.management import call_command from django.shortcuts import reverse from django.test import Client, TestCase +from django.test.utils import override_settings +from haystack import connections from common.models import User from marketing.models import (Campaign, CampaignLinkClick, CampaignLog, @@ -14,6 +17,14 @@ from marketing.models import (Campaign, CampaignLinkClick, CampaignLog, Tag) from marketing.views import * +TEST_INDEX = { + 'default': { + 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', + 'URL': 'http://127.0.0.1:9200/', + 'TIMEOUT': 60 * 10, + 'INDEX_NAME': 'test_index', + }, +} class TestMarketingModel(object): def setUp(self): @@ -125,9 +136,19 @@ class TestMarketingModel(object): campaign=self.campaign, contact=self.contact_1 ) + # connections.reload('default') + + # def tearDown(self): + # call_command('clear_index', interactive=False, verbosity=0) +@override_settings(HAYSTACK_CONNECTIONS=TEST_INDEX ) class TestTemplates(TestMarketingModel, TestCase): + + def setUp(self): + super(TestTemplates, self).setUp() + connections.reload('default') + def test_templates(self): url1 = reverse('marketing:dashboard') url2 = reverse('marketing:contact_lists') @@ -139,7 +160,7 @@ class TestTemplates(TestMarketingModel, TestCase): url13 = reverse('marketing:campaign_new') resp1 = self.client.get(url1) resp2 = self.client.get(url2) - resp4 = self.client.get(url4) + # resp4 = self.client.get(url4) resp5 = self.client.get(url5) resp8 = self.client.get(url8) resp9 = self.client.get(url9) @@ -147,7 +168,7 @@ class TestTemplates(TestMarketingModel, TestCase): resp13 = self.client.get(url13) self.assertEqual(resp1.status_code, 200) self.assertEqual(resp2.status_code, 200) - self.assertEqual(resp4.status_code, 200) + # self.assertEqual(resp4.status_code, 200) self.assertEqual(resp5.status_code, 200) self.assertEqual(resp8.status_code, 200) self.assertEqual(resp9.status_code, 200) @@ -229,8 +250,13 @@ class TestDasboardView(TestMarketingModel, TestCase): self.assertEqual(response.status_code, 200) +@override_settings(HAYSTACK_CONNECTIONS=TEST_INDEX ) class TestContactListsListPage(TestMarketingModel, TestCase): + def setUp(self): + super(TestContactListsListPage, self).setUp() + connections.reload('default') + def test_contact_lists_list_page(self): self.client.login(username='john@example.com', password='password') response = self.client.get( @@ -253,9 +279,16 @@ class TestContactListsListPage(TestMarketingModel, TestCase): self.assertEqual(response.status_code, 200) +@override_settings(HAYSTACK_CONNECTIONS=TEST_INDEX ) class TestContactsListPage(TestMarketingModel, TestCase): + def setUp(self): + super(TestContactsListPage, self).setUp() + connections.reload('default') + # connections = ConnectionHandler(TEST_INDEX ) + def test_contacts_list_page(self): + connections.reload('default') self.client.login(username='john@example.com', password='password') response = self.client.get( reverse('marketing:contacts_list')) @@ -774,7 +807,7 @@ class TestContactListDetail(TestMarketingModel, TestCase): def test_unsubscribe_from_campaign(self): response = self.client.get( reverse('marketing:unsubscribe_from_campaign', kwargs={'contact_id': self.contact.id,'campaign_id': self.campaign.id,})) - self.assertEqual(response.status_code, 302) + self.assertEqual(response.status_code, 200) def test_contact_detail(self): self.client.login(username='janeMarketing@example.com', password='password') diff --git a/marketing/urls.py b/marketing/urls.py index a56fb7cf..307b7dd3 100644 --- a/marketing/urls.py +++ b/marketing/urls.py @@ -10,7 +10,8 @@ from .views import ( add_email_for_campaigns, list_all_emails_for_campaigns, edit_email_for_campaigns, delete_email_for_campaigns, add_blocked_domain, blocked_domain_list, edit_blocked_domain, delete_blocked_domain, add_blocked_email, blocked_email_list, edit_blocked_email, delete_blocked_email, - # MarketingContactEmailSearch, + contacts_list_elastic_search, + # MarketingContactEmailSearch, # contacts_list_new # delete_contact_list # campaign_edit @@ -27,7 +28,7 @@ urlpatterns = [ path('contact-list/', contact_lists, name='contact_lists'), path('contacts//edit/', edit_contact, name='edit_contact'), path('contacts//delete/', delete_contact, name='delete_contact'), - path('contacts/', contacts_list, name='contacts_list'), + path('contacts/', contacts_list_elastic_search, name='contacts_list'), path('contact-list/create/', contact_list_new, name='contact_list_new'), # path('cl/list/cnew/', contacts_list_new, name='contacts_list_new'), path('contact-list//detail/', contact_list_detail, name='contact_list_detail'), @@ -77,4 +78,5 @@ urlpatterns = [ path('delete-blocked-email//', delete_blocked_email, name="delete_blocked_email"), path('search-marketing-contact-emails/', include('haystack.urls')), + # path('elastic-search/', contacts_list_elastic_search), ] diff --git a/marketing/views.py b/marketing/views.py index b9bc917f..65d31c29 100644 --- a/marketing/views.py +++ b/marketing/views.py @@ -878,7 +878,7 @@ def unsubscribe_from_campaign(request, contact_id, campaign_id): if campaign_obj: ContactUnsubscribedCampaign.objects.create( campaigns=campaign_obj, contacts=contact_obj, is_unsubscribed=True) - return HttpResponseRedirect('/') + return render(request, 'unsubscribe_from_campaign_template.html') @login_required @@ -1278,6 +1278,72 @@ def delete_blocked_email(request, blocked_email_id): block_email_obj.delete() return redirect(reverse('common:api_settings') + '?blocked_emails') +@login_required(login_url='/login') +@marketing_access_required +def contacts_list_elastic_search(request): + if (request.user.role == "ADMIN"): + # contacts = Contact.objects.filter(is_bounced=False) + contacts = SearchQuerySet().filter(is_bounced='false').models(Contact) + bounced_contacts = SearchQuerySet().filter(is_bounced='true').models(Contact) + failed_contacts = SearchQuerySet().models(FailedContact).filter() + # bounced_contacts = Contact.objects.filter(is_bounced=True) + # failed_contacts = FailedContact.objects.all() + contact_lists = ContactList.objects.all() + else: + contact_ids = request.user.marketing_contactlist.all().values_list('contacts', + flat=True) + contacts = SearchQuerySet().filter(is_bounced='false', id__in=contact_ids).models(Contact) + bounced_contacts = SearchQuerySet().filter(is_bounced='true', id__in=contact_ids).models(Contact) + failed_contacts = SearchQuerySet().models(FailedContact).filter(created_by_id=str(request.user.id)) + # contacts = Contact.objects.filter(id__in=contact_ids).exclude(is_bounced=True) + # bounced_contacts = Contact.objects.filter(id__in=contact_ids, is_bounced=True) + # failed_contacts = FailedContact.objects.filter(created_by=request.user) + contact_lists = ContactList.objects.filter(created_by=request.user) + # contacts = Contact.objects.filter(created_by=request.user) + + users = User.objects.filter( + id__in=[_id for _id in contacts.values_list('created_by_id', flat=True) if _id != '']) + + if request.method == 'GET': + context = {'contacts': contacts, 'users': users, 'contact_lists': contact_lists, + 'bounced_contacts': bounced_contacts, 'failed_contacts': '', + } + return render(request, 'search_contact_emails.html', context) + + if request.method == 'POST': + data = request.POST + if data.get('name'): + contacts = contacts.filter( + name__icontains=data.get('name')) + bounced_contacts = bounced_contacts.filter( + name__icontains=data.get('name')) + failed_contacts = failed_contacts.filter( + name__icontains=data.get('name')) + if data.get('email'): + contacts = contacts.filter(email__icontains=data.get('email')) + bounced_contacts = bounced_contacts.filter(email__icontains=data.get('email')) + failed_contacts = failed_contacts.filter(email__icontains=data.get('email')) + + if data.get('domain_name'): + contacts = contacts.filter(email_domain__icontains=data.get('domain_name')) + bounced_contacts = bounced_contacts.filter(email_domain__icontains=data.get('domain_name')) + failed_contacts = failed_contacts.filter(email_domain__icontains=data.get('domain_name')) + + if data.get('created_by'): + contacts = contacts.filter(created_by_id=data.get('created_by')) + bounced_contacts = bounced_contacts.filter(created_by_id=data.get('created_by')) + failed_contacts = failed_contacts.filter(created_by_id=data.get('created_by')) + + if data.get('contact_list'): + contacts = contacts.filter(contact_lists_id=data.get('contact_list')) + bounced_contacts = bounced_contacts.filter(contact_lists_id=data.get('contact_list')) + failed_contacts = failed_contacts.filter(contact_lists_id=data.get('contact_list')) + + context = {'contacts': contacts, 'users': users, 'contact_lists': contact_lists, + 'bounced_contacts': bounced_contacts, 'failed_contacts': failed_contacts, + } + return render(request, 'search_contact_emails.html', context) + # class MarketingContactEmailSearch(SearchView): # form_class = MarketingContactEmailSearchForm @@ -1298,4 +1364,4 @@ def delete_blocked_email(request, blocked_email_id): # # def get_context_data(self, *args, **kwargs): # # context = super(MarketingContactEmailSearch, self).get_context_data(*args, **kwargs) # # import pdb; pdb.set_trace() -# # return context \ No newline at end of file +# # return context diff --git a/opportunity/tasks.py b/opportunity/tasks.py index 63990132..57415de0 100644 --- a/opportunity/tasks.py +++ b/opportunity/tasks.py @@ -7,6 +7,7 @@ from django.template.loader import render_to_string from accounts.models import User from opportunity.models import Opportunity +from marketing.models import BlockedDomain, BlockedEmail @task @@ -14,25 +15,28 @@ def send_email_to_assigned_user(recipients, opportunity_id, domain='demo.django- """ Send Mail To Users When they are assigned to a opportunity """ opportunity = Opportunity.objects.get(id=opportunity_id) created_by = opportunity.created_by + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) for user in recipients: recipients_list = [] user = User.objects.filter(id=user, is_active=True).first() if user: - recipients_list.append(user.email) - context = {} - context["url"] = protocol + '://' + domain + \ - reverse('opportunity:opp_view', args=(opportunity.id,)) - context["user"] = user - context["opportunity"] = opportunity - context["created_by"] = created_by - subject = 'Assigned an opportunity for you.' - html_content = render_to_string( - 'assigned_to/opportunity_assigned.html', context=context) + if (user.email not in blocked_emails) and (user.email.split('@')[-1] not in blocked_domains): + recipients_list.append(user.email) + context = {} + context["url"] = protocol + '://' + domain + \ + reverse('opportunity:opp_view', args=(opportunity.id,)) + context["user"] = user + context["opportunity"] = opportunity + context["created_by"] = created_by + subject = 'Assigned an opportunity for you.' + html_content = render_to_string( + 'assigned_to/opportunity_assigned.html', context=context) - msg = EmailMessage( - subject, - html_content, - to=recipients_list - ) - msg.content_subtype = "html" - msg.send() + msg = EmailMessage( + subject, + html_content, + to=recipients_list + ) + msg.content_subtype = "html" + msg.send() diff --git a/requirements.txt b/requirements.txt index 576d17b2..0143a072 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -Django==2.2.4 +django==2.2.4 celery==4.3.0 django-simple-pagination==1.3 django-compressor==2.3 @@ -25,3 +25,4 @@ pytest-django==3.5.1 codacy-coverage==1.3.11 django-haystack==2.8.1 elasticsearch==5.5.3 + diff --git a/tasks/celery_tasks.py b/tasks/celery_tasks.py index c68db919..c99e2baf 100644 --- a/tasks/celery_tasks.py +++ b/tasks/celery_tasks.py @@ -8,31 +8,35 @@ from accounts.models import Account, Email from common.models import User from contacts.models import Contact from tasks.models import Task +from marketing.models import BlockedDomain, BlockedEmail @task def send_email(task_id, recipients, domain='demo.django-crm.io', protocol='http'): task = Task.objects.filter(id=task_id).first() created_by = task.created_by + blocked_domains = BlockedDomain.objects.values_list('domain', flat=True) + blocked_emails = BlockedEmail.objects.values_list('email', flat=True) for user in recipients: recipients_list = [] user = User.objects.filter(id=user, is_active=True).first() if user: - recipients_list.append(user.email) - subject = ' Assigned a task for you .' - context = {} - context['task_title'] = task.title - context['task_id'] = task.id - context['task_created_by'] = task.created_by - context["url"] = protocol + '://' + domain + \ - reverse('tasks:task_detail', args=(task.id,)) - context['user'] = user - html_content = render_to_string( - 'tasks_email_template.html', context=context) - msg = EmailMessage( - subject=subject, body=html_content, to=recipients_list) - msg.content_subtype = "html" - msg.send() + if (user.email not in blocked_emails) and (user.email.split('@')[-1] not in blocked_domains): + recipients_list.append(user.email) + subject = ' Assigned a task for you .' + context = {} + context['task_title'] = task.title + context['task_id'] = task.id + context['task_created_by'] = task.created_by + context["url"] = protocol + '://' + domain + \ + reverse('tasks:task_detail', args=(task.id,)) + context['user'] = user + html_content = render_to_string( + 'tasks_email_template.html', context=context) + msg = EmailMessage( + subject=subject, body=html_content, to=recipients_list) + msg.content_subtype = "html" + msg.send() # if task: # subject = ' Assigned a task for you .' diff --git a/templates/marketing/lists/detail.html b/templates/marketing/lists/detail.html index cc14e651..bef3c939 100644 --- a/templates/marketing/lists/detail.html +++ b/templates/marketing/lists/detail.html @@ -82,7 +82,7 @@ {% if contact_list.failed_contacts.all %} {% endif %} {% if contact_list.duplicate_contact_contact_list.all %} diff --git a/templates/root.html b/templates/root.html index 5ccf1f74..9618d5ee 100644 --- a/templates/root.html +++ b/templates/root.html @@ -12,18 +12,24 @@ Django CRM + + {% block date_picker %} + href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" /> + {% endblock date_picker %} + + {% block quill_editor_links %} + {% endblock quill_editor_links %} {% compress css %} {% endcompress %}