# Buggy code
def __init__(self, request):
    # Make it available for the authorization policy.
    self.prefixed_userid = getattr(request, "prefixed_userid", None)

    self._check_permission = request.registry.permission.check_permission

    # Partial collections of ProtectedResource:
    self.shared_ids = []

    # Store service, resource, record and required permission.
    service = utils.current_service(request)

    is_on_resource = (
        service is not None
        and hasattr(service, "viewset")
        and hasattr(service, "resource")
    )

    if is_on_resource:
        self.on_collection = getattr(service, "type", None) == "collection"
        self.permission_object_id = self.get_permission_object_id(request)

        # Decide what the required unbound permission is depending on the
        # method that's being requested.
        if request.method.lower() == "put":
            # In the case of a "PUT", check if the targetted record already
            # exists, return "write" if it does, "create" otherwise.

            # If the view exists, call it with the request and catch an
            # eventual NotFound.
            resource = service.resource(request=request, context=self)
            try:
                resource.collection.get_record(resource.record_id)
            except storage_exceptions.RecordNotFoundError:
                self.permission_object_id = service.collection_path.format(
                    **request.matchdict
                )
                self.required_permission = "create"
            else:
                self.required_permission = "write"
        else:
            method = request.method.lower()
            self.required_permission = self.method_permissions.get(method)

        self.resource_name = service.viewset.get_name(service.resource)

        if self.on_collection:
            object_id_match = self.get_permission_object_id(request, "*")
            self.get_shared_ids = functools.partial(
                request.registry.permission.principals_accessible_objects,
                object_id_match=object_id_match,
            )

        settings = request.registry.settings
        setting = "cliquet.%s_%s_principals" % (
            self.resource_name,
            self.required_permission,
        )
        self.allowed_principals = aslist(settings.get(setting, ""))


# Traceback (TypeError)
2016-02-22 15:21:36,730 ERROR [venusian][waitress] "POST  /v1/buckets/default/collections/test-collection-2aa00fc8-b6e2-41d2-a6a5-28d23ca6fce5/records" ? (? ms) 'str' object does not support item assignment lang=None; exception=Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/pyramid/tweens.py", line 20, in excview_tween
response = handler(request)
File "/usr/local/lib/python2.7/dist-packages/pyramid_tm/__init__.py", line 101, in tm_tween
reraise(*exc_info)
File "/usr/local/lib/python2.7/dist-packages/pyramid_tm/__init__.py", line 83, in tm_tween
response = handler(request)
File "/usr/local/lib/python2.7/dist-packages/pyramid/router.py", line 145, in handle_request
view_name
File "/usr/local/lib/python2.7/dist-packages/pyramid/view.py", line 541, in _call_view
response = view_callable(context, request)
File "/usr/local/lib/python2.7/dist-packages/pyramid/config/views.py", line 385, in viewresult_to_response
result = view(context, request)
File "/usr/local/lib/python2.7/dist-packages/pyramid/config/views.py", line 507, in _requestonly_view
response = view(request)
File "/usr/local/lib/python2.7/dist-packages/kinto/plugins/default_bucket/__init__.py", line 108, in default_bucket
create_bucket(request, bucket_id)
File "/usr/local/lib/python2.7/dist-packages/kinto/plugins/default_bucket/__init__.py", line 35, in create_bucket
bucket = resource.model.create_record({'id': bucket_id})
File "/usr/local/lib/python2.7/dist-packages/cliquet/resource/model.py", line 294, in create_record
unique_fields)
File "/usr/local/lib/python2.7/dist-packages/cliquet/resource/model.py", line 177, in create_record
auth=self.auth)
File "/usr/local/lib/python2.7/dist-packages/cliquet/storage/postgresql/__init__.py", line 234, in create
for_creation=True)
File "/usr/local/lib/python2.7/dist-packages/cliquet/storage/postgresql/__init__.py", line 734, in _check_unicity
record = self.get(collection_id, parent_id, existing['id'])
File "/usr/local/lib/python2.7/dist-packages/cliquet/storage/postgresql/__init__.py", line 263, in get
record[id_field] = object_id
TypeError: 'str' object does not support item assignment; uid=d3f58ae9f309fa494504a2347a13f80c811b64e9d8815299b9c1467f85854288; errno=None; agent=node-superagent/1.7.2; authn_type=BasicAuth; collection_id=bucket; collection_timestamp=1456154496656

# Fixed code
def __init__(self, request):
    # Make it available for the authorization policy.
    self.prefixed_userid = getattr(request, "prefixed_userid", None)

    self._check_permission = request.registry.permission.check_permission

    # Partial collections of ProtectedResource:
    self.shared_ids = []

    # Store service, resource, record and required permission.
    service = utils.current_service(request)

    is_on_resource = (
        service is not None
        and hasattr(service, "viewset")
        and hasattr(service, "resource")
    )

    if is_on_resource:
        self.on_collection = getattr(service, "type", None) == "collection"
        self.permission_object_id = self.get_permission_object_id(request)

        # Decide what the required unbound permission is depending on the
        # method that's being requested.
        if request.method.lower() == "put":
            # In the case of a "PUT", check if the targetted record already
            # exists, return "write" if it does, "create" otherwise.

            # If the view exists, use its collection to catch an
            # eventual NotFound.
            resource = service.resource(request=request, context=self)
            try:
                record = resource.collection.get_record(resource.record_id)
                self.current_record = record
            except storage_exceptions.RecordNotFoundError:
                self.permission_object_id = service.collection_path.format(
                    **request.matchdict
                )
                self.required_permission = "create"
            else:
                self.required_permission = "write"
        else:
            method = request.method.lower()
            self.required_permission = self.method_permissions.get(method)

        self.resource_name = service.viewset.get_name(service.resource)

        if self.on_collection:
            object_id_match = self.get_permission_object_id(request, "*")
            self.get_shared_ids = functools.partial(
                request.registry.permission.principals_accessible_objects,
                object_id_match=object_id_match,
            )

        settings = request.registry.settings
        setting = "cliquet.%s_%s_principals" % (
            self.resource_name,
            self.required_permission,
        )
        self.allowed_principals = aslist(settings.get(setting, ""))