diff --git a/bukuserver/README.md b/bukuserver/README.md index ee7a4a8..c83bd88 100644 --- a/bukuserver/README.md +++ b/bukuserver/README.md @@ -56,6 +56,7 @@ Following are available os env config available for bukuserver. | SECRET_KEY | server secret key | string [default: os.urandom(24)] | | URL_RENDER_MODE | url render mode | `full` or `netloc` [default: `full`] | | DB_FILE | full path to db file | path string [default: standard path for buku] | +| DISABLE_FAVICON | disable favicon | boolean [default: `false`] | Note: `BUKUSERVER_` is the common prefix. diff --git a/bukuserver/server.py b/bukuserver/server.py index 2cc6a06..cea0eeb 100644 --- a/bukuserver/server.py +++ b/bukuserver/server.py @@ -221,6 +221,9 @@ def create_app(db_file=None): url_render_mode = views.DEFAULT_URL_RENDER_MODE app.config['BUKUSERVER_URL_RENDER_MODE'] = url_render_mode app.config['SECRET_KEY'] = os.getenv('BUKUSERVER_SECRET_KEY') or os.urandom(24) + disable_favicon = os.getenv('BUKUSERVER_DISABLE_FAVICON', 'false') + app.config['BUKUSERVER_DISABLE_FAVICON'] = \ + False if disable_favicon.lower() in ['false', '0'] else bool(disable_favicon) app.config['BUKUSERVER_DB_FILE'] = os.getenv('BUKUSERVER_DB_FILE') or db_file bukudb = BukuDb(dbfile=app.config['BUKUSERVER_DB_FILE']) app.app_context().push() diff --git a/bukuserver/views.py b/bukuserver/views.py index 32cc513..4dd3bc6 100644 --- a/bukuserver/views.py +++ b/bukuserver/views.py @@ -1,11 +1,13 @@ """views module.""" +from argparse import Namespace from collections import Counter from types import SimpleNamespace +from typing import Any from urllib.parse import urlparse import itertools import logging -from flask import flash, redirect, request, url_for +from flask import current_app, flash, redirect, request, url_for from flask_admin.babel import gettext from flask_admin.base import AdminIndexView, BaseView, expose from flask_admin.model import BaseModelView @@ -75,7 +77,8 @@ class BookmarkModelView(BaseModelView): def _create_ajax_loader(self, name, options): pass - def _list_entry(self, context, model, name): + def _list_entry( + self, context: Any, model: Namespace, name: str) -> Markup: parsed_url = urlparse(model.url) netloc, scheme = parsed_url.netloc, parsed_url.scheme is_scheme_valid = scheme in ('http', 'https') @@ -91,9 +94,11 @@ class BookmarkModelView(BaseModelView): """.format( model, ''.join(tag_text), Markup.escape(model.url) )) - netloc_tmpl = ' ' - res = netloc_tmpl.format( - 'http://www.google.com/s2/favicons?domain=', netloc) + res = '' + if not current_app.config.get('BUKUSERVER_DISABLE_FAVICON', False): + netloc_tmpl = ' ' + res = netloc_tmpl.format( + 'http://www.google.com/s2/favicons?domain=', netloc) title = model.title if model.title else '<EMPTY TITLE>' if is_scheme_valid: res += '{1}'.format(model, title) diff --git a/tests/test_views.py b/tests/test_views.py new file mode 100644 index 0000000..c779250 --- /dev/null +++ b/tests/test_views.py @@ -0,0 +1,45 @@ +from argparse import Namespace + +from flask import current_app +import pytest + +from buku import BukuDb +from bukuserver import server +from bukuserver.views import BookmarkModelView + + +@pytest.fixture +def client(tmp_path): + test_db = tmp_path / 'test.db' + app = server.create_app(test_db.as_posix()) + app_context = app.test_request_context() + app_context.push() + client = app.test_client() + return client + + +@pytest.mark.parametrize('disable_favicon', [False, True]) +def test_bookmark_model_view(tmp_path, client, disable_favicon): + test_db = tmp_path / 'test.db' + bukudb = BukuDb(dbfile=test_db.as_posix()) + inst = BookmarkModelView(bukudb) + model = Namespace( + description='randomdesc', id=1, tags='tags1', + title='Example Domain', url='http://example.com') + # __import__('pdb').set_trace() + current_app.config['BUKUSERVER_DISABLE_FAVICON'] = disable_favicon + img_html = '' + if not disable_favicon: + img_html = \ + ' ' + res = inst._list_entry(None, model, 'Entry') + exp_res = \ + ( + 'Example Domain
' + 'http://example.com
' + 'netloc:example.com' + 'tags1' + '
randomdesc') + exp_res = ''.join([img_html, exp_res]) + assert str(res) == exp_res