├── .gitignore ├── LICENSE ├── README.md ├── examples ├── build_db_with_class.py ├── dnd_town_builder_nautobot_plugin.drawio └── flat_DB_data.yml ├── setup.py └── src └── nautobot_plugin_builder ├── DrawIoNautobot.py ├── __init__.py └── constants.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # draw.io-nautobot-work 2 | 3 | There is a demo here, sadly it's a little out of date: 4 | https://youtu.be/ZwiIgBtmoMU 5 | 6 | The goal is to making building a Nautobot plugin much easier. The idea is you bulid the database in draw.io, then use that file to build out the plugin. 7 | 8 | the project name should be the same as the file name, but without the drawio part. So for the file `test.drawio` file the project name would be `test` 9 | 10 | Run `python3 build_db_with_class.py`to build the info about the drawio file, it will prompt you for the name of the project (the name of the file minus the .drawio part) 11 | 12 | There are 2 ways to build the project: 13 | 14 | - This will build out the basics of the plugin 15 | - `project=BuildNautobotProject(project_name)` 16 | - `project.build_project()` 17 | - This will build out: 18 | - `[name of the project]_plugin_files` 19 | - `plugin` 20 | - `[name of the project]` 21 | - `admin.py` 22 | - `filters.py` 23 | - `jobs.py` 24 | - `models.py` 25 | - `api` 26 | - `__init__.py` 27 | - `serilizers.py` 28 | - `urls.py` 29 | - `views.py` 30 | - This way will build out everything `build_project()` does, **plus give instructions on how to install it**, and build out some other needed files. 31 | - `project=BuildNautobotProject(project_name)` 32 | - `project.full_build_project_with_help()` 33 | - In addition to everything that build_projects build this will also build: 34 | - `[name of the project]_plugin_files` 35 | - `plugin` 36 | - `setup.py` 37 | - `[name of the project]` 38 | - `__init__.py` 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /examples/build_db_with_class.py: -------------------------------------------------------------------------------- 1 | from nautobot_plugin_builder import BuildNautobotProject 2 | from pprint import pprint 3 | import os 4 | 5 | if os.environ.get("project_name") == None: 6 | project_name = input( 7 | "The name of the draw.io file should be the name of the plugin. Please enter the name of the plugin (Example: dnd_builder.drawio would be dnd_builder): " 8 | ) 9 | os.environ["project_name"] = project_name 10 | project_name = os.environ.get("project_name") 11 | 12 | project = BuildNautobotProject(project_name) 13 | 14 | # Builds with helpful steps 15 | project.full_build_project_with_help() 16 | 17 | # Builds out the core of the plugin, but not setup.py, or __init__.py 18 | # project.build_project() 19 | -------------------------------------------------------------------------------- /examples/dnd_town_builder_nautobot_plugin.drawio: -------------------------------------------------------------------------------- 1 | 7Ztdb5s8FIB/TS77KkBI28tB2m5aOlXL1nW7qRxwwJuxkXGaZL/+tcGGULcNnUppJUtVFB9/xJyH8+XCyAuz7QUDeXpJY4hH7jjejrzZyHWPxyfiUwp2lcCZuH4lSRiKlawRLNBfqIRjJV2jGBatgZxSzFHeFkaUEBjxlgwwRjftYSuK27+agwQagkUEsCn9gWKeVtITf9zIP0KUpPqXnbHqyYAerARFCmK62RN5ZyMvZJTy6lu2DSGWytN6qeadP9Jbb4xBwrtMWLrhNjvdfE5+sGhdsIvrK/L9SK1yB/BaXXAAoj8Jo2sSF2rffKeVUWxQhgERrWBFCV+onrFoRynC8Rzs6FpupuBiFd0KUsrQXzEeYNHlCIHoZlyx9satEQs5U63JYCHGXOkrdO6JLsG2NXAOCq53QzEGeYGW5f7kxAywBJGAck4zNchUoNYGZBxu90RKoReQZpCznRiiet2pgrtp7g1nomTp3n1xqmRA3Y5JvVRDTHxR0J4B0DUALmuABj9xVbxUP6N/YEgxZUJOaAUUYXxPBDBKiGhiuJLTpFqQsIwPSpyhOJYrB0UOIkSSeTlsNmkkX5UCpIiK6StcGkAqJkKxQpBTRHipET8Qf0JH4fg/f+SLvYai7TRt8SeHMx5SIrYPUIkMCuIbKKkHjHLAwbK+H024T97/h4krB+Z1BO71BdwzgPNdDoWmwhSwcwSxxd4D9q523ht238C+gOwORXBOI8ARJdZZH3LWvoajmLodmTp+X1CnBtSignpb2bS14wN27He+B96K+z55DLml/fK0B/fap48aeM7kp1Sx5f7i3E+H5q6r10fBp2ITlvzLk3e6BvX+0JsltcrUvoAM2pr6cJp20k7Tjmp+BxM1tzeoZplNBM1bcaHcmvHhcuv9ldmOWWdL+7Wwe4A9eJrmTWwh9trQh8/Rjg3o8gjtQSu3QfpekJ54/xykeztNcczamgFbWHex3uPO/N9MfDZLa5uR9Ul88CCtk/t7xC3sHmAPHpxds4ouxNYt65dnPfyRiWtW1wtOGbSpWJdUrPbMb+i8xDWr50ISteVUNyuuLeL9JGSuWUPbhKxP4sMnZOYzCTYh6wn28AmZ+axCGaM/cZjZf2ocDNL1Y6E6SHf11/0dl7jm+ZeN0c8x4mnnm+DNxGjzhAwJ+721brsn4sPHaPOYrCRePoVgkfeA/DUj9QydX0+Lq+tfR/CGfP/iXn44Qg880l8SZ4AhvrPIe0A+/AmKXngP+je6IfYA5Z9ys84HKL291eGZRmyL6e4mXJvD+0nNPPMM1GZlPcF+zaws8Cer67v5p+kydtyzya8LdDnXIXoPK4wTqD2xUFFKE0oAPmukQflGF4yVbpoxc0pz5R5/Q853yiODNafSJ/NM+2u4RfxGTZfff8rvAk3Vmm33umY73SDicm/0ArKxN0s2m2lla9dCJy+q5ZMfcNMFXbMIPhWOFQQRbRL4ZNx+mDmDGHB0197IM4iKZvOOZtm396ard/Y/ -------------------------------------------------------------------------------- /examples/flat_DB_data.yml: -------------------------------------------------------------------------------- 1 | DiFV6sPVZ-eXnUN2MA-i-1: 2 | parent: b2Cxm9wKgWrcusrGVPnU-26 3 | value: item_rarity 4 | arrows: 5 | B54fVvLI6bd12E4ZGiML-1: 6 | source: b2Cxm9wKgWrcusrGVPnU-20 7 | target: b2Cxm9wKgWrcusrGVPnU-2 8 | b2Cxm9wKgWrcusrGVPnU-1: 9 | parent: '1' 10 | value: Backgrounds 11 | b2Cxm9wKgWrcusrGVPnU-10: 12 | parent: b2Cxm9wKgWrcusrGVPnU-5 13 | value: service_price_high 14 | b2Cxm9wKgWrcusrGVPnU-11: 15 | parent: '1' 16 | value: ServiceNames 17 | b2Cxm9wKgWrcusrGVPnU-12: 18 | parent: b2Cxm9wKgWrcusrGVPnU-11 19 | value: name_part 20 | b2Cxm9wKgWrcusrGVPnU-13: 21 | parent: b2Cxm9wKgWrcusrGVPnU-11 22 | value: Name 23 | b2Cxm9wKgWrcusrGVPnU-17: 24 | parent: '1' 25 | value: CharName 26 | b2Cxm9wKgWrcusrGVPnU-18: 27 | parent: b2Cxm9wKgWrcusrGVPnU-17 28 | value: race 29 | b2Cxm9wKgWrcusrGVPnU-19: 30 | parent: b2Cxm9wKgWrcusrGVPnU-17 31 | value: name_part 32 | b2Cxm9wKgWrcusrGVPnU-2: 33 | parent: b2Cxm9wKgWrcusrGVPnU-1 34 | value: background 35 | b2Cxm9wKgWrcusrGVPnU-20: 36 | parent: b2Cxm9wKgWrcusrGVPnU-17 37 | value: name 38 | b2Cxm9wKgWrcusrGVPnU-21: 39 | parent: b2Cxm9wKgWrcusrGVPnU-17 40 | value: sex 41 | b2Cxm9wKgWrcusrGVPnU-22: 42 | parent: '1' 43 | value: StoreName 44 | b2Cxm9wKgWrcusrGVPnU-23: 45 | parent: b2Cxm9wKgWrcusrGVPnU-22 46 | value: store_type 47 | b2Cxm9wKgWrcusrGVPnU-24: 48 | parent: b2Cxm9wKgWrcusrGVPnU-22 49 | value: name_part 50 | b2Cxm9wKgWrcusrGVPnU-25: 51 | parent: b2Cxm9wKgWrcusrGVPnU-22 52 | value: name 53 | b2Cxm9wKgWrcusrGVPnU-26: 54 | parent: '1' 55 | value: StoreItems 56 | b2Cxm9wKgWrcusrGVPnU-27: 57 | parent: b2Cxm9wKgWrcusrGVPnU-26 58 | value: store_type 59 | b2Cxm9wKgWrcusrGVPnU-28: 60 | parent: b2Cxm9wKgWrcusrGVPnU-26 61 | value: item_name 62 | b2Cxm9wKgWrcusrGVPnU-29: 63 | parent: b2Cxm9wKgWrcusrGVPnU-26 64 | value: item_price 65 | b2Cxm9wKgWrcusrGVPnU-3: 66 | parent: b2Cxm9wKgWrcusrGVPnU-1 67 | value: type,CharField 68 | b2Cxm9wKgWrcusrGVPnU-30: 69 | parent: '1' 70 | value: TownName 71 | b2Cxm9wKgWrcusrGVPnU-31: 72 | parent: b2Cxm9wKgWrcusrGVPnU-30 73 | value: name_part 74 | b2Cxm9wKgWrcusrGVPnU-32: 75 | parent: b2Cxm9wKgWrcusrGVPnU-30 76 | value: name 77 | b2Cxm9wKgWrcusrGVPnU-34: 78 | parent: b2Cxm9wKgWrcusrGVPnU-11 79 | value: service_type 80 | b2Cxm9wKgWrcusrGVPnU-5: 81 | parent: '1' 82 | value: ServiceLocation 83 | b2Cxm9wKgWrcusrGVPnU-6: 84 | parent: b2Cxm9wKgWrcusrGVPnU-5 85 | value: service_type 86 | b2Cxm9wKgWrcusrGVPnU-8: 87 | parent: b2Cxm9wKgWrcusrGVPnU-5 88 | value: service 89 | b2Cxm9wKgWrcusrGVPnU-9: 90 | parent: b2Cxm9wKgWrcusrGVPnU-5 91 | value: service_price_low 92 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | from setuptools import find_packages 3 | 4 | with open("README.md", mode="r") as readme: 5 | long_description = readme.read() 6 | 7 | setup( 8 | name="nautobot_plugin_builder", 9 | version="1.0.0", 10 | author="Daniel Himes", 11 | author_email="dhimes@gmail.com", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | description="Builds out the base of a Nautobot plugin from a draw.io database drawing", 15 | install_requires=[ 16 | "PyYAML==6.0", 17 | "xmltodict==0.13.0", 18 | ], 19 | url="https://github.com/GoreNetwork/draw.io-nautobot-work", 20 | packages=find_packages(where="./src"), 21 | package_dir={"": "src"}, 22 | python_requires=">=3.7", 23 | ) 24 | -------------------------------------------------------------------------------- /src/nautobot_plugin_builder/DrawIoNautobot.py: -------------------------------------------------------------------------------- 1 | from pyparsing import col 2 | from . import constants 3 | import zlib 4 | import base64 5 | import xml.etree.ElementTree as ET 6 | import xmltodict 7 | from urllib.parse import unquote 8 | import yaml 9 | from pprint import pprint 10 | import os 11 | 12 | import os 13 | 14 | 15 | def countlines(start, lines=0, header=True, begin_start=None): 16 | if header: 17 | print("{:>10} |{:>10} | {:<20}".format("ADDED", "TOTAL", "FILE")) 18 | print("{:->11}|{:->11}|{:->20}".format("", "", "")) 19 | 20 | for thing in os.listdir(start): 21 | thing = os.path.join(start, thing) 22 | if os.path.isfile(thing): 23 | if thing.endswith(".py"): 24 | with open(thing, "r") as f: 25 | newlines = f.readlines() 26 | newlines = len(newlines) 27 | lines += newlines 28 | 29 | if begin_start is not None: 30 | reldir_of_thing = "." + thing.replace(begin_start, "") 31 | else: 32 | reldir_of_thing = "." + thing.replace(start, "") 33 | 34 | print( 35 | "{:>10} |{:>10} | {:<20}".format( 36 | newlines, lines, reldir_of_thing 37 | ) 38 | ) 39 | 40 | for thing in os.listdir(start): 41 | thing = os.path.join(start, thing) 42 | if os.path.isdir(thing): 43 | lines = countlines(thing, lines, header=False, begin_start=start) 44 | 45 | return lines 46 | 47 | 48 | def write_yml_file(dictionary, output_file_name): 49 | with open(output_file_name, "w") as outfile: 50 | yaml.dump(dictionary, outfile) 51 | 52 | 53 | def to_doc_w(file_name, varable): 54 | f = open(file_name, "w") 55 | f.write(varable) 56 | f.close() 57 | 58 | 59 | class ReadInDrawIoNautobot: 60 | def __init__(self, project_name): 61 | self.project_name = project_name 62 | self.db_data_raw = self.readed_in_draw_io_file() 63 | self.flat_data = self.build_flatened_data() 64 | self.table_data = self.build_table_data() 65 | self.tables = self.find_tables() 66 | # tables_with_column_name_type is new and can probably be used to clean up a lot of really ugly code 67 | self.tables_with_column_name_type = self.build_tables_with_column_name_type() 68 | 69 | def find_tables(self): 70 | table_data = self.table_data 71 | tables = [] 72 | for table in table_data: 73 | tables.append(table["name"]) 74 | return tables 75 | 76 | def pull_source_table_for_fk(self, column): 77 | column = column.replace("*", "") 78 | column = column.replace("ForeignKey:", "") 79 | column = column.replace(" ", "_") 80 | if column[0]=='!': 81 | column=column[1:] 82 | return column 83 | 84 | def normalize_columns_names(self, columns): 85 | normalized_columns = [] 86 | for column in columns: 87 | column = self.normalize_column_name(column) 88 | if column == None: 89 | continue 90 | column = column.replace(" ", "_") 91 | if '.' in column: 92 | column = column.split('.')[-1] 93 | if column not in normalized_columns: 94 | normalized_columns.append(column) 95 | return normalized_columns 96 | 97 | def normalize_column_name(self, column): 98 | if "***ForeignKey" in column: 99 | source_table = self.pull_source_table_for_fk(column) 100 | column = f"{source_table}_FK" 101 | if column == "PK" or column[-3:] == "_PK": 102 | return None 103 | column = column.replace(" ", "") 104 | if "," in column: 105 | column = column.split(",") 106 | column = column[0] 107 | return column 108 | 109 | def readed_in_draw_io_file(self): 110 | filename = f"{self.project_name}.drawio" 111 | tree = ET.parse(filename) 112 | data = base64.b64decode(tree.find("diagram").text) 113 | xml = zlib.decompress(data, wbits=-15) 114 | xml = xml.decode("utf-8") 115 | xml = unquote(xml) 116 | my_dict = xmltodict.parse(xml, dict_constructor=dict) 117 | return my_dict 118 | 119 | def build_flatened_data(self): 120 | db_data_raw = self.db_data_raw 121 | output = {} 122 | output["arrows"] = {} 123 | for cell in db_data_raw["mxGraphModel"]["root"]["mxCell"]: 124 | if "@value" in cell: 125 | output[cell["@id"]] = {} 126 | output[cell["@id"]]["value"] = cell["@value"] 127 | output[cell["@id"]]["parent"] = cell["@parent"] 128 | if "@edge" in cell: 129 | output["arrows"][cell["@id"]] = {} 130 | output["arrows"][cell["@id"]] = {} 131 | output["arrows"][cell["@id"]]["source"] = cell["@source"] 132 | output["arrows"][cell["@id"]]["target"] = cell["@target"] 133 | 134 | write_yml_file(output, "flat_DB_data.yml") 135 | 136 | return output 137 | 138 | def find_table_value_from_column_value(self, input, key): 139 | parent_id = input[key]["parent"] 140 | return input[parent_id]["value"] 141 | 142 | def build_table_data(self): 143 | input = self.flat_data 144 | tables = {} 145 | columns = [] 146 | for each in input: 147 | if each == "arrows": 148 | continue 149 | if input[each]["parent"] == "1": 150 | tables[each] = {} 151 | tables[each]["name"] = input[each]["value"] 152 | tables[each]["column"] = [] 153 | else: 154 | columns.append(input[each]) 155 | for each in columns: 156 | tables[each["parent"]]["column"].append(each["value"]) 157 | output = [] 158 | for each in tables: 159 | output.append(tables[each]) 160 | 161 | for each in input["arrows"]: 162 | relationship = {} 163 | relationship["relationship"] = {} 164 | source_cell_id = input["arrows"][each]["source"] 165 | target_cell_id = input["arrows"][each]["target"] 166 | source = self.find_table_value_from_column_value(input, source_cell_id) 167 | target = self.find_table_value_from_column_value(input, target_cell_id) 168 | for each in output: 169 | if each["name"] == target: 170 | each["column"].append(f"***ForeignKey:{source}***") 171 | 172 | for table in output: 173 | table["normalized_columns"] = self.normalize_columns_names(table["column"]) 174 | table["normalized_table_name"] = self.normalise_table_name(table["name"]) 175 | 176 | return output 177 | 178 | def normalize_column_name_for_models(self, column): 179 | if "***ForeignKey" in column: 180 | source_table = self.pull_source_table_for_fk(column) 181 | column = f"{source_table}_FK" 182 | if column == "PK" or column[-3:] == "_PK": 183 | return None 184 | column = column.replace(" ", "") 185 | 186 | if "," in column: 187 | column = column.split(",") 188 | return column 189 | 190 | def normalise_table_name(self, table_name): 191 | table_name = table_name.replace(" ", "_") 192 | return table_name 193 | 194 | def find_feild_types(self, table_data, defaults_for_fields): 195 | feild_types_in_tables = [] 196 | for each in table_data: 197 | if "relationship" in each: 198 | continue 199 | for column in each["column"]: 200 | column = column.replace(' ', '') 201 | if "," in column: 202 | column = column.split(",") 203 | if type(column) == list: 204 | key_name, feild_type = column[0], column[1] 205 | if defaults_for_fields[feild_type] != None: 206 | feild_types_in_tables.append(feild_type) 207 | 208 | return feild_types_in_tables 209 | 210 | def remove_forign_tables(self): 211 | tmp_list = [] 212 | for table in self.table_data: 213 | if "." in table['name']: 214 | continue 215 | else: 216 | tmp_list.append(table) 217 | self.table_data=tmp_list 218 | tmp_list=[] 219 | for table in self.tables: 220 | if "." in table: 221 | continue 222 | else: 223 | tmp_list.append(table) 224 | self.tables=tmp_list 225 | 226 | def build_tables_with_column_name_type(self): 227 | self.tables_with_column_name_type=[] 228 | for table in self.table_data: 229 | temp_dict={} 230 | table_name = self.normalise_table_name(table['name']) 231 | temp_dict['table_name']=table_name 232 | temp_dict['columns']=[] 233 | for column in table['column']: 234 | if "PK" in column: 235 | continue 236 | if "_FK" in column: 237 | continue 238 | 239 | column_dict = {} 240 | columns_data = column.split(',') 241 | if len(columns_data)==1 and "***" not in column: 242 | column_dict['column_name']= column 243 | column_dict['column_type']= 'TextField' 244 | if len(columns_data)==2: 245 | column_dict['column_name'], column_dict['column_type'] = columns_data 246 | if "***ForeignKey" in column: 247 | source_table = self.pull_source_table_for_fk(column) 248 | column = f"{source_table}_FK" 249 | column_dict['column_name']= self.normalize_column_name_for_models(column) 250 | column_dict['column_name']= self.pull_key_name(column_dict['column_name']) 251 | 252 | column_dict['source_table']=source_table 253 | column_dict['column_type'] = 'ForeignKey' 254 | temp_dict["columns"].append(column_dict) 255 | self.tables_with_column_name_type.append(temp_dict) 256 | # self.tables_with_column_name_type 257 | return self.tables_with_column_name_type 258 | 259 | 260 | 261 | class BuildNautobotProject(ReadInDrawIoNautobot): 262 | 263 | 264 | 265 | def build_project_dict_structure(self): 266 | project_name = self.project_name 267 | tmp_paths = [ 268 | f"./{project_name}_files", 269 | f"./{project_name}_files/plugin", 270 | f"./{project_name}_files/plugin/{project_name}", 271 | f"./{project_name}_files/plugin/{project_name}/templates", 272 | f"./{project_name}_files/plugin/{project_name}/api", 273 | ] 274 | for path in tmp_paths: 275 | if os.path.exists(path) == False: 276 | os.makedirs(path) 277 | self.api_path = f"./{project_name}_files/plugin/{project_name}/api" 278 | 279 | 280 | 281 | 282 | def build_models(self): 283 | project_name = self.project_name 284 | table_data = self.table_data 285 | defaults_for_fields = { 286 | # "name of feild type": {"default_value_name":"thing that needs a default value", 287 | # "default_value": "What the default value should be" }, 288 | "CharField": {"default_value_name": "max_length", "default_value": "100"}, 289 | "DateField": {"default_value_name": "auto_now", "default_value": "False"}, 290 | "FilePathField": {"default_value_name": "path", "default_value": "Ted"}, 291 | "JSONField":{"default_value_name": None, "default_value": None}, 292 | "TextField":{"default_value_name": None, "default_value": None}, 293 | 294 | } 295 | 296 | feild_types_in_tables = self.find_feild_types(table_data, defaults_for_fields) 297 | print(feild_types_in_tables) 298 | 299 | model_data = constants.model_table_imports.render( 300 | feild_types_in_tables=feild_types_in_tables, 301 | defaults_for_fields=defaults_for_fields, 302 | ) 303 | 304 | for each in table_data: 305 | 306 | # Table names with . in them are probably from another plugin/the core, and are being refrenced due to a FK that should be there. 307 | if "." in each['name']: 308 | continue 309 | if "relationship" in each: 310 | continue 311 | table_name = each["normalized_table_name"] 312 | model_data = model_data + constants.model_class_head_template.render( 313 | table_name=table_name 314 | ) 315 | for column in each["column"]: 316 | if column=="PK" or column[-3:]=="_FK": 317 | continue 318 | if "," in column: 319 | column = column.split(",") 320 | feild_type = "TextField" 321 | if "***ForeignKey" not in column: 322 | if " " in column: 323 | column = self.normalize_column_name_for_models(column) 324 | if type(column) == list: 325 | key_name, feild_type = column[0], column[1] 326 | feild_type = feild_type.replace(" ", "") 327 | elif column == None: 328 | continue 329 | else: 330 | key_name = column 331 | set_default_based_on_field_type='' 332 | if defaults_for_fields[feild_type]["default_value_name"]!=None: 333 | set_default_based_on_field_type=f"{defaults_for_fields[feild_type]['default_value_name']}={feild_type}_default" 334 | 335 | model_data = ( 336 | model_data 337 | + constants.model_class_body_non_foreign_key.render( 338 | column=key_name, 339 | feild_type=feild_type, 340 | set_default_based_on_field_type=set_default_based_on_field_type, 341 | ) 342 | ) 343 | else: 344 | source_table = self.pull_source_table_for_fk(column) 345 | key_name = f"{source_table}_FK" 346 | key_name=self.pull_key_name(key_name) 347 | model_data = ( 348 | model_data 349 | + constants.model_class_body_foreign_key.render( 350 | project_name=project_name, 351 | source_table=source_table, 352 | key_name=key_name, 353 | ) 354 | ) 355 | 356 | filename = f"./{project_name}_files/plugin/{project_name}/models.py" 357 | to_doc_w(filename, model_data) 358 | 359 | def pull_key_name(self, key_name): 360 | if '.' in key_name: 361 | key_name=key_name.split('.')[-1] 362 | return key_name 363 | 364 | def build__init__(self): 365 | file_name = f"./{self.api_path }/__init__.py" 366 | to_doc_w(file_name, "") 367 | 368 | def build_serializers(self): 369 | table_data = self.table_data 370 | table_names = [] 371 | for table in table_data: 372 | table_name = table["normalized_table_name"] 373 | table_names.append(table_name) 374 | output = constants.seralizer_imports.render( 375 | project_name=project_name, table_names=table_names 376 | ) 377 | for table in table_data: 378 | columns = table["normalized_columns"] 379 | table_name = table["normalized_table_name"] 380 | output = output + constants.serlizer_classes.render( 381 | table_name=table_name, columns=columns 382 | ) 383 | filename = f"./{self.api_path}/serializers.py" 384 | to_doc_w(filename, output) 385 | 386 | def build_serializers(self): 387 | table_data = self.table_data 388 | table_names = [] 389 | for table in table_data: 390 | table_names.append(table["normalized_table_name"]) 391 | output = constants.seralizer_imports.render( 392 | project_name=self.project_name, table_names=table_names 393 | ) 394 | for table in table_data: 395 | columns = table["normalized_columns"] 396 | table_name = table["normalized_table_name"] 397 | 398 | output = output + constants.serlizer_classes.render( 399 | table_name=table_name, columns=columns 400 | ) 401 | filename = f"./{self.api_path}/serializers.py" 402 | to_doc_w(filename, output) 403 | 404 | def build_filters(self): 405 | table_data = self.table_data 406 | tables = self.tables 407 | project_name = self.project_name 408 | output = constants.filter_imports.render(tables=tables) 409 | 410 | for table in table_data: 411 | columns = table["normalized_columns"] 412 | table_name = table["normalized_table_name"] 413 | output = output + constants.filter_classes.render( 414 | table_name=table_name, columns=columns 415 | ) 416 | filename = f"./{project_name}_files/plugin/{project_name}/filters.py" 417 | to_doc_w(filename, output) 418 | 419 | def build_api_views(self): 420 | tables = self.tables 421 | output = constants.api_views_imports.render( 422 | tables=tables, project_name=self.project_name 423 | ) 424 | 425 | for table in tables: 426 | output = output + constants.api_classes_imports.render(table=table) 427 | 428 | filename = f"./{self.api_path }/views.py" 429 | to_doc_w(filename, output) 430 | 431 | def build_api_urls(self): 432 | output = constants.api_urls_imports.render(project_name=self.project_name) 433 | for each_table in self.table_data: 434 | table_name = each_table["normalized_table_name"] 435 | output = output + constants.api_urls_classes.render(table_name=table_name) 436 | output = output + "urlpatterns = router.urls" 437 | filename = f"./{self.api_path }/urls.py" 438 | to_doc_w(filename, output) 439 | 440 | def build_admin(self): 441 | table_name_list = [] 442 | for table in self.table_data: 443 | table_name_list.append(table["name"]) 444 | output = constants.admin_table_imports.render( 445 | table_name_list=table_name_list, project_name=self.project_name 446 | ) 447 | for table in self.table_data: 448 | table_name = table["name"] 449 | columns = table["normalized_columns"] 450 | admin_class = constants.admin_class_template.render( 451 | table_name=table_name, columns=columns 452 | ) 453 | output = f"{output} {admin_class}" 454 | filename = f"./{self.project_name}_files/plugin/{self.project_name}/admin.py" 455 | to_doc_w(filename, output) 456 | 457 | def build_jobs_get_or_create(self): 458 | tables = [] 459 | for table in self.table_data: 460 | tables.append(table["name"]) 461 | output = constants.jobs_header.render( 462 | tables=tables, project_name=self.project_name 463 | ) 464 | for table in self.table_data: 465 | table_name = table["name"] 466 | columns = table["normalized_columns"] 467 | log_message = "" 468 | for column in columns: 469 | log_message = log_message + "{" + column + "}," 470 | log_message = log_message[0:-1] 471 | output = output + constants.jobs_get_or_create.render( 472 | table_name=table_name, columns=columns, log_message=log_message 473 | ) 474 | filename = f"./{self.project_name}_files/plugin/{self.project_name}/jobs.py" 475 | to_doc_w(filename, output) 476 | 477 | 478 | 479 | 480 | def build_project(self): 481 | self.build_project_dict_structure() 482 | self.build_models() 483 | self.remove_forign_tables() 484 | self.build__init__() 485 | self.build_serializers() 486 | self.build_filters() 487 | self.build_api_views() 488 | self.build_api_urls() 489 | self.build_admin() 490 | self.build_jobs_get_or_create() 491 | self.build_forms() 492 | self.build_tables_with_column_name_type() 493 | 494 | def build_setup_files(self): 495 | project_name = self.project_name 496 | if os.environ.get("plugin_description") == None: 497 | plugin_description = input("Plugin description: ") 498 | os.environ["plugin_description"] = plugin_description 499 | if os.environ.get("plugin_author") == None: 500 | plugin_author = input("Author of the plugin: ") 501 | os.environ["plugin_author"] = plugin_author 502 | 503 | setup_file_content = constants.setup_file.render( 504 | project_name=self.project_name, 505 | plugin_description=os.environ.get("plugin_description"), 506 | plugin_author=os.environ.get("plugin_author"), 507 | ) 508 | self.build_project() 509 | file_name = f"./{self.project_name}_files/plugin/setup.py" 510 | to_doc_w(file_name, setup_file_content) 511 | 512 | init_file_content = constants.init_file.render( 513 | project_name=self.project_name, 514 | plugin_description=os.environ.get("plugin_description"), 515 | plugin_author=os.environ.get("plugin_author"), 516 | ) 517 | 518 | file_name = ( 519 | f"./{self.project_name}_files/plugin/{self.project_name}/__init__.py" 520 | ) 521 | to_doc_w(file_name, init_file_content) 522 | 523 | def full_build_project_with_help(self): 524 | project_name = self.project_name 525 | self.build_setup_files() 526 | default_location = "/opt/nautobot/" 527 | instructions = [ 528 | f"""This assumes you have done the tasks on https://nautobot.readthedocs.io/en/stable/installation/ and https://nautobot.readthedocs.io/en/stable/installation/nautobot/ 529 | If you know Ansible this will setup most of the Nautobot for you: https://github.com/GoreNetwork/install-nautobot however it's not being kept up 530 | """ 531 | f"""On occastion there may be issues with permissions, by default 532 | chmod -R 777 {default_location} 533 | will fix it, but never do this on a device where security matters even a little bit!""", 534 | f"""copy the plugin folder under {project_name}_files to naubot home (Default: {default_location}) """, 535 | f"""edit nautobot_config.py by putting the string {project_name} inside the PLUGINS list 536 | It should look like this: 537 | PLUGINS = ['{project_name}'] 538 | """, 539 | f"""inside the plugin folder on the naubtobot server as the naubot user (sudo -iu nautobot) run: 540 | "python setup.py develop" """, 541 | f'''Next we need to make the migrations as part of the building out the database with: 542 | "nautobot-server makemigrations {project_name}"''', 543 | f'''Next we need to apply said migrations with: 544 | "nautobot-server migrate {project_name}"''', 545 | f'Run the demo server with "nautobot-server runserver 0.0.0.0:8081 --insecure"', 546 | ] 547 | 548 | for each in instructions: 549 | print("\n") 550 | print(each) 551 | input("Press return for next step: ") 552 | print("\n\n\n") 553 | lines_saved = countlines(start=f"./{project_name}_files") 554 | print(f"\n\nThis program saved you {lines_saved} lines of code") 555 | 556 | def build_forms(self): 557 | forms = constants.form_header.render(tables = self.tables) 558 | for table in self.table_data: 559 | table_name = table['normalized_table_name'] 560 | forms = forms+constants.form_top.render(table_name=table_name, columns = table['normalized_columns']) 561 | 562 | 563 | file_name = ( 564 | f"./{self.project_name}_files/plugin/{self.project_name}/forms.py" 565 | ) 566 | to_doc_w(file_name, forms) 567 | 568 | # self.normalize_columns_names(table["column"]) -------------------------------------------------------------------------------- /src/nautobot_plugin_builder/__init__.py: -------------------------------------------------------------------------------- 1 | __version__ = "0.1.0" 2 | from .DrawIoNautobot import ReadInDrawIoNautobot, BuildNautobotProject 3 | 4 | __all__ = ["BuildNautobotProject", "ReadInDrawIoNautobot"] 5 | -------------------------------------------------------------------------------- /src/nautobot_plugin_builder/constants.py: -------------------------------------------------------------------------------- 1 | from jinja2 import Template 2 | 3 | 4 | admin_table_imports = Template( 5 | """from django.contrib import admin 6 | from {{project_name}}.models import {{ table_name_list | join(', ')}} 7 | """ 8 | ) 9 | 10 | admin_class_template = Template( 11 | """ 12 | @admin.register({{table_name}}) 13 | class {{table_name}}Admin(admin.ModelAdmin): 14 | list_display = ( \"{{columns | join('", "')}}\",) 15 | 16 | """ 17 | ) 18 | 19 | model_table_imports = Template( 20 | """from django.db import models 21 | from nautobot.core.models import BaseModel 22 | from nautobot.core.models.generics import OrganizationalModel, PrimaryModel 23 | from datetime import datetime 24 | 25 | #Default values for field uses, these are defined in build_db under build_models, defaults_for_fields{% for feild_types_in_table in feild_types_in_tables %} 26 | {{ feild_types_in_table }}_default={{ defaults_for_fields[feild_types_in_table]['default_value']}}{% endfor %} 27 | model_type=PrimaryModel 28 | default_on_delete = models.RESTRICT 29 | """ 30 | ) 31 | 32 | model_class_head_template = Template( 33 | """ 34 | 35 | class {{table_name}}(model_type): 36 | """ 37 | ) 38 | 39 | model_class_body_non_foreign_key = Template( 40 | """ 41 | {{ column }}=models.{{feild_type}}({{set_default_based_on_field_type}})""" 42 | ) 43 | 44 | model_class_body_foreign_key = Template( 45 | """ 46 | {% if '.' not in source_table %}{{ key_name }}=models.ForeignKey("{{ project_name }}.{{source_table}}", on_delete=default_on_delete){%- endif %}{% if '.' in source_table %}{{ key_name }}=models.ForeignKey("{{source_table}}", on_delete=default_on_delete){%- endif %}""" 47 | ) 48 | 49 | seralizer_imports = Template( 50 | """from nautobot.core.api.serializers import ValidatedModelSerializer 51 | from rest_framework.serializers import StringRelatedField 52 | from {{ project_name }}.models import {{ table_names | join(', ')}} 53 | 54 | """ 55 | ) 56 | 57 | serlizer_classes = Template( 58 | """ 59 | class {{table_name}}Serializer(ValidatedModelSerializer): 60 | class Meta: 61 | model = {{table_name}} 62 | fields = ("pk", "{{ columns | join('", "')}}") 63 | 64 | """ 65 | ) 66 | 67 | filter_imports = Template( 68 | """from nautobot.utilities.filters import BaseFilterSet 69 | import django_filters 70 | from django.utils import timezone 71 | from .models import {{ tables | join(', ')}} 72 | 73 | """ 74 | ) 75 | filter_classes = Template( 76 | """ 77 | class {{table_name}}FilterSet(django_filters.FilterSet): 78 | class Meta: 79 | model = {{ table_name }} 80 | fields = ("{{ columns | join('", "')}}",) 81 | 82 | """ 83 | ) 84 | 85 | api_views_imports = Template( 86 | """from nautobot.core.api.views import ModelViewSet 87 | from {{project_name}}.models import {{ tables | join(', ')}} 88 | from .serializers import {{ tables | join('Serializer, ')}}Serializer 89 | from {{project_name}}.filters import {{ tables | join('FilterSet, ')}}FilterSet 90 | 91 | """ 92 | ) 93 | 94 | api_classes_imports = Template( 95 | """ 96 | class {{ table }}ViewSet(ModelViewSet): 97 | queryset = {{ table }}.objects.all() 98 | filterset_class = {{ table }}FilterSet 99 | serializer_class = {{ table }}Serializer 100 | 101 | """ 102 | ) 103 | 104 | api_urls_imports = Template( 105 | """from nautobot.core.api.routers import OrderedDefaultRouter 106 | from {{project_name}}.api import views 107 | 108 | router = OrderedDefaultRouter() 109 | 110 | """ 111 | ) 112 | 113 | api_urls_classes = Template( 114 | """router.register("{{ table_name }}", views.{{ table_name }}ViewSet) 115 | 116 | """ 117 | ) 118 | 119 | setup_file = Template( 120 | """from setuptools import find_packages, setup 121 | 122 | setup( 123 | name='{{project_name}}', 124 | version='0.1', 125 | description='{{plugin_description}}', 126 | author='{{plugin_author}}', 127 | packages=find_packages(), 128 | include_package_data=True, 129 | ) 130 | 131 | """ 132 | ) 133 | 134 | init_file = Template( 135 | """ 136 | 137 | from nautobot.extras.plugins import PluginConfig 138 | 139 | class {{project_name}}Config(PluginConfig): 140 | 141 | name = "{{project_name}}" 142 | verbose_name = "{{project_name}}" 143 | author = "{{plugin_author}}" 144 | description = "{{project_name}}" 145 | base_url = "{{project_name}}" 146 | required_settings = [] 147 | min_version = "1.1.0" 148 | max_version = "1.9999" 149 | default_settings = {} 150 | caching_config = {} 151 | 152 | 153 | config = {{project_name}}Config 154 | 155 | """ 156 | ) 157 | 158 | jobs_header = Template( 159 | """from nautobot.extras.jobs import IntegerVar, Job 160 | from yaml.loader import SafeLoader 161 | from {{project_name}}.models import {{ tables | join(', ')}} 162 | 163 | 164 | """ 165 | ) 166 | 167 | jobs_get_or_create = Template( 168 | """def update_{{table_name}}_get_or_create({{ columns | join(', ')}}): 169 | table_update, created = {{table_name}}.objects.get_or_create({% for column in columns %} 170 | {{column}}={{column}},{% endfor %}) 171 | 172 | if created: 173 | self.log_success( f"Added to {{table_name}}: {{ log_message}}") 174 | else: 175 | self.log_info( f"Already Exists in {{table_name}}: {{ log_message }}") 176 | 177 | return table_update 178 | 179 | 180 | """ 181 | ) 182 | 183 | form_header = Template("""from django import forms 184 | from django.forms import ModelForm 185 | from .models import {{ tables | join(', ')}} 186 | """ ) 187 | 188 | form_top = Template( 189 | """ 190 | 191 | class {{table_name}}Form(ModelForm): 192 | class Meta: 193 | model = {{table_name}} 194 | fields = (\"{{columns | join('", "')}}\") 195 | widgets = { 196 | """ 197 | ) --------------------------------------------------------------------------------