Added db_api for comments

The db api layer for comments.
Migration to add is_active to comments table.

Change-Id: Idc54a30c347020fdc66fb6e38a3522d9201218ce
This commit is contained in:
Nikita Konovalov 2014-03-18 15:14:47 +04:00
parent c8b0c40e6f
commit 58a7063366
5 changed files with 96 additions and 0 deletions

View File

@ -270,6 +270,32 @@ def story_delete(story_id):
_entity_update(models.Story, story_id, story.as_dict())
# BEGIN Comments
def comment_get(comment_id):
return _entity_get(models.Comment, comment_id)
def comment_get_all(story_id=None):
return _entity_get_all(models.Comment, story_id=story_id, is_active=True)
def comment_create(values):
return _entity_create(models.Comment, values)
def comment_update(comment_id, values):
return _entity_update(models.Comment, comment_id, values)
def comment_delete(comment_id):
comment = comment_get(comment_id)
if comment:
comment.is_active = False
_entity_update(models.Task, comment_id, comment.as_dict())
# BEGIN Tasks
def task_get(task_id):

View File

@ -0,0 +1,41 @@
# 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.
#
"""empty message
Revision ID: 007
Revises: 006
Create Date: 2014-04-18 14:55:09.622503
"""
# revision identifiers, used by Alembic.
revision = '007'
down_revision = '006'
from alembic import op
from sqlalchemy import Boolean
from sqlalchemy import Column
def upgrade(active_plugins=None, options=None):
op.add_column('comments',
Column('is_active',
Boolean(),
default=True,
server_default="1",
nullable=False))
def downgrade(active_plugins=None, options=None):
op.drop_column('comments', 'is_active')

View File

@ -232,6 +232,7 @@ class Comment(Base):
story_id = Column(Integer, ForeignKey('stories.id'))
author_id = Column(Integer, ForeignKey('users.id'), nullable=True)
author = relationship('User', primaryjoin=author_id == User.id)
is_active = Column(Boolean, default=True)
_public_fields = ["id", "action", "comment_type", "content", "story_id",
"author_id"]

View File

@ -115,3 +115,9 @@ class TestMigrations(base.BaseWalkMigrationTestCase, base.CommonTestsMixIn):
self.assertColumnNotExists(engine, 'users', 'first_name')
self.assertColumnNotExists(engine, 'users', 'last_name')
self.assertColumnExists(engine, 'users', 'full_name')
def _pre_upgrade_007(self, engine):
self.assertColumnNotExists(engine, 'comments', 'is_active')
def _check_007(self, engine, data):
self.assertColumnExists(engine, 'comments', 'is_active')

View File

@ -105,3 +105,25 @@ class TasksTest(BaseDbTestCase):
self._test_update(self.task_01, delta,
db_api.task_create, db_api.task_update)
class CommentsTest(BaseDbTestCase):
def setUp(self):
super(CommentsTest, self).setUp()
self.comment_01 = {
'content': u'A comment',
'story_id': 1
}
def test_create_comment(self):
self._test_create(self.comment_01, db_api.comment_create)
def test_update_comment(self):
delta = {
'content': u'An updated comment'
}
self._test_update(self.comment_01, delta,
db_api.comment_create, db_api.comment_update)