def clone_viz(request, id=0): parent_viz = SavedViz.objects.get(id=id) redirect_url = 'genericplot_id' new_name = 'Copy of %s' % parent_viz.name # Create new visualization clone = SavedViz(name=new_name, parent=parent_viz) clone.save() # Set Permissions on new viz perm = Viz_Perms(visualization=clone, user=request.user, perm=Viz_Perms.OWNER) perm.save() # Create copy plots plots = Plot.objects.filter(visualization=parent_viz) for plot in plots: old_plot_id = plot.id new_plot = plot new_plot.id = None new_plot.name = new_name new_plot.visualization = clone new_plot.save() # Create associated cohorts with new plots plot_cohorts = Plot_Cohorts.objects.filter(plot_id=old_plot_id) for plot_cohort in plot_cohorts: new_plot_cohort = plot_cohort new_plot_cohort.id = None new_plot_cohort.plot = new_plot new_plot_cohort.save() return redirect(reverse(redirect_url, args=[clone.id]))
def clone_viz(request, id=0): parent_viz = SavedViz.objects.get(id=id) redirect_url = 'genericplot_id' new_name = 'Copy of %s' % parent_viz.name # Create new visualization clone = SavedViz(name=new_name, parent=parent_viz) clone.save() # Set Permissions on new viz perm = Viz_Perms(visualization=clone, user=request.user, perm=Viz_Perms.OWNER) perm.save() # Create copy plots plots = Plot.objects.filter(visualization=parent_viz) for plot in plots: old_plot_id = plot.id new_plot = plot new_plot.id = None new_plot.name = new_name new_plot.visualization = clone new_plot.save() # Create associated cohorts with new plots plot_cohorts = Plot_Cohorts.objects.filter(plot_id=old_plot_id) for plot_cohort in plot_cohorts: new_plot_cohort = plot_cohort new_plot_cohort.id = None new_plot_cohort.plot = new_plot new_plot_cohort.save() return redirect(reverse(redirect_url,args=[clone.id]))
def save_viz(request): redirect_url = '/user_landing/' if request.method == 'POST': params = request.POST name = str(params.get('name', None)) viz_id = int(params.get('viz_id', None)) # Update or create visualization viz, created = SavedViz.objects.update_or_create( id=viz_id, defaults={ 'name': name, 'last_date_saved': datetime.datetime.now() }) # Update or create plots associated to visualizations plots = {} for key in params.keys(): if 'plot' in key: plot, index, attr = key.split('-') index = int(index) attr = str(attr) if index in plots.keys(): plots[index][attr] = params[key].encode('utf-8') else: plots[index] = {} plots[index][attr] = params[key].encode('utf-8') for key in plots.keys(): cohort_ids = plots[key]['cohort_ids'].split(',') cohorts = Cohort.objects.filter(id__in=cohort_ids) plot, created = Plot.objects.update_or_create( id=key, defaults={ 'visualization': viz, 'title': plots[key]['name'], 'x_axis': plots[key]['x_axis'], 'y_axis': plots[key]['y_axis'], 'color_by': plots[key]['color_by'], 'plot_type': plots[key]['plot_type'] }) plot_cohort_list = [] Plot_Cohorts.objects.filter(plot=plot).delete() for cohort in cohorts: plot_cohort_list.append(Plot_Cohorts(plot=plot, cohort=cohort)) Plot_Cohorts.objects.bulk_create(plot_cohort_list) # Create and save permissions perm = Viz_Perms(visualization=viz, user=request.user, perm=Viz_Perms.OWNER) perm.save() messages.info(request, 'Visualization, %s, saved successfully.' % viz.name) # return redirect(reverse(redirect_url, args=[viz.id])) return redirect(redirect_url)
def save_viz(request): redirect_url = '/user_landing/' if request.method == 'POST': params = request.POST name = str(params.get('name', None)) viz_id = int(params.get('viz_id', None)) # Update or create visualization viz, created = SavedViz.objects.update_or_create(id=viz_id, defaults={'name':name, 'last_date_saved': datetime.datetime.now()}) # Update or create plots associated to visualizations plots = {} for key in params.keys(): if 'plot' in key: plot, index, attr = key.split('-') index = int(index) attr = str(attr) if index in plots.keys(): plots[index][attr] = params[key].encode('utf-8') else: plots[index] = {} plots[index][attr] = params[key].encode('utf-8') for key in plots.keys(): cohort_ids = plots[key]['cohort_ids'].split(',') cohorts = Cohort.objects.filter(id__in=cohort_ids) plot, created = Plot.objects.update_or_create(id=key, defaults={'visualization':viz, 'title':plots[key]['name'], 'x_axis':plots[key]['x_axis'], 'y_axis':plots[key]['y_axis'], 'color_by':plots[key]['color_by'], 'plot_type':plots[key]['plot_type']}) plot_cohort_list = [] Plot_Cohorts.objects.filter(plot=plot).delete() for cohort in cohorts: plot_cohort_list.append(Plot_Cohorts(plot=plot, cohort=cohort)) Plot_Cohorts.objects.bulk_create(plot_cohort_list) # Create and save permissions perm = Viz_Perms(visualization=viz, user=request.user, perm=Viz_Perms.OWNER) perm.save() messages.info(request, 'Visualization, %s, saved successfully.' % viz.name) # return redirect(reverse(redirect_url, args=[viz.id])) return redirect(redirect_url)