@@ -25,26 +25,25 @@ For example::
25
25
"you have received an invitation"
26
26
)
27
27
28
+ Before Django-1.7, the typical way to automatically do this notice type creation
29
+ was in a ` management.py ` file for your app, attached to the syncdb signal.
28
30
29
- One good way to automatically do this notice type creation is in a
30
- ` management.py ` file for your app, attached to the syncdb signal.
31
+ Django-1.7 deprecated the ` post_syncdb ` signal, so this system needs to be changed. One possible way to do it is using a custom ` AppConfig ` .
31
32
32
33
Here is an example:
33
34
35
+ # myapp/signals/handlers.py
34
36
from django.conf import settings
35
- from django.db.models import signals
36
37
from django.utils.translation import ugettext_noop as _
37
38
38
- if "notification" in settings.INSTALLED_APPS:
39
- from pinax.notifications.models import NoticeType
40
-
41
- def create_notice_types(app, created_models, verbosity, **kwargs):
39
+ def create_notice_types(sender, **kwargs):
40
+ if " pinax.notifications" in settings.INSTALLED_APPS:
41
+ from pinax.notifications.models import NoticeType
42
+ print "Creating notices for myapp"
42
43
NoticeType.create("friends_invite", _("Invitation Received"), _("you have received an invitation"))
43
44
NoticeType.create("friends_accept", _("Acceptance Received"), _("an invitation you sent has been accepted"))
44
-
45
- signals.post_syncdb.connect(create_notice_types, sender=NoticeType)
46
- else:
47
- print "Skipping creation of NoticeTypes as notification app not found"
45
+ else:
46
+ print "Skipping creation of NoticeTypes as notification app not found"
48
47
49
48
Notice that the code is wrapped in a conditional clause so if
50
49
` pinax-notifications ` is not installed, your app will proceed anyway.
@@ -53,6 +52,23 @@ Note that the display and description arguments are marked for translation by
53
52
using ugettext_noop. That will enable you to use Django's makemessages
54
53
management command and use ` pinax-notifications ` i18n capabilities.
55
54
55
+ # myapp/apps.py
56
+ from django.apps import AppConfig
57
+ from django.db.models.signals import post_migrate
58
+
59
+ from myapp.signals import handlers
60
+
61
+ class MyAppConfig(AppConfig):
62
+ name = 'myapp'
63
+ verbose_name = 'My App'
64
+
65
+ def ready(self):
66
+ post_migrate.connect(handlers.create_notice_types, sender=self)
67
+
68
+ This will call the handler to create notices after the application is migrated.
69
+
70
+ # myapp/__init__.py
71
+ default_app_config = 'myapp.apps.MyAppConfig'
56
72
57
73
## Notification Templates
58
74
0 commit comments