From 74bbbdde2f2788e37fcdc81fba27e8434b60dbd1 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Mon, 27 May 2019 21:16:41 +0800 Subject: [PATCH 1/3] new: dev: disable favicon feature --- bukuserver/README.md | 1 + bukuserver/server.py | 1 + bukuserver/views.py | 15 ++++++++++----- tests/test_views.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/test_views.py diff --git a/bukuserver/README.md b/bukuserver/README.md index ee7a4a8..94dac77 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: `None`] | Note: `BUKUSERVER_` is the common prefix. diff --git a/bukuserver/server.py b/bukuserver/server.py index 2cc6a06..b79991f 100644 --- a/bukuserver/server.py +++ b/bukuserver/server.py @@ -221,6 +221,7 @@ 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) + app.config['BUKUSERVER_DISABLE_FAVICON'] = bool(os.getenv('BUKUSERVER_DISABLE_FAVICON', False)) 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 From db72d24af4128b9f9a8c31470d8f09e9375bc203 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Mon, 27 May 2019 21:23:26 +0800 Subject: [PATCH 2/3] chg: dev: better boolean conversion --- bukuserver/server.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bukuserver/server.py b/bukuserver/server.py index b79991f..cea0eeb 100644 --- a/bukuserver/server.py +++ b/bukuserver/server.py @@ -221,7 +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) - app.config['BUKUSERVER_DISABLE_FAVICON'] = bool(os.getenv('BUKUSERVER_DISABLE_FAVICON', False)) + 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() From ac6089214df792e6b1466c908505f41a669f2d78 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Mon, 27 May 2019 21:24:30 +0800 Subject: [PATCH 3/3] new: chg: disable favicon default --- bukuserver/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bukuserver/README.md b/bukuserver/README.md index 94dac77..c83bd88 100644 --- a/bukuserver/README.md +++ b/bukuserver/README.md @@ -56,7 +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: `None`] | +| DISABLE_FAVICON | disable favicon | boolean [default: `false`] | Note: `BUKUSERVER_` is the common prefix.