8000 tests: uses black by rerowep · Pull Request #3674 · rero/rero-ils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

tests: uses black #3674

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

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion docker-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ services:
- "INVENIO_SECRET_KEY=CHANGE_ME"
- "INVENIO_SQLALCHEMY_DATABASE_URI=postgresql+psycopg2://rero-ils:rero-ils@db/rero-ils"
- "INVENIO_WSGI_PROXIES=2"
- "INVENIO_RATELIMIT_STORAGE_URL=redis://cache:6379/3"
- "INVENIO_RATELIMIT_STORAGE_URI=redis://cache:6379/3"
- "COLLECT_STORAGE='flask_collect.storage.file'"
lb:
build: ./docker/haproxy/
Expand Down
1,996 changes: 1,028 additions & 968 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ rero-invenio-files = ">=1.0.0,<2.0.0"
## Python packages development dependencies (order matters)
#----------------------------------------------------------
## Default from Invenio
pytest-black-ng = ">=0.4.0"
pytest-invenio = ">=2.1.6,<3.0.0"
pydocstyle = ">=6.1.1"
pytest-black = ">=0.3.2"
pytest-black-ng = ">=0.4.0"
Sphinx = ">=4.5.0"
Flask-Debugtoolbar = ">=0.10.1"
## RERO ILS specific python packages
Expand Down Expand Up @@ -468,6 +469,17 @@ server = {cmd = "./scripts/server", help = "Starts the server "}
setup = {cmd = "./scripts/setup", help = "Runs setup"}
update = {cmd = "./scripts/update", help = "Runs update"}

[tool.isort]
profile = "black"

[tool.pytest]
addopts = "--color=yes --black --isort --pydocstyle --doctest-glob=\"*.rst\" --doctest-modules --cov=rero_ils --cov-report=term-missing --ignore=setup.py --ignore=docs/conf.py --ignore=rero_ils/config.py -m \"not external\""
testpaths = "docs tests rero_ils"
# custom markers
markers = "external: mark a test as dealing with external services."
# not displaying all the PendingDeprecationWarnings from invenio
filterwarnings = "ignore::PendingDeprecationWarning"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
29 changes: 0 additions & 29 deletions pytest.ini

This file was deleted.

2 changes: 1 addition & 1 deletion rero_ils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

from .version import __version__

__all__ = ('__version__', )
__all__ = ("__version__",)
60 changes: 28 additions & 32 deletions rero_ils/accounts_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from flask_security.confirmable import requires_confirmation
from flask_security.utils import get_message, verify_and_update_password
from invenio_accounts.utils import change_user_password
from invenio_accounts.views.rest import \
ChangePasswordView as BaseChangePasswordView
from invenio_accounts.views.rest import ChangePasswordView as BaseChangePasswordView
from invenio_accounts.views.rest import LoginView as CoreLoginView
from invenio_accounts.views.rest import _abort, _commit, use_args, use_kwargs
from marshmallow import Schema, fields, validates, validates_schema
Expand All @@ -34,20 +33,18 @@

from rero_ils.modules.patrons.api import Patron, current_librarian
from rero_ils.modules.users.api import User
from rero_ils.modules.utils import PasswordValidatorException, \
password_validator
from rero_ils.modules.utils import PasswordValidatorException, password_validator

current_datastore = LocalProxy(
lambda: current_app.extensions['security'].datastore)
current_datastore = LocalProxy(lambda: current_app.extensions["security"].datastore)


#
# Field validators
#
def validate_password(password):
"""Validate the password."""
length = current_app.config.get('RERO_ILS_PASSWORD_MIN_LENGTH', 8)
special_char = current_app.config.get('RERO_ILS_PASSWORD_SPECIAL_CHAR')
length = current_app.config.get("RERO_ILS_PASSWORD_MIN_LENGTH", 8)
special_char = current_app.config.get("RERO_ILS_PASSWORD_SPECIAL_CHAR")
try:
password_validator(password, length=length, special_char=special_char)
except PasswordValidatorException as pve:
Expand All @@ -57,15 +54,15 @@ def validate_password(password):
def validate_passwords(password, confirm_password):
"""Validate that the 2 passwords are identical."""
if password != confirm_password:
raise ValidationError(_('The 2 passwords are not identical.'))
raise ValidationError(_("The 2 passwords are not identical."))


class LoginView(CoreLoginView):
"""invenio-accounts Login REST View."""

post_args = {
'email': fields.String(required=True),
'password': fields.String(required=True)
"email": fields.String(required=True),
"password": fields.String(required=True),
}

@classmethod
Expand All @@ -79,19 +76,19 @@ def post(self, **kwargs):
"""Verify and login a user."""
user = self.get_user(**kwargs)
if not user:
_abort(_('INVALID_USER_OR_PASSWORD'))
_abort(_("INVALID_USER_OR_PASSWORD"))
self.verify_login(user, **kwargs)
self.login_user(user)
return self.success_response(user)

def verify_login(self, user, password=None, **kwargs):
"""Verify the login via password."""
if not user.password or not verify_and_update_password(password, user):
_abort(_('INVALID_USER_OR_PASSWORD'))
_abort(_("INVALID_USER_OR_PASSWORD"))
if requires_confirmation(user):
_abort(get_message('CONFIRMATION_REQUIRED')[0])
_abort(get_message("CONFIRMATION_REQUIRED")[0])
if not user.is_active:
_abort(get_message('DISABLED_ACCOUNT')[0])
_abort(get_message("DISABLED_ACCOUNT")[0])


class PasswordPassword(Schema):
Expand All @@ -109,14 +106,13 @@ def validate_password(self, value):
@validates_schema
def validate_passwords(self, data, **kwargs):
"""Validate that the 2 passwords are identical."""
validate_passwords(data['new_password'], data['new_password_confirm'])
validate_passwords(data["new_password"], data["new_password_confirm"])


class UsernamePassword(Schema):
"""Args validation when a professional change a password for a user."""

username = fields.String(required=True,
validate=[validate.Length(min=1, max=128)])
username = fields.String(required=True, validate=[validate.Length(min=1, max=128)])
new_password = fields.String(required=True)
new_password_confirm = fields.String(required=True)

Expand All @@ -128,22 +124,22 @@ def validate_password(self, value):
@validates_schema
def validate_passwords(self, data, **kwargs):
"""Validate that the 2 passwords are identical."""
validate_passwords(data['new_password'], data['new_password_confirm'])
validate_passwords(data["new_password"], data["new_password_confirm"])


def make_password_schema(request):
"""Select the right args validation depending on the context."""
# Filter based on 'fields' query parameter
fields = request.args.get('fields', None)
class="x x-first x-last">',') if fields else None
fields = request.args.get("fields", None)
class="x x-first x-last">",") if fields else None
# Respect partial updates for PATCH requests
partial = request.method == 'PATCH'
if request.json.get('username'):
return UsernamePassword(>
partial=partial, context={"request": request})
partial = request.method == "PATCH"
if request.json.get("username"):
return UsernamePassword(
partial=partial, context={"request": request}
)
# Add current request to the schema's context
return PasswordPassword(>
partial=partial, context={"request": request})
return PasswordPassword( partial=partial, context={"request": request})


class ChangePasswordView(BaseChangePasswordView):
Expand All @@ -158,21 +154,21 @@ def verify_permission(self, username, **args):
patrons = Patron.get_patrons_by_user(user.user)
# logged user is not librarian or no patron account match the logged
# user organisation
if not current_librarian or current_librarian.organisation_pid not in \
[ptrn.organisation_pid for ptrn in patrons]:
if not current_librarian or current_librarian.organisation_pid not in [
ptrn.organisation_pid for ptrn in patrons
]:
return current_app.login_manager.unauthorized()

def change_password_for_user(self, username, new_password, **kwargs):
"""Perform change password for a specific user."""
after_this_request(_commit)
user = User.get_by_username(username)
change_user_password(user=user.user,
password=new_password)
change_user_password(user=user.user, password=new_password)

@use_args(make_password_schema)
def post(self, args):
"""Change user password."""
if flask_request.json.get('username'):
if flask_request.json.get("username"):
self.verify_permission(**args)
self.change_password_for_user(**args)
else:
Expand Down
94 changes: 53 additions & 41 deletions rero_ils/alembic/0387b753585f_correct_subjects_bf_Organization.py
F438
Original file line number Diff line number Diff line change
Expand Up @@ -25,73 +25,85 @@

from rero_ils.modules.documents.api import Document, DocumentsSearch

LOGGER = getLogger('alembic')
LOGGER = getLogger("alembic")

# revision identifiers, used by Alembic.
revision = '0387b753585f'
down_revision = 'ce4923ba5286'
revision = "0387b753585f"
down_revision = "ce4923ba5286"
branch_labels = ()
depends_on = None


def upgrade():
"""Change subjects bf:Organization to bf:Organisation."""
query = DocumentsSearch().filter('bool', should=[
Q('term', subjects__type='bf:Organization'),
Q('term', subjects_imported__type='bf:Organization')
])
query = DocumentsSearch().filter(
"bool",
should=[
Q("term", subjects__type="bf:Organization"),
Q("term", subjects_imported__type="bf:Organization"),
],
)

LOGGER.info(f'Upgrade to {revision}')
LOGGER.info(f'Documents to change: {query.count()}')
pids = [hit.pid for hit in query.source('pid').scan()]
LOGGER.info(f"Upgrade to {revision}")
LOGGER.info(f"Documents to change: {query.count()}")
pids = [hit.pid for hit in query.source("pid").scan()]
errors = 0
idx = 0
for idx, pid in enumerate(pids, 1):
LOGGER.info(f'{idx} * Change document: {pid}')
LOGGER.info(f"{idx} * Change document: {pid}")
doc = Document.get_record_by_pid(pid)
for subject in doc.get('subjects', []):
if subject['type'] == 'bf:Organization':
subject['type'] = 'bf:Organisation'
for subjects_imported in doc.get('subjects_imported', []):
if subjects_imported['type'] == 'bf:Organization':
subjects_imported['type'] = 'bf:Organisation'
for subject in doc.get("subjects", []):
if subject["type"] == "bf:Organization":
subject["type"] = "bf:Organisation"
for subjects_imported in doc.get("subjects_imported", []):
if subjects_imported["type"] == "bf:Organization":
subjects_imported["type"] = "bf:Organisation"
try:
doc.update(data=doc, dbcommit=True, reindex=True)
except Exception as err:
LOGGER.error(f'\tError: {err}')
LOGGER.error(f"\tError: {err}")
errors += 1
LOGGER.info(f'Updated: {idx} Errors: {errors}')
LOGGER.info(f"Updated: {idx} Errors: {errors}")


def downgrade():
"""Change subjects bf:Organisation to bf:Organization."""
query = DocumentsSearch().filter('bool', should=[
Q('bool', must=[
Q('term', subjects__type='bf:Organisation'),
Q('exists', field='subjects.preferred_name')
]),
Q('bool', must=[
Q('term', subjects_imported__type='bf:Organisation'),
Q('exists', field='subjects_imported.preferred_name')
])
])
LOGGER.info(f'Downgrade to {down_revision}')
LOGGER.info(f'Documents to change: {query.count()}')
pids = [hit.pid for hit in query.source('pid').scan()]
query = DocumentsSearch().filter(
"bool",
should=[
Q(
"bool",
must=[
Q("term", subjects__type="bf:Organisation"),
Q("exists", field="subjects.preferred_name"),
],
),
Q(
"bool",
must=[
Q("term", subjects_imported__type="bf:Organisation"),
Q("exists", field="subjects_imported.preferred_name"),
],
),
],
)
LOGGER.info(f"Downgrade to {down_revision}")
LOGGER.info(f"Documents to change: {query.count()}")
pids = [hit.pid for hit in query.source("pid").scan()]
errors = 0
idx = 0
for idx, pid in enumerate(pids, 1):
LOGGER.info(f'{idx} * Change document: {pid}')
LOGGER.info(f"{idx} * Change document: {pid}")
doc = Document.get_record_by_pid(pid)
for subject in doc.get('subjects', []):
if subject['type'] == 'bf:Organisation':
subject['type'] = 'bf:Organization'
for subjects_imported in doc.get('subjects_imported', []):
if subjects_imported['type'] == 'bf:Organisation':
subjects_imported['type'] = 'bf:Organization'
for subject in doc.get("subjects", []):
if subject["type"] == "bf:Organisation":
subject["type"] = "bf:Organization"
for subjects_imported in doc.get("subjects_imported", []):
if subjects_imported["type"] == "bf:Organisation":
subjects_imported["type"] = "bf:Organization"
try:
doc.update(data=doc, dbcommit=True, reindex=True)
except Exception as err:
LOGGER.error(f'\tError: {err}')
LOGGER.error(f"\tError: {err}")
errors += 1
LOGGER.info(f'Updated: {idx} Errors: {errors}')
LOGGER.info(f"Updated: {idx} Errors: {errors}")
Loading
Loading
0