Merge pull request #391 from rachmadaniHaryono/feature/disable-favicon

Feature/disable favicon
This commit is contained in:
Mischievous Meerkat 2019-05-27 18:57:22 +05:30 committed by GitHub
commit 9aee5652f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 5 deletions

View File

@ -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.

View File

@ -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()

View File

@ -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 = '<img src="{}{}"/> '
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 = '<img src="{}{}"/> '
res = netloc_tmpl.format(
'http://www.google.com/s2/favicons?domain=', netloc)
title = model.title if model.title else '&lt;EMPTY TITLE&gt;'
if is_scheme_valid:
res += '<a href="{0.url}">{1}</a>'.format(model, title)

45
tests/test_views.py Normal file
View File

@ -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 = \
'<img src="http://www.google.com/s2/favicons?domain=example.com"/> '
res = inst._list_entry(None, model, 'Entry')
exp_res = \
(
'<a href="http://example.com">Example Domain</a><br/>'
'<a href="http://example.com">http://example.com</a><br/>'
'<a class="btn btn-default" '
'href="/bookmark/?flt2_url_netloc_match=example.com">netloc:example.com</a>'
'<a class="btn btn-default" href="/bookmark/?flt2_tags_contain=tags1">tags1</a>'
'<br/>randomdesc')
exp_res = ''.join([img_html, exp_res])
assert str(res) == exp_res