This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
old-scribeengine/scribeengine/models.py

242 lines
8.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# ScribeEngine - Open Source Blog Software #
# --------------------------------------------------------------------------- #
# Copyright (c) 2010-2017 Raoul Snyman #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
The :mod:`~scribeengine.models` module contains all the database models
"""
from flask_user import UserMixin
from sqlalchemy import func
from scribeengine.db import Model, Table, Column, ForeignKey, String, Text, Integer, Boolean, DateTime, \
relationship, backref
categories_posts = Table(
'categories_posts',
Column('category_id', Integer, ForeignKey('categories.id'), primary_key=True),
Column('post_id', Integer, ForeignKey('posts.id'), primary_key=True)
)
permissions_roles = Table(
'permissions_roles',
Column('permission_id', Integer, ForeignKey('permissions.id'), primary_key=True),
Column('role_id', Integer, ForeignKey('roles.id'), primary_key=True)
)
posts_tags = Table(
'posts_tags',
Column('post_id', Integer, ForeignKey('posts.id'), primary_key=True),
Column('tag_id', Integer, ForeignKey('tags.id'), primary_key=True)
)
roles_users = Table(
'roles_users',
Column('user_id', Integer, ForeignKey('users.id'), primary_key=True),
Column('role_id', Integer, ForeignKey('roles.id'), primary_key=True)
)
class Category(Model):
"""
This is a category for blog posts.
"""
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
description = Column(Text)
url = Column(String(255), nullable=False, index=True, unique=True)
class Comment(Model):
"""
All blog posts have comments. This is a single comment.
"""
__tablename__ = 'comments'
id = Column(Integer, primary_key=True)
post_id = Column(Integer, ForeignKey('posts.id'), nullable=True)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
title = Column(String(100), nullable=False)
body = Column(Text, nullable=False)
status = Column(String(10), default='moderated')
created = Column(DateTime, server_default=func.now())
modified = Column(DateTime, server_default=func.now())
class File(Model):
"""
A file in the media library.
"""
__tablename__ = 'files'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
media_type_id = Column(Integer, ForeignKey('media_types.id'), nullable=False)
filename = Column(String(255), nullable=False, index=True)
mimetype = Column(String(255))
path = Column(String(255))
size = Column(Integer, default=0)
class MediaType(Model):
"""
Distinguishes between different types of media.
"""
__tablename__ = 'media_types'
id = Column(Integer, primary_key=True)
title = Column(String(255), nullable=False, index=True)
files = relationship('File', backref='media_type')
class Page(Model):
"""
A page on the blog. This is separate from a blog entry, for things like
about pages.
"""
__tablename__ = 'pages'
id = Column(Integer, primary_key=True)
title = Column(String(255), nullable=False)
body = Column(Text)
url = Column(String(255), nullable=False, index=True, unique=True)
created = Column(DateTime, server_default=func.now())
modified = Column(DateTime, server_default=func.now())
class Permission(Model):
"""
A single permission.
"""
__tablename__ = 'permissions'
id = Column(Integer, primary_key=True)
name = Column(String(80), nullable=False, index=True)
description = Column(Text)
class Post(Model):
"""
The most import part of all of this, the blog post.
"""
__tablename__ = 'posts'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
title = Column(String(255), nullable=False, index=True)
body = Column(Text, nullable=False, index=True)
url = Column(String(255), nullable=False, index=True)
status = Column(String(10), default='draft', index=True)
comment_status = Column(String(10), default='open')
created = Column(DateTime, server_default=func.now())
modified = Column(DateTime, server_default=func.now())
categories = relationship('Category', backref='posts', secondary=categories_posts)
comments = relationship('Comment', backref='post', order_by='Comment.created.asc()')
tags = relationship('Tag', backref=backref('posts', order_by='Post.created.desc()'), secondary=posts_tags)
class Role(Model):
"""
A role defines a set of permissions.
"""
__tablename__ = 'roles'
id = Column(Integer, primary_key=True)
name = Column(String(80), nullable=False, index=True)
description = Column(Text)
permissions = relationship('Permission', backref='roles', secondary=permissions_roles)
class Tag(Model):
"""
A tag, an unstructured category, for blog posts.
"""
__tablename__ = 'tags'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
url = Column(String(255), nullable=False, index=True)
class User(Model, UserMixin):
"""
The user.
"""
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
email = Column(String(200), nullable=False, unique=True, index=True)
username = Column(String(200), nullable=False, unique=True, index=True)
password = Column(String(64), nullable=False)
nick = Column(String(50), nullable=False, index=True)
first_name = Column(String(100), default='')
last_name = Column(String(100), default='')
homepage = Column(String(200), default='')
timezone = Column(String(200), default='UTC')
activation_key = Column(String(40), default=None)
confirmed_at = Column(DateTime)
active = Column('is_active', Boolean, nullable=False, server_default='0')
comments = relationship('Comment', backref='user'),
files = relationship('File', backref='user'),
posts = relationship('Post', backref='user'),
roles = relationship('Role', backref='users', secondary=roles_users)
def has_permission(self, permission):
if isinstance(permission, str):
for role in self.roles:
for perm in role.permissions:
if perm.name == permission:
return True
return False
elif isinstance(permission, Permission):
for role in self.roles:
for perm in role.permissions:
if perm == permission:
return True
return False
elif isinstance(permission, list):
for role in self.roles:
for perm in role.permissions:
if perm.name in permission:
return True
return False
else:
return False
class Variable(Model):
"""
System variables.
"""
__tablename__ = 'variables'
key = Column(String(100), primary_key=True, index=True)
value = Column(String(100), nullable=False)
type = Column(String(10), default='string')