diff --git a/HISTORY.md b/HISTORY.md index 7fba544..c5c3281 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.3.0] - 2019-10-30 +### Added +- Added `generate_default_bootstraptheme` management command + +### Changed +- Added more informative error messages when using template tags without a theme + ## [0.2.0] - 2019-10-02 ### Changed - Updated to Bootstrap 4.3.1 diff --git a/bootstrap_customizer/__init__.py b/bootstrap_customizer/__init__.py index 7fd229a..0404d81 100644 --- a/bootstrap_customizer/__init__.py +++ b/bootstrap_customizer/__init__.py @@ -1 +1 @@ -__version__ = '0.2.0' +__version__ = '0.3.0' diff --git a/bootstrap_customizer/management/__init__.py b/bootstrap_customizer/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bootstrap_customizer/management/commands/__init__.py b/bootstrap_customizer/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bootstrap_customizer/management/commands/create_default_bootstraptheme.py b/bootstrap_customizer/management/commands/create_default_bootstraptheme.py new file mode 100644 index 0000000..32d9e8e --- /dev/null +++ b/bootstrap_customizer/management/commands/create_default_bootstraptheme.py @@ -0,0 +1,23 @@ +from django.core.management.base import BaseCommand, CommandError +from bootstrap_customizer.models import BootstrapTheme +from bootstrap_customizer.models import SiteBootstrapTheme + +class Command(BaseCommand): + help = 'Creates a BootstrapTheme and SiteBootstrapTheme if none exist' + + def handle(self, *args, **options): + theme_count = BootstrapTheme.objects.count() + if theme_count > 0: + raise CommandError('A BootstrapTheme already exists') + site_theme_count = SiteBootstrapTheme.objects.count() + if theme_count > 0: + raise CommandError('A SiteBootstrapTheme already exists') + + self.stdout.write('Creating BootstrapTheme and SiteBootstrapTheme') + theme = BootstrapTheme() + theme.save() + site_theme = SiteBootstrapTheme(bootstrap_theme=theme, site_id=1) + site_theme.save() + + self.stdout.write(self.style.SUCCESS('Successfully created BootstrapTheme')) + diff --git a/bootstrap_customizer/templatetags/bootstrap_customizer.py b/bootstrap_customizer/templatetags/bootstrap_customizer.py index 1defb1d..1565c11 100644 --- a/bootstrap_customizer/templatetags/bootstrap_customizer.py +++ b/bootstrap_customizer/templatetags/bootstrap_customizer.py @@ -1,26 +1,42 @@ from django import template +from django.core.exceptions import ImproperlyConfigured from django.urls import reverse from django.utils.safestring import mark_safe register = template.Library() +missing_theme_message = ( + "Could not find a BootstrapTheme for this site. " + "Create a BootstrapTheme entry in the admin or run " + "the create_default_bootstraptheme management command." +) + @register.simple_tag(takes_context=True) def bootstrap_theme_css_above_the_fold(context): - bootstrap_theme = context['request'].bootstrap_theme + try: + bootstrap_theme = context['request'].bootstrap_theme + except AttributeError: + raise ImproperlyConfigured(missing_theme_message) return mark_safe(bootstrap_theme.css_above_the_fold) @register.simple_tag(takes_context=True) def bootstrap_theme_css_below_the_fold(context): - bootstrap_theme = context['request'].bootstrap_theme + try: + bootstrap_theme = context['request'].bootstrap_theme + except AttributeError: + raise ImproperlyConfigured(missing_theme_message) return mark_safe(bootstrap_theme.css_below_the_fold) @register.simple_tag(takes_context=True) def bootstrap_theme_css_below_the_fold_url(context): - bootstrap_theme = context['request'].bootstrap_theme + try: + bootstrap_theme = context['request'].bootstrap_theme + except AttributeError: + raise ImproperlyConfigured(missing_theme_message) return reverse('bootstrap_customizer:bootstrap_css_below_the_fold', kwargs=dict( hash=bootstrap_theme.get_hash() )) diff --git a/docs/index.md b/docs/index.md index aaec31d..f518e23 100644 --- a/docs/index.md +++ b/docs/index.md @@ -18,6 +18,7 @@ Enjoy tailored Bootstrap CSS without having to set up a Node front-end assets pi ## Pages - [Models](models.md) +- [Management Commands](management_commands.md) - [Template Tags](template_tags.md) - [Views](views.md) - [Middleware](middleware.md) @@ -72,7 +73,11 @@ urlpatterns = [ ] ``` -Create a [`BootstrapTheme`](models.md#bootstraptheme) and [`SiteBootstrapTheme`](models.md#sitebootstraptheme) from the Django admin. +Create a [`BootstrapTheme`](models.md#bootstraptheme) and [`SiteBootstrapTheme`](models.md#sitebootstraptheme) from the Django admin or use the management command: + +```console +./manage.py create_default_bootstraptheme +``` Add above-the-fold and below-the-fold CSS to your template: diff --git a/docs/management_commands.md b/docs/management_commands.md new file mode 100644 index 0000000..aaa1bb5 --- /dev/null +++ b/docs/management_commands.md @@ -0,0 +1,13 @@ +# Management Commands + +## `create_default_bootstraptheme` + +This management command creates an initial [`BootstrapTheme`](./models.md#bootstraptheme) and [`SiteBootstrapTheme`](./models.md#sitebootstraptheme). + +Usage: + +```console +$ ./manage.py create_default_bootstraptheme +Creating BootstrapTheme and SiteBootstrapTheme +Successfully created BootstrapTheme +``` diff --git a/mkdocs.yml b/mkdocs.yml index 9003567..fc1abf5 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -6,6 +6,7 @@ repo_url: 'https://github.com/johnfraney/django-bootstrap-customizer' nav: - Home: index.md - Models: models.md + - Management Commands: management_commands.md - Template Tags: template_tags.md - Views: views.md - Middleware: middleware.md