Skip to content

Commit

Permalink
Bones: form with Bernhard
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Jan 20, 2025
1 parent 6d88fba commit dc31181
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 24 deletions.
15 changes: 9 additions & 6 deletions install/upgrade/bones.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ BEGIN;

INSERT INTO model.entity (cidoc_class_code, openatlas_class_name, name) VALUES
('E55', 'type_tools', 'Bone preservation'),
('E55', 'type_tools', 'absent'),
('E55', 'type_tools', 'less than 25%'),
('E55', 'type_tools', '25-75%'),
('E55', 'type_tools', '0%'),
('E55', 'type_tools', '1-24%'),
('E55', 'type_tools', '25-74%'),
('E55', 'type_tools', '75-99%'),
('E55', 'type_tools', '100%');

INSERT INTO model.link (property_code, domain_id, range_id) VALUES
('P127', (SELECT id FROM model.entity WHERE name='absent'), (SELECT id FROM model.entity WHERE name='Bone preservation')),
('P127', (SELECT id FROM model.entity WHERE name='less than 25%'), (SELECT id FROM model.entity WHERE name='Bone preservation')),
('P127', (SELECT id FROM model.entity WHERE name='25-75%'), (SELECT id FROM model.entity WHERE name='Bone preservation')),
('P127', (SELECT id FROM model.entity WHERE name='0%'), (SELECT id FROM model.entity WHERE name='Bone preservation')),
('P127', (SELECT id FROM model.entity WHERE name='1-24%'), (SELECT id FROM model.entity WHERE name='Bone preservation')),
('P127', (SELECT id FROM model.entity WHERE name='25-74%'), (SELECT id FROM model.entity WHERE name='Bone preservation')),
('P127', (SELECT id FROM model.entity WHERE name='75-99%'), (SELECT id FROM model.entity WHERE name='Bone preservation')),
('P127', (SELECT id FROM model.entity WHERE name='100%'), (SELECT id FROM model.entity WHERE name='Bone preservation'));

INSERT INTO web.hierarchy (id, name, category, multiple, directional) VALUES
((SELECT id FROM model.entity WHERE name='Bone preservation'), 'Bone preservation', 'tools', False, False);

END;
21 changes: 11 additions & 10 deletions openatlas/forms/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,17 @@ def __call__(self, field: TreeField, **kwargs: Any) -> str:
if isinstance(field.data, list) else field.data
selection = g.types[int(field.data)].name
selected_ids.append(g.types[int(field.data)].id)
return Markup(render_template(
'forms/tree_select.html',
field=field,
selection=selection,
root=g.types[int(field.type_id)],
data=Type.get_tree_data(
int(field.type_id),
selected_ids,
field.filters_ids,
field.is_type_form))) + super().__call__(field, **kwargs)
return Markup(
render_template(
'forms/tree_select.html',
field=field,
selection=selection,
root=g.types[int(field.type_id)],
data=Type.get_tree_data(
int(field.type_id),
selected_ids,
field.filters_ids,
field.is_type_form))) + super().__call__(field, **kwargs)


class TreeField(HiddenField):
Expand Down
2 changes: 1 addition & 1 deletion openatlas/templates/tools/bones.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% for name in data %}
<p>
{{ name }}
<a href="{{ url_for('bones_update', id_=entity.id, category=name|replace(' ', '')) }}">{{ _('edit') }}</a>
<a href="{{ url_for('bones_update', id_=entity.id, category=name|replace(' ', '_')) }}">{{ _('edit') }}</a>
</p>
{% endfor %}
68 changes: 61 additions & 7 deletions openatlas/views/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from flask import flash, g, json, render_template, request, url_for
from flask_babel import lazy_gettext as _
from flask_wtf import FlaskForm
from markupsafe import Markup
from werkzeug.utils import redirect
from werkzeug.wrappers import Response
from wtforms import IntegerField, SelectField, StringField
Expand All @@ -20,6 +21,7 @@
from openatlas.models.entity import Entity, Link
from openatlas.models.tools import (
SexEstimation, get_carbon_link, get_sex_types, update_carbon)
from openatlas.models.type import Type


def name_result(result: float) -> str:
Expand Down Expand Up @@ -275,7 +277,6 @@ class Form(FlaskForm):
def bones(id_: int) -> str | Response:
entity = Entity.get_by_id(id_, types=True)
buttons = [manual('tools/anthropological_analyses')]
# types = Bones.get_types(entity)
if is_authorized('contributor'):
pass
return render_template(
Expand All @@ -300,7 +301,6 @@ def bones_update(id_: int, category) -> str | Response:
entity = Entity.get_by_id(id_, types=True)
buttons = [manual('tools/anthropological_analyses')]
form = get_bones_form(entity, category)
# types = Bones.get_types(entity)
if is_authorized('contributor'):
pass
return render_template(
Expand All @@ -309,19 +309,73 @@ def bones_update(id_: int, category) -> str | Response:
tabs={
'info': Tab(
'bones',
content=category + display_form(form),
content=display_bone_form(form, category),
buttons=buttons)},
crumbs=tools_start_crumbs(entity) + [
[_('tools'), url_for('tools_index', id_=entity.id)],
_('bone inventory')])
[_('bone inventory'), url_for('bones', id_=entity.id)],
_('edit')])


def get_bones_form(entity: Entity, category: str) -> Any:
class Form(FlaskForm):
pass

inventory = structure[category.replace('_', ' ')]
for name, item in inventory['subs'].items():
setattr(Form, name.replace(' ', '_'), SelectField(name))
inventory = structure[category.replace('-', ' ')]
options = {
g.types[id_].name: id_ for id_
in Type.get_hierarchy('Bone preservation').subs}

choices = [
(0, _('undefined')),
(options['0%'], '0%'),
(options['1-24%'], '1-24%'),
(options['25-74%'], '25-74%'),
(options['75-99%'], '75-99%'),
(options['100%'], '100%')]
if inventory['preservation'] == 'percent':
setattr(
Form,
category.replace(' ', '-'),
SelectField(category, choices=choices))
for label, sub in inventory['subs'].items():
add_bone_fields_recursive(Form, label, sub, choices)
setattr(Form, 'save', SubmitField(_('insert')))
return Form()


def add_bone_fields_recursive(form, label, item, choices):
if item['preservation'] == 'percent':
setattr(
form,
label.replace(' ', '-'),
SelectField(label, choices=choices))
if 'subs' in item:
for label, sub in item['subs'].items():
add_bone_fields_recursive(form, label, sub, choices)


def display_bone_form(form: Any, category: str) -> str:
# Todo: add manual page
html = Markup(
'<form method="post">'
f'{form.csrf_token}')
html += display_bone_row(form, category, structure[category])
return html + Markup('</form>')


def display_bone_row(
form: Any,
label: str,
item: dict[str, Any],
offset: float = 0):
html = Markup(
f'<div style="margin:0.5em;margin-left:{0.5 + offset * 2}em">'
f'<span style="margin-right:2em;">{label}</span>')
if item['preservation'] == 'percent':
html += str(getattr(form, label.replace(' ', '-')))
html += Markup('</div>')
if 'subs' in item:
for label, sub in item['subs'].items():
html += display_bone_row(form, label, sub, offset + 0.5)
return html

0 comments on commit dc31181

Please sign in to comment.