def handle(self, updates_filename, *args, **options): argv = [] check_consistency = False kwarg = {} logging.basicConfig(stream=sys.stderr, level=((5 - int(options['verbosity'])) * 10)) updatures.UPD_LOG = logging.getLogger("updatures") updatures.UPD_LOG.setLevel((5 - (int(options['verbosity']))) * 10) logging_curr = updatures.UPD_LOG.getEffectiveLevel() updatures.UPD_LOG.error("LOGGING LEVEL TEST: %d" % logging_curr) kwarg['sort_output'] = options['sort'] if options['fakeold']: kwarg['fakeold'] = options['fakeold'] argv += [ updates_filename ] for k,v in MODELS_DESCR.iteritems(): if not v.refs: continue for kr,r in v.refs.iteritems(): if r.is_many: updatures.UPD_LOG.error("model: %s, field %s is_many" % (k, kr) ) updatures_app(argv, **kwarg)
def update_pk(updates_gr, updatesk_gr, model, item, maxpks, new_pk): """ updates_gr grouped updates items updatesk_gr grouped updates items (dict with keys) model item model item item to be updated maxpks dict of maximum numeric pk reached new_pk new key """ md = MODELS_DESCR[model] if md.pk_natural: return False if item['pk'] == new_pk: return True # search a previous defined item with the same pk for item_same_pk in updates_gr[model]: if item_same_pk['pk'] == new_pk: old_pk = item['pk'] item['pk'] = maxpks[model]['maxpk'] + 1 update_pk(updates_gr, updatesk_gr, model, item_same_pk, maxpks, maxpks[model]['maxpk']) item['pk'] = old_pk break else: item_same_pk = None # update all references if md.natural is None: for ref_model, ref_md in MODELS_DESCR.iteritems(): # found each model has a refs value associated with # the current item model for ref_reffield, ref in ref_md.refs.iteritems(): if isinstance(ref.model, types.FunctionType): continue if ref.model != model: continue updatures.UPD_LOG.debug("MDREF: %s" % ref_model) for itemod in updates_gr[ref_model]: # if field not set or empty list continue ref_value = get_value(itemod, ref_reffield) if not ref_value: continue updatures.UPD_LOG.debug("ITEMOD: %s" % itemod) if ref.is_many: if type(ref_value[0]) is list: updatures.UPD_LOG.debug( "itemod list of lists case not managed") sys.exit(10) for i, pk in enumerate(ref_value): if pk == item['pk']: ref_value[i] = new_pk break else: if ref_value == item['pk']: updatures.UPD_LOG.debug("UPDATE KEY: %s" % itemod) set_value(itemod, ref_reffield, new_pk) # remove the item from the key based list of items updatesk_gr[model].pop(key_get(md, item)) # update the pk with the new value item['pk'] = new_pk # add the item again to the key based list of items updatesk_gr[model][key_get(md, item)] = item ret = True for backinhe in item.get('__backinhe__', []): if ret: update_pk(updates_gr, updatesk_gr, backinhe.model_descr.name, backinhe.item, maxpks, new_pk) # if another item had the same pk value of new_pk before swap it # with the old pk value of the updated item if item_same_pk is not None: if ret: ret = update_pk(updates_gr, updatesk_gr, model, item_same_pk, maxpks, old_pk) return ret
def update_pk(updates_gr, updatesk_gr, model, item, maxpks, new_pk): """ updates_gr grouped updates items updatesk_gr grouped updates items (dict with keys) model item model item item to be updated maxpks dict of maximum numeric pk reached new_pk new key """ md = MODELS_DESCR[model] if md.pk_natural: return False if item['pk'] == new_pk: return True # search a previous defined item with the same pk for item_same_pk in updates_gr[model]: if item_same_pk['pk'] == new_pk: old_pk = item['pk'] item['pk'] = maxpks[model]['maxpk'] + 1 update_pk(updates_gr, updatesk_gr, model, item_same_pk, maxpks, maxpks[model]['maxpk']) item['pk'] = old_pk break else: item_same_pk = None # update all references if md.natural is None: for ref_model, ref_md in MODELS_DESCR.iteritems(): # found each model has a refs value associated with # the current item model for ref_reffield, ref in ref_md.refs.iteritems(): if isinstance(ref.model, types.FunctionType): continue if ref.model != model: continue updatures.UPD_LOG.debug("MDREF: %s" % ref_model) for itemod in updates_gr[ref_model]: # if field not set or empty list continue ref_value = get_value(itemod, ref_reffield) if not ref_value: continue updatures.UPD_LOG.debug("ITEMOD: %s" % itemod) if ref.is_many: if type(ref_value[0]) is list: updatures.UPD_LOG.debug("itemod list of lists case not managed") sys.exit(10) for i, pk in enumerate(ref_value): if pk == item['pk']: ref_value[i] = new_pk break else: if ref_value == item['pk']: updatures.UPD_LOG.debug("UPDATE KEY: %s" % itemod) set_value(itemod, ref_reffield, new_pk) # remove the item from the key based list of items updatesk_gr[model].pop(key_get(md, item)) # update the pk with the new value item['pk'] = new_pk # add the item again to the key based list of items updatesk_gr[model][key_get(md, item)] = item ret = True for backinhe in item.get('__backinhe__', []): if ret: update_pk(updates_gr, updatesk_gr, backinhe.model_descr.name, backinhe.item, maxpks, new_pk) # if another item had the same pk value of new_pk before swap it # with the old pk value of the updated item if item_same_pk is not None: if ret: ret = update_pk(updates_gr, updatesk_gr, model, item_same_pk, maxpks, old_pk) return ret
if (finals[i]['model'] > finals[e]['model'] or (finals[i]['model'] == finals[e]['model'] and finals[i]['pk'] > finals[e]['pk'])): tmp = finals[i] finals[i] = finals[e] finals[e] = tmp updatures.UPD_LOG.warning("FINAL: ") json.dump(finals, output, indent=4, sort_keys=True) output.write("\n") return 0 if __name__ == "__main__": for k, v in MODELS_DESCR.iteritems(): if not v.refs: continue for kr, r in v.refs.iteritems(): if r.is_many: updatures.UPD_LOG.info("model: %s, field %s is_many" % (k, kr)) argv = [] debug = 0 check_consistency = False kwarg = {} skipnext = False for idx in range(1, len(sys.argv)): arg = sys.argv[idx] if skipnext: skipnext = False continue
for e in range(i + 1, fin_n): if (finals[i]['model'] > finals[e]['model'] or (finals[i]['model'] == finals[e]['model'] and finals[i]['pk'] > finals[e]['pk'])): tmp = finals[i] finals[i] = finals[e] finals[e] = tmp updatures.UPD_LOG.warning("FINAL: ") json.dump(finals, output, indent=4, sort_keys=True) output.write("\n") return 0 if __name__ == "__main__": for k, v in MODELS_DESCR.iteritems(): if not v.refs: continue for kr, r in v.refs.iteritems(): if r.is_many: updatures.UPD_LOG.info("model: %s, field %s is_many" % (k, kr) ) argv = [] debug = 0 check_consistency = False kwarg = {} skipnext = False for idx in range(1, len(sys.argv)): arg = sys.argv[idx] if skipnext: skipnext = False continue