def index(): context = {} if request.method == 'POST': text = request.form.get('keyword', None) if text == '': flash('Empty search?', 'error') elif len(text) < 5: flash('Search text is too short, use at least 5 characters', 'error') else: results = search(db_session, text) if not results: flash('No results', 'info') else: # not sure if this len check is a good idea, I think it forces to load all items from db ? if len(results) > 500: flash('Too many results, displaying first 500', 'error') results = results[0:500] context['results'] = results return render_template('archive/archive.html', **context)
def cli_search(options): search_term = ' '.join(options.keywords) tags = options.tags sources = options.sources query = re.sub(r'[ \(\)]+', ' ', search_term).strip() table_data = [] with Session() as session: for archived_entry in plugin_archive.search(session, query, tags=tags, sources=sources): days_ago = (datetime.now() - archived_entry.added).days source_names = ', '.join([s.name for s in archived_entry.sources]) tag_names = ', '.join([t.name for t in archived_entry.tags]) table_data.append(['ID', str(archived_entry.id)]) table_data.append(['Title', archived_entry.title]) table_data.append(['Added', str(days_ago) + ' days ago']) table_data.append(['URL', archived_entry.url]) table_data.append(['Source(s)', source_names or 'N/A']) table_data.append(['Tag(s)', tag_names or 'N/A']) if archived_entry.description: table_data.append( ['Description', strip_html(archived_entry.description)]) table_data.append([]) if not table_data: console('No results found for search') return try: table = TerminalTable(options.table_type, table_data, wrap_columns=[1]) table.table.inner_heading_row_border = False console(table.output) except TerminalTableError as e: console('ERROR: %s' % str(e))