diff --git a/app/models/book_version.py b/app/models/book_version.py index 0949fc0..587c77d 100644 --- a/app/models/book_version.py +++ b/app/models/book_version.py @@ -19,9 +19,11 @@ class BookVersion(BaseModel): derivative_id = db.Column(db.Integer, db.ForeignKey("book_versions.id")) book_id = db.Column(db.Integer, db.ForeignKey("books.id")) user_id = db.Column(db.ForeignKey("users.id")) + updated_by = db.Column(db.ForeignKey("users.id")) # Relationships - user = db.relationship("User", viewonly=True) + user = db.relationship("User", viewonly=True, foreign_keys=[user_id]) + updated_by_user = db.relationship("User", viewonly=True, foreign_keys=[updated_by]) book = db.relationship("Book", viewonly=True) derivative = db.relationship("BookVersion", remote_side=[id]) sections = db.relationship("Section", viewonly=True, order_by="desc(Section.id)") diff --git a/app/models/utils.py b/app/models/utils.py index 080915f..ded4a77 100644 --- a/app/models/utils.py +++ b/app/models/utils.py @@ -1,11 +1,17 @@ from datetime import datetime +from flask_login import current_user + from app import db class ModelMixin(object): def save(self, commit=True): # Save this model to the database. + if hasattr(self, "updated_at"): + self.updated_at = datetime.now() + self.updated_by = current_user.id + db.session.add(self) if commit: db.session.commit() diff --git a/app/templates/book/collection_view.html b/app/templates/book/collection_view.html index afe79a4..1959281 100644 --- a/app/templates/book/collection_view.html +++ b/app/templates/book/collection_view.html @@ -15,7 +15,7 @@ {% include 'book/modals/delete_sub_collection_modal.html' %} {% include 'book/modals/add_section_modal.html' %} {% include 'book/modals/delete_section_modal.html' %} -{% else %} +{% elif current_user.is_authenticated %} {% include 'book/modals/fork_version_modal.html' %} {% endif %} diff --git a/app/templates/book/components/book_list_item.html b/app/templates/book/components/book_list_item.html index f01b01c..75b2ecc 100644 --- a/app/templates/book/components/book_list_item.html +++ b/app/templates/book/components/book_list_item.html @@ -1,7 +1,7 @@
{% if not hide_fork_label and book.original_book %} - +

Fork

@@ -12,17 +12,19 @@ {% if not hide_contributing_label and book.user_id != current_user.id %} Contributing {% endif %} - {{label or book.label}} + + {{label or book.label}} +
{% if type(book) == m.BookVersion %} - {% set owner = book.book.owner %} + {% set updated_by = book.updated_by_user if book.updated_by else book.owner %} {% set updated_at = book.updated_at %} {% else %} - {% set owner = book.owner %} + {% set updated_by = book.active_version.updated_by_user if book.updated_by else book.owner %} {% set updated_at = book.active_version.updated_at %} {% endif %} @@ -31,10 +33,10 @@

Last updated by - {{owner.username}} + {{updated_by.username}} on {{updated_at.strftime('%B %d, %Y')}}

diff --git a/app/views/book/book.py b/app/views/book/book.py index 8dae337..7672292 100644 --- a/app/views/book/book.py +++ b/app/views/book/book.py @@ -1,3 +1,5 @@ +from datetime import datetime + from flask import render_template, flash, redirect, url_for, request from flask_login import login_required, current_user from sqlalchemy import and_, or_, func, distinct @@ -152,10 +154,16 @@ def edit(book_id: int): tags = form.tags.data or "" set_book_tags(book, tags) + active_version: m.BookVersion = book.active_version + active_version.updated_at = datetime.now() + active_version.updated_by = current_user.id + book.label = label book.about = about log(log.INFO, "Update Book: [%s]", book) book.save() + log(log.INFO, "Update version updated at: [%s]", active_version) + active_version.save() flash("Success!", "success") return redirect(url_for("book.collection_view", book_id=book_id)) else: diff --git a/migrations/versions/4f49ff89f7b8_updated_by.py b/migrations/versions/4f49ff89f7b8_updated_by.py new file mode 100644 index 0000000..0bd8018 --- /dev/null +++ b/migrations/versions/4f49ff89f7b8_updated_by.py @@ -0,0 +1,34 @@ +"""updated_by + +Revision ID: 4f49ff89f7b8 +Revises: 94ee8daa0c25 +Create Date: 2023-06-20 14:46:26.950182 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '4f49ff89f7b8' +down_revision = '94ee8daa0c25' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('book_versions', schema=None) as batch_op: + batch_op.add_column(sa.Column('updated_by', sa.Integer(), nullable=True)) + batch_op.create_foreign_key(None, 'users', ['updated_by'], ['id']) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('book_versions', schema=None) as batch_op: + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.drop_column('updated_by') + + # ### end Alembic commands ###