This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
themis.fields/themisfields/__init__.py

96 lines
2.7 KiB
Python

from zope.interface import implements, implementsOnly
from zope.schema import Choice, Field, List, Date
from zope.schema.interfaces import IFromUnicode
from zope.schema.interfaces import WrongType
from themisfields.interfaces import ICommission
from themisfields.vocabs import CommissionsSource
from themisfields.interfaces import IAuthor
from themisfields.vocabs import AuthorsSource
from themisfields.interfaces import IAuthors
from themisfields.interfaces import IDate
from themisfields.interfaces import ISubjects, IList
class Commission(Field):
implements(ICommission, IFromUnicode)
def __init__(self, **kw):
self.vocabulary = CommissionsSource
super(Commission, self).__init__(**kw)
source = property(lambda self: self.vocabulary)
def bind(self, object):
"""See zope.schema._bootstrapinterfaces.IField."""
clone = super(Commission, self).bind(object)
clone.vocabulary = self.vocabulary(object)
return clone
def _validate(self, value):
super(Commission, self)._validate(value)
vocabulary = self.vocabulary
if value not in vocabulary:
raise ConstraintNotSatisfied(value)
def fromUnicode(self, str):
self.validate(str)
return str
class Author(Field):
implements(IAuthor, IFromUnicode)
def __init__(self, **kw):
self.vocabulary = AuthorsSource
super(Author, self).__init__(**kw)
source = property(lambda self: self.vocabulary)
def bind(self, object):
"""See zope.schema._bootstrapinterfaces.IField."""
clone = super(Author, self).bind(object)
clone.vocabulary = self.vocabulary(object)
return clone
def _validate(self, value):
super(Author, self)._validate(value)
vocabulary = self.vocabulary
if value not in vocabulary:
raise ConstraintNotSatisfied(value)
def fromUnicode(self, str):
self.validate(str)
return str
class Authors(List):
implements(IAuthors)
def __init__(self, **kw):
kw['value_type'] = Author(title=u'Author')
kw['unique'] = False
super(Authors, self).__init__(**kw)
from vocabs import SubjectsVocabulary, SubjectsVocabularyFactory
from vocabs import SubjectsSource
class Subjects(Field):
implements(ISubjects, IList)
_type = list
def __init__(self, **kw):
self.required = False
self.value_type = Choice(source=SubjectsSource())
self.unique = True
self.min_length = None
self.max_length = None
for attr in ('min_length', 'max_length', 'unique', 'value_type'):
if attr in kw: del kw[attr]
super(Subjects, self).__init__(**kw)