Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #164 from Connexions/issue-138
Browse files Browse the repository at this point in the history
Fix issue #138
  • Loading branch information
reedstrm committed Dec 11, 2014
2 parents 9f20214 + db1381c commit 5a7ec0c
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 14 deletions.
62 changes: 61 additions & 1 deletion cnxauthoring/schemata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,66 @@ def deferred_datetime_missing(node, kw):
return dt


class Trinary(colander.SchemaType):
"""A type representing a trivalued logic object with - 3 states.
That is true, false and unknown. These are represented in Python
as True, False and None.
"""

def __init__(self, false_choices=('false', '0',),
true_choices=('true', '1',),
unknown_choices=('none', '',),
true_val=True, false_val=False, unknown_val=None):
self.true_val = true_val
self.false_val = false_val
self.unknown_val = unknown_val
self.false_choices = false_choices
self.true_choices = true_choices
self.unknown_choices = unknown_choices

def serialize(self, node, appstruct):
if appstruct is colander.null:
return colander.null

if appstruct is None:
return self.unknown_val
else:
return appstruct and self.true_val or self.false_val

def deserialize(self, node, cstruct):
_ = colander._
if cstruct is colander.null:
return colander.null
elif cstruct is None:
return None

try:
result = str(cstruct)
except:
raise colander.Invalid(
node,
_('${val} is not a string', mapping={'val':cstruct})
)
result = result.lower()

if result in self.unknown_choices:
state = None
elif result in self.false_choices:
state = False
elif result in self.true_choices:
state = True
else:
raise colander.Invalid(
node,
_('"${val}" is neither in (${false_choices}) '
'nor in (${true_choices})',
mapping={'val':cstruct,
'false_choices': self.false_reprs,
'true_choices': self.true_reprs })
)
return state


class LicenseSchema(colander.MappingSchema):
"""Schema for ``License``"""

Expand Down Expand Up @@ -74,7 +134,7 @@ def schema_type(self, **kw):

class RoleSchema(UserSchema):
has_accepted = colander.SchemaNode(
colander.Boolean(),
Trinary(),
missing=colander.drop,
)
requester = colander.SchemaNode(
Expand Down
64 changes: 63 additions & 1 deletion cnxauthoring/tests/intercept.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,69 @@ def _amend_publishing_data():
"""This contains data modifications of the publishing/archive data
that are specific to the authoring tests.
"""
# NOOP
conn_str = publishing_settings()[config.CONNECTION_STRING]

with psycopg2.connect(conn_str) as db_connection:
with db_connection.cursor() as cursor:
cursor.execute("""\
INSERT INTO role_acceptances (uuid, user_id, role_type, accepted)
SELECT uuid, unnest(authors), 'Author', TRUE FROM modules
UNION
SELECT uuid, unnest(maintainers), 'Publisher'::role_types, TRUE FROM modules
UNION
SELECT uuid, unnest(licensors), 'Copyright Holder'::role_types, TRUE
FROM modules
UNION
SELECT m.uuid, unnest(personids), 'Author'::role_types, TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 1
UNION
SELECT m.uuid, unnest(personids), 'Copyright Holder'::role_types, TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 2
UNION
SELECT m.uuid, unnest(personids), 'Publisher'::role_types, TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 3
UNION
SELECT m.uuid, unnest(personids), 'Translator'::role_types, TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 4
UNION
SELECT m.uuid, unnest(personids), 'Editor'::role_types, TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 5
-- Note, no legacy mapping for Illustrator
;
INSERT INTO license_acceptances (uuid, user_id, accepted)
SELECT uuid, unnest(authors), TRUE FROM modules
UNION
SELECT uuid, unnest(maintainers), TRUE FROM modules
UNION
SELECT uuid, unnest(licensors), TRUE
FROM modules
UNION
SELECT m.uuid, unnest(personids), TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 1
UNION
SELECT m.uuid, unnest(personids), TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 2
UNION
SELECT m.uuid, unnest(personids), TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 3
UNION
SELECT m.uuid, unnest(personids), TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 4
UNION
SELECT m.uuid, unnest(personids), TRUE
FROM moduleoptionalroles NATURAL JOIN latest_modules AS m
WHERE roleid = 5
;""")


def install_intercept():
Expand Down
Loading

0 comments on commit 5a7ec0c

Please sign in to comment.