def board(id=None): if request.method == 'POST': board_data = request.get_json() if id == 'new': new_id = binascii.b2a_hex(os.urandom(5)) board = Board(id=new_id) board_data['id'] = new_id else: board = Board.query.filter_by(id=id).first_or_404() old_panels = board.json()['panels'] new_panels = board_data['panels'] # Make panel dicts so they are searchable by ID try: old_panels = {p['id']: p for p in old_panels} except KeyError: # If the old panels do not have an id, then they are pre-precompute era. # Therefore none of them have precompute turned on, so for the purpose of # determining whether precompute has been enabled or changed, we can # pretend they don't exist. old_panels = {} new_panels = {p['id']: p for p in new_panels} # Find any changes to precompute settings for panel in old_panels.values(): new_panel = new_panels.get(panel['id']) # Check for deletions if (panel['data_source']['precompute']['enabled'] and not new_panel): disable_precompute(panel) # Check for precompute disabled elif (panel['data_source']['precompute']['enabled'] and new_panel and not new_panel['data_source']['precompute']['enabled']): disable_precompute(panel) for panel in new_panels.values(): if panel['data_source']['precompute']['enabled']: old_panel = old_panels.get(panel['id']) # Check for precompute enabled if (not old_panel or not old_panel['data_source']['precompute']['enabled']): task_id = enable_precompute(panel) panel['data_source']['precompute']['task_id'] = task_id # Check for code change or precompute settings change elif (old_panel['data_source']['code'] != panel['data_source']['code'] or old_panel['data_source']['precompute'] != panel['data_source']['precompute'] or old_panel['data_source']['timeframe'] != panel['data_source']['timeframe']): disable_precompute(old_panel) task_id = enable_precompute(panel) panel['data_source']['precompute']['task_id'] = task_id # Transform panel dict back into list for saving new_panels = new_panels.values() board.set_board_data(board_data) board.save() else: board = Board.query.filter_by(id=id).first_or_404() return board.json()