This commit is contained in:
Kostiantyn Stoliarskyi 2023-05-05 12:02:35 +03:00
parent a1982d0127
commit 2ab55217d8
7 changed files with 53 additions and 148 deletions

View File

@ -1,10 +1,7 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, ValidationError
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, Length
from app import models as m, db
from app.logger import log
class BaseInterpretationForm(FlaskForm):
label = StringField("Label", [DataRequired(), Length(3, 256)])
@ -16,47 +13,8 @@ class CreateInterpretationForm(BaseInterpretationForm):
text = StringField("Text")
submit = SubmitField("Create")
def validate_label(self, field):
label = field.data
section_id = self.section_id.data
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
is_deleted=False, label=label, section_id=section_id
).first()
if interpretation:
log(
log.WARNING,
"Interpretation with label [%s] already exists: [%s]",
label,
interpretation,
)
raise ValidationError("Interpretation label must be unique!")
class EditInterpretationForm(BaseInterpretationForm):
interpretation_id = StringField("Interpretation ID", [DataRequired()])
text = StringField("Text")
submit = SubmitField("Edit")
def validate_label(self, field):
label = field.data
interpretation_id = self.interpretation_id.data
section_id = db.session.get(m.Interpretation, interpretation_id).section_id
interpretation: m.Interpretation = (
m.Interpretation.query.filter_by(
is_deleted=False, label=label, section_id=section_id
)
.filter(m.Interpretation.id != interpretation_id)
.first()
)
if interpretation:
log(
log.WARNING,
"Interpretation with label [%s] already exists: [%s]",
label,
interpretation,
)
raise ValidationError("Interpretation label must be unique!")

View File

@ -47,5 +47,13 @@ class Section(BaseModel):
_sub_collection_id = grand_parent.id
return _sub_collection_id
@property
def active_interpretations(self):
return [
interpretation
for interpretation in self.interpretations
if not interpretation.is_deleted
]
def __repr__(self):
return f"<{self.id}: {self.label}>"

File diff suppressed because one or more lines are too long

View File

@ -66,7 +66,7 @@
<input type="hidden" name="section_id" id="section_id" value="{{section.id}}" />
<input type="hidden" name="label" id="label" value="{{section.label}}" />
<input type="hidden" name="interpretation-text" id="interpretation-text" />
<input type="hidden" name="text" id="interpretation-text" />
<!-- Form body -->
<div class="p-6 space-y-6">
<div class="grid gap-6">
@ -89,20 +89,21 @@
</form>
<dl class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700">
<!-- prettier-ignore -->
{% for interpretation in section.interpretations %}
{% for interpretation in section.active_interpretations %}
{% if sub_collection %}
<a href="{{url_for('book.interpretation_view', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id, section_id=section.id)}}" >
{% else %}
<a href="{{url_for('book.interpretation_view', book_id=book.id, collection_id=collection.id, section_id=section.id)}}" >
{% endif %}
<h1>Other</h1>
<dl class="bg-white dark:bg-gray-900 max-w-full p-3 text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700 m-3 border-2 border-gray-200 border-solid rounded-lg dark:border-gray-700">
<div class="flex flex-col pb-3 p-3 w-full">
<!-- prettier-ignore -->
<dt class="flex w-full mb-1 text-gray-500 md:text-lg dark:text-gray-400 flex-col">
<div>
<h1>{{ section.label }}</h1>
<p>{{ interpretation.text }}</p>
<div id="editor" class="dark:text-white h-80">
<p>{{ interpretation.text|safe }}</p>
</div>
</div>
<div class="flex ml-auto align-center justify-center space-x-3">
<span class="space-x-0.5 flex items-center">

View File

@ -912,17 +912,25 @@ def interpretation_create(
)
)
redirect_url = url_for(
section: m.Section = db.session.get(m.Section, section_id)
if not section or collection.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section)
flash("Section not found", "danger")
return redirect(
url_for(
"book.section_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
section: m.Section = db.session.get(m.Section, section_id)
if not section or collection.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section)
flash("Section not found", "danger")
return redirect(redirect_url)
)
redirect_url = url_for(
"book.interpretation_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section.id,
)
form = f.CreateInterpretationForm()

View File

@ -1,38 +1,39 @@
const quill_value_to_textarea = (): undefined => {
const aboutInput: HTMLButtonElement = document.querySelector('#about');
const qlEditor: HTMLButtonElement = document.querySelector('.ql-editor');
const aboutInput: HTMLInputElement = document.querySelector('#about');
const qlEditor: HTMLElement = document.querySelector('.ql-editor');
const editorContent = qlEditor.innerHTML;
aboutInput.value = editorContent;
return undefined;
};
const quill_interpretation_to_textarea = (): undefined => {
const interpretationTextInput: HTMLButtonElement = document.querySelector(
const interpretationTextInput: HTMLInputElement = document.querySelector(
'#interpretation-text',
);
const qlInterpretationEditor: HTMLButtonElement = document.querySelector(
'.ql-interpretation-editor',
const qlInterpretationEditor: HTMLElement = document.querySelector(
'#interpretation-editor',
);
const editorContent = qlInterpretationEditor.innerHTML;
console.log('editorContent', editorContent);
interpretationTextInput.value = editorContent;
return undefined;
};
export function initQuillValueToTextArea() {
const qlEditor: HTMLButtonElement = document.querySelector('.ql-editor');
const qlEditor: HTMLElement = document.querySelector('.ql-editor');
if (qlEditor) {
qlEditor.addEventListener('DOMSubtreeModified', async e => {
qlEditor.addEventListener('DOMSubtreeModified', () => {
quill_value_to_textarea();
});
}
//
const qlInterpretationEditor: HTMLButtonElement = document.querySelector(
'.ql-interpretation-editor',
const qlInterpretationEditor: HTMLElement = document.querySelector(
'#interpretation-editor',
);
if (!qlInterpretationEditor) {
qlInterpretationEditor.addEventListener('DOMSubtreeModified', async e => {
if (qlInterpretationEditor) {
qlInterpretationEditor.addEventListener('DOMSubtreeModified', () => {
quill_interpretation_to_textarea();
});
}

View File

@ -757,14 +757,6 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
assert interpretation.section_id == section_in_subcollection.id
assert not interpretation.comments
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/create_interpretation",
data=dict(section_id=section_in_subcollection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert b"Interpretation label must be unique!" in response.data
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/create_interpretation",
data=dict(section_id=section_in_collection.id, label=label_1, text=text_1),
@ -779,14 +771,6 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
assert interpretation.section_id == section_in_collection.id
assert not interpretation.comments
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/create_interpretation",
data=dict(section_id=section_in_collection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert b"Interpretation label must be unique!" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/999/create_section",
data=dict(collection_id=999, label=label_1, text=text_1),
@ -832,19 +816,6 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
label=label_1, section_id=section_in_collection.id
).first()
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/{interpretation.id}/edit_interpretation",
data=dict(
interpretation_id=interpretation.id,
label="Test",
text="Test",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Interpretation label must be unique!" in response.data
new_label = "Test Interpretation #1 Label(edited)"
new_text = "Test Interpretation #1 Text(edited)"