def test_filter_community(app, db, communities_for_filtering, user, case_modifier): """Test the community filter task.""" (comm0, comm1, comm2) = communities_for_filtering # Case insensitive results = Community.filter_communities(p=case_modifier('beautiful'), so='title').all() assert len(results) == 1 assert {c.id for c in results} == {comm0.id} # Keyword search results = Community.filter_communities(p=case_modifier('errors'), so='title').all() assert len(results) == 1 assert {c.id for c in results} == {comm2.id} # Partial keyword present results = Community.filter_communities(p=case_modifier('test'), so='title').all() assert len(results) == 3 assert {c.id for c in results} == {comm0.id, comm1.id, comm2.id} # Order matter results = Community.filter_communities( p=case_modifier('explicit implicit'), so='title').all() assert len(results) == 1 assert {c.id for c in results} == {comm0.id} results = Community.filter_communities( p=case_modifier('implicit explicit'), so='title').all() assert len(results) == 0 assert {c.id for c in results} == set()
def index(): """Index page with uploader and list of existing depositions.""" ctx = mycommunities_ctx() p = request.args.get('p', type=str) so = request.args.get('so', type=str) page = request.args.get('page', type=int, default=1) so = so or current_app.config.get('COMMUNITIES_DEFAULT_SORTING_OPTION') communities = Community.filter_communities(p, so).all() communities = [c for c in communities if _get_permission("communities-read", c).can() or DynamicPermission(ActionNeed('admin-access')).can()] featured_community = FeaturedCommunity.get_featured_or_none() form = SearchForm(p=p) per_page = 10 page = max(page, 1) p = Pagination(page, per_page, len(communities)) ctx.update({ 'r_from': max(p.per_page * (p.page - 1), 0), 'r_to': min(p.per_page * p.page, p.total_count), 'r_total': p.total_count, 'pagination': p, 'form': form, 'title': _('Communities'), 'communities': communities[per_page * (page - 1):per_page * page], 'featured_community': featured_community }) return render_template( "invenio_communities/index.html", **ctx )
def index(): """Index page with uploader and list of existing depositions.""" ctx = mycommunities_ctx() p = request.args.get('p', type=str) so = request.args.get('so', type=str) page = request.args.get('page', type=int, default=1) so = so or current_app.config.get('COMMUNITIES_DEFAULT_SORTING_OPTION') communities = Community.filter_communities(p, so) featured_community = FeaturedCommunity.get_featured_or_none() form = SearchForm(p=p) per_page = 10 page = max(page, 1) p = Pagination(page, per_page, communities.count()) ctx.update({ 'r_from': max(p.per_page * (p.page - 1), 0), 'r_to': min(p.per_page * p.page, p.total_count), 'r_total': p.total_count, 'pagination': p, 'form': form, 'title': _('Communities'), 'communities': communities.slice( per_page * (page - 1), per_page * page).all(), 'featured_community': featured_community, }) return render_template( "invenio_communities/index.html", **ctx )
def index(): """Index page with uploader and list of existing depositions.""" ctx = mycommunities_ctx() p = request.args.get('p', type=str) so = request.args.get('so', type=str) page = request.args.get('page', type=int, default=1) so = so or current_app.config.get('COMMUNITIES_DEFAULT_SORTING_OPTION') communities = Community.filter_communities(p, so) featured_community = FeaturedCommunity.get_featured_or_none() form = SearchForm(p=p) per_page = 10 page = max(page, 1) p = Pagination(page, per_page, communities.count()) ctx.update({ 'r_from': max(p.per_page * (p.page - 1), 0), 'r_to': min(p.per_page * p.page, p.total_count), 'r_total': p.total_count, 'pagination': p, 'form': form, 'title': _('Communities'), 'communities': communities.slice( per_page * (page - 1), per_page * page).all(), 'featured_community': featured_community, }) return render_template( current_app.config['COMMUNITIES_INDEX_TEMPLATE'], **ctx)
def test_filter_community(app, db, communities_for_filtering, user, case_modifier): """Test the community filter task.""" (comm0, comm1, comm2) = communities_for_filtering # Case insensitive results = Community.filter_communities( p=case_modifier('beautiful'), so='title').all() assert len(results) == 1 assert {c.id for c in results} == {comm0.id} # Keyword search results = Community.filter_communities( p=case_modifier('errors'), so='title').all() assert len(results) == 1 assert {c.id for c in results} == {comm2.id} # Partial keyword present results = Community.filter_communities( p=case_modifier('test'), so='title').all() assert len(results) == 3 assert {c.id for c in results} == {comm0.id, comm1.id, comm2.id} # Order matter results = Community.filter_communities( p=case_modifier('explicit implicit'), so='title').all() assert len(results) == 1 assert {c.id for c in results} == {comm0.id} results = Community.filter_communities( p=case_modifier('implicit explicit'), so='title').all() assert len(results) == 0 assert {c.id for c in results} == set()
def get(self, query, sort, page, size): """Get a list of all the communities. .. http:get:: /communities/(string:id) Returns a JSON list with all the communities. **Request**: .. sourcecode:: http GET /communities HTTP/1.1 Accept: application/json Content-Type: application/json Host: localhost:5000 :reqheader Content-Type: application/json **Response**: .. sourcecode:: http HTTP/1.0 200 OK Content-Length: 334 Content-Type: application/json [ { "id": "comm1" }, { "id": "comm2" } ] :resheader Content-Type: application/json :statuscode 200: no error """ urlkwargs = { 'q': query, 'sort': sort, 'size': size, } communities = Community.filter_communities(query, sort) page = communities.paginate(page, size) links = default_links_pagination_factory(page, urlkwargs) links_headers = map( lambda key: ('link', 'ref="{0}" href="{1}"'.format(key, links[key])), links) return self.make_response( page, headers=links_headers, links_item_factory=default_links_item_factory, page=page, urlkwargs=urlkwargs, links_pagination_factory=default_links_pagination_factory, )
def mycommunities_ctx(): """Helper method for return ctx used by many views.""" communities = Community.filter_communities("", "title").all() mycommunities = [c for c in communities if _get_permission("communities-read", c).can() or DynamicPermission(ActionNeed('admin-access')).can()] return { "mycommunities": mycommunities, "permission_admin": DynamicPermission(ActionNeed('admin-access')), "permission_cadmin": partial(_get_permission, "communities-admin"), "permission_curate": partial(_get_permission, "communities-curate"), "permission_manage": partial(_get_permission, "communities-manage"), "permission_read": partial(_get_permission, "communities-read"), }
def get(self, query, sort, page, size): """Get a list of all the communities. .. http:get:: /communities/(string:id) Returns a JSON list with all the communities. **Request**: .. sourcecode:: http GET /communities HTTP/1.1 Accept: application/json Content-Type: application/json Host: localhost:5000 :reqheader Content-Type: application/json **Response**: .. sourcecode:: http HTTP/1.0 200 OK Content-Length: 334 Content-Type: application/json [ { "id": "comm1" }, { "id": "comm2" } ] :resheader Content-Type: application/json :statuscode 200: no error """ urlkwargs = { 'q': query, 'sort': sort, 'size': size, } communities = Community.filter_communities(query, sort) page = communities.paginate(page, size) links = default_links_pagination_factory(page, urlkwargs) links_headers = map(lambda key: ('link', 'ref="{0}" href="{1}"'.format( key, links[key])), links) return self.make_response( page, headers=links_headers, links_item_factory=default_links_item_factory, page=page, urlkwargs=urlkwargs, links_pagination_factory=default_links_pagination_factory, )
def community_list(): """Index page with uploader and list of existing depositions.""" ctx = mycommunities_ctx() from weko_theme.utils import get_design_layout # Get the design for widget rendering render_page, render_widgets = get_design_layout( current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) p = request.args.get('p', type=str) so = request.args.get('so', type=str) page = request.args.get('page', type=int, default=1) so = so or current_app.config.get('COMMUNITIES_DEFAULT_SORTING_OPTION') communities = Community.filter_communities(p, so) featured_community = FeaturedCommunity.get_featured_or_none() form = SearchForm(p=p) per_page = 10 page = max(page, 1) p = Pagination(page, per_page, communities.count()) ctx.update({ 'r_from': max(p.per_page * (p.page - 1), 0), 'r_to': min(p.per_page * p.page, p.total_count), 'r_total': p.total_count, 'pagination': p, 'form': form, 'title': _('Communities'), 'communities': communities.slice(per_page * (page - 1), per_page * page).all(), 'featured_community': featured_community, }) return render_template('invenio_communities/communities_list.html', page=render_page, render_widgets=render_widgets, **ctx)