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 %} + + +
+