Skip to content

Commit

Permalink
Merge pull request #546 from rachmadaniHaryono/bugfix/ftp-link
Browse files Browse the repository at this point in the history
bugfix bukuserver
  • Loading branch information
jarun authored Jan 22, 2022
2 parents 9e9c7d1 + ea6fdd8 commit c7243ad
Show file tree
Hide file tree
Showing 10 changed files with 543 additions and 357 deletions.
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

0 comments on commit c7243ad

Please sign in to comment.