PKEN-OOdrf_lighten/__init__.py"""A way to remove unwanted fields from a serializer""" __version__ = "0.3.3" PK"FN drf_lighten/serializers.pyimport operator from django.utils.translation import ugettext class DynamicFieldsMixin(object): def __init__(self, *args, **kwargs): fields = kwargs.pop('fields', None) exclude = kwargs.pop('exclude', None) if fields and exclude: raise Exception(ugettext( "You can't enable 'fields' AND 'exclude' at the same time" )) super(DynamicFieldsMixin, self).__init__(*args, **kwargs) if fields: self.lighten(fields, 'fields') elif exclude: self.lighten(exclude, 'exclude') def lighten(self, entries, argument): if argument == 'fields': field_selector = operator.sub else: field_selector = operator.and_ field_strings, field_dictionary = self.split_fields(entries) for field_name, field_entry in field_dictionary.items(): self.pass_down_structure(field_name, field_entry, argument) subset = set(field_strings) never_pop = set(field_dictionary.keys()) existing = set(self.fields.keys()) for field_name in field_selector(existing, subset) - never_pop: self.fields.pop(field_name, None) def split_fields(self, fields): strings = [] dictionary = {} for field_entry in fields: if isinstance(field_entry, dict): dictionary.update(field_entry) if isinstance(field_entry, basestring): strings.append(field_entry) return strings, dictionary def get_field_and_kwargs(self, field_name): field = self.fields[field_name] many = getattr(field, 'many', False) field = getattr(field, 'child', field) inherit = ('instance', 'data', 'partial', 'context', # 'many', 'read_only', 'write_only', 'required', 'default', 'source', 'initial', 'label', 'help_text', 'style', 'error_messages', 'validators', 'allow_null') kwargs = { attribute_name: getattr(field, attribute_name, None) for attribute_name in inherit } kwargs['many'] = many if kwargs['source'] in ['', field_name]: kwargs.pop('source') return field, kwargs def pass_down_structure(self, field_name, field_entry, arg_name): field, kwargs = self.get_field_and_kwargs(field_name) kwargs[arg_name] = field_entry if not hasattr(field, 'fields'): return self.fields[field_name] = field.__class__(**kwargs) PKEN_`FFdrf_lighten/views.pyimport json from django.conf import settings class DynamicFieldsMixin(object): def get_serializer(self, *args, **kwargs): setting_names = ('DRF_LIGHTEN_INCLUDE', 'DRF_LIGHTEN_EXCLUDE') defaults = ('fields', 'exclude') argument_names = ('fields', 'exclude') self.request.query_params._mutable = True bundle = zip(setting_names, defaults, argument_names) for setting_name, default, argument_name in bundle: try: query_param = getattr(settings, setting_name, default) structure = self.request.query_params.pop(query_param) kwargs[argument_name] = json.loads(structure) except (KeyError, ValueError, TypeError): pass return super(DynamicFieldsMixin, self).get_serializer(*args, **kwargs) PKR@N_99#drf_lighten-0.3.3.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2019 Henock Tesfaye Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HMuSa!drf_lighten-0.3.3.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UD"PK!HJ![X $drf_lighten-0.3.3.dist-info/METADATAVmOF_>*I N"l챳`nw .B/:K;jD3;15fHxCC* 3N po%ws9$sZ#\̀|N$ XDPA1Ay")4;cp88,T9.r:%,ېSu!/w~H;)& "n M M,;ʨ bO}r(„b<_,ŞFU]FFʸ/jcjԽ !ԀE:) ~Fn4ً_ǎRf$w;AYOxFS& VPQ=Q]giV/xkksb̀9f1S GD ̂PC"3Lf@dF#[ND(ؚ[ylV@Ka9&#9A3I0AHc-!bII( <+K;${.>a;X2IstƕF{ Ҩ ˲O%ȥP56^_d8XѤY?;vP'k1:"1KPW(DR4d^ľ-g(>)_Scuw1.y xՌ/ YӋa]^5sוnaoPK!Hd͸Z#"drf_lighten-0.3.3.dist-info/RECORD}ѱr0g m$SPD>}]&Ki$m|M;`OSڄ!9YVAI\,IQ/%6b/H={ڈFɵFcض>"jqv zc"hejX)z667h ؖn9v3"?GVUv\̧ .'21%s'~8 Wu-PьS-Nh@N^y3W}vߝp