Source

cubicweb-photoalbum / schema.py

# copyright 2010-2014 Sylvain Thenault - Adrien Di Mascio, all rights reserved.
# contact mailto:thenault@gmail.com
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""cubicweb-photoalbum schema"""

from yams.buildobjs import EntityType, RelationDefinition, String, Date
from yams.constraints import StaticVocabularyConstraint

from cubicweb.schema import ERQLExpression

from cubes.file.schema import File
from cubes.comment.schema import Comment


VISIBILITY_PERMISSIONS = {
    'read':   ('managers',
               ERQLExpression('X computed_visibility "public"'),
               ERQLExpression('X computed_visibility "authenticated", U in_group G, G name "users"'),
               ERQLExpression('X may_be_readen_by U')),
    'add':    ('managers', 'users'),
    'update': ('managers', 'owners',),
    'delete': ('managers', 'owners'),
    }


class Album(EntityType):
    __permissions__ = VISIBILITY_PERMISSIONS.copy()
    name = String(maxsize=128, required=True, fulltextindexed=True, unique=True)
    date = Date()


class in_album(RelationDefinition):
    __permissions__ = {'read':   ('managers', 'users', 'guests'),
                       'add':    ('managers', 'users'),
                       'delete': ('managers', 'owners'),
                       }
    subject = 'File'
    object = 'Album'
    cardinality = '?*'
    # composite = 'object'
    inlined = True


class use_thumbnail(RelationDefinition):
    subject = 'File'
    object = 'File'
    cardinality = '?*'
    inlined = True


class lowres_version(RelationDefinition):
    subject = 'File'
    object = 'File'
    cardinality = '?*'
    inlined = True


class width(RelationDefinition):
    subject = 'File'
    object = 'Int'
    cardinality = '?*'

class height(RelationDefinition):
    subject = 'File'
    object = 'Int'
    cardinality = '?*'


class latitude(RelationDefinition):
    subject = 'File'
    object = 'Float'
    cardinality = '?*'

class longitude(RelationDefinition):
    subject = 'File'
    object = 'Float'
    cardinality = '?*'


class comments(RelationDefinition):
    subject = 'Comment'
    object = 'File'
    cardinality = '1*'
    composite = 'object'


# security #####################################################################

File.__permissions__ = VISIBILITY_PERMISSIONS.copy()
Comment.__permissions__ = VISIBILITY_PERMISSIONS.copy()


class visibility(RelationDefinition):
    __permissions__ = {'read':   ('managers',),
                       'add':    ('managers',),
                       'update': ('managers',),
                       }
    subject = ('Album', 'File', 'Comment')
    object = 'String'
    constraints = [StaticVocabularyConstraint(('public', 'authenticated',
                                               'restricted', 'parent'))]
    default = 'parent'
    cardinality = '11' # required


class computed_visibility(RelationDefinition):
    __permissions__ = {'read':   ('managers', 'users', 'guests'),
                       'add':    ('managers',),
                       'update': ('managers',),
                       }
    subject = ('Album', 'File', 'Comment')
    object = 'String'
    constraints = [StaticVocabularyConstraint(('public', 'authenticated',
                                               'restricted'))]
    cardinality = '?1' # automatically computed


class may_be_readen_by(RelationDefinition):
    __permissions__ = {'read':   ('managers',),
                       'add':    ('managers',),
                       'delete': ('managers',),
                       }
    subject = ('Album', 'File', 'Comment')
    object = 'CWUser'


# cool urls ####################################################################

class entity_url_of(RelationDefinition):
    subject = 'EntityURL'
    object = 'Album'