Ticket #3995: 3995.diff

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

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

  • django/contrib/auth/models.py

     
    261261    def get_and_delete_messages(self):
    262262        messages = []
    263263        for m in self.message_set.all():
    264             messages.append(m.message)
     264            messages.append(m)
    265265            m.delete()
    266266        return messages
    267267
     
    293293    The message system is a lightweight way to queue messages for given
    294294    users. A message is associated with a User instance (so it is only
    295295    applicable for registered users). There's no concept of expiration or
    296     timestamps. Messages are created by the Django admin after successful
    297     actions. For example, "The poll Foo was created successfully." is a
    298     message.
     296    timestamps. A message consists of the actual message text and an optional
     297    30 characters or fewer category string. Messages are created by the Django
     298    admin after successful actions. For example, "The poll Foo was created
     299    successfully." is a message.
    299300    """
    300301    user = models.ForeignKey(User)
    301302    message = models.TextField(_('message'))
     303    category = models.CharField(_('category'), max_length=30, blank=True)
    302304
    303305    def __unicode__(self):
    304306        return self.message
  • django/contrib/auth/management/__init__.py

     
    44
    55from django.db.models import get_models, signals
    66from django.contrib.auth import models as auth_app
     7from django.contrib.auth.models import Message
    78
    89def _get_permission_codename(action, opts):
    910    return u'%s_%s' % (action, opts.object_name.lower())
     
    4445                call_command("createsuperuser", interactive=True)
    4546            break
    4647
     48def 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
    4759signals.post_syncdb.connect(create_permissions,
    4860    dispatch_uid = "django.contrib.auth.management.create_permissions")
    4961signals.post_syncdb.connect(create_superuser,
    5062    sender=auth_app, dispatch_uid = "django.contrib.auth.management.create_superuser")
     63signals.post_syncdb.connect(add_category_field, sender=auth_app)
  • docs/topics/auth.txt

     
    12781278The message system is a lightweight way to queue messages for given users.
    12791279
    12801280A message is associated with a :class:`~django.contrib.auth.models.User`.
    1281 There's no concept of expiration or timestamps.
     1281There's no concept of expiration or timestamps. A message consists of the actual
     1282``message`` text and an optional 30 characters or fewer ``category`` string.
    12821283
    12831284Messages are used by the Django admin after successful actions. For example,
    12841285``"The poll Foo was created successfully."`` is a message.
     
    12881289.. method:: models.User.message_set.create(message)
    12891290
    12901291    To create a new message, use
    1291     ``user_obj.message_set.create(message='message_text')``.
     1292    ``user_obj.message_set.create(message='message_text',
     1293    category='message_category')``. )``.
    12921294
    12931295    To retrieve/delete messages, use
    12941296    :meth:`user_obj.get_and_delete_messages() <django.contrib.auth.models.User.get_and_delete_messages>`,
     
    13011303    def create_playlist(request, songs):
    13021304        # Create the playlist with the given songs.
    13031305        # ...
    1304         request.user.message_set.create(message="Your playlist was added successfully.")
     1306        request.user.message_set.create(message="Your playlist was added successfully.",
     1307            category="information")
    13051308        return render_to_response("playlists/create.html",
    13061309            context_instance=RequestContext(request))
    13071310
     
    13151318    {% if messages %}
    13161319    <ul>
    13171320        {% for message in messages %}
    1318         <li>{{ message }}</li>
     1321        <li class="{{ message.category }}">{{ message }}</li>
    13191322        {% endfor %}
    13201323    </ul>
    13211324    {% endif %}
  • tests/regressiontests/auth_backends/tests.py

     
    7575False
    7676>>> user.has_perms(['auth.test2', 'auth.test3'])
    7777False
     78
     79>>> user = User.objects.create_user('test2', 'test2@example.com', 'test')
     80>>> message = user.message_set.create(message="uncategorized message")
     81>>> messages = user.get_and_delete_messages()
     82>>> messages[0]
     83<Message: uncategorized message>
     84>>> messages[0].category
     85u''
     86>>> message = user.message_set.create(message="Categorized message", category="test")
     87>>> messages = user.get_and_delete_messages()
     88>>> messages[0].category
     89u'test'
    7890"""}
Back to Top