def test_get(): r = registry.Registry() r.register(DemoReport) assert r.get(fqn(DemoReport), None) == DemoReport assert r.get("Wrong", None) is None assert r.get("Wrong", DemoReport) == DemoReport assert r[0] == r.reports[0] assert r[fqn(DemoReport)] == DemoReport assert r.choices() == [(fqn(DemoReport), fqn(DemoReport))]
def col_to_css_class(row_value): if isinstance(row_value, RowValue): return fqn(row_value.column).lower().replace('.', '_') elif isinstance(row_value, RowValueError): return str(row_value) else: raise ValueError('wrong value for `col_to_css_class` ({0}, {1})'.format(row_value, type(row_value)))
def monitor_model(model): version = _cache.get(EREPORTS_CACHE_MODELS_PREFIX % fqn(model)) if version is None: reset(model) post_save.connect(invalidate, sender=model, dispatch_uid="ereports_save_%s" % str(model)) post_delete.connect(invalidate, sender=model, dispatch_uid="ereports_delete_%s" % str(model))
def register(self, report_class): model = get_attr(report_class, 'datasource.model', None) if model is None: logger.error("Unable to monitor {0}".format(report_class)) elif model not in self.monitors: self.monitors.append(model) monitor_model(model) self.reports.append(_Entry(fqn(report_class), report_class))
def get_model_gen_part(self, model): """ returns a tuple composed by 'generation prefix', 'generation value' fro the passed model :param model: :return: """ prefix = fqn(model) return prefix, self.get_last_cache_version(model)
def __init__(self, **kwargs): self.extras = kwargs.pop('extras', {}) for key, value in kwargs.iteritems(): setattr(self, key, value) self.title = self.title or fqn(self)
def reset(model, **kwargs): _cache.set(EREPORTS_CACHE_MODELS_PREFIX % fqn(model), 0)
def invalidate(sender, **kwargs): try: _cache.incr(EREPORTS_CACHE_MODELS_PREFIX % fqn(sender), 1) except ValueError: reset(sender)
def get_last_cache_version(self, model): return _cache.get(EREPORTS_CACHE_MODELS_PREFIX % fqn(model), 0)