Skip to content

Commit cab0fc1

Browse files
authored
Fix import issues with swappable SentNotifications model (#32)
* Fix import issues with swappable SentNotifications model - #31 * fix lint error for black * Update for 0.4.1
1 parent a525623 commit cab0fc1

14 files changed

Lines changed: 81 additions & 72 deletions

File tree

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ source = herald
55
branch = True
66
omit =
77
*/migrations/*
8+
*/tests/*
9+
runtests.py

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ nosetests.xml
4444
coverage.xml
4545
*,cover
4646
.hypothesis/
47+
htmlcov
4748

4849
# Translations
4950
*.mo
@@ -66,4 +67,7 @@ target/
6667

6768
# Herald Specific
6869
*.sqlite3
69-
.venv
70+
.venv
71+
72+
# MacOS Specific
73+
.DS_store

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 0.4.1 (08-21-2025)
4+
- #31 - Fix AppRegistryNotReady error on importing from herald.base
5+
6+
37
## 0.4.0 (06-10-2025)
48
- #19 - Removed Python 2 support and Six
59
- #28 - Set default_auto_field to django.db.models.AutoField in HeraldConfig

CONTRIBUTING.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Contrinbuting
2+
3+
## Running Tests
4+
5+
This will run with a SQLite3 in-memory database:
6+
7+
```bash
8+
python runtests.py -v 2
9+
```
10+
11+
## Running Server to See Views
12+
13+
You will need to run migrations:
14+
15+
```bash
16+
python manage.py migrate
17+
```
18+
19+
Run server:
20+
21+
```bash
22+
python manage.py runserver 0.0.0.0:8000
23+
```

README.md

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -452,28 +452,4 @@ You can also attach any MIMEBase objects as regular attachments, but you must ad
452452
my_image.add_header('Content-Disposition', 'attachment; filename="python.jpg"')
453453
```
454454
455-
Attachments can cause your database to become quite large, so you should be sure to run the management commands to purge the database of old messages.
456-
457-
# Development
458-
459-
## Running Tests
460-
461-
This will run with a SQLite3 in-memory database:
462-
463-
```bash
464-
python runtests.py -v 2
465-
```
466-
467-
## Running Server to See Views
468-
469-
You will need to run migrations:
470-
471-
```bash
472-
python manage.py migrate
473-
```
474-
475-
Run server:
476-
477-
```bash
478-
python manage.py runserver 0.0.0.0:8000
479-
```
455+
Attachments can cause your database to become quite large, so you should be sure to run the management commands to purge the database of old messages.

herald/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.4.0"
1+
__version__ = "0.4.1"
22

33
default_app_config = "herald.apps.HeraldConfig"
44

herald/admin.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@
33
"""
44

55
from functools import update_wrapper
6-
7-
try:
8-
from django.urls import re_path as url
9-
except ImportError:
10-
from django.conf.urls import url
11-
126
from django.contrib import admin, messages
137
from django.contrib.admin.options import csrf_protect_m
148
from django.contrib.admin.utils import unquote
15-
from django.urls import reverse
9+
from django.urls import re_path, reverse
1610
from django.utils.safestring import mark_safe
1711

18-
from herald.utils import get_sent_notification_model
1912
from .models import Notification
2013

21-
SentNotification = get_sent_notification_model()
22-
2314

24-
@admin.register(SentNotification)
2515
class SentNotificationAdmin(admin.ModelAdmin):
2616
"""
2717
Admin for viewing historical notifications sent
@@ -80,7 +70,9 @@ def wrapper(*args, **kwargs):
8070
info = opts.app_label, opts.model_name
8171

8272
return [
83-
url(r"^(.+)/resend/$", wrap(self.resend_view), name="%s_%s_resend" % info),
73+
re_path(
74+
r"^(.+)/resend/$", wrap(self.resend_view), name="%s_%s_resend" % info
75+
),
8476
] + urls
8577

8678
@csrf_protect_m
@@ -110,7 +102,6 @@ def resend_view(
110102
return self.response_post_save_change(request, obj)
111103

112104

113-
@admin.register(Notification)
114105
class NotificationAdmin(admin.ModelAdmin):
115106
"""
116107
Admin for viewing/managing notifications in the system

herald/apps.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def ready(self):
1919

2020
self.module.autodiscover()
2121

22+
self.register_admins()
23+
2224
Notification = self.get_model("Notification")
2325

2426
try:
@@ -43,3 +45,21 @@ def ready(self):
4345
except ProgrammingError:
4446
# if the database is not created yet, keep going (ie: during testing)
4547
pass
48+
49+
def register_admins(self):
50+
"""
51+
Register admin classes if they are not already registered.
52+
This is the correct way to handle admin registration for swappable models.
53+
"""
54+
from django.contrib import admin
55+
56+
from herald.admin import NotificationAdmin, SentNotificationAdmin
57+
from herald.models import Notification
58+
from herald.utils import get_sent_notification_model
59+
60+
SentNotification = get_sent_notification_model()
61+
62+
admin.site.register(Notification, NotificationAdmin)
63+
64+
if not admin.site.is_registered(SentNotification):
65+
admin.site.register(SentNotification, SentNotificationAdmin)

herald/base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
"""
2-
Base notification classes
3-
"""
4-
51
import json
62
import re
73
from email.mime.base import MIMEBase
@@ -18,8 +14,6 @@
1814

1915
from herald.utils import get_sent_notification_model
2016

21-
SentNotification = get_sent_notification_model()
22-
2317

2418
class NotificationBase:
2519
"""
@@ -97,6 +91,8 @@ def send(self, raise_exception=False, user=None):
9791
subject = self.get_subject()
9892
extra_data = self.get_extra_data()
9993

94+
SentNotification = get_sent_notification_model()
95+
10096
sent_notification = SentNotification(
10197
recipients=",".join(recipients),
10298
text_content=text_content,
@@ -139,6 +135,8 @@ def _delete_expired_notifications():
139135
if not retention_time:
140136
return
141137

138+
SentNotification = get_sent_notification_model()
139+
142140
cutoff_date = timezone.now() - retention_time
143141

144142
notifications = SentNotification.objects.filter(date_sent__lt=cutoff_date)

herald/contrib/auth/notifications.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from django.utils.encoding import force_bytes
1010

1111
try:
12-
from django.utils.encoding import force_str as force_text
12+
from django.utils.encoding import force_str
1313
except ImportError:
14-
from django.utils.encoding import force_text
14+
# Django 3.2 compatibility: force_text was renamed to force_str in Django 4.0
15+
from django.utils.encoding import force_text as force_str
1516

1617
from django.urls import reverse
1718
from django.utils.http import urlsafe_base64_encode
@@ -60,7 +61,7 @@ def get_context_data(self):
6061
self.domain = current_site.domain
6162

6263
protocol = "https" if self.use_https else "http"
63-
uid = force_text(urlsafe_base64_encode(force_bytes(self.user.pk)))
64+
uid = force_str(urlsafe_base64_encode(force_bytes(self.user.pk)))
6465
token = self.token_generator.make_token(self.user)
6566

6667
context.update(

0 commit comments

Comments
 (0)