Code

Ticket #3995: 3995.diff

File 3995.diff, 5.2 KB (added by timo, 5 years ago)

updated patch to apply to trunk, added post_syncdb handler to add field

Line 
1Index: django/contrib/auth/models.py
2===================================================================
3--- django/contrib/auth/models.py       (revision 11204)
4+++ django/contrib/auth/models.py       (working copy)
5@@ -261,7 +261,7 @@
6     def get_and_delete_messages(self):
7         messages = []
8         for m in self.message_set.all():
9-            messages.append(m.message)
10+            messages.append(m)
11             m.delete()
12         return messages
13 
14@@ -293,12 +293,14 @@
15     The message system is a lightweight way to queue messages for given
16     users. A message is associated with a User instance (so it is only
17     applicable for registered users). There's no concept of expiration or
18-    timestamps. Messages are created by the Django admin after successful
19-    actions. For example, "The poll Foo was created successfully." is a
20-    message.
21+    timestamps. A message consists of the actual message text and an optional
22+    30 characters or fewer category string. Messages are created by the Django
23+    admin after successful actions. For example, "The poll Foo was created
24+    successfully." is a message.
25     """
26     user = models.ForeignKey(User)
27     message = models.TextField(_('message'))
28+    category = models.CharField(_('category'), max_length=30, blank=True)
29 
30     def __unicode__(self):
31         return self.message
32Index: django/contrib/auth/management/__init__.py
33===================================================================
34--- django/contrib/auth/management/__init__.py  (revision 11204)
35+++ django/contrib/auth/management/__init__.py  (working copy)
36@@ -4,6 +4,7 @@
37 
38 from django.db.models import get_models, signals
39 from django.contrib.auth import models as auth_app
40+from django.contrib.auth.models import Message
41 
42 def _get_permission_codename(action, opts):
43     return u'%s_%s' % (action, opts.object_name.lower())
44@@ -44,7 +45,19 @@
45                 call_command("createsuperuser", interactive=True)
46             break
47 
48+def add_category_field(app, created_models, verbosity, **kwargs):
49+    from django.db import connection
50+    cursor = connection.cursor()
51+    data_type = connection.creation.data_types['CharField'] % {'max_length': 30}
52+    alter = 'ALTER TABLE %s ADD COLUMN `category` %s NOT NULL;' % (Message._meta.db_table, data_type)
53+    try:
54+        cursor.execute(alter)
55+    except:
56+        # column may already exist
57+        pass
58+
59 signals.post_syncdb.connect(create_permissions,
60     dispatch_uid = "django.contrib.auth.management.create_permissions")
61 signals.post_syncdb.connect(create_superuser,
62     sender=auth_app, dispatch_uid = "django.contrib.auth.management.create_superuser")
63+signals.post_syncdb.connect(add_category_field, sender=auth_app)
64Index: docs/topics/auth.txt
65===================================================================
66--- docs/topics/auth.txt        (revision 11204)
67+++ docs/topics/auth.txt        (working copy)
68@@ -1278,7 +1278,8 @@
69 The message system is a lightweight way to queue messages for given users.
70 
71 A message is associated with a :class:`~django.contrib.auth.models.User`.
72-There's no concept of expiration or timestamps.
73+There's no concept of expiration or timestamps. A message consists of the actual
74+``message`` text and an optional 30 characters or fewer ``category`` string.
75 
76 Messages are used by the Django admin after successful actions. For example,
77 ``"The poll Foo was created successfully."`` is a message.
78@@ -1288,7 +1289,8 @@
79 .. method:: models.User.message_set.create(message)
80 
81     To create a new message, use
82-    ``user_obj.message_set.create(message='message_text')``.
83+    ``user_obj.message_set.create(message='message_text',
84+    category='message_category')``. )``.
85 
86     To retrieve/delete messages, use
87     :meth:`user_obj.get_and_delete_messages() <django.contrib.auth.models.User.get_and_delete_messages>`,
88@@ -1301,7 +1303,8 @@
89     def create_playlist(request, songs):
90         # Create the playlist with the given songs.
91         # ...
92-        request.user.message_set.create(message="Your playlist was added successfully.")
93+        request.user.message_set.create(message="Your playlist was added successfully.",
94+            category="information")
95         return render_to_response("playlists/create.html",
96             context_instance=RequestContext(request))
97 
98@@ -1315,7 +1318,7 @@
99     {% if messages %}
100     <ul>
101         {% for message in messages %}
102-        <li>{{ message }}</li>
103+        <li class="{{ message.category }}">{{ message }}</li>
104         {% endfor %}
105     </ul>
106     {% endif %}
107Index: tests/regressiontests/auth_backends/tests.py
108===================================================================
109--- tests/regressiontests/auth_backends/tests.py        (revision 11204)
110+++ tests/regressiontests/auth_backends/tests.py        (working copy)
111@@ -75,4 +75,16 @@
112 False
113 >>> user.has_perms(['auth.test2', 'auth.test3'])
114 False
115+
116+>>> user = User.objects.create_user('test2', 'test2@example.com', 'test')
117+>>> message = user.message_set.create(message="uncategorized message")
118+>>> messages = user.get_and_delete_messages()
119+>>> messages[0]
120+<Message: uncategorized message>
121+>>> messages[0].category
122+u''
123+>>> message = user.message_set.create(message="Categorized message", category="test")
124+>>> messages = user.get_and_delete_messages()
125+>>> messages[0].category
126+u'test'
127 """}