# -*- 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 datetime import datetime from box import Box from flask_user import UserMixin from scribeengine.db import Model, Table, Column, ForeignKey, String, Text, Integer, Boolean, DateTime, \ relationship 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) ) nodes_taxonomies = Table( 'nodes_terms', Column('node_id', Integer, ForeignKey('nod.id'), primary_key=True), Column('taxonomy_id', Integer, ForeignKey('taxonomy.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 Taxonomy(Model): """ This is a grouping of related terms, like a tags or categories """ __tablename__ = 'taxonomies' id = Column(Integer, primary_key=True) name = Column(String(100), nullable=False) description = Column(Text) slug = Column(String(255), nullable=False, index=True, unique=True) def __str__(self): return self.name class Node(Model): """ Nodes are the basic content type """ __tablename__ = 'nodes' id = Column(Integer, primary_key=True) slug = Column(String(255), nullable=False, index=True, unique=True) type = Column(String(255), nullable=False, index=True) created = Column(DateTime, nullable=False, default=datetime.utcnow) modified = Column(DateTime, nullable=False, default=datetime.utcnow) revision_id = Column(Integer, ForeignKey('revisions.id'), nullable=False, index=True) current_revision = relationship('Revision', backref='node') @property def complete(self): """Return a "full" or "complete" node, with all fields and data""" node_dict = Box({ 'id': self.id, 'slug': self.slug, 'type': self.type, 'created': self.created, 'modified': self.modified, 'title': self.current_revision.title, 'body': self.current_revision.body, 'format': self.current_revision.format }) for field in self.fields: node_dict[field.name] = { 'name': field.name, 'type': field.type, 'title': field.field.title, 'data': field.data } return node_dict class Field(Model): """ A field is a model for field types on nodes """ __tablename__ = 'fields' id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False, unique=True, index=True) type = Column(String(255), nullable=False) title = Column(String(255), nullable=False) description = Column(Text) created = Column(DateTime, nullable=False, default=datetime.utcnow) modified = Column(DateTime, nullable=False, default=datetime.utcnow) class FieldRevision(Model): """ A revision of a field on a node """ __tablename__ = 'field_revisions' id = Column(Integer, primary_key=True) version = Column(Integer, nullable=False) title = Column(String(255), nullable=False) body = Column(Text) format = Column(Text, nullable=False) slug = Column(String(255), nullable=False) created = Column(DateTime, nullable=False, index=True, default=datetime.utcnow) node_id = Column(Integer, ForeignKey('nodes.id'), nullable=False) class NodeField(Model): """ A node field is a field on a particular node """ __tablename__ = 'node_fields' id = Column(Integer, primary_key=True) field_id = Column(Integer, ForeignKey('fields.id')) node_id = Column(Integer, ForeignKey('nodes.id')) revision_id = Column(Integer, ForeignKey('field_revisions.id'), nullable=False, index=True) title = Column(String(255), nullable=False) description = Column(Text) created = Column(DateTime, nullable=False, default=datetime.utcnow) modified = Column(DateTime, nullable=False, default=datetime.utcnow) current_revision = relationship('FieldRevision', backref='node_field') field = relationship('Field', backref='node_field') 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) def __str__(self): return self.filename 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') def __str__(self): return self.title 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) def __str__(self): return self.name 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) def __str__(self): return self.name class Term(Model): """ A term is an item in a taxonomy. A term can be a category name, or a tag in a blog post. """ __tablename__ = 'terms' id = Column(Integer, primary_key=True) name = Column(String(100), nullable=False) slug = Column(String(255), nullable=False, index=True) taxonomy_id = Column(Integer, ForeignKey('taxonomies.id')) def __str__(self): return self.name 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='author') 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 def __str__(self): return str(id(self)) 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') class Site(object): """ Generic model to hold the site-specfic data """ name = None slogan = None about = None def __init__(self, name, slogan=None, about=None): self.name = name self.slogan = slogan self.about = about