PKNun OOdrf_lighten/__init__.py"""A way to remove unwanted fields from a serializer""" __version__ = "0.2.2" PKPN-ƻ drf_lighten/serializers.pyfrom rest_framework import serializers from operator import and_, sub class DynamicFieldsMixin(object): def __init__(self, *args, **kwargs): fields = kwargs.pop('fields', None) exclude = kwargs.pop('exclude', None) error_message = ( "You can't enable 'fields' AND 'exclude' at the same time" ) if fields and exclude: raise Exception(error_message) super(DynamicFieldsMixin, self).__init__(*args, **kwargs) if not (fields or exclude): return field_selector = sub if fields else and_ field_entries = fields or exclude field_strings, field_dictionaries = self.split_fields(field_entries) argument = 'fields' if fields else 'exclude' subset = set(field_strings) existing = set(self.fields.keys()) for field_name in field_selector(existing, subset): self.fields.pop(field_name, None) for field_entry in field_dictionaries: self.pass_down_structure(field_entry, argument) def split_fields(self, fields): strings = [] dictionaries = [] for field_entry in fields: if isinstance(field_entry, dict): dictionaries.append(field_entry) if isinstance(field_entry, basestring): strings.append(field_entry) return strings, dictionaries def get_field_and_kwargs(self, field_name): field = self.fields[field_name] many = isinstance(field, serializers.ListSerializer) 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) 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_entry, arg_name): if not field_entry.keys(): return field_name = field_entry.keys().pop() field, kwargs = self.get_field_and_kwargs(field_name) kwargs[arg_name] = field_entry[field_name] self.fields[field_name] = field.__class__(**kwargs) PK FNH{÷<<drf_lighten/views.pyimport json from django.conf import settings class DynamicStructureMixin(object): def get_serializer(self, *args, **kwargs): try: query_param = getattr(settings, 'DRF_LIGHTEN_INCLUDE', 'fields') structure = self.request.query_params[query_param] kwargs['fields'] = json.loads(structure) except (KeyError, ValueError, TypeError): pass try: query_param = getattr(settings, 'DRF_LIGHTEN_EXCLUDE', 'exclude') structure = self.request.query_params[query_param] kwargs['exclude'] = json.loads(structure) except (KeyError, ValueError, TypeError): pass return super(DynamicStructureMixin, self).get_serializer(*args, **kwargs) PKR@N_99#drf_lighten-0.2.2.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.2.2.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UD"PK!H,*4X $drf_lighten-0.2.2.dist-info/METADATAVN#78 ?5.#E-D쪄m%&fL0^CEH4}>I=-J:BDw.߹'x|Dc1cLbzP%>;ʒ:=EKBnT ,  ;R%v4/رHQڵ])Jmkow䢈¡濽~?JUЦFhG;Hך<8s]~dVU5'q O2>0cp;Z6ya2ZtxD3+Kh% 't m1!seJ {uCRAV+O+h tI(Vc*(Y/9Ԫ "Jv &ZhbvaȞY* L&HJʪ6uCkQje(jaa5sњB@'NFPBg, ء9]o=7~D:B`viT=B<0yKlh% /IZlIQ(evKKOoa]0qx j,m0 8rp+pM O*Ll?d#n{ͅ|>@.1%%z*I~PJ 4 ,\9mFǕ.~ՍNεvN>VeV tn驖!/h N*ѵ.c"WCu As$p?oWzuvs +0vX@. @s}P* _*>,"%r?u߼Yۯ%wI2^v߫=cs硎U~Q%!kG=شzE.i.aSK yDiY 1, !2A=%hZȮ%ii8'[2m9vӫ 9|4>%!Mͦߗ0ޥEf_PK!HcFhY#"drf_lighten-0.2.2.dist-info/RECORD}̻v0н,18t P@QBD"_.c/ir\ccVI=WvL -:uP7AKdiuWh49sO*IQ .H%TWrӣQan. L8 ;k& n2[CyvdrcSkxtauAqeET^/k;Uϧc-g}> PGYD"']̓=)^xEx{KfMxݢM䲟'Oq)R޺>7\g`߾.T]g=E9֑^&ݼXqz@3 QVy~ PKNun OOdrf_lighten/__init__.pyPKPN-ƻ drf_lighten/serializers.pyPK FNH{÷<<w drf_lighten/views.pyPKR@N_99# drf_lighten-0.2.2.dist-info/LICENSEPK!HMuSa!_drf_lighten-0.2.2.dist-info/WHEELPK!H,*4X $drf_lighten-0.2.2.dist-info/METADATAPK!HcFhY#"drf_lighten-0.2.2.dist-info/RECORDPK