Store history of a comment when editing

At the moment it is possible to edit comments via the API, but this
causes the previous content of the comment to be permanently lost.

This commit fixes this problem by keeping a record of the previous version
of a comment each time it is edited.

Change-Id: I0bcf4ba3555c8d96e91497742f424056e3123312
This commit is contained in:
Adam Coldrick 2016-06-21 15:31:21 +00:00
parent fc51eefce7
commit 51e2a99f09
3 changed files with 64 additions and 0 deletions

View File

@ -26,6 +26,12 @@ def comment_create(values):
def comment_update(comment_id, values):
comment = api_base.entity_get(models.Comment, comment_id)
old_dict = {
'comment_id': comment_id,
'content': comment.content
}
api_base.entity_create(models.HistoricalComment, old_dict)
return api_base.entity_update(models.Comment, comment_id, values)

View File

@ -0,0 +1,49 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
"""Add a table for comment history
Revision ID: 059
Revises: 058
Create Date: 2016-06-21 14:00:20.515139
"""
# revision identifiers, used by Alembic.
revision = '059'
down_revision = '058'
from alembic import op
import sqlalchemy as sa
from storyboard.db.decorators import UTCDateTime
from storyboard.db.models import MYSQL_MEDIUM_TEXT
def upgrade(active_plugins=None, options=None):
op.create_table(
'comments_history',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', UTCDateTime(), nullable=True),
sa.Column('updated_at', UTCDateTime(), nullable=True),
sa.Column('content', MYSQL_MEDIUM_TEXT, nullable=True),
sa.Column('comment_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['comment_id'], ['comments.id'],
name='fk_comment_id'),
sa.PrimaryKeyConstraint('id')
)
def downgrade(active_plugins=None, options=None):
op.drop_table('comments_history')

View File

@ -508,6 +508,15 @@ class Comment(FullText, ModelBuilder, Base):
parent = relationship('Comment', remote_side=[id], backref='children')
class HistoricalComment(FullText, ModelBuilder, Base):
__tablename__ = 'comments_history'
__fulltext_columns__ = ['content']
content = Column(MYSQL_MEDIUM_TEXT)
comment_id = Column(Integer, ForeignKey('comments.id'), nullable=False)
current = relationship(Comment, backref='history')
# Subscription and notifications
class Subscription(ModelBuilder, Base):