|
| 1 | +Django Model Search Tool |
| 2 | +------------------------ |
| 3 | + |
| 4 | +code_annotations django_find_annotations:: |
| 5 | + Usage: code_annotations django_find_annotations [OPTIONS] |
| 6 | + |
| 7 | + Subcommand for dealing with annotations in Django models. |
| 8 | + |
| 9 | + --config_file FILE Path to the configuration file |
| 10 | + --seed_safelist |
| 11 | + Generate an initial safelist file based on |
| 12 | + the current Django environment. [default: |
| 13 | + False] |
| 14 | + |
| 15 | + --list_local_models |
| 16 | + List all locally defined models (in the |
| 17 | + current repo) that require annotations. |
| 18 | + [default: False] |
| 19 | + |
| 20 | + --report_path TEXT Location to write the report |
| 21 | + -v Verbosity level (-v through -vvv) |
| 22 | + --lint Enable or disable linting checks [default: |
| 23 | + False] |
| 24 | + --report Enable or disable writing the report |
| 25 | + [default: False] |
| 26 | + --coverage Enable or disable coverage checks [default: |
| 27 | + False] |
| 28 | + --help Show this message and exit. |
| 29 | + |
| 30 | + |
| 31 | +Overview |
| 32 | +======== |
| 33 | +The Django Model Search Tool, or Django Tool, is written to provide more structured searching and validation in a place |
| 34 | +where data is often stored. Since all of the models in a package can be enumerated it is possible, though not required, |
| 35 | +to use this tool to positively assert that **all** concrete (non-proxy, non-abstract) models in a project are annotated |
| 36 | +in some way. If you do not need this functionality and simply want to find annotations and create a report, the static |
| 37 | +search tool is much easier to configure and can search all of your code (instead of just model docstrings). |
| 38 | + |
| 39 | +.. important:: |
| 40 | + To use the Django tool you must first set the ``DJANGO_SETTINGS_MODULE`` environment variable to point to |
| 41 | + a valid settings file. The tool will initialize Django and use its introspection to find models. The settings file |
| 42 | + should have ``INSTALLED_APPS`` configured for all Django apps that you wish to have annotated. See the |
| 43 | + `Django Docs`_ for details. |
| 44 | + |
| 45 | +.. _Django Docs: https://docs.djangoproject.com/en/dev/topics/settings/#designating-the-settings |
| 46 | + |
| 47 | +The edX use case which prompted the creation of this tool is evident in many of our tests and code samples. It is to |
| 48 | +be able to track the storage, use, and retirement of personally identifiable information (PII) across our many projects |
| 49 | +and repositories. Since the majority of our information is stored via Django models, this tool helps us make sure that |
| 50 | +at least all of those are annotated to assert whether they contain PII or not. |
| 51 | + |
| 52 | +The tool works by actually running your Django app or project in a development-like environment. It then uses Django's |
| 53 | +introspection tools to find all installed apps and enumerate their models. Each model further enumerates its inheritance |
| 54 | +tree and all model docstrings are checked for annotations. All annotations in all models and their ancestors are |
| 55 | +added to the list. |
| 56 | + |
| 57 | +The Safelist |
| 58 | +============ |
| 59 | +In order to assert that **all** concrete models in a project are annotated, it is also necessary to be able to annotate |
| 60 | +models that are otherwise installed in the Python virtual environment and are not part of your source tree. Models in |
| 61 | +your source tree are called "local models", and ones otherwise installed in the Python environment are "non-local" |
| 62 | +models. In order to annotate non-local models, which may come from other repositories or PyPI packages, use the |
| 63 | +"safelist" feature. |
| 64 | + |
| 65 | +"Safe" in safelist doesn't mean that the models themselves do not require annotation, but rather it gives developers a |
| 66 | +place to annotate those models and put them in a known state. When setting up a repository to use the Django tool, you |
| 67 | +should use the ``--seed_safelist`` option to generate an initial safelist template that contains empty entries for all |
| 68 | +non-local models. In order for those models to count as "covered", you must add annotations to them in the safelist. |
| 69 | + |
| 70 | +An freshly created safelist: |
| 71 | + |
| 72 | +.. code-block:: yaml |
| 73 | +
|
| 74 | + social_django.Association: {} |
| 75 | + social_django.Code: {} |
| 76 | +
|
| 77 | +And one that has been annotated: |
| 78 | + |
| 79 | +.. code-block:: yaml |
| 80 | +
|
| 81 | + social_django.Association: |
| 82 | + ".. no_pii::": "This model has no PII" |
| 83 | + social_django.Code: |
| 84 | + ".. pii::": "Email address" |
| 85 | + ".. pii_types::": other |
| 86 | + ".. pii_retirement::": local_api |
| 87 | +
|
| 88 | +.. note:: |
| 89 | + Note that each model can only have one annotation for each token type. For example, it would be invalid to add a |
| 90 | + second ``.. no_pii::`` annotation to ``social_django.Association``. |
| 91 | + |
| 92 | +.. important:: |
| 93 | + Some types of "local" models are procedurally generated and do not have files in code, e.g. models created by |
| 94 | + django-simple-history. In those unusual circumstances you can choose to annotate them in the safelist to make |
| 95 | + sure they are covered. |
| 96 | + |
| 97 | +Coverage |
| 98 | +======== |
| 99 | +The second unique part of the Django tool is the model coverage report and check. Since we are able to find all models |
| 100 | +in a project with a reasonable degree of accuracy we can target a percentage of them that must be annotated. When you |
| 101 | +run the tool with the ``--coverage`` option it will compare the percentage of annotated models against the configuration |
| 102 | +variable ``coverage_target``. If the ``coverage_target`` is not met the search will fail and a list of the un-annotated |
| 103 | +models will be displayed. |
| 104 | + |
| 105 | +Having annotations at any level of a model's inheritance will result in that model being considered "covered". |
| 106 | + |
| 107 | +Lint and Report |
| 108 | +=============== |
| 109 | +This tool supports the same ``--lint`` and ``--report`` options as the :doc:`static_search` tool, and |
| 110 | +they are functionally the same. Linting will fail on malformed annotations found in model docstrings, such as bad |
| 111 | +choices or incomplete groups. Reporting will write out a report file in the same format as the Static Tool, but with |
| 112 | +some additional information in the ``extra`` key such as the ``model_id``, which is a string in the format of |
| 113 | +"parentApp.ModelClassName", as Django uses to represent models internally. It also has the full model docstring in |
| 114 | +``full_comment``. |
| 115 | + |
| 116 | +If a model inherits from another model that has annotations, those annotations will be included in the report under the |
| 117 | +child model's name, as well as any annotations in the model itself. |
| 118 | + |
| 119 | +Local Models |
| 120 | +============ |
| 121 | +Finally, to help find models in the local source tree that still need to be annotated, the tool has a |
| 122 | +``--list_local_models`` option. This will output the model id of all models that still need to be annotated. |
0 commit comments