version updated by

This commit is contained in:
SvyatoslavArtymovych 2023-06-20 15:13:08 +03:00
parent 20ae184e08
commit df1a447b34
6 changed files with 61 additions and 9 deletions

View File

@ -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)")

View File

@ -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()

View File

@ -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 %}

View File

@ -1,7 +1,7 @@
<dl class=" bg-white dark:bg-gray-900 max-w-full p-5 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">
<dt class="flex mb-4">
{% if not hide_fork_label and book.original_book %}
<a href="{{ url_for('book.collection_view', book_id=book.original_book.id ) }}" target="_blank" class="flex pb-4">
<a href="{{ url_for('book.collection_view', book_id=book.original_book.id ) }}" target="_blank" class="flex">
<span class="flex items-center space-x-1 mr-2 bg-fuchsia-400 dark:!text-black rounded text-center px-1">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"> <path stroke-linecap="round" stroke-linejoin="round" d="M13.19 8.688a4.5 4.5 0 011.242 7.244l-4.5 4.5a4.5 4.5 0 01-6.364-6.364l1.757-1.757m13.35-.622l1.757-1.757a4.5 4.5 0 00-6.364-6.364l-4.5 4.5a4.5 4.5 0 001.242 7.244" /> </svg>
<p>Fork</p>
@ -12,17 +12,19 @@
{% if not hide_contributing_label and book.user_id != current_user.id %}
<span class="mr-2 bg-blue-400 dark:!text-black font-normal rounded text-center px-1">Contributing</span>
{% endif %}
{{label or book.label}}
<span>
{{label or book.label}}
</span>
</a>
</dt>
<dd
class="flex flex-col md:flex-row text-gray-500 dark:text-gray-400"
>
{% 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 @@
<p class="text-sm">
Last updated by
<a
href="{{ url_for('user.profile',user_id=owner.id) }}"
class="text-blue-500 {% if owner.is_deleted %}line-through{% endif %}"
href="{{ url_for('user.profile',user_id=updated_by.id) }}"
class="text-blue-500 {% if updated_by.is_deleted %}line-through{% endif %}"
>
{{owner.username}}
{{updated_by.username}}
</a>
on {{updated_at.strftime('%B %d, %Y')}}
</p>

View File

@ -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:

View File

@ -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 ###