Skip to content

muremwa/read-urls-extension

Repository files navigation

This repo is going to be archived and later versions built in purely typescript. Repository found here.

Django URLs configurations Reader.

Reads all urls configurations (urls.py) declared in all Django projects within a VSCODE workspace and copy to clipboard.

New in version 2.1

✨✨Introducing settings for the extension ✨✨
Models in the project are now automatically detected by the extension.
You can now switch between keyword and positional arguments in settings for your urls. Built-in auth/django.contrib.auth URL configurations can be included or not depending on settings using builtInAuth: true


Gives the option of copying the url as reverse, reverse_lazy or as a template tag to your clipboard.
Install from here.
Jump to:

Usage.
Settings.
Custom Url configurations.
Creating Custom URL configurations.
Using ModelAdmin urls.
Multiple Django folders support.


Usage.

The extension adds a view on your side bar with an icon like a chain 🔗. Navigate to that view and click to open it and activate the extension.


acitvate extension


Give it a few moments to read all urls and populate the view. Once done it will populate the view with your URL configurations.

Each app will be a collapsable tree with its urls as the children. App names are in ALL CAPS. If an app has no app_name it will be displayed in the following format PARENT_FOLDER\URLS.PY.
A url may have children if it's arguments are defined. If not the it's a single item.


image explaining the project urls view


Actions.

On hovering over a url name, there are three buttons (from left to right).

  1. Copy 'reverse' url.
  2. Copy 'reverse_lazy' url.
  3. Copy as a template tag.

All three copy to clipboard and can be pasted in your code, all you need to change is names of the arguments to match you namespace (The arguments are surrounded by '%' to make sure your editor or linter catches it to remind you to change it).

The three are copied using keyword arguments i.e. kwargs to switch to positional arguments i.e. args add urlWithKeywords: false to settings.


hover buttons


Settings

The extension now has settings. They live inside .vscode/urlConfigs/settings.json inside in your project. They are on a project by project basis. If you have two projects in one workspace, each project shall have it's own sets of settings.

Options for settings are explained below showing an example .vscode/urlConfigs/settings.json file;

    {
        "adminUrls": true,
        "autoLoadModels": true,
        "registeredAppsOnly": false,
        "builtInAuth": false,
        "expandApps": "normal",
        "urlWithKeywords": true
    }
  1. adminUrls [Boolean]: true to show admin site urls configurations and false to not show.
  2. autoLoadModels [Boolean]: true to automatically discover models and false to use just .vscode/urlConfigs/models.json (the traditional approach).
  3. registeredAppsOnly [Boolean]: Automatically detect models from registered apps only. Default is false.
  4. builtInAuth [Boolean]: true to show url configurations from the built-in authentication sytem i.e. django.contrib.auth.urls or false to not.
  5. expandApps [String]: Collapse or expand the Apps to show urls. Choices are 3; collapsed, expanded or normal. normal is the default.
  6. urlWithKeywords [Boolean]: The reverse & reverse_lazy functions and url template tag use keywords arguments if true and positional arguments if false i.e. args or kwargs. The default is true.

expandApps requires a window reload to show changes


Custom URL configurations.

Sometimes you need 3rd party apps in your project which may have URL configurations. These configurations can be described in a JSON file named in the format; 'app.conf.json'. These files are saved in '.vscode/urlConfigs/' folder in the root of your project to allow the extension to find them. They are combined with your project's configurations. To describe the URL configurations click here.

The extension comes pre-loaded with the AdminSite, ModelAdmin, django.contrib.auth and UserAdmin configurations.
To turn off AdminSite, ModelAdmin and UserAdmin, add adminUrls: false to settings. To turn on django.contrib.auth URL configurations, add builtInAuth: true to settings.


Creating custom configurations.

The JSON file, named in the format described in the previous section, contains a list/array of Whole app configurations. i.e. Your whole project, no matter home many apps/urls.py there are, would be added to one *.conf.json file. Say there are three apps in your project, api, store, billing, they can be described in one file or a separate file for each.
Custom configurations appear at the top and are collapsed by default.

Each app entry, in the array of apps, is an object with two properties:

  1. appName [String]: A string with the name of the app. (Note this is not the usual app_name declared in the urls.py. It will just appear as the title in our URl configurations view).

  2. urls [Array]: An array of url configurations objects for that app. If no URL configurations leave as an empty array []. A url configuration object is in the following format:

    1. reverseName [String]: This is the actual reverse name. If the app has an app_name and the name of the url is index, enter this as app_name:index. If no app_name enter as index.

    2. arguments [Array]: An array of a URL configuration parameters object. If there are no arguments leave this an empty array []. These objects are in the format:

      1. name [String]: The name of the parameter as described in the urls.py file.
      2. argType [String]: The type of the parameter. Options are either: string, slug, uuid, integer.
    3. viewName [String]: The name of the view that handles that URL.

    4. hasArgs [Boolean]: false if arguments / entry 2 above is empty and vice versa.

To load changes click the reload button.


Example in a *.conf.json file:

[
    {
        "appName": "example",
        "urls": [
            {
                "reverseName": "url1",
                "arguments": [
                    {
                        "name": "name",
                        "argType": "string"
                    },
                    {
                        "name": "name_2",
                        "argType": "slug"
                    }
                ],
                "viewName": "views.example_1",
                "hasArgs": true
            },
            {
                "reverseName": "url2",
                "arguments": [],
                "viewName": "views.example_2",
                "hasArgs": false
            }
        ]
    },
    {
        "appName": "example_app_2",
        "urls": [
            {
                "reverseName": "url3",
                "arguments": [],
                "viewName": "views.example_3",
                "hasArgs": false
            }
        ]
    }
]

Incase of incorrect configurations, the file is ignored.
Check out the admin configurations file🧐.


ModelAdmin URLs.

Models in your project are AUTOMATICALLY detected by the extension. This option is enabled by default, to turn off add autoLoadModels: false to settings. To add more models, third party models or others, add an object in .vscode/urlConfigs/models.json with properties as app_labels (peak into apps.py) and a list of model names.

To load changes click the reload button.

Changes to detect models automatically are in development.
Models can now be automatically detected.

    {
        "app_label": ["model1", "model2"],
        "app_label_2": ["model3"]
    }

*The extension detects models if you use the built-in django.contrib.admin and have admin.py or /admin/__init__.py in your apps. Support for others coming soon :)


Multiple Projects in a workspace.

The extension now supports multiple projects in one workspace. Once you add a folder to the workspace, reload the window to view changes.
Multiple projects appear like this


MISC

• Created by Muremwa.
• Copying to clipboard made possible by clipboardy.
• Released under the MIT License.