def get_revision(database, vis_id, rev_id, details=False, context=False): session = select_session(sessions.get(database)) try: if rev_id is None: vis, rev = (session.query(Visualizations, Revisions).select_from( Visualizations, Revisions).filter(Visualizations.vis_id == vis_id).filter( Revisions.rev_vis == vis_id).filter( Revisions.rev_id == Visualizations.vis_latest) ).first() else: vis, rev = (session.query(Visualizations, Revisions).select_from( Visualizations, Revisions).filter(Visualizations.vis_id == vis_id).filter( Revisions.rev_vis == vis_id).filter( Revisions.rev_id == rev_id)).first() if context is True: res = { "rev_vis": rev.rev_vis, "vis_query": rev.vis_query, "vis_title": vis.vis_title, "rev_timestamp": rev.rev_timestamp, "vis_params": vis.vis_params, "rev_data": rev.rev_data } return res else: if details is True: return rev.as_dict() else: return rev.rev_data except TypeError: persistence_ns.logger.info("Vis ID not found: %s in database %s" % (vis_id, database)) return None
def select_session(Session=None): """Select session according to database, else select session for default database.""" if Session is not None: return Session() else: return sessions.get(settings.DEFAULT["db"])()
def create_visualization(database, vis_id, vis_title, data, vis_clean_query=None, vis_query=None, vis_params=None): if not exists_visualization(database, vis_id): session = select_session(sessions.get(database)) new_vis = Visualizations(vis_id=vis_id, vis_clean_query=vis_clean_query, vis_query=vis_query, vis_title=vis_title, vis_params=vis_params) session.add(new_vis) session.commit() write_revision(database, vis_id, data, 1)
def get_context(database, vis_id, revision_context=False): session = select_session(sessions.get(database)) vis, rev = (session.query(Visualizations, Revisions).select_from( Visualizations, Revisions).filter(Visualizations.vis_id == vis_id).filter( Revisions.rev_vis == vis_id).filter( Revisions.rev_id == Visualizations.vis_latest)).first() res = { "rev_vis": rev.rev_vis, "vis_query": rev.vis_query, "vis_title": vis.vis_title, "rev_timestamp": rev.rev_timestamp, "vis_params": vis.vis_params } if revision_context == 'true': data = json.loads(rev.rev_data) res["additional_context"] = data.get("additional_context", {}) return res
def write_revision(database, vis_id, data, rev_id=None): session = select_session(sessions.get(database)) vis = session.query(Visualizations).filter_by(vis_id=vis_id).first() if rev_id is None: if vis.vis_latest is None: rev_id = 1 else: rev_id = vis.vis_latest + 1 query = vis.vis_clean_query new_rev = Revisions(rev_id=rev_id, rev_vis=vis_id, rev_user="******", rev_timestamp=datetime.utcnow(), rev_comment="Visualization created", rev_data=data, vis_query=query) session.add(new_rev) vis.vis_latest = rev_id session.commit()
def exists_visualization(database, vis_id): session = select_session(sessions.get(database)) vis = session.query(Visualizations).filter_by(vis_id=vis_id).first() exists = True if vis else False return exists