convert.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. from marshmallow_mongoengine.conversion import fields
  3. def _is_field(value):
  4. return (
  5. isinstance(value, type) and
  6. issubclass(value, fields.Field)
  7. )
  8. class ModelConverter(object):
  9. """Class that converts a mongoengine Document into a dictionary of
  10. corresponding marshmallow `Fields <marshmallow.fields.Field>`.
  11. """
  12. def fields_for_model(self, model, fields_kwargs=None, fields=None):
  13. result = {}
  14. fields_kwargs = fields_kwargs or {}
  15. for field_name, field_me in model._fields.items():
  16. if fields and field_name not in fields:
  17. continue
  18. if field_name in fields_kwargs:
  19. field_ma_cls = self.convert_field(field_me,
  20. **fields_kwargs[field_name])
  21. else:
  22. field_ma_cls = self.convert_field(field_me)
  23. if field_ma_cls:
  24. result[field_name] = field_ma_cls
  25. return result
  26. def convert_field(self, field_me, instance=True, **kwargs):
  27. field_builder = fields.get_field_builder_for_data_type(field_me)
  28. if not instance:
  29. return field_builder.marshmallow_field_cls
  30. return field_builder.build_marshmallow_field(**kwargs)
  31. def field_for(self, model, property_name, **kwargs):
  32. field_me = getattr(model, property_name)
  33. field_builder = fields.get_field_builder_for_data_type(field_me)
  34. return field_builder.build_marshmallow_field(**kwargs)
  35. default_converter = ModelConverter()
  36. fields_for_model = default_converter.fields_for_model
  37. """Generate a dict of field_name: `marshmallow.fields.Field` pairs for the
  38. given model.
  39. :param model: The Mongoengine Document model
  40. :return: dict of field_name: Field instance pairs
  41. """
  42. convert_field = default_converter.convert_field
  43. """Convert a Mongoengine `Field` to a field instance or class.
  44. :param Property field_me: Mongoengine Field Property.
  45. :param fields_kwargs: Dict of per-field kwargs to pass at field creation.
  46. :param bool instance: If `True`, return `Field` instance, computing
  47. relevant kwargs from the given property. If `False`, return
  48. the `Field` class.
  49. :param kwargs: Additional keyword arguments to pass to the field constructor.
  50. :return: A `marshmallow.fields.Field` class or instance.
  51. """
  52. field_for = default_converter.field_for
  53. """Convert a property for a mapped Mongoengine Document class to a
  54. marshmallow `Field`.
  55. Example: ::
  56. date_created = field_for(Author, 'date_created', dump_only=True)
  57. author = field_for(Book, 'author')
  58. :param type ,: A Mongoengine Document mapped class.
  59. :param str property_name: The name of the property to convert.
  60. :param kwargs: Extra keyword arguments to pass to `property2field`
  61. :return: A `marshmallow.fields.Field` class or instance.
  62. """