def all(cls): indexes = Env.config().get_schema().get(cls.__column_family(), 'indexes') print cls.__column_family() print Env.config().get_schema().get("Post") if "__global__" in indexes: return cls.by_index("__global__", "__global__") raise IndexNotDefined("Global (__global__) index is not defined")
def after_delete(self): indexes = Env.config().get_schema().get(self.__class__.__column_family(), 'indexes') db = Env.db_connection() for index in indexes: if index == "__global__": db.remove_from_index(indexes[index]['name'], index, self.pk()) else: db.remove_from_index(indexes[index]['name'], self[index], self.pk()) pass
def after_create(self): indexes = Env.config().get_schema().get(self.__class__.__column_family(), 'indexes') db = Env.db_connection() for index in indexes: if index == "__global__": db.add_to_index(indexes[index]['name'], index, self.pk(), indexes[index]['comparator']) else: db.add_to_index(indexes[index]['name'], self[index], self.pk(), indexes[index]['comparator']) pass
def by_index(cls, column, value): indexes = Env.config().get_schema().get(cls.__column_family(), 'indexes') db = Env.db_connection() if column in indexes: index = indexes[column] results = [] rows = db.get_by_index(cls.__column_family(), index['name'], value, bool(index['reversed'])) for row_key in rows: results.append(cls(rows[row_key], False, row_key)) return results raise IndexNotDefined("Index for %s is not defined" % column) pass
def __initialize_columns_from_schema(self): schema = Env.config().get_schema(self.__class__.__column_family(), "fields") for field in schema: if 'default' in schema[field]: self.__columns[field] = schema[field]['default'] else: self.__columns[field] = ""
def get(cls, pk): try: result = Env.db_connection().get(cls.__column_family(), pk) return cls(result, False, pk) except: return None pass
def __column_family(cls): if not cls.___column_family: column_family = Env.config().get_schema(cls.__name__, 'column_family') if not column_family: column_family = cls.__name__ cls.___column_family = column_family return cls.___column_family
def save(self): if not self.is_valid(): raise ModelNotValid() db = Env.db_connection() # before callbacks if self.is_new(): self.before_create() self.before_save() result = db.save(self.__class__.__column_family(), self.pk(), self.__columns) # after callbacks if self.is_new(): self.after_create() self.after_save() self.__new = False return result
def __init__(self, environment='development', flush_database=False): print "Initializing..." Env.setup("./config/%s.yml" % environment, './config/schema.yml') Env.db_connection().connect() if flush_database: Env.db_connection().flush() from app_controller.main_controller import MainController self.main = MainController() controllers = [os.path.splitext(os.path.basename(path))[0] for path in glob.glob("./app_controller/*_controller.py")] for module in controllers: if module == "main_controller": continue url_path = module.split("_")[0] class_name = "".join([word.capitalize() for word in module.split("_")]) exec("from app_controller.%s import %s" % (module, class_name)) exec("self.main.%s = %s()" % (url_path, class_name))
def pk(self): if not self.__pk: comparator = Env.config().get_schema(self.__class__.__column_family(), "key_type") self.__pk = getattr(Env.db_connection(), "generate_%s_pk" % comparator.lower())() return self.__pk
def by_range(cls): db = Env.db_connection() results = [] for row in db.get_by_range(cls.__column_family()): results.append(cls(row[1], False, row[0])) return results
def delete(self): self.before_delete() db = Env.db_connection() db.remove(self.__class__.__column_family(), self.pk()) self.__deleted = True self.after_delete()