Adding subsription model and DB API

Model classes and a migration added for subscriptions handling. This
will be used later to build user feed.

Change-Id: I9012d3bc8b6c20d884100b23d431cdfd80457c1e
This commit is contained in:
Nikita Konovalov 2014-07-10 16:28:20 +04:00 committed by Michael Krotscheck
parent 160e5002ec
commit 810efd226e
3 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,46 @@
# Copyright (c) 2014 Mirantis Inc.
#
# 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.
from storyboard.db.api import base as api_base
from storyboard.db import models
def subscription_get(subscription_id):
return api_base.entity_get(models.Subscription, subscription_id)
def subscription_get_all(user_id):
return api_base.entity_get_all(models.Subscription, user_id=user_id)
def subscription_get_all_by_target(target_type, target_id):
return api_base.entity_get_all(models.Subscription,
target_type=target_type,
target_id=target_id)
def subscription_get_count(**kwargs):
return api_base.entity_get_count(models.Subscription, **kwargs)
def subscription_create(values):
return api_base.entity_create(models.Subscription, values)
def subscription_delete(subscription_id):
subscription = subscription_get(subscription_id)
if subscription:
api_base.entity_hard_delete(models.Subscription, subscription_id)

View File

@ -0,0 +1,56 @@
# 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.
#
"""This migration creates The subscription model primary and secondary tables.
Revision ID: 021
Revises: 020
Create Date: 2014-07-10 15:37:30.662966
"""
# revision identifiers, used by Alembic.
revision = '021'
down_revision = '020'
from alembic import op
import sqlalchemy as sa
MYSQL_ENGINE = 'InnoDB'
MYSQL_CHARSET = 'utf8'
target_type_enum = sa.Enum('task', 'story', 'project', 'project_group')
def upgrade(active_plugins=None, options=None):
op.create_table(
'subscriptions',
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('target_type', target_type_enum, nullable=True),
sa.Column('target_id', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id'),
mysql_engine=MYSQL_ENGINE,
mysql_charset=MYSQL_CHARSET
)
def downgrade(active_plugins=None, options=None):
op.drop_table('subscriptions')
target_type_enum.drop(op.get_bind())

View File

@ -289,3 +289,15 @@ class Comment(Base):
content = Column(UnicodeText)
is_active = Column(Boolean, default=True)
# Subscription and notifications
class Subscription(Base):
_SUBSCRIPTION_TARGETS = ('task', 'story', 'project', 'project_group')
user_id = Column(Integer, ForeignKey('users.id'))
target_type = Column(Enum(*_SUBSCRIPTION_TARGETS))
# Cant use foreign key here as it depends on the type
target_id = Column(Integer)