From 810efd226e9ebe6c98680b49517d8b266f3f79d5 Mon Sep 17 00:00:00 2001 From: Nikita Konovalov Date: Thu, 10 Jul 2014 16:28:20 +0400 Subject: [PATCH] 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 --- storyboard/db/api/subscriptions.py | 46 +++++++++++++++ .../versions/021_subscription.py | 56 +++++++++++++++++++ storyboard/db/models.py | 12 ++++ 3 files changed, 114 insertions(+) create mode 100644 storyboard/db/api/subscriptions.py create mode 100644 storyboard/db/migration/alembic_migrations/versions/021_subscription.py diff --git a/storyboard/db/api/subscriptions.py b/storyboard/db/api/subscriptions.py new file mode 100644 index 00000000..7537c344 --- /dev/null +++ b/storyboard/db/api/subscriptions.py @@ -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) diff --git a/storyboard/db/migration/alembic_migrations/versions/021_subscription.py b/storyboard/db/migration/alembic_migrations/versions/021_subscription.py new file mode 100644 index 00000000..4f16dade --- /dev/null +++ b/storyboard/db/migration/alembic_migrations/versions/021_subscription.py @@ -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()) diff --git a/storyboard/db/models.py b/storyboard/db/models.py index fddf37c4..6937dc71 100644 --- a/storyboard/db/models.py +++ b/storyboard/db/models.py @@ -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)