Esempio n. 1
0
def dashboard_copy(form):
    """Duplicate a dashboard and its widgets"""
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['duplicate']['title'],
        controller=TRANSLATIONS['dashboard']['title'])
    error = []

    try:
        # Get current dashboard and its widgets
        dashboard = Dashboard.query.filter(
            Dashboard.unique_id == form.dashboard_id.data).first()
        widgets = Widget.query.filter(
            Widget.dashboard_id == dashboard.unique_id).all()

        # Duplicate dashboard with new unique_id and name
        new_dashboard = clone_model(dashboard,
                                    unique_id=set_uuid(),
                                    name="New Dashboard")

        # Duplicate all widgets and assign them to the new dashboard
        for each_widget in widgets:
            clone_model(each_widget,
                        unique_id=set_uuid(),
                        dashboard_id=new_dashboard.unique_id)
    except Exception as msg:
        error.append(msg)
        logger.exception("Duplicating dashboard")

    flash_success_errors(error, action,
                         url_for('routes_page.page_dashboard_default'))
Esempio n. 2
0
def input_duplicate(form_mod):
    messages = {"success": [], "info": [], "warning": [], "error": []}

    mod_input = Input.query.filter(
        Input.unique_id == form_mod.input_id.data).first()

    if not mod_input:
        return None, None

    # Duplicate dashboard with new unique_id and name
    new_input = clone_model(mod_input,
                            unique_id=set_uuid(),
                            name=f"Copy of {mod_input.name}")

    # Deactivate Input
    mod_input = Input.query.filter(
        Input.unique_id == new_input.unique_id).first()
    if mod_input:
        mod_input.is_activated = False
        mod_input.save()

        dev_measurements = DeviceMeasurements.query.filter(
            DeviceMeasurements.device_id == form_mod.input_id.data).all()
        for each_dev in dev_measurements:
            clone_model(each_dev,
                        unique_id=set_uuid(),
                        device_id=mod_input.unique_id)

        dev_channels = InputChannel.query.filter(
            InputChannel.input_id == form_mod.input_id.data).all()
        for each_dev in dev_channels:
            clone_model(each_dev,
                        unique_id=set_uuid(),
                        input_id=mod_input.unique_id)

    messages["success"].append(
        f"{TRANSLATIONS['duplicate']['title']} {TRANSLATIONS['input']['title']}"
    )

    return messages, new_input.unique_id