Esempio n. 1
0
 def sent_on_format(self):
     if self.schedule_date_time:
         c_schedule_date_time = convert_to_custom_timezone(
             self.schedule_date_time, self.timezone)
         return c_schedule_date_time.strftime('%b %d, %Y %I:%M %p')
     else:
         c_created_on = convert_to_custom_timezone(self.created_on,
                                                   self.timezone)
         return c_created_on.strftime('%b %d, %Y %I:%M %p')
Esempio n. 2
0
 def sent_on_arrow(self):
     if self.schedule_date_time:
         c_schedule_date_time = convert_to_custom_timezone(
             self.schedule_date_time, self.timezone)
         # return c_schedule_date_time.strftime('%b %d, %Y %I:%M %p')
         return arrow.get(c_schedule_date_time).humanize()
     else:
         c_created_on = convert_to_custom_timezone(self.created_on,
                                                   self.timezone)
         # return c_created_on.strftime('%b %d, %Y %I:%M %p')
         return arrow.get(self.created_on).humanize()
Esempio n. 3
0
def send_scheduled_emails():
    email_objs = Email.objects.filter(scheduled_later=True)
    # TODO: modify this later , since models are updated
    for each in email_objs:
        scheduled_date_time = each.scheduled_date_time

        sent_time = datetime.now().strftime("%Y-%m-%d %H:%M")
        sent_time = datetime.strptime(sent_time, "%Y-%m-%d %H:%M")
        local_tz = pytz.timezone(settings.TIME_ZONE)
        sent_time = local_tz.localize(sent_time)
        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) == str(sent_time.minute)):
            send_email.delay(each.id)
Esempio n. 4
0
def send_scheduled_campaigns():
    from datetime import datetime

    campaigns = Campaign.objects.filter(schedule_date_time__isnull=False)
    for each in campaigns:
        completed = CampaignCompleted.objects.filter(is_completed=True).values_list(
            "campaign_id", flat=True
        )

        if each.id not in completed:
            schedule_date_time = each.schedule_date_time

            sent_time = datetime.now().strftime("%Y-%m-%d %H:%M")
            sent_time = datetime.strptime(sent_time, "%Y-%m-%d %H:%M")
            local_tz = pytz.timezone(settings.TIME_ZONE)
            sent_time = local_tz.localize(sent_time)
            sent_time = convert_to_custom_timezone(
                sent_time, each.timezone, to_utc=True
            )

            if str(each.schedule_date_time.date()) == str(sent_time.date()) and str(
                schedule_date_time.hour
            ) == str(sent_time.hour):
                run_campaign.delay(each.id)
                CampaignCompleted.objects.create(campaign=each, is_completed=True)
Esempio n. 5
0
def send_scheduled_campaigns():
    from datetime import datetime
    campaigns = Campaign.objects.filter(schedule_date_time__isnull=False)
    for each in campaigns:
        sent_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        sent_time = datetime.strptime(sent_time, '%Y-%m-%d %H:%M:%S')
        local_tz = pytz.timezone('UTC')
        schedule_date_time_hour = str(each.schedule_date_time.time()).replace(
            ':00:00', '')
        sent_time = local_tz.localize(sent_time)
        sent_time = convert_to_custom_timezone(sent_time, each.timezone)
        if (str(each.schedule_date_time.date()) == str(sent_time.date())
                and str(schedule_date_time_hour) == str(sent_time.hour)):
            run_campaign(each)
Esempio n. 6
0
def send_scheduled_campaigns():
    from datetime import datetime
    campaigns = Campaign.objects.filter(schedule_date_time__isnull=False)
    for each in campaigns:
        schedule_date_time = each.schedule_date_time

        sent_time = datetime.now().strftime('%Y-%m-%d %H:%M')
        sent_time = datetime.strptime(sent_time, '%Y-%m-%d %H:%M')
        local_tz = pytz.timezone(settings.TIME_ZONE)
        sent_time = local_tz.localize(sent_time)
        sent_time = convert_to_custom_timezone(sent_time,
                                               each.timezone,
                                               to_utc=True)

        if (str(each.schedule_date_time.date()) == str(sent_time.date())
                and str(schedule_date_time.hour) == str(sent_time.hour)):
            run_campaign(each)
Esempio n. 7
0
def campaign_new(request):
    if request.method == 'GET':
        if request.user.is_admin or request.user.is_superuser:
            email_templates = EmailTemplate.objects.all()
        else:
            email_templates = EmailTemplate.objects.filter(
                created_by=request.user)
        if request.user.is_admin or request.user.is_superuser:
            contact_lists = ContactList.objects.all()
        else:
            contact_lists = ContactList.objects.filter(created_by=request.user)
        # if request.GET.get('contact_list'):
        #     contacts = Contact.objects.filter(
        #         contact_list__id__in=json.loads(request.GET.get('contact_list'))).distinct()

        data = {
            'contact_lists': contact_lists,
            'email_templates': email_templates,
            "timezones": TIMEZONE_CHOICES,
            "settings_timezone": settings.TIME_ZONE
        }
        # return JsonResponse(data, status=status.HTTP_200_OK)
        return render(request, 'marketing/campaign/new.html', data)
    else:
        form = SendCampaignForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.created_by = request.user
            if request.POST.get('from_email'):
                instance.from_email = request.POST['from_email']
            if request.POST.get('from_name'):
                instance.from_name = request.POST['from_name']
            if request.POST.get('reply_to_email'):
                instance.reply_to_email = request.POST['reply_to_email']
            # if request.FILES.get('attachment'):
            #     instance.attachment = request.FILES.get('attachment')
            instance.save()
            for each in request.POST.getlist('contact_list'):
                instance.contact_lists.add(ContactList.objects.get(id=each))
            # contacts = Contact.objects.filter(contact_list__in=json.loads(request.POST['contact_list'])).distinct()
            # for each_contact in contacts:
            #     instance.contacts.add(each_contact)

            camp = instance

            html = camp.html
            dom = lxml.html.document_fromstring(html)
            selAnchor = CSSSelector('a')
            links = selAnchor(dom)
            llist = []
            # Extract links and unique links
            for e in links:
                llist.append(e.get('href'))

            # get uniaue linnk
            links = set(llist)

            # Replace Links with new One
            # domain_url = '%s://%s' % (request.scheme, request.META['HTTP_HOST'])
            domain_url = settings.URL_FOR_LINKS
            if Link.objects.filter(campaign_id=camp.id):
                Link.objects.filter(campaign_id=camp.id).delete()
            for l in links:
                link = Link.objects.create(campaign_id=camp.id, original=l)
                html = html.replace(
                    'href="' + l + '"', 'href="' + domain_url + '/m/cm/link/' +
                    str(link.id) + '/e/{{email_id}}"')
            camp.html_processed = html
            camp.sent_status = "scheduled"
            camp.save()

            if request.POST.get('schedule_later', None) == 'true':
                schedule_date_time = request.POST.get('schedule_date_time', '')
                user_timezone = request.POST.get('timezone', '')
                if user_timezone and schedule_date_time:
                    start_time_object = ddatetime.strptime(
                        schedule_date_time, '%Y-%m-%d %H:%M')
                    schedule_date_time = convert_to_custom_timezone(
                        start_time_object, user_timezone, to_utc=True)
                instance.schedule_date_time = schedule_date_time
                instance.timezone = user_timezone
                instance.save()
            else:
                run_campaign.delay(instance.id)

            return JsonResponse({
                'error': False,
                'data': form.data
            },
                                status=status.HTTP_201_CREATED)
        return JsonResponse({
            'error': True,
            'errors': form.errors
        },
                            status=status.HTTP_200_OK)