django-planet
Feb. 2, 2024

Neapolitan 24.2 release

in blog Carlton's latest posts
original entry Neapolitan 24.2 release

A quick flit publish and Neapolitan 24.2 is available on PyPI.

$ pip install -U neapolitan 

Go get yours now! 🚀

Quicker template overrides

The release adds a new mktemplate management command, that lets you quickly bootstrap an override of your active neapolitan templates, on a per model and per CRUD action basis.

This makes ”Oh, I need to customise the list template” much easier, so you don’t have to break your flow.

Basic usage is like this:

$ ./manage.py mktemplate cakeshop.Ingredient --list

You pass pass your model as <app_name.ModelName> — e.g. auth.User, yay 💃— and a flag for the CRUD operation you want to customise.

That’s it. You’ll get a new cakeshop/templates/ingredient_list.html template, where it’ll be picked up by your IngredientCRUDView. * This is a copy of the default neapolitan/object_list.html which is used otherwise. * But it will use the template loader, so you’ve overridden the default templates, your (correct) active templates will be the ones that are used.

I blocked mktemplate out on Thursday morning. I knew I had a series of overrides coming up that morning, and this has been on the backlog a while.

The first --list was good. By the time I’d done a --detail, a --form and a --delete to complete the set, I was hooked.

See the --help command for full options. I hope you enjoy. 🎁

The minimal test

I wrote the mktemplate command locally, against my development project. I didn’t have a test. It worked. I was happy.

I didn’t want to release it though without any tests at all.

No doubt there will be bugs: not everyone’s project looks exactly like mine.

When the bugs do come up, I’m going to ask for a test case, so we can fix it, and avoid regressions going forwards.

The hardest thing though with tests is bootstrapping them: that very first run. I wanted to avoid having a potential contributor put off by that hurdle.

So I added a very basic test. Testing a management command you use call_command, and then assert something:

    def test_mktemplate_command(self):
        # Run the command
        call_command('mktemplate', 'tests.Bookmark', '--list')

        # Check if the file was created
        file_path = 'tests/templates/tests/bookmark_list.html'
        self.assertTrue(os.path.isfile(file_path))

        # Remove the created file
        os.remove(file_path)

It’s not the most glamorous thing. Looking at it now, I’m immediately like I should have used Path.

But it’ll will do as a starter. Any issue is going to need a test looking something similar. It can serve as the setting off point for a future PR.

I mention it only because I think that kind of minimal test is pretty much always worth it.

Even if you only test the simplest Happy Path. At least you know you didn’t break that. But you also have at least some scaffolding up for when you do need a real test, when you do hit a real bug later.

Anyhow


How to update (again)

$ pip install -U neapolitan  

đŸ„ł