diff options
Diffstat (limited to 'src/web/models')
-rw-r--r-- | src/web/models/__init__.py | 3 | ||||
-rw-r--r-- | src/web/models/article.py | 9 | ||||
-rw-r--r-- | src/web/models/tag.py | 22 | ||||
-rw-r--r-- | src/web/models/user.py | 2 |
4 files changed, 34 insertions, 2 deletions
diff --git a/src/web/models/__init__.py b/src/web/models/__init__.py index 53b692e2..1fc0c3e0 100644 --- a/src/web/models/__init__.py +++ b/src/web/models/__init__.py @@ -32,8 +32,9 @@ from .user import User from .article import Article from .icon import Icon from .category import Category +from .tag import Tag -__all__ = ['Feed', 'Role', 'User', 'Article', 'Icon', 'Category'] +__all__ = ['Feed', 'Role', 'User', 'Article', 'Icon', 'Category', 'Tag'] import os diff --git a/src/web/models/article.py b/src/web/models/article.py index 23708f6b..5261cb0d 100644 --- a/src/web/models/article.py +++ b/src/web/models/article.py @@ -29,6 +29,8 @@ __license__ = "GPLv3" from bootstrap import db from datetime import datetime from sqlalchemy import asc, desc, Index +from sqlalchemy.ext.associationproxy import association_proxy + from web.models.right_mixin import RightMixin @@ -49,6 +51,13 @@ class Article(db.Model, RightMixin): feed_id = db.Column(db.Integer(), db.ForeignKey('feed.id')) category_id = db.Column(db.Integer(), db.ForeignKey('category.id')) + # relationships + tag_objs = db.relationship('Tag', back_populates='article', + cascade='all,delete-orphan', + lazy=False, + foreign_keys='[Tag.article_id]') + tags = association_proxy('tag_objs', 'text') + # index idx_article_uid = Index('user_id') idx_article_uid_cid = Index('user_id', 'category_id') diff --git a/src/web/models/tag.py b/src/web/models/tag.py new file mode 100644 index 00000000..8d7fe4d4 --- /dev/null +++ b/src/web/models/tag.py @@ -0,0 +1,22 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +from sqlalchemy import Column, ForeignKey, Integer, String +from sqlalchemy.orm import relationship + +from bootstrap import db + + +class Tag(db.Model): + text = Column(String, primary_key=True, unique=False) + + # foreign keys + article_id = Column(Integer, ForeignKey('article.id', ondelete='CASCADE'), + primary_key=True) + + # relationships + article = relationship('Article', back_populates='tag_objs', + foreign_keys=[article_id]) + + def __init__(self, text): + self.text = text diff --git a/src/web/models/user.py b/src/web/models/user.py index 37bc78fd..460958e0 100644 --- a/src/web/models/user.py +++ b/src/web/models/user.py @@ -64,7 +64,7 @@ class User(db.Model, UserMixin, RightMixin): is_admin = db.Column(db.Boolean(), default=False) is_api = db.Column(db.Boolean(), default=False) - # relationship + # relationships categories = db.relationship('Category', backref='user', cascade='all, delete-orphan', foreign_keys=[Category.user_id]) |