Skip to main content

Pinax Announcements integration for Django Oscar dashboards

Project description

django-oscar-announcements

Staff-managed site-wide announcements for Django Oscar, built on top of pinax-announcements.

Features:

  • Dashboard CRUD (Customers → Announcements)
  • Info (blue) and Warning (red) levels that match Oscar/Bootstrap alert colours
  • Extensible visibility — built-in Registered and Staff audiences; add your own (e.g. Verified)
  • AJAX dismiss with no-JS form fallback
  • Test button — preview an announcement on the public site without publishing it
  • Re-send checkbox — clear dismissals so users see it again after an edit
  • Auto-delete via django-background-tasks when an expiry date passes


Installation

pip install django-oscar-announcements   # or install editable from source

1. INSTALLED_APPS

INSTALLED_APPS = [
    ...
    "pinax.announcements",   # required dependency
    "oscar_announcements",
    'background_task',
    ...
]

2. Context processor

Add to TEMPLATES[0]["OPTIONS"]["context_processors"]:

"oscar_announcements.context_processors.announcements",

This populates site_announcements in every template context.

3. Public dismiss URL

# urls.py
from django.urls import include, path

urlpatterns = [
    ...
    path(
        "announcements/",
        include("oscar_announcements.urls", namespace="oscar_announcements"),
    ),
]

4. Oscar dashboard

Wire the CRUD views into your Oscar DashboardConfig:

# myapp/apps/dashboard/apps.py
from oscar.apps.dashboard.apps import DashboardConfig as OscarDashboardConfig
from django.urls import include, path


class DashboardConfig(OscarDashboardConfig):
    def configure_permissions(self):
        super().configure_permissions()
        from oscar_announcements.dashboard.urls import permissions as ann_permissions

        self.permissions_map.update(ann_permissions)

    def get_urls(self):
        from oscar_announcements.dashboard.urls import urlpatterns as ann_urls

        return super().get_urls() + self.post_process_urls(ann_urls)

Add a nav entry (Customers section is index 3 in default Oscar navigation):

from django.utils.translation import gettext_lazy as _

OSCAR_DASHBOARD_NAVIGATION[3]["children"].append(
    {"label": _("Announcements"), "url_name": "dashboard:announcement-list"}
)

5. Templates — render announcements

Load the tag in any base template and call {% render_announcements %}:

{% load oscar_announcements_tags %}

{# Public site (myco_layout.html, after flash messages): #}
{% render_announcements %}

{# Dashboard (e.g. inside {% block extrascripts %} or any layout block): #}
{% render_announcements %}

Add the JavaScript once (before </body>):

{% load static %}
<script src="{% static 'oscar_announcements/js/announcements.js' %}"></script>

Add the CSS (only needed on non-Bootstrap pages; Oscar dashboard gets colours from Bootstrap):

<link rel="stylesheet" href="{% static 'oscar_announcements/css/announcements.css' %}">

Running background tasks

Background tasks delete the announcements that have expired.

See Running Tasks(https://django-background-tasks.readthedocs.io/en/latest/#running-tasks).

Extending visibility

Register custom audience handlers in your app's AppConfig.ready():

# myapp/apps.py
from oscar_announcements.visibility import register
from django.utils.translation import gettext_lazy as _

class MyAppConfig(AppConfig):
    def ready(self):
        register(
            "verified",
            _("Verified users"),
            lambda user: getattr(user, "is_verified", False) or user.is_staff,
        )

The new option then appears automatically in the dashboard form's Audience dropdown.


Template tags reference

{% load oscar_announcements_tags %}

# Render the built-in partial (oscar_announcements/partials/announcements.html):
{% render_announcements %}

# Assign the list to a variable for custom rendering:
{% get_announcements as my_announcements %}
{% for ann in my_announcements %}
    <p class="oa-announcement--{{ ann.level }}">{{ ann.content }}</p>
{% endfor %}

Development

Setup:

git clone https://github.com/niccokunzmann/django-oscar-announcements
cd django-oscar-announcements
make dev

Run tests:

make test

Run the example:

cd example
make superuser # creates a superuser
make test # runs the tests
make run # runs the example

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_oscar_announcements-0.0.2.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_oscar_announcements-0.0.2-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

Details for the file django_oscar_announcements-0.0.2.tar.gz.

File metadata

  • Download URL: django_oscar_announcements-0.0.2.tar.gz
  • Upload date:
  • Size: 32.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_oscar_announcements-0.0.2.tar.gz
Algorithm Hash digest
SHA256 ed5795db9292beb6c7a34a20e978b06ab42dfa82b7e7e56e448682700efd5de1
MD5 c5bceb41253a0cd6d4b680e04494ede5
BLAKE2b-256 e390253d2bc6dd08437b97fc609dda3d8958b5ed9c742a1285bd3c09793c6a12

See more details on using hashes here.

File details

Details for the file django_oscar_announcements-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_oscar_announcements-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 32.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_oscar_announcements-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7deb787187b766e9557bb0fdc5384e01e70aab1715c568e1d578524ef82237dd
MD5 b9d10d34fcb5e99c2f00919ec7e4b6c4
BLAKE2b-256 8c5de61f0ebd3d8bfb72ff37798ad4456cccff2b98a059fbbc09e2f11182800d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page