Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def count(n):
while True:
yield n
n += 1
class User(Model):
id = IntType(default=next(count(42)))
name = StringType()
email = StringType()
password = StringType()
class Options:
roles = {
'create': all_fields - ['id'],
'public': all_fields - ['password'],
'nospam': blacklist('password') + blacklist('email'),
'empty': whitelist(),
'everything': blacklist(),
}
roles = User.Options.roles
assert len(roles['create']) == 3
assert len(roles['public']) == 3
assert len(roles['nospam']) == 2
assert len(roles['empty']) == 0
assert len(roles['everything']) == 0
# Sets sort different with different Python versions. We should be getting something back
# like: "whitelist('password', 'email', 'name')"
s = str(roles['create'])
assert s.startswith('whitelist(') and s.endswith(')')
assert sorted(s[10:-1].split(', ')) == ["'email'", "'name'", "'password'"]
yield n
n += 1
class User(Model):
id = IntType(default=next(count(42)))
name = StringType()
email = StringType()
password = StringType()
class Options:
roles = {
'create': all_fields - ['id'],
'public': all_fields - ['password'],
'nospam': blacklist('password') + blacklist('email'),
'empty': whitelist(),
'everything': blacklist(),
}
roles = User.Options.roles
assert len(roles['create']) == 3
assert len(roles['public']) == 3
assert len(roles['nospam']) == 2
assert len(roles['empty']) == 0
assert len(roles['everything']) == 0
# Sets sort different with different Python versions. We should be getting something back
# like: "whitelist('password', 'email', 'name')"
s = str(roles['create'])
assert s.startswith('whitelist(') and s.endswith(')')
assert sorted(s[10:-1].split(', ')) == ["'email'", "'name'", "'password'"]
# Similar, but now looking for:
def test_subclassing_overides_roles():
class Parent(Model):
id = StringType()
gender = StringType()
name = StringType()
class Options:
roles = {
'public': blacklist("id", "gender"),
'gender': blacklist("gender")
}
class GrandParent(Parent):
age = IntType()
family_secret = StringType()
class Options:
roles = {
'grandchildren': whitelist("age"),
'public': blacklist("id", "family_secret")
}
gramps = GrandParent({
"id": "1",
"name": "Edward",
def test_EmbeddedDocType_overriding_Options(self):
class User3(User2):
class Options:
roles = {
"embedded": (blacklist("_password") +
SchematicsDocument.Options.roles['embedded']),
}
class SuperUser(SchematicsDocument):
user = EmbeddedDocType(User3)
u = User3(dict(name="Ryan", password="ChangeMe"))
assert 'id' not in u.serialize()
u.store(self.db)
su = SuperUser()
su.user = u
su.store(self.db)
assert isinstance(su.user, User3)
print su.user.Options.roles
assert '_password' in su.user.Options.roles['embedded']
title = StringType()
class Options:
roles = {
"public": wholelist(),
}
class Player(Model):
id = StringType()
secret = StringType()
xp_level = ModelType(ExperienceLevel)
class Options:
roles = {
"public": blacklist("secret")
}
p = Player(dict(
id="1",
secret="super_secret",
xp_level={
"level": 1,
"title": "Starter"
}
))
d = p.serialize(role="public")
assert d == {
"id": "1",
"xp_level": {
"level": 1,
def test_subclassing_overides_roles():
class Parent(Model):
id = StringType()
gender = StringType()
name = StringType()
class Options:
roles = {
'public': blacklist("id", "gender"),
'gender': blacklist("gender")
}
class GrandParent(Parent):
age = IntType()
family_secret = StringType()
class Options:
roles = {
'grandchildren': whitelist("age"),
'public': blacklist("id", "family_secret")
}
gramps = GrandParent({
"id": "1",
"name": "Edward",
"gender": "Male",
if not attrval.name:
attrval.name = attrname
return ModelMeta.__new__(mcs, name, bases, attrs)
class Document(Model):
"""
Schematics-based CouchdDB Document class.
"""
__metaclass__ = DocumentMeta
class Options(object):
"""Export options for Document."""
serialize_when_none = False
roles = {
"default": blacklist("doc_id"),
"embedded": blacklist("_id", "_rev", "doc_type"),
}
_id = StringType(deserialize_from=['id', 'doc_id'])
_rev = StringType()
doc_type = StringType()
def __init__(self, raw_data=None, deserialize_mapping=None):
if raw_data is None:
raw_data = { }
super(Document, self).__init__(raw_data=raw_data,
deserialize_mapping=deserialize_mapping)
self.doc_type = self.__class__.__name__
def __repr__(self):
return '<%s %r@%r %r>' % (type(self).__name__, self.id, self.rev,
def validate_cpv_group(items, *args):
if items and len(set([i.classification.id[:3] for i in items])) != 1:
raise ValidationError(u"CPV group of items be identical")
plain_role = (blacklist('_attachments', 'revisions', 'dateModified') + schematics_embedded_role)
create_role = (blacklist('owner_token', 'owner', '_attachments', 'revisions', 'dateModified', 'doc_id', 'tenderID', 'bids', 'documents', 'awards', 'questions', 'complaints', 'auctionUrl', 'status', 'auctionPeriod', 'awardPeriod', 'procurementMethod', 'awardCriteria', 'submissionMethod') + schematics_embedded_role)
edit_role = (blacklist('lots', 'owner_token', 'owner', '_attachments', 'revisions', 'dateModified', 'doc_id', 'tenderID', 'bids', 'documents', 'awards', 'questions', 'complaints', 'auctionUrl', 'auctionPeriod', 'awardPeriod', 'procurementMethod', 'awardCriteria', 'submissionMethod', 'mode') + schematics_embedded_role)
cancel_role = whitelist('status')
view_role = (blacklist('owner_token', '_attachments', 'revisions') + schematics_embedded_role)
listing_role = whitelist('dateModified', 'doc_id')
auction_view_role = whitelist('tenderID', 'dateModified', 'bids', 'auctionPeriod', 'minimalStep', 'auctionUrl', 'features', 'lots')
auction_post_role = whitelist('bids')
auction_patch_role = whitelist('auctionUrl', 'bids', 'lots')
enquiries_role = (blacklist('owner_token', '_attachments', 'revisions', 'bids', 'numberOfBids') + schematics_embedded_role)
auction_role = (blacklist('owner_token', '_attachments', 'revisions', 'bids') + schematics_embedded_role)
chronograph_role = whitelist('status', 'enquiryPeriod', 'tenderPeriod', 'auctionPeriod', 'awardPeriod', 'lots')
chronograph_view_role = whitelist('status', 'enquiryPeriod', 'tenderPeriod', 'auctionPeriod', 'awardPeriod', 'awards', 'lots', 'doc_id', 'submissionMethodDetails', 'mode', 'numberOfBids', 'complaints')
Administrator_role = whitelist('status', 'mode', 'procuringEntity')
class Tender(SchematicsDocument, Model):
"""Data regarding tender process - publicly inviting prospective contractors to submit bids for evaluation and selecting a winner or winners."""
class Options:
roles = {
'plain': plain_role,
'create': create_role,
'edit': edit_role,
'edit_active.enquiries': edit_role,
'edit_active.tendering': cancel_role,
'edit_active.auction': cancel_role,
documents = ListType(ModelType(Document), default=list())
items = ListType(ModelType(Item))
suppliers = ListType(ModelType(Organization), min_size=1, max_size=1)
date = IsoDateTimeType()
class ContractAuctions(Contract):
class Options:
roles = {
'create': blacklist(
'id',
'status',
'date',
'documents',
'dateSigned'),
'edit': blacklist(
'id',
'documents',
'date',
'awardID',
'suppliers',
'items',
'contractID'),
'embedded': schematics_embedded_role,
'view': schematics_default_role,
}
awardID = StringType(required=True)
def validate_awardID(self, data, awardID):
if (
awardID and
isinstance(data['__parent__'], Model) and
from couchdb_schematics.document import SchematicsDocument
schematics_default_role = SchematicsDocument.Options.roles['default'] + blacklist("__parent__")
schematics_embedded_role = SchematicsDocument.Options.roles['embedded'] + blacklist("__parent__")
plain_role = (blacklist('_attachments', 'revisions', 'dateModified') + schematics_embedded_role)
listing_role = whitelist('dateModified', 'doc_id')
draft_role = whitelist('status')
document_create_role = blacklist('id', 'datePublished', 'dateModified', 'author', 'download_url')
document_edit_role = blacklist('id', 'url', 'datePublished', 'dateModified', 'author', 'hash', 'download_url')
document_embedded_role = (blacklist('url', 'download_url') + schematics_embedded_role)
document_view_role = (blacklist('revisions') + schematics_default_role)
document_revisions_role = whitelist('url', 'dateModified')
item_create_role = blacklist()
item_edit_role = blacklist('id')
item_view_role = (schematics_default_role + blacklist())
item_roles = {
'create': item_create_role,
'edit': item_edit_role,
'view': item_view_role,
}
document_roles = {
'create': document_create_role,
'edit': document_edit_role,
'embedded': document_embedded_role,
'view': document_view_role,