Skip to content

Commit

Permalink
Refactor imports
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Feb 10, 2024
1 parent b7ad423 commit c094912
Show file tree
Hide file tree
Showing 37 changed files with 122 additions and 116 deletions.
9 changes: 4 additions & 5 deletions openatlas/display/base_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
from flask_login import current_user

from openatlas import app
from openatlas.display.string_functions import is_authorized, manual
from openatlas.display.tab import Tab
from openatlas.display.util import (
bookmark_toggle, button, description, edit_link, format_date,
format_entity_date, get_appearance, get_base_table_data,
get_chart_data, get_system_data, link, profile_image_table_link,
remove_link)
bookmark_toggle, button, description, edit_link, format_entity_date,
get_appearance, get_base_table_data, get_chart_data, get_system_data,
link, profile_image_table_link, remove_link)
from openatlas.display.util2 import format_date, is_authorized, manual
from openatlas.models.entity import Entity
from openatlas.models.gis import Gis
from openatlas.models.link import Link
Expand Down
2 changes: 1 addition & 1 deletion openatlas/display/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from openatlas.display.base_display import (
ActorDisplay, BaseDisplay, EventsDisplay, PlaceBaseDisplay,
ReferenceBaseDisplay, TypeBaseDisplay)
from openatlas.display.string_functions import is_authorized, uc_first
from openatlas.display.tab import Tab
from openatlas.display.table import Table
from openatlas.display.util import (
button, description, edit_link, format_entity_date, get_base_table_data,
get_file_path, link, remove_link)
from openatlas.display.util2 import is_authorized, uc_first
from openatlas.models.entity import Entity
from openatlas.models.reference_system import ReferenceSystem
from openatlas.views.tools import carbon_result, sex_result
Expand Down
2 changes: 1 addition & 1 deletion openatlas/display/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from flask_babel import lazy_gettext as _
from flask_wtf import FlaskForm

from openatlas.display.string_functions import is_authorized, manual
from openatlas.display.table import Table
from openatlas.display.util import (
button, check_iiif_activation, check_iiif_file_exist)
from openatlas.display.util2 import is_authorized, manual

if TYPE_CHECKING: # pragma: no cover
from openatlas.models.entity import Entity
Expand Down
2 changes: 1 addition & 1 deletion openatlas/display/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask_babel import lazy_gettext as _
from flask_login import current_user

from openatlas.display.string_functions import uc_first
from openatlas.display.util2 import uc_first

# Needed for translations
_('previous')
Expand Down
59 changes: 4 additions & 55 deletions openatlas/display/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import os
import smtplib
import subprocess
from datetime import datetime, timedelta
from email.header import Header
from email.mime.text import MIMEText
from functools import wraps
from pathlib import Path
from typing import Any, Optional, TYPE_CHECKING

import numpy
from bcrypt import hashpw
from flask import flash, g, render_template, request, url_for
from flask_babel import LazyString, lazy_gettext as _
Expand All @@ -22,14 +20,15 @@

from openatlas import app
from openatlas.display.image_processing import check_processed_image
from openatlas.display.string_functions import (
convert_size, datetime64_to_timestamp, is_authorized, uc_first)
from openatlas.display.util2 import format_date, is_authorized, uc_first
from openatlas.models.cidoc_class import CidocClass
from openatlas.models.cidoc_property import CidocProperty
from openatlas.models.content import get_translation
from openatlas.models.entity import Entity
from openatlas.models.imports import Project
from openatlas.models.user import User

if TYPE_CHECKING: # pragma: no cover
from openatlas.models.entity import Entity
from openatlas.models.link import Link
from openatlas.models.type import Type

Expand Down Expand Up @@ -301,28 +300,6 @@ def format_name_and_aliases(entity: Entity, show_links: bool) -> str:
f'{"".join(f"<p>{alias}</p>" for alias in entity.aliases.values())}'


def get_backup_file_data() -> dict[str, Any]:
path = app.config['EXPORT_PATH']
latest_file = None
latest_file_date = None
for file in [
f for f in path.iterdir()
if (path / f).is_file() and f.name != '.gitignore']:
file_date = datetime.utcfromtimestamp((path / file).stat().st_ctime)
if not latest_file_date or file_date > latest_file_date:
latest_file = file
latest_file_date = file_date
file_data: dict[str, Any] = {'backup_too_old': True}
if latest_file and latest_file_date:
yesterday = datetime.today() - timedelta(days=1)
file_data['file'] = latest_file.name
file_data['backup_too_old'] = \
bool(yesterday > latest_file_date and not app.testing)
file_data['size'] = convert_size(latest_file.stat().st_size)
file_data['date'] = format_date(latest_file_date)
return file_data


def get_base_table_data(entity: Entity, show_links: bool = True) -> list[Any]:
data: list[Any] = [format_name_and_aliases(entity, show_links)]
if entity.class_.view in [
Expand Down Expand Up @@ -422,7 +399,6 @@ def system_warnings(_context: str, _unneeded_string: str) -> str:
for path in g.writable_paths:
check_write_access(path, warnings)
if is_authorized('admin'):
from openatlas.models.user import User
user = User.get_by_username('OpenAtlas')
if user and user.active:
hash_ = hashpw(
Expand Down Expand Up @@ -472,15 +448,6 @@ def get_file_path(
return app.config['UPLOAD_PATH'] / f"{id_}{ext}"


def format_date(value: datetime | numpy.datetime64) -> str:
if not value:
return ''
if isinstance(value, numpy.datetime64):
date_ = datetime64_to_timestamp(value)
return date_.lstrip('0').replace(' 00:00:00', '') if date_ else ''
return value.date().isoformat().replace(' 00:00:00', '')


@app.template_filter()
def link(
object_: Any,
Expand All @@ -489,9 +456,6 @@ def link(
uc_first_: Optional[bool] = True,
js: Optional[str] = None,
external: bool = False) -> str:
from openatlas.models.user import User
from openatlas.models.entity import Entity
from openatlas.models.imports import Project
html = ''
if isinstance(object_, (str, LazyString)):
js = f'onclick="{uc_first(js)}"' if js else ''
Expand Down Expand Up @@ -611,21 +575,6 @@ def display_content_translation(_context: str, text: str) -> str:
return get_translation(text)


@app.template_filter()
def display_form(
form: Any,
form_id: Optional[str] = None,
manual_page: Optional[str] = None) -> str:
from openatlas.forms.display import html_form
form_id = f'id="{form_id}"' if form_id else ''
multipart = 'enctype="multipart/form-data"' if 'file' in form else ''
return \
f'<form method="post" {form_id} {multipart}>' \
'<table class="table table-no-style">' \
f'{html_form(form, form_id, manual_page)}' \
f'</table></form>'


def get_entities_linked_to_type_recursive(
id_: int,
data: list[Entity]) -> list[Entity]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# util2.py functions don't need the model and help preventing circular imports
from __future__ import annotations

import math
import re
from datetime import datetime, timedelta
from html.parser import HTMLParser
from pathlib import Path
from typing import Any, Optional
Expand Down Expand Up @@ -145,3 +149,34 @@ def manual(site: str) -> str:
f'href="/static/manual/{site}.html" class="manual" ' \
f'target="_blank" rel="noopener noreferrer">' \
f'<i class="fas fs-4 fa-book"></i></a>'


def get_backup_file_data() -> dict[str, Any]:
path = app.config['EXPORT_PATH']
latest_file = None
latest_file_date = None
for file in [
f for f in path.iterdir()
if (path / f).is_file() and f.name != '.gitignore']:
file_date = datetime.utcfromtimestamp((path / file).stat().st_ctime)
if not latest_file_date or file_date > latest_file_date:
latest_file = file
latest_file_date = file_date
file_data: dict[str, Any] = {'backup_too_old': True}
if latest_file and latest_file_date:
yesterday = datetime.today() - timedelta(days=1)
file_data['file'] = latest_file.name
file_data['backup_too_old'] = \
bool(yesterday > latest_file_date and not app.testing)
file_data['size'] = convert_size(latest_file.stat().st_size)
file_data['date'] = format_date(latest_file_date)
return file_data


def format_date(value: datetime | numpy.datetime64) -> str:
if not value:
return ''
if isinstance(value, numpy.datetime64):
date_ = datetime64_to_timestamp(value)
return date_.lstrip('0').replace(' 00:00:00', '') if date_ else ''
return value.date().isoformat().replace(' 00:00:00', '')
2 changes: 1 addition & 1 deletion openatlas/forms/add_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from wtforms.validators import (
InputRequired, NoneOf, NumberRange, Optional as OptionalValidator)

from openatlas.display.string_functions import is_authorized
from openatlas.display.util2 import is_authorized
from openatlas.forms.field import (
ReferenceField, TreeField, TreeMultiField, ValueTypeField,
ValueTypeRootField)
Expand Down
18 changes: 17 additions & 1 deletion openatlas/forms/display.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any, Optional

from flask import g, render_template
Expand All @@ -6,7 +8,7 @@
from wtforms.validators import Email

from openatlas import app
from openatlas.display.string_functions import manual
from openatlas.display.util2 import manual
from openatlas.forms.field import ValueTypeField


Expand Down Expand Up @@ -153,3 +155,17 @@ def add_dates(form: Any) -> str:
style='' if valid_dates else 'display:table-row',
label=_('hide')
if form.begin_year_from.data or form.end_year_from.data else _('show'))


@app.template_filter()
def display_form(
form: Any,
form_id: Optional[str] = None,
manual_page: Optional[str] = None) -> str:
form_id = f'id="{form_id}"' if form_id else ''
multipart = 'enctype="multipart/form-data"' if 'file' in form else ''
return \
f'<form method="post" {form_id} {multipart}>' \
'<table class="table table-no-style">' \
f'{html_form(form, form_id, manual_page)}' \
f'</table></form>'
2 changes: 1 addition & 1 deletion openatlas/forms/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from wtforms.widgets import FileInput, HiddenInput, Input, TextInput

from openatlas import app
from openatlas.display.string_functions import is_authorized, uc_first
from openatlas.display.table import Table
from openatlas.display.util import get_base_table_data
from openatlas.display.util2 import is_authorized, uc_first
from openatlas.models.entity import Entity
from openatlas.models.type import Type

Expand Down
2 changes: 1 addition & 1 deletion openatlas/forms/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from flask import g

from openatlas.display.string_functions import format_date_part
from openatlas.display.util2 import format_date_part


def populate_types(manager: Any) -> None:
Expand Down
2 changes: 1 addition & 1 deletion openatlas/forms/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask import g
from werkzeug.exceptions import abort

from openatlas.display.string_functions import sanitize
from openatlas.display.util2 import sanitize
from openatlas.forms.util import form_to_datetime64
from openatlas.models.entity import Entity
from openatlas.models.reference_system import ReferenceSystem
Expand Down
2 changes: 1 addition & 1 deletion openatlas/forms/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flask_wtf import FlaskForm
from wtforms import validators

from openatlas.display.string_functions import uc_first
from openatlas.display.util2 import uc_first
from openatlas.forms.util import form_to_datetime64
from openatlas.models.entity import Entity
from openatlas.models.type import Type
Expand Down
2 changes: 1 addition & 1 deletion openatlas/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from openatlas import app
from openatlas.database.date import Date
from openatlas.database.entity import Entity as Db
from openatlas.display.string_functions import (
from openatlas.display.util2 import (
convert_size, datetime64_to_timestamp, format_date_part, sanitize,
timestamp_to_datetime64)
from openatlas.models.gis import Gis
Expand Down
2 changes: 1 addition & 1 deletion openatlas/models/gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flask import g, json

from openatlas.database.gis import Gis as Db
from openatlas.display.string_functions import sanitize
from openatlas.display.util2 import sanitize

if TYPE_CHECKING: # pragma: no cover
from openatlas.models.entity import Entity
Expand Down
2 changes: 1 addition & 1 deletion openatlas/models/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask_login import current_user

from openatlas.database.imports import Import as Db
from openatlas.display.string_functions import sanitize
from openatlas.display.util2 import sanitize
from openatlas.models.entity import Entity
from openatlas.models.gis import Gis

Expand Down
2 changes: 1 addition & 1 deletion openatlas/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from openatlas.database.date import Date
from openatlas.database.link import Link as Db
from openatlas.database.tools import Tools
from openatlas.display.string_functions import (
from openatlas.display.util2 import (
datetime64_to_timestamp, format_date_part, timestamp_to_datetime64)

if TYPE_CHECKING: # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion openatlas/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from flask_login import UserMixin, current_user

from openatlas.database.user import User as Db
from openatlas.display.string_functions import sanitize
from openatlas.display.util2 import sanitize
from openatlas.models.entity import Entity


Expand Down
9 changes: 5 additions & 4 deletions openatlas/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
from openatlas.database.connect import Transaction
from openatlas.display.image_processing import (
create_resized_images, delete_orphaned_resized_images)
from openatlas.display.string_functions import (
convert_size, is_authorized, manual, sanitize, uc_first)
from openatlas.display.tab import Tab
from openatlas.display.table import Table
from openatlas.display.util import (
button, check_iiif_activation, check_iiif_file_exist,
convert_image_to_iiif, display_form, display_info, format_date,
get_file_path, link, required_group, send_mail)
convert_image_to_iiif, display_info, get_file_path, link, required_group,
send_mail)
from openatlas.display.util2 import (
convert_size, format_date, is_authorized, manual, sanitize, uc_first)
from openatlas.forms.display import display_form
from openatlas.forms.field import SubmitField
from openatlas.forms.setting import (
ApiForm, ContentForm, FilesForm, FrontendForm, GeneralForm, IiifForm,
Expand Down
2 changes: 1 addition & 1 deletion openatlas/views/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from openatlas import app
from openatlas.database.connect import Transaction
from openatlas.display.string_functions import uc_first
from openatlas.display.util import required_group
from openatlas.display.util2 import uc_first
from openatlas.forms.field import get_table_content
from openatlas.models.entity import Entity
from openatlas.models.type import Type
Expand Down
5 changes: 2 additions & 3 deletions openatlas/views/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
from wtforms.validators import InputRequired

from openatlas import app
from openatlas.display.string_functions import is_authorized, manual
from openatlas.display.tab import Tab
from openatlas.display.table import Table
from openatlas.display.util import (
format_date, get_file_path, link, required_group)
from openatlas.display.util import get_file_path, link, required_group
from openatlas.display.util2 import format_date, is_authorized, manual
from openatlas.forms.field import SubmitField, TableField
from openatlas.models.annotation import Annotation
from openatlas.models.entity import Entity
Expand Down
2 changes: 1 addition & 1 deletion openatlas/views/arche.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from openatlas.api.import_scripts.arche import (
fetch_collection_data, import_arche_data)
from openatlas.database.connect import Transaction
from openatlas.display.string_functions import is_authorized, manual
from openatlas.display.tab import Tab
from openatlas.display.table import Table
from openatlas.display.util import button, display_info, required_group
from openatlas.display.util2 import is_authorized, manual


@app.route('/arche')
Expand Down
Loading

0 comments on commit c094912

Please sign in to comment.