def search_by_files(keywords, content_type, offset=0, limit=0, use_tags=True): files = current_app.db.Document.find(content_type) if not keywords: results = files else: results = [] if isinstance(keywords, str): keywords = keywords.split() elif not isinstance(keywords, list): keywords = [] def _search_match(keyword, f): if keyword in f['_keywords'] and f['status']: return True return False results = files for kw in keywords: results = [f for f in results if _search_match(kw, f)] limit = parse_int(limit, 1, 1) offset = parse_int(offset, 0, 0) return results[offset:offset + limit], len(results)
def index(): paged = parse_int(request.args.get('paged'), 1, 1) files = current_app.db.Media.find() limit = current_app.db.Media.MAXIMUM_QUERY offset = max(limit * (paged - 1), 0) total_count = len(files) max_pages = max(int(math.ceil(total_count / float(limit))), 1) has_next = paged < max_pages has_previous = paged > 1 mediafiles = [f.info for f in files[offset:offset + limit]] uploads_url = act.get_uploads_url() for media in mediafiles: media['src'] = '{}/{}'.format(uploads_url, media['filename']) prev_url = act.url_as(request.endpoint, paged=max(paged - 1, 1)) next_url = act.url_as(request.endpoint, paged=min(paged + 1, max_pages)) paginator = { 'next': next_url if has_next else None, 'prev': prev_url if has_previous else None, 'paged': paged, } return render_template('mediafiles.html', mediafiles=mediafiles, p=paginator)
def straw(raw_list, value, key='id', recursive_key='nodes', limit=60): """return a item matched with key/value form a list. some_page = straw(pages, some_id, key='id', recursive_key='nodes', limit=600) """ if not isinstance(key, str): key = 'id' if not isinstance(recursive_key, str): recursive_key = None limit = min(parse_int(limit), 600) def _find(items, level=0): if not isinstance(items, list) or level > 2: return None for item in items[:limit]: if item.get(key) == value: return item if recursive_key and item.get(recursive_key): node = _find(item[recursive_key], level + 1) if node: return node return None return _find(raw_list, 0)
def add_site_menu_node(menu_key): parent_key = request.args.get('parent_key', '') name = request.form.get('name', '') link = request.form.get('link', '') target = request.form.get('target', '') fixed = request.form.get('fixed', '') path_scope = request.form.get('path_scope', '') css = request.form.get('css', '') pos = request.form.get('pos', 0) _index = parse_int(pos) new_node = { 'key': process_slug(name), 'name': name, 'link': link, 'target': target, 'path_scope': path_scope, 'fixed': bool(fixed), 'class': css } site = current_app.db.Site() nodes = site['menus'].get(menu_key, []) if parent_key: parent = _find_parent_node(nodes, parent_key) parent['nodes'].insert(_index, new_node) else: nodes.insert(_index, new_node) site.save() flash('SAVED') return_url = act.url_as('.site') return redirect(return_url)
def repository(): offset = parse_int(request.args.get('offset'), 0) files = current_app.db.Media.find_images() offset = parse_int(offset, 0) limit = current_app.db.Media.MAXIMUM_QUERY count = len(files) mediafiles = [f.info for f in files[offset:offset + limit]] has_more = offset + limit < count uploads_url = act.get_uploads_url() for media in mediafiles: media['src'] = '{}/{}'.format(uploads_url, media['filename']) media['_more'] = has_more media['_count'] = count return mediafiles
def filter_column_offset(data, pattern=None, column=4, row_columns=12): row_columns = parse_int(row_columns, 12, 0) column = parse_int(column, 4, True) if isinstance(pattern, str): if '{}' not in pattern: pattern += '{}' else: pattern = None length = len(data) if isinstance(data, list) else parse_int(data, 0, 0) offset = int((row_columns - length * column) / 2) if pattern: if offset > 0: output = pattern.format(offset) else: output = '' else: output = offset return output
def update_configure(): locale = request.form.get('locale', 'en_US') acc_mode = request.form.get('acc_mode', 0) acc_url = request.form.get('acc_url', '') configure = g.configure configure['locale'] = locale.replace('-', '_') configure['acc_mode'] = parse_int(acc_mode) configure['acc_url'] = str(acc_url) configure.save() flash('SAVED') return_url = act.url_as('.configuration') return redirect(return_url)
def update_term(term_key): name = request.form.get('name', '') caption = request.form.get('caption', '') figure = request.form.get('figure', '') parent = request.form.get('parent', '') priority = request.form.get('priority', 1) status = request.form.get('status', '') site = current_app.db.Site() site.update_category_term(term_key, { 'meta': { 'name': str(name), 'caption': str(caption), 'figure': str(figure), }, 'parent': str(parent), 'priority': parse_int(priority, 1), 'status': parse_int(status), }) site.save() flash('SAVED') return_url = act.url_as('.index') return redirect(return_url)
def _save_document(document, meta, content): tags = meta.pop('tags', []) terms = meta.pop('terms', []) date = meta.pop('date', '') parent = meta.pop('parent', '') template = meta.pop('template', '') redirect_url = meta.pop('redirect', '') priority = meta.pop('priority', 1) status = meta.pop('status') title = meta.pop('title', '') description = meta.pop('description', '') featured_img_src = meta.pop('featured_img', '') document['meta'] = {k: v for k, v in meta.items() if k} # clear no key. document['meta'].update({ 'title': title, 'description': description, 'featured_img': { 'src': featured_img_src } }) document['date'] = date document['tags'] = [tag.strip() for tag in tags if tag and isinstance(tag, str)] document['terms'] = [term.strip() for term in terms if term and isinstance(term, str)] document['template'] = template document['parent'] = parent document['redirect'] = redirect_url document['priority'] = parse_int(priority, 1) document['status'] = parse_int(status) document['content'] = content document.save() return document
def query_by_files(content_type=None, attrs=None, term=None, tag=None, offset=0, limit=1, sortby=None): # query files = _query(content_type=content_type, attrs=attrs, term=term, tag=tag) total_count = len(files) # sorting sorting = _sorting(files, parse_sortby(sortby)) limit = parse_int(limit, 1, 1) offset = parse_int(offset, 0, 0) if sorting: ids = [item['_id'] for item in sorting[offset:offset + limit]] order_dict = {_id: index for index, _id in enumerate(ids)} files = [f for f in files if f['_id'] in ids] files.sort(key=lambda x: order_dict[x['_id']]) else: files = files[offset:offset + limit] return files, total_count
def index(content_type): paged = parse_int(request.args.get('paged'), 1, 1) curr_content_type = _find_content_type(content_type) files = current_app.db.Document.find(content_type) limit = current_app.db.Media.MAXIMUM_QUERY offset = max(limit * (paged - 1), 0) total_count = len(files) max_pages = max(int(math.ceil(total_count / float(limit))), 1) has_next = paged < max_pages has_previous = paged > 1 contents = files[offset:offset + limit] for content in contents: content.url = act.gen_preview_url(content.content_type, content.slug) prev_url = act.url_as(request.endpoint, content_type=content_type, paged=max(paged - 1, 1)) next_url = act.url_as(request.endpoint, content_type=content_type, paged=min(paged + 1, max_pages)) paginator = { 'next': next_url if has_next else None, 'prev': prev_url if has_previous else None, 'paged': paged, } return render_template('content_files.html', contents=contents, content_type=curr_content_type, count=total_count, p=paginator)
def _query_contents(content_type=None, attrs=[], term=None, tag=None, paged=0, perpage=0, sortby=None, with_content=False): _check_query_limit('_query_contents', current_app.config.get('CONTENT_QUERY_LIMIT', 3)) curr_id = g.curr_file_id theme_meta = g.curr_app['theme_meta'] theme_opts = theme_meta.get('options', {}) # set default params if not content_type: content_type = current_app.db.Document.STATIC_TYPE if not sortby: sortby = theme_opts.get('sortby', 'updated') if not perpage: perpage = theme_opts.get('perpage') perpage = parse_int(perpage, 12, 1) paged = parse_int(paged, 1, 1) perpage = min(perpage, current_app.db.Document.MAXIMUM_QUERY) # position limit = perpage offset = max(perpage * (paged - 1), 0) # query content files results, total_count = query_by_files(content_type=content_type, attrs=attrs, term=term, tag=tag, offset=offset, limit=limit, sortby=sortby) pages = [] for p in results: p_content = p.get('content', '') p = parse_page_metas(p, curr_id) if with_content: p['content'] = parse_page_content(p_content) pages.append(p) run_hook('get_pages', pages=pages, current_page_id=curr_id) max_pages = max(int(math.ceil(total_count / float(perpage))), 1) page_range = [n for n in range(1, max_pages + 1)] pages = make_dotted_dict(pages) return { 'contents': pages, 'perpage': perpage, 'paged': paged, 'total_count': total_count, 'total_pages': max_pages, 'page_range': page_range, 'has_prev': paged > 1, 'has_next': paged < max_pages, }