|
| 1 | +# |
| 2 | +# Copyright © 2021 Ingram Micro Inc. All rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +import json |
| 6 | +import re |
| 7 | + |
| 8 | +from dj_rql.filter_cls import NestedAutoRQLFilterClass |
| 9 | + |
| 10 | +from django.core.management import BaseCommand |
| 11 | +from django.db.models import ForeignKey, OneToOneField, OneToOneRel |
| 12 | +from django.utils.module_loading import import_string |
| 13 | + |
| 14 | + |
| 15 | +TEMPLATE = """from {model_package} import {model_name} |
| 16 | +
|
| 17 | +from dj_rql.filter_cls import RQLFilterClass |
| 18 | +{optimizations_import} |
| 19 | +
|
| 20 | +class {model_name}Filters(RQLFilterClass): |
| 21 | + MODEL = {model_name} |
| 22 | + SELECT = {select_flag} |
| 23 | + EXCLUDE_FILTERS = {exclusions} |
| 24 | + FILTERS = {filters} |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +class Command(BaseCommand): |
| 29 | + help = ( |
| 30 | + 'Automatically generates a filter class for a model ' |
| 31 | + 'with all relations to the specified depth.' |
| 32 | + ) |
| 33 | + |
| 34 | + def add_arguments(self, parser): |
| 35 | + parser.add_argument( |
| 36 | + 'model', |
| 37 | + nargs=1, |
| 38 | + type=str, |
| 39 | + help='Importable model location string.', |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + '-d', |
| 43 | + '--depth', |
| 44 | + type=int, |
| 45 | + default=1, |
| 46 | + help='Max depth of traversed model relations.', |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + '-s', |
| 50 | + '--select', |
| 51 | + action='store_true', |
| 52 | + default=True, |
| 53 | + help='Flag to include QuerySet optimizations: true by default.', |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + '-e', |
| 57 | + '--exclude', |
| 58 | + type=str, |
| 59 | + help='List of coma separated filter names or namespace to be excluded from generation.', |
| 60 | + ) |
| 61 | + |
| 62 | + def handle(self, *args, **options): |
| 63 | + model_import = options['model'][0] |
| 64 | + model = import_string(model_import) |
| 65 | + is_select = options['select'] |
| 66 | + exclusions = options['exclude'].split(',') if options['exclude'] else [] |
| 67 | + |
| 68 | + class Cls(NestedAutoRQLFilterClass): |
| 69 | + MODEL = model |
| 70 | + DEPTH = options['depth'] |
| 71 | + SELECT = is_select |
| 72 | + EXCLUDE_FILTERS = exclusions |
| 73 | + |
| 74 | + def _get_init_filters(self): |
| 75 | + self.init_filters = super()._get_init_filters() |
| 76 | + |
| 77 | + self.DEPTH = 0 |
| 78 | + self.SELECT = False |
| 79 | + return super()._get_init_filters() |
| 80 | + |
| 81 | + def _get_field_optimization(self, field): |
| 82 | + if not self.SELECT: |
| 83 | + return |
| 84 | + |
| 85 | + if isinstance(field, (ForeignKey, OneToOneField, OneToOneRel)): |
| 86 | + return "NSR('{0}')".format(field.name) |
| 87 | + |
| 88 | + if not self._is_through_field(field): |
| 89 | + return "NPR('{0}')".format(field.name) |
| 90 | + |
| 91 | + filters = Cls(model._default_manager.all()).init_filters |
| 92 | + filters_str = json.dumps(filters, sort_keys=False, indent=4).replace( |
| 93 | + '"ordering": true', '"ordering": True', |
| 94 | + ).replace( |
| 95 | + '"ordering": false', '"ordering": False', |
| 96 | + ).replace( |
| 97 | + '"search": true', '"search": True', |
| 98 | + ).replace( |
| 99 | + '"search": false', '"search": False', |
| 100 | + ).replace( |
| 101 | + '"qs": null', '"qs": None', |
| 102 | + ) |
| 103 | + |
| 104 | + filters_str = re.sub(r"\"((NPR|NSR)\('\w+?'\))\"", r'\1', filters_str) |
| 105 | + |
| 106 | + model_package, model_name = model_import.rsplit('.', 1) |
| 107 | + code = TEMPLATE.format( |
| 108 | + model_package=model_package, |
| 109 | + model_name=model_name, |
| 110 | + filters=filters_str, |
| 111 | + select_flag='True' if is_select else 'False', |
| 112 | + optimizations_import='from dj_rql.qs import NPR, NSR\n' if is_select else '', |
| 113 | + exclusions=exclusions, |
| 114 | + ) |
| 115 | + |
| 116 | + return code |
0 commit comments