def filter_communities(cls, p, so, with_deleted=False): """Search for communities. Helper function which takes from database only those communities which match search criteria. Uses parameter 'so' to set communities in the correct order. Parameter 'page' is introduced to restrict results and return only slice of them for the current page. If page == 0 function will return all communities that match the pattern. """ query = cls.query if with_deleted else \ cls.query.filter(cls.deleted_at.is_(None)) if p: query = query.filter(db.or_( cls.id.like("%" + p + "%"), cls.title.like("%" + p + "%"), cls.description.like("%" + p + "%"), )) if so in current_app.config['COMMUNITIES_SORTING_OPTIONS']: order = so == 'title' and db.asc or db.desc query = query.order_by(order(getattr(cls, so))) else: query = query.order_by(db.desc(cls.ranking)) return query
def filter_communities(cls, p, so, with_deleted=False): """Search for communities. Helper function which takes from database only those communities which match search criteria. Uses parameter 'so' to set communities in the correct order. Parameter 'page' is introduced to restrict results and return only slice of them for the current page. If page == 0 function will return all communities that match the pattern. """ query = cls.query if with_deleted else \ cls.query.filter(cls.deleted_at.is_(None)) if p: p = p.replace(' ', '%') query = query.filter(db.or_( cls.id.ilike('%' + p + '%'), cls.title.ilike('%' + p + '%'), cls.description.ilike('%' + p + '%'), )) if so in current_app.config['COMMUNITIES_SORTING_OPTIONS']: order = so == 'title' and db.asc or db.desc query = query.order_by(order(getattr(cls, so))) else: query = query.order_by(db.desc(cls.ranking)) return query
def query_by_action(cls, action): """Prepare query object with filtered action.""" query = cls.query.filter_by(action=action.value) if getattr(action, 'argument', None) is not None: query = query.filter(db.or_( cls.argument == str(action.argument), cls.argument.is_(None), )) else: query = query.filter(cls.argument.is_(None)) return query
def get_self_list(cls, node_path, community_id=None): """ Get index list info. :param node_path: Identifier of the index. :return: the list of index. """ if community_id: index = node_path.rfind('/') pid = node_path[index + 1:] from invenio_communities.models import Community community_obj = Community.get(community_id) recursive_t = cls.recs_query() query = db.session.query(recursive_t).filter( db.or_(recursive_t.c.cid == pid, recursive_t.c.pid == pid)) if not get_user_roles()[0]: query = query.filter(recursive_t.c.public_state) q = query.order_by(recursive_t.c.path).all() lst = list() if node_path != '0': for item in q: if item.cid == community_obj.root_node_id \ and item.pid == '0': lst.append(item) if item.pid != '0': lst.append(item) return lst else: index = node_path.rfind('/') pid = node_path[index + 1:] recursive_t = cls.recs_query() query = db.session.query(recursive_t).filter( db.or_(recursive_t.c.pid == pid, recursive_t.c.cid == pid)) if not get_user_roles()[0]: query = query.filter(recursive_t.c.public_state) q = query.order_by(recursive_t.c.path).all() return q
def get_self_list(cls, node_path): """ Get index list info. :param node_path: Identifier of the index. :return: the list of index. """ index = node_path.rfind('/') pid = node_path[index + 1:] recursive_t = cls.recs_query() q = db.session.query(recursive_t).filter( db.or_(recursive_t.c.pid == pid, recursive_t.c.cid == pid)). \ order_by(recursive_t.c.path).all() return q
def query_by_action(cls, action, argument=None): """Prepare query object with filtered action. :param action: The action to deny. :param argument: The action argument. If it's ``None`` then, if exists, the ``action.argument`` will be taken. In the worst case will be set as ``None``. (Default: ``None``) :returns: A query object. """ query = cls.query.filter_by(action=action.value) argument = argument or getattr(action, "argument", None) if argument is not None: query = query.filter(db.or_(cls.argument == str(argument), cls.argument.is_(None))) else: query = query.filter(cls.argument.is_(None)) return query
def get_child_list(cls, node_path): """ Get index list info. :param node_path: Identifier of the index. :return: the list of index. """ index = node_path.rfind('/') pid = node_path[index + 1:] recursive_t = cls.recs_query() query = db.session.query(recursive_t).filter( db.or_(recursive_t.c.pid == pid, recursive_t.c.cid == pid)) if not get_user_roles()[0]: query = query.filter(recursive_t.c.public_state) q = query.order_by(recursive_t.c.path).all() return q
def handle_not_found(exception, **extra): """Custom blueprint exception handler.""" assert isinstance(exception, NotFound) page = Page.query.filter( db.or_(Page.url == request.path, Page.url == request.path + "/")).first() if page: _add_url_rule(page.url) return render_template( [page.template_name, current_app.config['PAGES_DEFAULT_TEMPLATE']], page=page) elif 'wrapped' in extra: return extra['wrapped'](exception) else: return exception
def query_by_action(cls, action, argument=None): """Prepare query object with filtered action. :param action: The action to deny. :param argument: The action argument. If it's ``None`` then, if exists, the ``action.argument`` will be taken. In the worst case will be set as ``None``. (Default: ``None``) :returns: A query object. """ query = cls.query.filter_by(action=action.value) argument = argument or getattr(action, 'argument', None) if argument is not None: query = query.filter( db.or_( cls.argument == str(argument), cls.argument.is_(None), )) else: query = query.filter(cls.argument.is_(None)) return query
def handle_not_found(exception, **extra): """Custom blueprint exception handler.""" assert isinstance(exception, NotFound) page = Page.query.filter(db.or_(Page.url == request.path, Page.url == request.path + "/")).first() if page: _add_url_rule(page.url) return render_template( [ page.template_name, current_app.config['PAGES_DEFAULT_TEMPLATE'] ], page=page ) elif 'wrapped' in extra: return extra['wrapped'](exception) else: return exception
def query_by_group(cls, group_or_id, with_invitations=False, **kwargs): """Get a group's members.""" if isinstance(group_or_id, Group): id_group = group_or_id.id else: id_group = group_or_id if not with_invitations: return cls._filter( cls.query.filter_by(id_group=id_group), **kwargs ) else: return cls.query.filter( Membership.id_group == id_group, db.or_( Membership.state == MembershipState.PENDING_USER, Membership.state == MembershipState.ACTIVE ) )
def query_by_group(cls, group_or_id, with_invitations=False, **kwargs): """ Get a group's members. :param group_or_id: :param bool with_invitations: Verify if group has invitations. Default False. :returns: Group members. """ if isinstance(group_or_id, Group): id_group = group_or_id.id else: id_group = group_or_id if not with_invitations: return cls._filter(cls.query.filter_by(id_group=id_group), **kwargs) else: return cls.query.filter( Membership.id_group == id_group, db.or_(Membership.state == MembershipState.PENDING_USER, Membership.state == MembershipState.ACTIVE))