Skip to content

Commit 3625082

Browse files
committed
Fix import issues with swappable SentNotifications model
1 parent a525623 commit 3625082

12 files changed

Lines changed: 73 additions & 69 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

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/admin.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,14 @@
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-
23-
24-
@admin.register(SentNotification)
2514
class SentNotificationAdmin(admin.ModelAdmin):
2615
"""
2716
Admin for viewing historical notifications sent
@@ -80,7 +69,7 @@ def wrapper(*args, **kwargs):
8069
info = opts.app_label, opts.model_name
8170

8271
return [
83-
url(r"^(.+)/resend/$", wrap(self.resend_view), name="%s_%s_resend" % info),
72+
re_path(r"^(.+)/resend/$", wrap(self.resend_view), name="%s_%s_resend" % info),
8473
] + urls
8574

8675
@csrf_protect_m
@@ -110,7 +99,6 @@ def resend_view(
11099
return self.response_post_save_change(request, obj)
111100

112101

113-
@admin.register(Notification)
114102
class NotificationAdmin(admin.ModelAdmin):
115103
"""
116104
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,
@@ -138,6 +134,8 @@ def _delete_expired_notifications():
138134

139135
if not retention_time:
140136
return
137+
138+
SentNotification = get_sent_notification_model()
141139

142140
cutoff_date = timezone.now() - retention_time
143141

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(

herald/management/commands/delnotifs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.core.management.base import BaseCommand
44
from django.utils import timezone
55

6-
from ...models import SentNotification
6+
from ...utils import get_sent_notification_model
77

88

99
def valid_date(s):
@@ -22,6 +22,7 @@ def add_arguments(self, parser):
2222
)
2323

2424
def handle(self, *args, **options):
25+
SentNotification = get_sent_notification_model()
2526
start_date = options.get("start")
2627
end_date = options.get("end")
2728

herald/urls.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
"""
22
Urls for herald app
33
"""
4-
5-
try:
6-
from django.urls import re_path as url
7-
except ImportError:
8-
from django.conf.urls import url
4+
from django.urls import re_path
95

106
from .views import TestNotification, TestNotificationList
117

128
urlpatterns = [
13-
url(r"^$", TestNotificationList.as_view(), name="herald_preview_list"),
14-
url(
9+
re_path(r"^$", TestNotificationList.as_view(), name="herald_preview_list"),
10+
re_path(
1511
r"^(?P<index>\d+)/(?P<type>[\w\-]+)/$",
1612
TestNotification.as_view(),
1713
name="herald_preview",

0 commit comments

Comments
 (0)