def jxDelete(request, **args): """ Deletes registers associated to form. In case more than one instance associated to form, button must define instances to delete. This operation is executed when "Delete" button is clicked on forms. Deletes the register by pk: dbInstance.objects.get(pk=myPk).delete() ** Attributes ** * ``request`` * ``**args`` """ logger.debug("jxDelete ...") logger.debug("jxDelete :: args: %s" % (args)) request.session.set_test_cookie() request.session.delete_test_cookie() if (request.REQUEST.has_key("action")) and request.is_ajax() == True: action = request.REQUEST["action"] logger.debug("action: %s" % (action)) if action == "delete": # resolve form, set to args['ctx'].form logger.debug("jxDelete :: form: %s" % (request.REQUEST["form"])) formId = request.REQUEST["form"] app = request.REQUEST["app"] logger.debug("jxDelete :: app: {}".format(app)) formModule = __import__(app, globals(), locals(), ["forms"], -1).forms classes = dir(formModule) resolvedForm = None for myClass in classes: try: formIdTarget = eval("formModule." + myClass + "._XP_FORM_ID") if formIdTarget == formId: resolvedForm = eval("formModule." + myClass) except AttributeError: pass logger.debug("jxDelete :: resolvedForm: %s" % (resolvedForm)) args["ctx"].form = resolvedForm(args["ctx"].post, ctx=args["ctx"]) args["ctx"].jsData = JsResultDict() # Instantiate form, validate form logger.debug("jxDelete :: post: %s" % (args["ctx"].post)) # instantiate form for create and update with db instances dbObjects from form # dbObjects : pk, model obj = CommonService(args["ctx"]) obj._set_main_form(args["ctx"].form) obj.request = request result = obj.delete() else: logger.debug("Invalid action name. Only save is allowed") raise Http404 else: logger.debug("Unvalid business request") raise Http404 return result
def jxSave(request, **args): """ Save register. Operation executed when clicking on "Save" button on forms. Saves all instances related to forms, included many to many relationships. ** Attributes ** * ``request`` * ``**args`` """ logger.debug("jxSave...") logger.debug(json.dumps(request.REQUEST.items())) request.session.set_test_cookie() request.session.delete_test_cookie() if (request.REQUEST.has_key("action")) and request.is_ajax() == True: action = request.REQUEST["action"] logger.debug("action: %s" % (action)) if action == "save": # resolve form, set to args['ctx'].form logger.debug("jxSave :: form: %s" % (request.REQUEST["form"])) formId = request.REQUEST["form"] app = request.REQUEST["app"] app_path = get_app_full_path(app) logger.debug("formId: {} app: {} app_path: {}".format(formId, app, app_path)) formModule = getattr(getattr(__import__(app_path + ".forms"), app_path.split(".")[1]), "forms") logger.debug("formModule: {}".format(formModule)) classes = dir(formModule) resolvedForm = None for myClass in classes: try: formIdTarget = eval("formModule." + myClass + "._XP_FORM_ID") if formIdTarget == formId: resolvedForm = eval("formModule." + myClass) except AttributeError: pass logger.debug("jxSave :: resolvedForm: %s" % (resolvedForm)) # Instantiate form, validate form logger.debug("jxSave :: post: %s" % (args["ctx"].post)) # instantiate form for create and update with db instances dbObjects from form # dbObjects : pk, model instances = {} dbObjects = json.loads(args["ctx"].post["dbObjects"].replace("'", '"')) logger.debug("jxSave :: dbObjects: %s" % (dbObjects)) # TODO: In case we support more masters than 'default', resolve appropiate master db name for key in dbObjects: # Get instance model by pk impl = dbObjects[key]["impl"] cls = get_class(impl) instances[key] = cls.objects.using("default").get(pk=dbObjects[key]["pk"]) logger.debug("jxSave :: instances. %s" % (instances)) if len(instances) == 0: args["ctx"].form = resolvedForm(args["ctx"].post, ctx=args["ctx"]) else: args["ctx"].form = resolvedForm(args["ctx"].post, ctx=args["ctx"], instances=instances) logger.debug("jxSave :: instantiated form") args["ctx"].jsData = JsResultDict() isFormValid = args["ctx"].form.is_valid() # isFormValid = False logger.debug("jxSave :: isFormValid: %s" % (isFormValid)) obj = CommonService(args["ctx"]) obj.request = request if isFormValid == True: logger.debug("jxSave :: Form is valid!!!") obj._set_main_form(args["ctx"].form) result = obj.save() else: if settings.DEBUG == True: logger.debug("Validation error!!!!!") logger.debug(args["ctx"].form.errors) if args["ctx"].form.errors.has_key("invalid"): logger.debug(args["ctx"].form.errors["invalid"]) traceback.print_exc() if args["ctx"].form.errors.has_key("invalid"): errorDict = {"": args["ctx"].form.errors["invalid"][0]} logger.debug("errorDict: %s" % (errorDict)) result = obj._buildJSONResult(obj._getErrorResultDict(errorDict, pageError=True)) else: # Build errordict errorDict = {} for field in args["ctx"].form.errors: if field != "__all__": errorDict[field] = args["ctx"].form.errors[field][0] logger.debug("errorDict: %s" % (errorDict)) result = obj._buildJSONResult(obj._getErrorResultDict(errorDict, pageError=False)) return result else: logger.debug("Invalid action name. Only save is allowed") raise Http404 else: logger.debug("Unvalid business request") raise Http404 return result