Code Duplication    Length = 21-23 lines in 2 locations

flask_jsondash/charts_builder.py 2 locations

@@ 404-426 (lines=23) @@
401
402
403
def is_global_dashboard(view):
404
    """Check if a dashboard is considered global."""
405
    return all([
406
        setting('JSONDASH_GLOBALDASH'),
407
        view.get('created_by') == setting('JSONDASH_GLOBAL_USER'),
408
    ])
409
410
411
def check_global():
412
    """Allow overriding of the user by making it global.
413
    This also checks if the setting is enabled for the app,
414
    otherwise it will not allow it.
415
    """
416
    global_enabled = setting('JSONDASH_GLOBALDASH')
417
    global_flag = request.form.get('is_global', '') == 'on'
418
    can_make_global = auth(authtype='edit_global')
419
    if all([global_flag, global_enabled, can_make_global]):
420
        return dict(created_by=setting('JSONDASH_GLOBAL_USER'))
421
    return dict()
422
423
424
@charts.route('/charts/create', methods=['POST'])
425
def create():
426
    """Normalize the form POST and setup the json view config object."""
427
    if not auth(authtype='create'):
428
        flash('You do not have access to create dashboards.', 'error')
429
        return redirect(url_for('jsondash.dashboard'))
@@ 381-401 (lines=21) @@
378
            data = db.reformat_data(data, c_id)
379
        except (TypeError, ValueError):
380
            flash('Invalid JSON config.', 'error')
381
            return redirect(view_url)
382
    else:
383
        data = dict(
384
            name=form_data['name'],
385
            modules=db.format_charts(form_data),
386
            date=str(dt.now()),
387
            id=c_id,
388
        )
389
    # Update metadata, but exclude some fields that should never
390
    # be overwritten by user, once the view has been created.
391
    data.update(**metadata(exclude=['created_by']))
392
    # Possibly override global user, if configured and valid.
393
    data.update(**check_global())
394
    # Update db
395
    if edit_raw:
396
        adapter.update(c_id, data=data, fmt_charts=False)
397
    else:
398
        adapter.update(c_id, data=data)
399
    flash('Updated view "{}"'.format(c_id))
400
    return redirect(view_url)
401
402
403
def is_global_dashboard(view):
404
    """Check if a dashboard is considered global."""