Ejemplo n.º 1
0
def csvdownload_view(
    request: HttpRequest,
    pk: int,
    workflow: Optional[Workflow] = None,
    view: Optional[View] = None,
) -> HttpResponse:
    """Download the data in a given view.

    :param request: HTML request

    :param pk: View ID

    :param workflow: Set by the decorator to the current workflow.

    :param view: Set by the decorator to the view with the given PK

    :return: Return a CSV download of the data in the table
    """
    # Fetch the data frame
    return _respond_csv(
        get_subframe(
            workflow.get_data_frame_table_name(),
            view.formula,
            [col.name for col in view.columns.all()],
        ), )
Ejemplo n.º 2
0
    def test_eval_conditions(self):
        # Get the action first
        self.action = Action.objects.get(name=self.action_name)

        # Get wflow table, filter and column names
        wflow_table = self.action.workflow.get_data_frame_table_name()
        filter_formula = self.action.get_filter_formula()
        column_names = self.action.workflow.get_column_names()
        conditions = self.action.conditions.filter(is_filter=False)

        # Get dataframe
        df = get_subframe(wflow_table, filter_formula, column_names)

        # Get the query set
        qs = get_rows(wflow_table,
                      column_names=column_names,
                      filter_formula=filter_formula)

        # Iterate over the rows in the dataframe and compare
        for idx, row in enumerate(qs):
            row_value_df = dict(list(zip(column_names, df.loc[idx, :])))
            row_value_qs = dict(list(zip(column_names, row)))

            cond_eval1 = [
                evaluate_formula(x.formula, EVAL_EXP, row_value_df)
                for x in conditions
            ]

            cond_eval2 = [
                evaluate_formula(x.formula, EVAL_EXP, row_value_qs)
                for x in conditions
            ]

            assert cond_eval1 == cond_eval2
Ejemplo n.º 3
0
def _create_single_message(
    msg_body_sbj_to: List[str],
    track_str: str,
    from_email: str,
    cc_email_list: List[str],
    bcc_email_list: List[str],
    attachments: Optional[List[models.View]] = None,
    filter_formula: Optional[Dict] = None,
) -> Union[EmailMessage, EmailMultiAlternatives]:
    """Create either an EmailMessage or EmailMultiAlternatives object.

    :param msg_body_sbj_to: List with body, subject, to
    :param track_str: String to add to track
    :param from_email: From email
    :param cc_email_list: CC list
    :param bcc_email_list: BCC list
    :param attachments: List of views to attach to the message (optional)
    :param filter_formula: Filter attached ot the action (optional)
    :return: Either EmailMessage or EmailMultiAlternatives
    """
    if settings.EMAIL_HTML_ONLY:
        # Message only has the HTML text
        msg = EmailMessage(
            msg_body_sbj_to[1],
            msg_body_sbj_to[0] + track_str,
            from_email,
            [msg_body_sbj_to[2]],
            bcc=bcc_email_list,
            cc=cc_email_list,
        )
        msg.content_subtype = 'html'
    else:
        # Get the plain text content and bundle it together with the HTML
        # in a message to be added to the list.
        msg = EmailMultiAlternatives(
            subject=html2text.html2text(msg_body_sbj_to[0]),
            body=msg_body_sbj_to[1],
            from_email=from_email,
            to=[msg_body_sbj_to[2]],
            bcc=bcc_email_list,
            cc=cc_email_list,
        )
        msg.attach_alternative(msg_body_sbj_to[0] + track_str, 'text/html')

    if attachments:
        for attachment in attachments:
            data_frame = pandas.get_subframe(
                attachment.workflow.get_data_frame_table_name(),
                filter_formula, [col.name for col in attachment.columns.all()])

            mime_obj = MIMEText(data_frame.to_csv(), 'csv')
            mime_obj.add_header('Content-Disposition',
                                'attachment',
                                filename=attachment.name + '.csv')
            msg.attach(mime_obj)

    return msg
Ejemplo n.º 4
0
def vis_html_content(context, column_name):
    """Create the HTML visualization code."""
    # Get the action
    action = context.get(evaluate.ACTION_CONTEXT_VAR)
    if not action:
        raise Exception(_('Action object not found when processing tag'))
    workflow = action.workflow

    # Check if the column is correct
    if not workflow.columns.filter(name=column_name).exists():
        raise Exception(_('Column {0} does not exist').format(column_name))

    # Get the visualization number to generate unique IDs
    viz_number = context[evaluate.VIZ_NUMBER_CONTEXT_VAR]

    # Create the context for the visualization
    viz_ctx = {
        'style': 'width:400px; height:225px;',
        'id': 'viz_tag_{0}'.format(viz_number)
    }

    # If there is a column name in the context, insert it as individual value
    # If the template is simply being saved and rendered to detect syntax
    # errors, we may not have the data of an individual, so we have to relax
    # this restriction.
    ivalue = context.get(evaluate.TR_ITEM(column_name))
    if ivalue is not None:
        viz_ctx['individual_value'] = ivalue

    # Get the data from the data frame
    df = pandas.get_subframe(
        workflow.get_data_frame_table_name(),
        action.get_filter_formula(),
        [column_name])

    # Get the visualisation
    viz = plotly.PlotlyColumnHistogram(data=df, context=viz_ctx)

    prefix = ''
    if viz_number == 0:
        prefix = ''.join([
            '<script src="{0}"></script>'.format(x)
            for x in plotly.PlotlyColumnHistogram.get_engine_scripts()
        ])

    # Update viz number
    context[evaluate.VIZ_NUMBER_CONTEXT_VAR] = viz_number + 1

    # Return the rendering of the viz marked as safe
    return mark_safe(prefix + viz.render())
Ejemplo n.º 5
0
def csvdownload(
    request: HttpRequest,
    workflow: Optional[models.Workflow] = None,
) -> HttpResponse:
    """Download the data in the workflow.

    :param request: HTML request
    :param workflow: Set by the decorator to the current workflow.
    :return: Return a CSV download of the data in the table
    """
    del request
    return services.create_response_with_csv(
        pandas.get_subframe(workflow.get_data_frame_table_name(), None,
                            workflow.get_column_names()))
Ejemplo n.º 6
0
def csvdownload_view(
    request: HttpRequest,
    pk: int,
    workflow: Optional[models.Workflow] = None,
    view: Optional[models.View] = None,
) -> HttpResponse:
    """Download the data in a given view.

    :param request: HTML request
    :param pk: View ID
    :param workflow: Set by the decorator to the current workflow.
    :param view: Set by the decorator to the view with the given PK
    :return: Return a CSV download of the data in the table
    """
    del request, pk
    return services.create_response_with_csv(
        pandas.get_subframe(workflow.get_data_frame_table_name(), view.formula,
                            [col.name for col in view.columns.all()]))
Ejemplo n.º 7
0
def csvdownload(
    request: HttpRequest,
    workflow: Optional[Workflow] = None,
) -> HttpResponse:
    """Download the data in the workflow.

    :param request: HTML request

    :param workflow: Set by the decorator to the current workflow.

    :return: Return a CSV download of the data in the table
    """
    # Fetch the data frame
    return _respond_csv(
        get_subframe(
            workflow.get_data_frame_table_name(),
            None,
            workflow.get_column_names(),
        ), )