From 2fbcdea5cf80a5543a82cc0dd1a21de639e46ef2 Mon Sep 17 00:00:00 2001 From: Kevin Renskers Date: Sun, 3 Aug 2025 13:52:07 +0200 Subject: [PATCH 1/3] feat: Include user preference helpers with the library --- README.md | 25 +- example/notifications/templates/settings.html | 4 +- example/notifications/views.py | 89 +------ generic_notifications/preferences.py | 105 ++++++++ pyproject.toml | 4 +- tests/test_preferences.py | 231 ++++++++++++++++++ 6 files changed, 373 insertions(+), 85 deletions(-) create mode 100644 generic_notifications/preferences.py create mode 100644 tests/test_preferences.py diff --git a/README.md b/README.md index d45ca87..e3411e4 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,29 @@ By default every user gets notifications of all registered types delivered to ev All notification types default to daily digest, except for `SystemMessage` which defaults to real-time. Users can choose different frequency per notification type. +### Using the preference helpers + +The library provides helper functions to simplify building preference management UIs: + +```python +from generic_notifications.preferences import ( + get_notification_preferences, + save_notification_preferences +) + +# Get preferences for display in a form +# Returns a list of dicts with notification types, channels, and current settings +preferences = get_notification_preferences(user) + +# Save preferences from form data +# Form field format: {notification_type_key}__{channel_key} and {notification_type_key}__frequency +save_notification_preferences(user, request.POST) +``` + +### Manual preference management + +You can also manage preferences directly: + ```python from generic_notifications.models import DisabledNotificationTypeChannel, EmailFrequency from generic_notifications.channels import EmailChannel @@ -107,7 +130,7 @@ EmailFrequency.objects.update_or_create( ) ``` -This project doesn’t come with a UI (view + template) for managing user preferences, but an example is provided in the [example app](#example-app). +This project doesn't come with a UI (view + template) for managing user preferences, but an example is provided in the [example app](#example-app). ## Custom Channels diff --git a/example/notifications/templates/settings.html b/example/notifications/templates/settings.html index d2a39c4..481deaf 100644 --- a/example/notifications/templates/settings.html +++ b/example/notifications/templates/settings.html @@ -38,7 +38,7 @@

Notification settings

{% with channel_data=type_data.channels|dict_get:channel_key %} Notification settings {% if "email" in channels %} -