Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix bukuserver #546

Merged
merged 33 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
aeec058
fix(bukuserver.views): ftp on valid scheme
rachmadaniHaryono Jan 6, 2022
e4ba8a7
fix(BookmarkModelView): update
rachmadaniHaryono Jan 10, 2022
1d42014
refactor(bukuserver.views): black format and pylint
rachmadaniHaryono Jan 11, 2022
3a2c2ee
refactor(bukuserver.views): pyright
rachmadaniHaryono Jan 11, 2022
bb1d6d3
build(pre-commit): new hooks
rachmadaniHaryono Jan 12, 2022
44be0e9
refactor(bukuserver.views): isort
rachmadaniHaryono Jan 12, 2022
9db41b7
test(BookmarkModelView): simpler
rachmadaniHaryono Jan 12, 2022
71a1274
test(buku): update
rachmadaniHaryono Jan 13, 2022
16b88db
test(parse_tags): merge unittest
rachmadaniHaryono Jan 13, 2022
4380537
fix(bukuserver.views): use buku parse_tags
rachmadaniHaryono Jan 13, 2022
b0ca0f5
refactor(bukuserver.views): unify msg between log and flash
rachmadaniHaryono Jan 13, 2022
7eb9e00
build(pre-commit): autoupdate
rachmadaniHaryono Jan 13, 2022
dd77595
test(flake8): config
rachmadaniHaryono Jan 13, 2022
be0a9c8
Revert "test(flake8): config"
rachmadaniHaryono Jan 13, 2022
4bcd21f
test(flake8): config with tox.ini
rachmadaniHaryono Jan 13, 2022
08fc444
fix(bukuserver.__main__): import server
rachmadaniHaryono Jan 14, 2022
ea0c5cd
build(pre-commit): hook darker
rachmadaniHaryono Jan 14, 2022
ab14932
fix(ApiTagView): big tag list
rachmadaniHaryono Jan 14, 2022
bf7b223
refactor(test_buku): revert unrelated code
rachmadaniHaryono Jan 14, 2022
be08fb9
build(pre-commit): hook pylint
rachmadaniHaryono Jan 14, 2022
b655d62
refactor(search_tag): type hint and simpler variable
rachmadaniHaryono Jan 14, 2022
4ae5c08
fix(pylint): too-few-public-methods BookmarkletView
rachmadaniHaryono Jan 14, 2022
880c93c
fix(search_tag): no key on dic
rachmadaniHaryono Jan 14, 2022
9802ccc
fix(search_tag): simpler query
rachmadaniHaryono Jan 14, 2022
d9e9d20
feat(BookmarkModelView): handle magnet
rachmadaniHaryono Jan 14, 2022
a42c125
fix(BookmarkForm): remove url validators
rachmadaniHaryono Jan 14, 2022
8f318f1
fix(BookmarkModelView): don't check scheme manually
rachmadaniHaryono Jan 14, 2022
f1d11b4
fix(BookmarkModelView): dont get favicons if no netloc
rachmadaniHaryono Jan 14, 2022
af794a7
build(bukuserver): use fixed version of flask-reverse-proxy-fix
rachmadaniHaryono Jan 17, 2022
f9eb421
build(setup): add fork of flask-reverse-proxy-fix
rachmadaniHaryono Jan 18, 2022
db698e8
build: flask-reverse-proxy-fix based on pep508
rachmadaniHaryono Jan 18, 2022
715d0c9
fix(BookmarkModelView): use buku method when creating bookmark
rachmadaniHaryono Jan 21, 2022
ea6fdd8
fix(BookmarkModelView): dont render tag with netloc when no netloc
rachmadaniHaryono Jan 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
# - id: trailing-whitespace
# - id: end-of-file-fixer
# - id: check-yaml
- id: check-added-large-files
- repo: https://github.com/PyCQA/pylint/
rev: '2.6'
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
args: ['--rcfile', 'tests/.pylintrc']
# exclude: tests/functional/|tests/input|tests/extensions/data|tests/regrtest_data/|tests/data/|doc/
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
# - id: trailing-whitespace
# - id: end-of-file-fixer
# - id: check-yaml
- id: check-added-large-files
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort (python)
args: [--profile, black, --filter-files]
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.2.0
hooks:
- id: pretty-format-toml
args: [--autofix]
- id: pretty-format-yaml
args: [--autofix]
exclude: .copier-answers.yml
# based on
# https://gitlab.com/smop/pre-commit-hooks/-/blob/master/.pre-commit-hooks.yaml
- repo: https://github.com/akaihola/darker
rev: 1.3.2
hooks:
- id: darker
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
args: [-rn, -sn, --rcfile=tests/.pylintrc]
# "-rn", # Only display messages
# "-sn", # Don't display the score
# based on
# https://pylint.pycqa.org/en/latest/user_guide/pre-commit-integration.html
5 changes: 4 additions & 1 deletion bukuserver/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from . import server
try:
from . import server
except ImportError:
from bukuserver import server


if __name__ == '__main__':
Expand Down
5 changes: 2 additions & 3 deletions bukuserver/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Forms module."""
# pylint: disable=too-few-public-methods, missing-docstring
from flask_wtf import FlaskForm
import wtforms
from flask_wtf import FlaskForm


class SearchBookmarksForm(FlaskForm):
Expand All @@ -16,8 +16,7 @@ class HomeForm(SearchBookmarksForm):


class BookmarkForm(FlaskForm):
url = wtforms.StringField(
validators=[wtforms.validators.DataRequired(), wtforms.validators.URL(require_tld=False)])
url = wtforms.StringField(validators=[wtforms.validators.DataRequired()])
title = wtforms.StringField()
tags = wtforms.StringField()
description = wtforms.TextAreaField()
2 changes: 1 addition & 1 deletion bukuserver/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Flask-Admin>=1.5.1
Flask-API>=0.6.9
Flask-Bootstrap>=3.3.7.1
flask-paginate>=0.5.1
flask-reverse-proxy-fix>=0.2.1
flask-reverse-proxy-fix @ https://github.com/rachmadaniHaryono/flask-reverse-proxy-fix/archive/refs/tags/v0.2.2rc1.zip
Flask-WTF>=0.14.2
Flask>=1.0.2,<2.0
idna>=2.5
Expand Down
76 changes: 59 additions & 17 deletions bukuserver/server.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
#!/usr/bin/env python
# pylint: disable=wrong-import-order, ungrouped-imports
"""Server module."""
import collections
import os
import sys
import typing as T
from typing import Any, Dict, Union # NOQA; type: ignore
from unittest import mock
from urllib.parse import urlparse
import os
import sys

from buku import BukuDb, __version__, network_handler
from flask.cli import FlaskGroup
from flask.views import MethodView
from flask_admin import Admin
from flask_api import exceptions, FlaskAPI, status
from flask_api import FlaskAPI, exceptions, status
from flask_bootstrap import Bootstrap
from flask_paginate import Pagination, get_page_parameter, get_per_page_parameter

import buku
from buku import BukuDb, __version__, network_handler

try:
from flask_reverse_proxy_fix.middleware import ReverseProxyPrefixFix
except ImportError:
ReverseProxyPrefixFix = None
from markupsafe import Markup
import click
import flask
from flask import ( # type: ignore
__version__ as flask_version,
from flask import __version__ as flask_version # type: ignore
from flask import (
abort,
current_app,
flash,
Expand All @@ -32,11 +36,12 @@
request,
url_for,
)
from markupsafe import Markup

try:
from . import response, forms, views
from . import forms, response, views
except ImportError:
from bukuserver import response, forms, views
from bukuserver import forms, response, views


STATISTIC_DATA = None
Expand Down Expand Up @@ -301,19 +306,56 @@ def shell_context():
return app


def search_tag(
db: BukuDb, stag: T.Optional[str] = None, limit: T.Optional[int] = None
) -> T.Tuple[T.List[str], T.Dict[str, int]]:
"""search tag.

db:
buku db instance
stag:
search tag
limit:
positive integer limit

Returns
-------
tuple
list of unique tags sorted alphabetically and dictionary of tag and its usage count

Raises
------
ValueError
if limit is not positive
"""
if limit is not None and limit < 1:
raise ValueError("limit must be positive")
tags: T.Set[str] = set()
counter = collections.Counter()
query_list = ["SELECT DISTINCT tags , COUNT(tags) FROM bookmarks"]
if stag:
query_list.append("where tags LIKE :search_tag")
query_list.append("GROUP BY tags")
row: T.Tuple[str, int]
for row in db.cur.execute(" ".join(query_list), {"search_tag": f"%{stag}%"}):
for tag in row[0].strip(buku.DELIM).split(buku.DELIM):
if not tag:
continue
tags.add(tag)
counter[tag] += row[1]
return list(sorted(tags)), dict(counter.most_common(limit))


class ApiTagView(MethodView):

def get(self, tag: Union[str, None]):
def get(self, tag: T.Optional[str]):
bukudb = get_bukudb()
if tag is None:
tags = bukudb.get_tag_all()
result = {'tags': tags[0]}
return result
tags = bukudb.get_tag_all()
return {"tags": search_tag(db=bukudb, limit=5)[0]}
tags = search_tag(db=bukudb, stag=tag)
if tag not in tags[1]:
raise exceptions.NotFound()
res = dict(name=tag, usage_count=tags[1][tag])
return res
return dict(name=tag, usage_count=tags[1][tag])

def put(self, tag: str):
bukudb = get_bukudb()
Expand Down Expand Up @@ -554,7 +596,7 @@ def delete(self):
return res


class BookmarkletView(MethodView):
class BookmarkletView(MethodView): # pylint: disable=too-few-public-methods
def get(self):
url = request.args.get('url')
title = request.args.get('title')
Expand Down
Loading