Exemplo n.º 1
0
Arquivo: views.py Projeto: aeud/sing
def prepare_email(schedule):
    visualization = schedule.visualization
    err = None
    try:
        job = Job.objects.filter(completed_at__isnull=False, query__visualization=visualization, cached_until__gte=timezone.now()).order_by('-start_at')[:1].get()
        if job.query_checksum != job.query.checksum:
            err, job = execute_query(schedule.created_by, visualization)
    except Job.DoesNotExist:
        err, job = execute_query(schedule.created_by, visualization)
    if not err:
        rows, schema = job.get_rows()
        header = rows.pop(0)
        num_indexes = [i for i, v in enumerate(schema) if v.get('type') in ['FLOAT', 'INTEGER']]
        integer_indexes = [i for i, v in enumerate(schema) if v.get('type') in ['INTEGER']]
        totals = None
        if schedule.show_sum:
            totals = [reduce(lambda x,y: x + y, list(map(lambda x: x[i], rows)), 0) if v.get('type') in ['FLOAT', 'INTEGER'] else None for i, v in enumerate(schema)]
        body = transform(loader.render_to_string('emails/visualization.html', dict(visualization=visualization,
                                                                                   job=job,
                                                                                   rows=rows,
                                                                                   absolute_url=settings.MAIN_HOST + reverse('visualizations_show', kwargs=dict(visualization_id=visualization.id)),
                                                                                   schema=schema,
                                                                                   num_indexes=num_indexes,
                                                                                   integer_indexes=integer_indexes,
                                                                                   header=header,
                                                                                   schedule=schedule,
                                                                                   totals=totals,)))
    return body
Exemplo n.º 2
0
Arquivo: cron.py Projeto: aeud/sing
def run(*args):
    time = args[0] if len(args) > 0 else 'morning'
    query = """
select distinct
    s.id
from
    pilot.schedules_schedule s
    left join pilot.schedules_scheduleoption o on (s.id = o.schedule_id)
where
    s.is_active
    and s.time = %s
    and (
        (s.frequency = 'monthly' and o.option = extract(day from current_timestamp + interval '8 hours'))
        or (s.frequency = 'weekly' and o.option = extract(dow from current_timestamp + interval '8 hours'))
        or s.frequency = 'daily'
    )
;
        """
    cursor = connection.cursor()
    cursor.execute(query, [time])
    schedules = cursor.fetchall()
    for schedule_id in schedules:
        schedule = Schedule.objects.get(pk=schedule_id[0])
        visualization = schedule.visualization
        err = None
        try:
            job = Job.objects.filter(
                completed_at__isnull=False,
                query__visualization=visualization,
                cached_until__gte=timezone.now()).order_by(
                    '-start_at')[:1].get()
            if job.query_checksum != job.query.checksum:
                err, job = execute_query(schedule.created_by, visualization)
        except Job.DoesNotExist:
            err, job = execute_query(schedule.created_by, visualization)
        if not err:
            rows, schema = job.get_rows()
            num_indexes = [
                i for i, v in enumerate(schema)
                if v.get('type') in ['FLOAT', 'INTEGER']
            ]
            body = prepare_email(schedule)
            email_message = EmailMultiAlternatives(
                schedule.generate_subject(),
                body,
                visualization.name + ' <colors+' + str(visualization.id) +
                '@luxola.com>', [schedule.email],
                reply_to=['*****@*****.**'],
                bcc=['*****@*****.**'])
            html_email = body
            email_message.attach_alternative(html_email, 'text/html')
            email_message.send()
Exemplo n.º 3
0
Arquivo: views.py Projeto: aeud/sing
def prepare_email(schedule):
    visualization = schedule.visualization
    err = None
    try:
        job = Job.objects.filter(
            completed_at__isnull=False,
            query__visualization=visualization,
            cached_until__gte=timezone.now()).order_by('-start_at')[:1].get()
        if job.query_checksum != job.query.checksum:
            err, job = execute_query(schedule.created_by, visualization)
    except Job.DoesNotExist:
        err, job = execute_query(schedule.created_by, visualization)
    if not err:
        rows, schema = job.get_rows()
        header = rows.pop(0)
        num_indexes = [
            i for i, v in enumerate(schema)
            if v.get('type') in ['FLOAT', 'INTEGER']
        ]
        integer_indexes = [
            i for i, v in enumerate(schema) if v.get('type') in ['INTEGER']
        ]
        totals = None
        if schedule.show_sum:
            totals = [
                reduce(lambda x, y: x + y, list(map(lambda x: x[i], rows)), 0)
                if v.get('type') in ['FLOAT', 'INTEGER'] else None
                for i, v in enumerate(schema)
            ]
        body = transform(
            loader.render_to_string(
                'emails/visualization.html',
                dict(
                    visualization=visualization,
                    job=job,
                    rows=rows,
                    absolute_url=settings.MAIN_HOST +
                    reverse('visualizations_show',
                            kwargs=dict(visualization_id=visualization.id)),
                    schema=schema,
                    num_indexes=num_indexes,
                    integer_indexes=integer_indexes,
                    header=header,
                    schedule=schedule,
                    totals=totals,
                )))
    return body
Exemplo n.º 4
0
Arquivo: views.py Projeto: aeud/sing
def prepare_email_globale(schedule):
    schedule_visualizations = GlobaleScheduleVisualization.objects.filter(schedule=schedule).order_by('position')
    jobs = []
    for schedule_visualization in schedule_visualizations:
        visualization = schedule_visualization.visualization
        err = None
        try:
            job = Job.objects.filter(completed_at__isnull=False, query__visualization=visualization, cached_until__gte=timezone.now()).order_by('-start_at')[:1].get()
            if job.query_checksum != job.query.checksum:
                err, job = execute_query(schedule.created_by, visualization)
        except Job.DoesNotExist:
            err, job = execute_query(schedule.created_by, visualization)
        if not err:
            rows, schema = job.get_rows()
            header = rows.pop(0)
            num_indexes = [i for i, v in enumerate(schema) if v.get('type') in ['FLOAT', 'INTEGER']]
            integer_indexes = [i for i, v in enumerate(schema) if v.get('type') in ['INTEGER']]
            totals = None
            if schedule_visualization.show_sum:
                totals = [reduce(lambda x,y: x + y, list(map(lambda x: x[i], rows)), 0) if v.get('type') in ['FLOAT', 'INTEGER'] else None for i, v in enumerate(schema)]
            jobs.append(dict(visualization=visualization,
                             job=job,
                             rows=rows,
                             absolute_url=settings.MAIN_HOST + reverse('visualizations_show', kwargs=dict(visualization_id=visualization.id)),
                             schema=schema,
                             num_indexes=num_indexes,
                             integer_indexes=integer_indexes,
                             header=header,
                             schedule=schedule_visualization,
                             totals=totals,))
    if schedule.linked_dashboard:
        if schedule.anonymous_link:
            shared_dashboard = SharedDashboard(dashboard=schedule.linked_dashboard,
                                               created_by=schedule.created_by,
                                               valid_until=timezone.now() + timedelta(days=2))
            shared_dashboard.save()
            dashbaord_url = settings.MAIN_HOST + shared_dashboard.generate_path()
        else:
            dashbaord_url = settings.MAIN_HOST + reverse('dashboards_play', kwargs=dict(dashboard_slug=schedule.linked_dashboard.slug))
    else:
        dashbaord_url = None 
    body = transform(loader.render_to_string('emails/visualization_globale.html', dict(jobs=jobs,
                                                                                       schedule=schedule,
                                                                                       absolute_url=settings.MAIN_HOST + reverse('visualizations_show', kwargs=dict(visualization_id=visualization.id)),
                                                                                       dashbaord_url=dashbaord_url,)))
    return body
Exemplo n.º 5
0
Arquivo: cron.py Projeto: aeud/sing
def run(*args):
    time = args[0] if len(args) > 0 else 'morning'
    query = """
select distinct
    s.id
from
    pilot.schedules_schedule s
    left join pilot.schedules_scheduleoption o on (s.id = o.schedule_id)
where
    s.is_active
    and s.time = %s
    and (
        (s.frequency = 'monthly' and o.option = extract(day from current_timestamp + interval '8 hours'))
        or (s.frequency = 'weekly' and o.option = extract(dow from current_timestamp + interval '8 hours'))
        or s.frequency = 'daily'
    )
;
        """
    cursor = connection.cursor()
    cursor.execute(query, [time])
    schedules = cursor.fetchall()
    for schedule_id in schedules:
        schedule = Schedule.objects.get(pk=schedule_id[0])
        visualization = schedule.visualization
        err = None
        try:
            job = Job.objects.filter(completed_at__isnull=False, query__visualization=visualization, cached_until__gte=timezone.now()).order_by('-start_at')[:1].get()
            if job.query_checksum != job.query.checksum:
                err, job = execute_query(schedule.created_by, visualization)
        except Job.DoesNotExist:
            err, job = execute_query(schedule.created_by, visualization)
        if not err:
            rows, schema = job.get_rows()
            num_indexes = [i for i, v in enumerate(schema) if v.get('type') in ['FLOAT', 'INTEGER']]
            body = prepare_email(schedule)
            email_message = EmailMultiAlternatives(schedule.generate_subject(), body, visualization.name + ' <colors+' + str(visualization.id) + '@luxola.com>', [schedule.email], reply_to=['*****@*****.**'], bcc=['*****@*****.**'])
            html_email = body
            email_message.attach_alternative(html_email, 'text/html')
            email_message.send()
Exemplo n.º 6
0
Arquivo: views.py Projeto: aeud/sing
def prepare_email_globale(schedule):
    schedule_visualizations = GlobaleScheduleVisualization.objects.filter(
        schedule=schedule).order_by('position')
    jobs = []
    for schedule_visualization in schedule_visualizations:
        visualization = schedule_visualization.visualization
        err = None
        try:
            job = Job.objects.filter(
                completed_at__isnull=False,
                query__visualization=visualization,
                cached_until__gte=timezone.now()).order_by(
                    '-start_at')[:1].get()
            if job.query_checksum != job.query.checksum:
                err, job = execute_query(schedule.created_by, visualization)
        except Job.DoesNotExist:
            err, job = execute_query(schedule.created_by, visualization)
        if not err:
            rows, schema = job.get_rows()
            header = rows.pop(0)
            num_indexes = [
                i for i, v in enumerate(schema)
                if v.get('type') in ['FLOAT', 'INTEGER']
            ]
            integer_indexes = [
                i for i, v in enumerate(schema)
                if v.get('type') in ['INTEGER']
            ]
            totals = None
            if schedule_visualization.show_sum:
                totals = [
                    reduce(lambda x, y: x + y, list(map(lambda x: x[i], rows)),
                           0)
                    if v.get('type') in ['FLOAT', 'INTEGER'] else None
                    for i, v in enumerate(schema)
                ]
            jobs.append(
                dict(
                    visualization=visualization,
                    job=job,
                    rows=rows,
                    absolute_url=settings.MAIN_HOST +
                    reverse('visualizations_show',
                            kwargs=dict(visualization_id=visualization.id)),
                    schema=schema,
                    num_indexes=num_indexes,
                    integer_indexes=integer_indexes,
                    header=header,
                    schedule=schedule_visualization,
                    totals=totals,
                ))
    if schedule.linked_dashboard:
        if schedule.anonymous_link:
            shared_dashboard = SharedDashboard(
                dashboard=schedule.linked_dashboard,
                created_by=schedule.created_by,
                valid_until=timezone.now() + timedelta(days=2))
            shared_dashboard.save()
            dashbaord_url = settings.MAIN_HOST + shared_dashboard.generate_path(
            )
        else:
            dashbaord_url = settings.MAIN_HOST + reverse(
                'dashboards_play',
                kwargs=dict(dashboard_slug=schedule.linked_dashboard.slug))
    else:
        dashbaord_url = None
    body = transform(
        loader.render_to_string(
            'emails/visualization_globale.html',
            dict(
                jobs=jobs,
                schedule=schedule,
                absolute_url=settings.MAIN_HOST +
                reverse('visualizations_show',
                        kwargs=dict(visualization_id=visualization.id)),
                dashbaord_url=dashbaord_url,
            )))
    return body