def save_design(request, form, type, design, explicit_save): """ save_design(request, form, type, design, explicit_save) -> SavedQuery A helper method to save the design: * If ``explicit_save``, then we save the data in the current design. * If the user clicked the submit button, we do NOT overwrite the current design. Instead, we create a new "auto" design (iff the user modified the data). This new design is named after the current design, with the AUTO_DESIGN_SUFFIX to signify that it's different. Need to return a SavedQuery because we may end up with a different one. Assumes that form.saveform is the SaveForm, and that it is valid. """ assert form.saveform.is_valid() if type == models.HQL: design_cls = beeswax.design.HQLdesign elif type == models.IMPALA: design_cls = beeswax.design.HQLdesign else: raise ValueError(_('Invalid design type %(type)s') % {'type': type}) old_design = design design_obj = design_cls(form) new_data = design_obj.dumps() # Auto save if (1) the user didn't click "save", and (2) the data is different. # Don't generate an auto-saved design if the user didn't change anything if explicit_save: design.name = form.saveform.cleaned_data['name'] design.desc = form.saveform.cleaned_data['desc'] design.is_auto = False elif new_data != old_design.data: # Auto save iff the data is different if old_design.id is not None: # Clone iff the parent design isn't a new unsaved model design = old_design.clone() if not old_design.is_auto: design.name = old_design.name + models.SavedQuery.AUTO_DESIGN_SUFFIX else: design.name = models.SavedQuery.DEFAULT_NEW_DESIGN_NAME design.is_auto = True design.type = type design.data = new_data design.save() LOG.info('Saved %s design "%s" (id %s) for %s' % (explicit_save and '' or 'auto ', design.name, design.id, design.owner)) if design.doc.exists(): design.doc.update(name=design.name, description=design.desc) else: Document.objects.link(design, owner=design.owner, extra=design.type, name=design.name, description=design.desc) if design.is_auto: design.doc.get().add_to_history() return design
def save_design(request, form, type, design, explicit_save): """ save_design(request, form, type, design, explicit_save) -> SavedQuery A helper method to save the design: * If ``explicit_save``, then we save the data in the current design. * If the user clicked the submit button, we do NOT overwrite the current design. Instead, we create a new "auto" design (iff the user modified the data). This new design is named after the current design, with the AUTO_DESIGN_SUFFIX to signify that it's different. Need to return a SavedQuery because we may end up with a different one. Assumes that form.saveform is the SaveForm, and that it is valid. """ assert form.saveform.is_valid() if type == models.SavedQuery.HQL: design_cls = beeswax.design.HQLdesign elif type == models.SavedQuery.IMPALA: design_cls = beeswax.design.HQLdesign else: raise ValueError(_("Invalid design type %(type)s") % {"type": type}) old_design = design design_obj = design_cls(form) new_data = design_obj.dumps() # Auto save if (1) the user didn't click "save", and (2) the data is different. # Don't generate an auto-saved design if the user didn't change anything if explicit_save: design.name = form.saveform.cleaned_data["name"] design.desc = form.saveform.cleaned_data["desc"] design.is_auto = False elif new_data != old_design.data: # Auto save iff the data is different if old_design.id is not None: # Clone iff the parent design isn't a new unsaved model design = old_design.clone() if not old_design.is_auto: design.name = old_design.name + models.SavedQuery.AUTO_DESIGN_SUFFIX else: design.name = models.SavedQuery.DEFAULT_NEW_DESIGN_NAME design.is_auto = True design.type = type design.data = new_data design.save() LOG.info( 'Saved %s design "%s" (id %s) for %s' % (explicit_save and "" or "auto ", design.name, design.id, design.owner) ) if explicit_save: messages.info(request, _('Saved design "%(name)s"') % {"name": design.name}) # Design may now have a new/different id return design
def save_design(request, form, type, design, explicit_save): """ save_design(request, form, type, design, explicit_save) -> SavedQuery A helper method to save the design: * If ``explicit_save``, then we save the data in the current design. * If the user clicked the submit button, we do NOT overwrite the current design. Instead, we create a new "auto" design (iff the user modified the data). This new design is named after the current design, with the AUTO_DESIGN_SUFFIX to signify that it's different. Need to return a SavedQuery because we may end up with a different one. Assumes that form.saveform is the SaveForm, and that it is valid. """ assert form.saveform.is_valid() if type == models.HQL: design_cls = beeswax.design.HQLdesign elif type == models.IMPALA: design_cls = beeswax.design.HQLdesign else: raise ValueError(_('Invalid design type %(type)s') % {'type': type}) old_design = design design_obj = design_cls(form) new_data = design_obj.dumps() # Auto save if (1) the user didn't click "save", and (2) the data is different. # Don't generate an auto-saved design if the user didn't change anything if explicit_save: design.name = form.saveform.cleaned_data['name'] design.desc = form.saveform.cleaned_data['desc'] design.is_auto = False elif new_data != old_design.data: # Auto save iff the data is different if old_design.id is not None: # Clone iff the parent design isn't a new unsaved model design = old_design.clone() if not old_design.is_auto: design.name = old_design.name + models.SavedQuery.AUTO_DESIGN_SUFFIX else: design.name = models.SavedQuery.DEFAULT_NEW_DESIGN_NAME design.is_auto = True design.type = type design.data = new_data design.save() LOG.info('Saved %s design "%s" (id %s) for %s' % (explicit_save and '' or 'auto ', design.name, design.id, design.owner)) if explicit_save: messages.info(request, _('Saved design "%(name)s"') % {'name': design.name}) # Design may now have a new/different id return design
def _save_design(user, design, type_, design_obj, explicit_save, name=None, desc=None): # Design here means SavedQuery old_design = design new_data = design_obj.dumps() # Auto save if (1) the user didn't click "save", and (2) the data is different. # Create an history design if the user is executing a shared design. # Don't generate an auto-saved design if the user didn't change anything. if explicit_save and (not design.doc.exists() or design.doc.get().can_write_or_exception(user)): design.name = name design.desc = desc design.is_auto = False elif design_obj != old_design.get_design(): # Auto save iff the data is different if old_design.id is not None: # Clone iff the parent design isn't a new unsaved model design = old_design.clone(new_owner=user) if not old_design.is_auto: design.name = old_design.name + models.SavedQuery.AUTO_DESIGN_SUFFIX else: design.name = models.SavedQuery.DEFAULT_NEW_DESIGN_NAME design.is_auto = True design.name = design.name[:64] design.type = type_ design.data = new_data design.save() LOG.info('Saved %s design "%s" (id %s) for %s' % (explicit_save and '' or 'auto ', design.name, design.id, design.owner)) if design.doc.exists(): design.doc.update(name=design.name, description=design.desc) else: Document.objects.link(design, owner=design.owner, extra=design.type, name=design.name, description=design.desc) if design.is_auto: design.doc.get().add_to_history() return design
def restore_design(request): if request.method == 'POST': ids = request.POST.getlist('designs_selection') designs = dict([(design_id, authorized_get_design(request, design_id)) for design_id in ids]) if None in designs.values(): LOG.error('Cannot restore non-existent design(s) %s' % ','.join([key for key, name in designs.items() if name is None])) return list_designs(request) for design in designs.values(): design.is_trashed = False design.save() return redirect(reverse(get_app_name(request) + ':list_designs')) else: return render('confirm.html', request, dict(url=request.path, title=_('Restore design(s)?')))
def save_design_properties(request): response = {'status': -1, 'data': ''} try: if request.method != 'POST': raise PopupException(_('POST request required.')) design_id = request.POST.get('pk') design = authorized_get_design(request, design_id) field = request.POST.get('name') if field == 'name': design.name = request.POST.get('value') elif field == 'description': design.desc = request.POST.get('value') design.save() response['status'] = 0 except Exception, e: response['data'] = str(e)
def save_design_properties(request): response = {"status": -1, "data": ""} try: if request.method != "POST": raise PopupException(_("POST request required.")) design_id = request.POST.get("pk") design = authorized_get_design(request, design_id) field = request.POST.get("name") if field == "name": design.name = request.POST.get("value") elif field == "description": design.desc = request.POST.get("value") design.save() response["status"] = 0 except Exception, e: response["data"] = str(e)
def _save_design(user, design, type_, design_obj, explicit_save, name=None, desc=None): # Design here means SavedQuery old_design = design new_data = design_obj.dumps() # Auto save if (1) the user didn't click "save", and (2) the data is different. # Create an history design if the user is executing a shared design. # Don't generate an auto-saved design if the user didn't change anything. if explicit_save and (not design.doc.exists() or design.doc.get().can_write_or_exception(user)): design.name = name design.desc = desc design.is_auto = False elif design_obj != old_design.get_design(): # Auto save iff the data is different if old_design.id is not None: # Clone iff the parent design isn't a new unsaved model design = old_design.clone(new_owner=user) if not old_design.is_auto: design.name = old_design.name + models.SavedQuery.AUTO_DESIGN_SUFFIX else: design.name = models.SavedQuery.DEFAULT_NEW_DESIGN_NAME design.is_auto = True design.name = design.name[:64] design.type = type_ design.data = new_data design.save() LOG.info( 'Saved %s design "%s" (id %s) for %s' % (explicit_save and "" or "auto ", design.name, design.id, design.owner) ) if design.doc.exists(): design.doc.update(name=design.name, description=design.desc) else: Document.objects.link(design, owner=design.owner, extra=design.type, name=design.name, description=design.desc) if design.is_auto: design.doc.get().add_to_history() return design
def delete_design(request): if request.method == "POST": ids = request.POST.getlist("designs_selection") designs = dict([(design_id, authorized_get_design(request, design_id)) for design_id in ids]) if None in designs.values(): LOG.error( "Cannot delete non-existent design(s) %s" % ",".join([key for key, name in designs.items() if name is None]) ) return list_designs(request) for design in designs.values(): if request.POST.get("skipTrash", "false") == "false": design.is_trashed = True design.save() else: design.delete() return redirect(reverse(get_app_name(request) + ":list_designs")) else: return render("confirm.html", request, dict(url=request.path, title=_("Delete design(s)?")))
def save_design(request, form, type_, design, explicit_save): """ save_design(request, form, type_, design, explicit_save) -> SavedQuery A helper method to save the design: * If ``explicit_save``, then we save the data in the current design. * If the user clicked the submit button, we do NOT overwrite the current design. Instead, we create a new "auto" design (iff the user modified the data). This new design is named after the current design, with the AUTO_DESIGN_SUFFIX to signify that it's different. Need to return a SavedQuery because we may end up with a different one. Assumes that form.saveform is the SaveForm, and that it is valid. """ assert form.saveform.is_valid() sub_design_form = form # Beeswax/Impala case if type_ == models.HQL: design_cls = beeswax.design.HQLdesign elif type_ == models.IMPALA: design_cls = beeswax.design.HQLdesign elif type_ == models.SPARK: from spark.design import SparkDesign design_cls = SparkDesign sub_design_form = form.query else: raise ValueError(_('Invalid design type %(type)s') % {'type': type_}) # Design here means SavedQuery old_design = design design_obj = design_cls(sub_design_form, query_type=type_) new_data = design_obj.dumps() # Auto save if (1) the user didn't click "save", and (2) the data is different. # Don't generate an auto-saved design if the user didn't change anything if explicit_save: design.name = form.saveform.cleaned_data['name'] design.desc = form.saveform.cleaned_data['desc'] design.is_auto = False elif design_obj != old_design.get_design(): # Auto save iff the data is different if old_design.id is not None: # Clone iff the parent design isn't a new unsaved model design = old_design.clone() if not old_design.is_auto: design.name = old_design.name + models.SavedQuery.AUTO_DESIGN_SUFFIX else: design.name = models.SavedQuery.DEFAULT_NEW_DESIGN_NAME design.is_auto = True design.type = type_ design.data = new_data design.save() LOG.info('Saved %s design "%s" (id %s) for %s' % (explicit_save and '' or 'auto ', design.name, design.id, design.owner)) if design.doc.exists(): design.doc.update(name=design.name, description=design.desc) else: Document.objects.link(design, owner=design.owner, extra=design.type, name=design.name, description=design.desc) if design.is_auto: design.doc.get().add_to_history() return design