From df816321515dff8242395a8126e8cedb3b491261 Mon Sep 17 00:00:00 2001 From: Alexander Watzinger Date: Sun, 10 Dec 2023 12:38:57 +0100 Subject: [PATCH] Solving Pylint warnings --- .pylintrc | 1 - openatlas/api/endpoints/iiif.py | 2 +- openatlas/api/import_scripts/arche.py | 3 ++- openatlas/forms/display.py | 1 - openatlas/forms/validation.py | 7 +++---- openatlas/models/export.py | 2 +- openatlas/views/imports.py | 15 +++++++-------- openatlas/views/search.py | 2 -- tests/test_admin.py | 6 ++++-- 9 files changed, 18 insertions(+), 21 deletions(-) diff --git a/.pylintrc b/.pylintrc index dbdde7770..d81f0e0b3 100644 --- a/.pylintrc +++ b/.pylintrc @@ -3,7 +3,6 @@ disable=C0111, broad-except, cyclic-import, duplicate-code good-names=f,i,j,k,ex,Run,_,e,js,bc,id,rv,ip,to # default is i,j,k,ex,Run,_ ignore=openatlas.wsgi min-public-methods=0 ---disable=W cidoc_rtfs_parser [FORMAT] max-line-length=79 diff --git a/openatlas/api/endpoints/iiif.py b/openatlas/api/endpoints/iiif.py index 40b7deef0..aa353d136 100644 --- a/openatlas/api/endpoints/iiif.py +++ b/openatlas/api/endpoints/iiif.py @@ -132,7 +132,7 @@ def get_manifest_version_2(id_: int) -> dict[str, Any]: def get_metadata(entity: Entity) -> dict[str, Any]: ext = '.tiff' if g.settings['iiif_conversion'] else entity.get_file_ext() image_url = f"{g.settings['iiif_url']}{entity.id}{ext}" - req = requests.get(f"{image_url}/info.json") + req = requests.get(f"{image_url}/info.json", timeout=30) image_api = req.json() return {'entity': entity, 'img_url': image_url, 'img_api': image_api} diff --git a/openatlas/api/import_scripts/arche.py b/openatlas/api/import_scripts/arche.py index 3149bb763..d6fc0a3c5 100644 --- a/openatlas/api/import_scripts/arche.py +++ b/openatlas/api/import_scripts/arche.py @@ -54,7 +54,8 @@ def get_existing_ids() -> list[int]: def fetch_exif(id_: str) -> dict[str, Any]: req = requests.get( 'https://arche-exif.acdh.oeaw.ac.at/', - params={'id': id_}) + params={'id': id_}, + timeout=300) return req.json() diff --git a/openatlas/forms/display.py b/openatlas/forms/display.py index 99404933d..e02df311f 100644 --- a/openatlas/forms/display.py +++ b/openatlas/forms/display.py @@ -2,7 +2,6 @@ from flask import g, render_template from flask_babel import lazy_gettext as _ -from markupsafe import Markup from wtforms import Field, FileField, IntegerField, SelectField, StringField from wtforms.validators import Email diff --git a/openatlas/forms/validation.py b/openatlas/forms/validation.py index 0d263fdb0..fda768770 100644 --- a/openatlas/forms/validation.py +++ b/openatlas/forms/validation.py @@ -27,10 +27,9 @@ def hierarchy_name_exists(form: FlaskForm, field: TreeField) -> None: def validate(form: FlaskForm, extra_validators: validators = None) -> bool: - valid = FlaskForm.validate(form) - if hasattr(form, 'begin_year_from'): # Dates - if not validate_dates(form): - valid = False + valid = FlaskForm.validate(form, extra_validators) + if hasattr(form, 'begin_year_from') and not validate_dates(form): + valid = False for field_id, field in form.__dict__.items(): # External reference systems if field_id.startswith('reference_system_id_') \ and field.data \ diff --git a/openatlas/models/export.py b/openatlas/models/export.py index bf361b04a..41248f3eb 100644 --- a/openatlas/models/export.py +++ b/openatlas/models/export.py @@ -37,7 +37,7 @@ def sql_export(format_: str, postfix: Optional[str] = '') -> bool: env={ 'PGPASSWORD': app.config['DATABASE_PASS'], 'SYSTEMROOT': root}).wait() - with open(os.devnull, 'w') as null: + with open(os.devnull, 'w', encoding='utf8') as null: subprocess.Popen( ['7z', 'a', f'{file}.7z', file], stdout=null).wait() diff --git a/openatlas/views/imports.py b/openatlas/views/imports.py index c90b44908..6d8a54f67 100644 --- a/openatlas/views/imports.py +++ b/openatlas/views/imports.py @@ -1,5 +1,5 @@ import collections -import pathlib +from pathlib import Path from typing import Optional, Union import numpy @@ -17,9 +17,9 @@ from openatlas.display.tab import Tab from openatlas.display.table import Table from openatlas.display.util import ( - button, datetime64_to_timestamp, display_form, format_date, - get_backup_file_data, is_authorized, link, manual, required_group, - button_bar, uc_first, description) + button, button_bar, datetime64_to_timestamp, description, display_form, + format_date, get_backup_file_data, is_authorized, link, manual, + required_group, uc_first) from openatlas.forms.field import SubmitField from openatlas.models.entity import Entity from openatlas.models.imports import Import, is_float @@ -178,8 +178,7 @@ class ImportForm(FlaskForm): def validate(self, extra_validators: validators=None) -> bool: valid = FlaskForm.validate(self) - if pathlib.Path(str(request.files['file'].filename)) \ - .suffix.lower() != '.csv': + if Path(str(request.files['file'].filename)) .suffix.lower() != '.csv': self.file.errors.append(_('file type not allowed')) valid = False return valid @@ -216,7 +215,7 @@ def import_data(project_id: int, class_: str) -> str: headers = list(data_frame.columns.values) if 'name' not in headers: messages['error'].append(_('missing name column')) - raise Exception() + raise ValueError() for item in headers: if item not in columns['allowed']: columns['invalid'].append(item) @@ -304,7 +303,7 @@ def import_data(project_id: int, class_: str) -> str: messages['warn'].append( f"{_('possible duplicates')}: {', '.join(duplicates)}") if messages['error']: - raise Exception() + raise ValueError() except Exception as e: g.logger.log('error', 'import', 'import check failed', e) flash(_('error at import'), 'error') diff --git a/openatlas/views/search.py b/openatlas/views/search.py index 6267b0be9..00c8e2eaf 100644 --- a/openatlas/views/search.py +++ b/openatlas/views/search.py @@ -1,5 +1,3 @@ -from typing import Any - from flask import g, render_template, request from flask_babel import lazy_gettext as _ from flask_wtf import FlaskForm diff --git a/tests/test_admin.py b/tests/test_admin.py index b727f173d..0cfd91338 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -45,9 +45,11 @@ def test_admin(self) -> None: assert b'Invalid linked entity' in rv.data file_ = 'Test77.txt' - with open(Path(app.config['UPLOAD_PATH'] / file_), 'w') as _: + file_path = Path(app.config['UPLOAD_PATH'] / file_) + with open(file_path, 'w', encoding='utf8') as _: pass - with open(Path(Path(g.settings['iiif_path']) / file_), 'w') as _: + iiif_path = Path(Path(g.settings['iiif_path']) / file_) + with open(iiif_path, 'w', encoding='utf8') as _: pass rv = self.app.get(