def __new__(cls, name, bases, attrs): if name not in ('Model', '_Model'): # generate the default table name if 'coll_name' not in attrs: attrs.update(coll_name='{}s'.format(name.lower())) if '_id' not in attrs: attrs.update(_id=fields.objectid()) if attrs.get('use_ts', True): if 'created_at' not in attrs: attrs.update(created_at=fields.str()) if 'updated_at' not in attrs: attrs.update(updated_at=fields.str()) attrs.update(_db_fields=dict()) for k, v in attrs.iteritems(): if isinstance(v, BaseField): attrs['_db_fields'][k] = v v.name = k # generates the unique fields attrs.update(_unique_fields=list(), _index_fields=list()) if 'created_at' in attrs: attrs['_index_fields'].append(get_sort('created_at')) if 'updated_at' in attrs: attrs['_index_fields'].append(get_sort('updated_at desc')) for k, v in attrs['_db_fields'].iteritems(): if 'unique' in v.validators: uk = v.validators['unique'] if uk is True or uk is False: ukey = k else: ukey = uk attrs['_unique_fields'].append(ukey) if 'index' in v.validators: _idx = v.validators['index'] if _idx is True: idx = get_sort('{0} {1}'.format(k, _idx)) elif _idx.lower() in ('asc', 'desc'): idx = get_sort('{0} {1}'.format(k, _idx)) else: idx = get_sort(_idx) if idx: if isinstance(idx[0], list): attrs['_index_fields'].extend(idx) else: attrs['_index_fields'].append(idx) return type.__new__(cls, name, bases, attrs)
def find_one(cls, alias, model, id=None, **kwargs): if 'spec' in kwargs or 'filter' in kwargs: spec = kwargs.get('spec') if not spec: spec = kwargs.get('filter') else: if isinstance(id, basestring): try: id = ObjectId(id) except InvalidId: return None spec = dict(_id=id) fields = kwargs.get('fields') if not fields: fields = kwargs.get('projection') sort = get_sort(kwargs.get('sort')) skip = kwargs.get('skip', 0) kargs = dict(sort=sort, skip=skip) if fields: if PM3: kargs.update(projection=fields) else: kargs.update(fields=fields) doc = MongoBit._get_coll(alias, model).find_one(spec, **kargs) return model(**doc) if doc else None
def find(cls, alias, model, **kwargs): obj = cls() obj.alias = alias obj.__model = model spec = kwargs.get('spec') or None if not spec: spec = kwargs.get('filter') or None fields = kwargs.get('fields') or None if not fields: fields = kwargs.get('projection') or None sort = get_sort(kwargs.get('sort')) limit = kwargs.get('limit', 0) skip = kwargs.get('skip', 0) hint = kwargs.get('hint') coll_ = MongoBit._get_coll(alias, model) kargs = dict(sort=sort, limit=limit, skip=skip) if fields: if PM3: kargs.update(projection=fields) else: kargs.update(fields=fields) if hint: obj.__cursor = coll_.find(spec, **kargs).hint(hint) else: obj.__cursor = coll_.find(spec, **kargs) obj.__count = obj.__cursor.count() return obj
def find(cls, alias, model, **kwargs): obj = cls() obj.alias = alias obj.__model = model spec = kwargs.get("spec") or None fields = kwargs.get("fields") or None sort = get_sort(kwargs.get("sort")) limit = kwargs.get("limit", 0) skip = kwargs.get("skip", 0) hint = kwargs.get("hint") coll_ = MongoBit._get_coll(alias, model) if hint: obj.__cursor = coll_.find(spec, fields=fields, sort=sort, limit=limit, skip=skip).hint(hint) else: obj.__cursor = coll_.find(spec, fields=fields, sort=sort, limit=limit, skip=skip) obj.__count = obj.__cursor.count() return obj
def find_one(cls, alias, model, id=None, **kwargs): if "spec" in kwargs: spec = kwargs["spec"] else: if isinstance(id, basestring): try: id = ObjectId(id) except InvalidId: return None spec = dict(_id=id) fields = kwargs.get("fields") sort = get_sort(kwargs.get("sort")) skip = kwargs.get("skip", 0) doc = MongoBit._get_coll(alias, model).find_one(spec, fields=fields, sort=sort, skip=skip) return model(**doc) if doc else None
def user_topic_sort(): uid = request.args.get('uid') field = request.args.get('field') results = get_sort(uid,field) return results