diff --git a/app/forms/__init__.py b/app/forms/__init__.py index 060813d..74aa770 100644 --- a/app/forms/__init__.py +++ b/app/forms/__init__.py @@ -8,3 +8,4 @@ from .contributor import ( EditContributorRoleForm, ) from .collection import CreateCollectionForm, EditCollectionForm +from .section import CreateSectionForm, EditSectionForm diff --git a/app/forms/section.py b/app/forms/section.py new file mode 100644 index 0000000..82aecf1 --- /dev/null +++ b/app/forms/section.py @@ -0,0 +1,68 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, SubmitField, ValidationError +from wtforms.validators import DataRequired, Length + +from app import models as m, db +from app.logger import log + + +class BaseSectionForm(FlaskForm): + label = StringField("Label", [DataRequired(), Length(3, 256)]) + about = StringField("About") + + +class CreateSectionForm(BaseSectionForm): + collection_id = StringField("Collection ID", [DataRequired()]) + submit = SubmitField("Create") + + def validate_collection_id(self, field): + collection_id = field.data + collection: m.Collection = db.session.get(m.Collection, collection_id) + + if not collection or collection.sub_collections: + log(log.WARNING, "Collection [%s] it not leaf", collection) + + raise ValidationError("You can't create section for this collection") + + def validate_label(self, field): + label = field.data + collection_id = self.collection_id.data + + section: m.Section = m.Section.query.filter_by( + is_deleted=False, label=label, collection_id=collection_id + ).first() + if section: + log( + log.WARNING, + "Section with label [%s] already exists: [%s]", + label, + section, + ) + raise ValidationError("Section label must be unique!") + + +class EditSectionForm(BaseSectionForm): + section_id = StringField("Section ID", [DataRequired()]) + submit = SubmitField("Edit") + + def validate_label(self, field): + label = field.data + section_id = self.section_id.data + + collection_id = db.session.get(m.Section, section_id).collection_id + section: m.Section = ( + m.Section.query.filter_by( + is_deleted=False, label=label, collection_id=collection_id + ) + .filter(m.Section.id != section_id) + .first() + ) + + if section: + log( + log.WARNING, + "Section with label [%s] already exists: [%s]", + label, + section, + ) + raise ValidationError("Section label must be unique!") diff --git a/app/models/book.py b/app/models/book.py index 1541c47..6d0d496 100644 --- a/app/models/book.py +++ b/app/models/book.py @@ -19,3 +19,7 @@ class Book(BaseModel): def __repr__(self): return f"<{self.id}: {self.label}>" + + @property + def last_version(self): + return self.versions[-1] diff --git a/app/models/collection.py b/app/models/collection.py index cec8a90..3053de8 100644 --- a/app/models/collection.py +++ b/app/models/collection.py @@ -25,3 +25,15 @@ class Collection(BaseModel): def __repr__(self): return f"<{self.id}: {self.label}>" + + @property + def active_sections(self): + return [section for section in self.sections if not section.is_deleted] + + @property + def sub_collections(self): + return [ + sub_collection + for sub_collection in self.children + if not sub_collection.is_deleted + ] diff --git a/app/templates/book/add_section_modal.html b/app/templates/book/add_section_modal.html new file mode 100644 index 0000000..a811884 --- /dev/null +++ b/app/templates/book/add_section_modal.html @@ -0,0 +1,43 @@ + + +
diff --git a/app/templates/book/delete_section_modal.html b/app/templates/book/delete_section_modal.html new file mode 100644 index 0000000..209630b --- /dev/null +++ b/app/templates/book/delete_section_modal.html @@ -0,0 +1,26 @@ + + diff --git a/app/templates/book/edit_section_modal.html b/app/templates/book/edit_section_modal.html new file mode 100644 index 0000000..5500378 --- /dev/null +++ b/app/templates/book/edit_section_modal.html @@ -0,0 +1,43 @@ + + diff --git a/app/templates/book/interpretation_view.html b/app/templates/book/interpretation_view.html index 741b734..ede4f2e 100644 --- a/app/templates/book/interpretation_view.html +++ b/app/templates/book/interpretation_view.html @@ -1,5 +1,18 @@ {% extends 'base.html' %} + +{% include 'book/delete_section_modal.html' %} +{% include 'book/edit_section_modal.html' %} + + +{% set show_delete_section = True %} +{% set show_edit_section = True %} + + +{% block right_sidebar %} + {% include 'book/right_sidebar.html' %} +{% endblock %} + {% block content %}