Metadata-Version: 2.5
Name: wagtail-gunsnprices
Version: 0.1.0
Summary: Wagtail StreamField blocks for gun and ammo content: caliber tables, cost per round, firearm spec tables and a caliber field.
Project-URL: Homepage, https://gunsnprices.com
Project-URL: Documentation, https://github.com/gunsnprices/wagtail-gunsnprices#readme
Project-URL: Source, https://github.com/gunsnprices/wagtail-gunsnprices
Project-URL: Issues, https://github.com/gunsnprices/wagtail-gunsnprices/issues
Project-URL: Changelog, https://github.com/gunsnprices/wagtail-gunsnprices/blob/main/CHANGELOG.md
Project-URL: Developers, https://gunsnprices.com/developers
Author-email: GunsNPrices <app@gunsnprices.com>
License-Expression: MIT
License-File: LICENSE
Keywords: ammo,ammunition,caliber,cost-per-round,django,firearms,guns,gunsnprices,streamfield,wagtail
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.1
Classifier: Framework :: Django :: 5.2
Classifier: Framework :: Django :: 6.0
Classifier: Framework :: Django :: 6.1
Classifier: Framework :: Wagtail
Classifier: Framework :: Wagtail :: 6
Classifier: Framework :: Wagtail :: 7
Classifier: Framework :: Wagtail :: 8
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP :: Site Management
Classifier: Topic :: Office/Business
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: django>=4.2
Requires-Dist: gunsnprices>=0.1.2
Requires-Dist: wagtail>=6
Description-Content-Type: text/markdown

# wagtail-gunsnprices

[![PyPI](https://img.shields.io/pypi/v/wagtail-gunsnprices)](https://pypi.org/project/wagtail-gunsnprices/)
[![Tests](https://github.com/gunsnprices/wagtail-gunsnprices/actions/workflows/ci.yml/badge.svg)](https://github.com/gunsnprices/wagtail-gunsnprices/actions/workflows/ci.yml)
[![License](https://img.shields.io/badge/license-MIT-blue)](LICENSE)

StreamField blocks for gun and ammo content in [Wagtail](https://wagtail.org): a caliber table, a cost per round
calculator, a spec table built from a product title, and a caliber field for your own models.

Editors type what they already have, a store's product title or a box price, and the blocks fill in the rest:
brand, model, caliber, magazine capacity, barrel length, rounds per box, cost per round. The parsing is done by the
[`gunsnprices`](https://pypi.org/project/gunsnprices/) package, the title parser behind
[GunsNPrices](https://gunsnprices.com), a price comparison site for 40+ US gun and ammo stores. The caliber list
(293 calibers) is the one the site uses, so every caliber can link to its price comparison page.

No network requests, no API key. Works with Wagtail 6, 7 and 8, Django 4.2 to 6.1, Python 3.9 to 3.13.

## Install

```bash
pip install wagtail-gunsnprices
```

```python
INSTALLED_APPS = [
    ...
    "wagtail_gunsnprices",
]
```

The app only provides templates; it has no models and no migrations.

## Blocks

```python
from wagtail.fields import StreamField
from wagtail.models import Page

from wagtail_gunsnprices.blocks import CaliberTableBlock, CostPerRoundBlock, FirearmSpecsBlock


class ArticlePage(Page):
    body = StreamField([
        ("caliber_table", CaliberTableBlock()),
        ("cost_per_round", CostPerRoundBlock()),
        ("firearm_specs", FirearmSpecsBlock()),
    ])
```

Render them as usual with `{% include_block block %}`. Each block has its own template under
`wagtail_gunsnprices/blocks/`; override it in your project to change the markup. The tables carry `gnp-*` CSS
classes and no styles of their own.

### CaliberTableBlock

The editor picks calibers from a list grouped by type (handgun, rifle, rimfire, shotgun, muzzleloader, airgun) and
gets a table:

| Caliber | Type | Prices |
|---|---|---|
| 9×19mm Parabellum | Handgun | [Compare prices](https://gunsnprices.com/ammo/handgun-ammo/9-19mm-parabellum) |
| .45 ACP | Handgun | [Compare prices](https://gunsnprices.com/ammo/handgun-ammo/45-acp) |

Fields: `heading` (optional), `calibers`, `show_type`, `link_prices`. The template gets `rows`, a list with `name`,
`type`, `type_label` and `url` for each caliber.

### CostPerRoundBlock

Fields: `title` (an ammo listing title, optional), `price`, `rounds` (optional) and `currency` (default `$`). When
`rounds` is empty it is read from the title: `50rd`, `Box of 20`, `1000 Rounds`, `20/25` all work. The caliber is
read from the title too. Saving fails with a clear message when no round count can be found.

```
Ammo            Federal 9mm 115gr FMJ 50rd
Caliber         9×19mm Parabellum
Price           $18.99
Rounds          50
Cost per round  $0.38
```

### FirearmSpecsBlock

Fields: `title` (the product title as a store writes it), optional `brand`, `mpn` (part number), `price`,
`currency` and `link_prices`. The template gets `specs.rows`, each with `label`, `value` and `url`:

```
Glock 19 Gen 5 MOS 9mm 4.02in 15rd Pistol
Type            Firearm
Brand           Glock
Model           Glock 19 Gen 5 MOS
Configuration   MOS
Caliber         9×19mm Parabellum  (linked to the price page)
Capacity        15 rounds
Barrel          4.02"
```

Models are recognized for 50 brands (Glock, Sig Sauer, Smith & Wesson, Ruger, Springfield, Mossberg, Remington,
Savage, CZ, Beretta, Benelli, Taurus, Kimber, Walther, HK, FN, Colt, Henry, Marlin, Tikka, Winchester and more);
other brands still get caliber, capacity and barrel length. Ammo titles work too and add rounds per box and cost
per round. A title with nothing recognizable in it is rejected when the page is saved.

### Caliber choice blocks

```python
from wagtail_gunsnprices.blocks import CaliberChoiceBlock, CaliberMultipleChoiceBlock

("caliber", CaliberChoiceBlock()),                       # one caliber, all 293
("calibers", CaliberMultipleChoiceBlock(types=["handgun", "rimfire"])),
```

Values are caliber ids such as `9x19mmparabellum`. Unlike a plain `ChoiceBlock`, these blocks reference their own
class when deconstructed, so the 293 choices are not copied into every migration.

## Caliber field

```python
from django.db import models
from wagtail_gunsnprices.fields import CaliberField


class Firearm(models.Model):
    name = models.CharField(max_length=100)
    caliber = CaliberField()                   # all calibers
    backup = CaliberField(blank=True, types=["handgun"])
```

`CaliberField` is a `CharField` with the caliber list as grouped choices, so the admin shows a select and
`get_caliber_display()` returns the name. It also adds `get_caliber_caliber()`, which returns the
`gunsnprices.Caliber` with `id`, `name`, `type` and the price page `url`. The choices stay out of migrations.

For plain Django forms there is `wagtail_gunsnprices.fields.CaliberChoiceField`.

## Helpers

Everything the blocks compute is available as plain functions, for your own views, templates and snippets:

```python
from wagtail_gunsnprices.calibers import caliber_choices, calibers, get_caliber
from wagtail_gunsnprices.specs import caliber_rows, cost_per_round_data, firearm_specs

get_caliber("9mm Luger").url          # 'https://gunsnprices.com/ammo/handgun-ammo/9-19mm-parabellum'
caliber_choices(types="rifle")        # [('17hornet', '.17 Hornet'), ...]
cost_per_round_data("18.99", title="Federal 9mm 115gr FMJ 50rd").cost_per_round_display   # '$0.38'
firearm_specs("Sig Sauer P365 XL 9mm 3.7in 12+1").rows   # [SpecRow(key='kind', label='Type', value='Firearm'), ...]
```

## Development

```bash
pip install -e . build twine
python runtests.py            # Wagtail test project in tests/, Django test runner
python -m build && twine check dist/*
```

Tests run on Wagtail 6.4, 7.4 and 8.1 with Django 4.2, 5.2 and 6.1 in CI.

## Links

- Price comparison and caliber pages: [gunsnprices.com](https://gunsnprices.com)
- Libraries, datasets and other integrations: [gunsnprices.com/developers](https://gunsnprices.com/developers)
- The parser this package is built on: [gunsnprices on PyPI](https://pypi.org/project/gunsnprices/)

## License

MIT. The caliber and model data come from the `gunsnprices` package (CC BY 4.0, attribution: GunsNPrices).

Questions, corrections and missing models: open an issue or write to app@gunsnprices.com.
