Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MongoDB BinaryField #81

Open
danaki opened this issue May 3, 2012 · 3 comments
Open

Add support for MongoDB BinaryField #81

danaki opened this issue May 3, 2012 · 3 comments
Labels

Comments

@danaki
Copy link

danaki commented May 3, 2012

Here's what works for me:

class BinaryField(BaseField):
    def __init__(self, subtype=None, **kwargs):
        self.subtype = subtype
        super(BinaryField, self).__init__(**kwargs)

    def __set__(self, instance, value):
        if isinstance(value, (str, unicode)):
            kwargs = {}
            if not self.subtype is None:
                kwargs = {'subtype': self.subtype}
            value = pymongo.binary.Binary(value, **kwargs)

        instance._data[self.field_name] = value

    def _jsonschema_type(self):
        return 'string'

    def for_python(self, value):
        try:
            return pymongo.binary.Binary(value)
        except Exception, e:
            raise ShieldException('Invalid Binary', self.field_name, value)

    def for_json(self, value):
        return str(value)

    def validate(self, value):
        if not isinstance(value, pymongo.binary.Binary):
            try:
                kwargs = {}
                if not self.subtype is None:
                    kwargs = {'subtype': self.subtype}
                value = pymongo.binary.Binary(value, **kwargs)
            except Exception, e:
                raise ShieldException('Invalid Binary', self.field_name, value)
        return value
@j2labs
Copy link

j2labs commented Nov 26, 2012

If you submit a pull request, I'd be happy to take this.

@bdickason
Copy link
Contributor

@danaki is this still a feature you'd like to see supported? @j2labs I know you have some Mongo stuff going on in /contrib, is this still relevant?

@j2labs
Copy link

j2labs commented Sep 25, 2013

I could see it being useful. I'd be happy to have some more mongo'ish
support in contrib, but I think it should be thought of as bson stuff
instead, since that's the library where this stuff lives AND bson has no
database knowledge, just like Schematics.

Full bson support would be superb. I'm not sure what that would look like
though. Perhaps the output from to_native could be fed into a call for
bson conversion, or maybe something more clever.

But, let's move it to contrib if we do anything new here.

On Wed, Sep 25, 2013 at 6:22 PM, Brad Dickason notifications@github.comwrote:

@danaki https://github.com/danaki is this still a feature you'd like to
see supported? @j2labs https://github.com/j2labs I know you have some
Mongo stuff going on in /contrib, is this still relevant?


Reply to this email directly or view it on GitHubhttps://github.com/j2labs/schematics/issues/81#issuecomment-25129715
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants