def remove(self, **kwargs): kwargs.setdefault('w', 1) return MongoBit.remove(alias=self.__class__._db_alias, model=self.__class__, spec=dict(_id=self.id), **kwargs )
def insert_doc(self, **kwargs): kwargs.setdefault('w', 1) skip, update_ts, is_valid = self.pre_action(**kwargs) if not is_valid: return if 'updated_at' in self.__class__._db_fields: if update_ts is not False: self.update(updated_at=strftime(time_fmt)) return MongoBit.insert(alias=self.__class__._db_alias, model=self.__class__, doc=self.get_clear_fields(), **kwargs )
def update_doc(self, **kwargs): kwargs.setdefault('w', 1) up_doc = self.get_update_doc(**kwargs) if '$set' in up_doc: self.update(up_doc['$set']) skip, update_ts, is_valid = self.pre_action(**kwargs) if not is_valid: return if up_doc: if 'updated_at' in self.__class__._db_fields: if update_ts is not False: if '$set' not in up_doc: up_doc['$set'] = dict(updated_at=strftime(time_fmt)) else: up_doc['$set'].update(updated_at=strftime(time_fmt)) return MongoBit.update(alias=self.__class__._db_alias, model=self.__class__, spec=dict(_id=self.id), up_doc=up_doc, **kwargs )
def find(cls, **kwargs): paginate = kwargs.get('paginate', False) if paginate is False: return MongoBit.find(cls._db_alias, cls, **kwargs) from flask import request, current_app from flask.ext.paginate import Pagination if hasattr(current_app, 'y18n'): t = current_app.y18n.t else: t = None page1 = int(kwargs.get('page', 1)) page2 = int(request.view_args.get('page', 1)) page3 = int(request.args.get('page', 1)) page = max((page1, page2, page3)) if 'per_page' in kwargs: per_page = kwargs['per_page'] elif 'PER_PAGE' in current_app.config: per_page = current_app.config['PER_PAGE'] else: per_page = 10 if 'link_size' in kwargs: link_size = kwargs['link_size'] elif 'LINK_SIZE' in current_app.config: link_size = current_app.config['LINK_SIZE'] else: link_size = '' if 'link_align' in kwargs: alignment = kwargs['link_align'] elif 'LINK_ALIGN' in current_app.config: alignment = current_app.config['LINK_ALIGN'] else: alignment = '' bs_version = kwargs.get('bs_version') or \ current_app.config.get('BS_VERSION') or 2 css_framework = kwargs.get('css_framework') or \ current_app.config.get('CSS_FRAMEWORK') or 'bootstrap' skip = (page - 1) * per_page kwargs.update(limit=per_page, skip=skip) objs = MongoBit.find(cls._db_alias, cls, **kwargs) total = kwargs.get('total', 'all') if total == 'all': total = cls.total_count() elif total == 'docs': total = objs.count args = dict(page=page, per_page=per_page, inner_window=kwargs.get('inner_window', 2), outer_window=kwargs.get('outer_window', 1), prev_label=kwargs.get('prev_label'), next_label=kwargs.get('next_label'), search=kwargs.get('search', False), total=total, display_msg=kwargs.get('display_msg'), search_msg=kwargs.get('search_msg'), record_name=kwargs.get('record_name'), link_size=link_size, alignment=alignment, bs_version=bs_version, css_framework=css_framework, ) if t: for k in ('display_msg', 'search_msg', 'prev_label', 'next_label', 'record_name'): if not args[k]: args[k] = t(k) objs.pagination = Pagination(found=objs.count, **args) objs.skip = skip return objs
def find_one(cls, id=None, **kwargs): return MongoBit.find_one(cls._db_alias, cls, id=id, **kwargs)
def distinct(cls, field, spec=None): return MongoBit.distinct(cls._db_alias, cls, field, spec=spec)
def get_count(cls, spec=None): return MongoBit.get_count(cls._db_alias, cls, spec)
def total_count(cls): return MongoBit.get_total_count(cls._db_alias, cls)