Ticket #3995: 3995.django.contrib.auth.diff

File 3995.django.contrib.auth.diff, 3.8 KB (added by Pete Crosier, 17 years ago)

Updated messages with a type, with tests and documentation

  • django/contrib/auth/models.py

     
    277277    def get_and_delete_messages(self):
    278278        messages = []
    279279        for m in self.message_set.all():
    280             messages.append(m.message)
     280            messages.append(m)
    281281            m.delete()
    282282        return messages
    283283
     
    308308    The message system is a lightweight way to queue messages for given
    309309    users. A message is associated with a User instance (so it is only
    310310    applicable for registered users). There's no concept of expiration or
    311     timestamps. Messages are created by the Django admin after successful
    312     actions. For example, "The poll Foo was created successfully." is a
    313     message.
     311    timestamps. A message consists of the actual message text and an optional
     312    30 characters or fewer category string. Messages are created by the Django
     313    admin after successful actions. For example, "The poll Foo was created
     314    successfully." is a message.
    314315    """
    315316    user = models.ForeignKey(User)
    316317    message = models.TextField(_('message'))
     318    category = models.CharField(_('category'), max_length=30, blank=True)
    317319
    318320    def __unicode__(self):
    319321        return self.message
  • tests/regressiontests/auth_backends/tests.py

     
    6969True
    7070>>> user.has_perms(['auth.test3', 'auth.test_group'])
    7171True
     72
     73>>> user = User.objects.create_user('test2', 'test2@example.com', 'test')
     74>>> message = user.message_set.create(message="uncategorised message")
     75>>> messages = user.get_and_delete_messages()
     76>>> messages[0]
     77<Message: uncategorised message>
     78>>> messages[0].category
     79u''
     80>>> message = user.message_set.create(message="Categorised message", category="test")
     81>>> messages = user.get_and_delete_messages()
     82>>> messages[0].category
     83u'test'
    7284"""}
  • docs/authentication.txt

     
    959959The message system is a lightweight way to queue messages for given users.
    960960
    961961A message is associated with a ``User``. There's no concept of expiration or
    962 timestamps.
     962timestamps. A message consists of the actual ``message`` text and an optional
     96330 characters or fewer ``category`` string.
    963964
    964965Messages are used by the Django admin after successful actions. For example,
    965966``"The poll Foo was created successfully."`` is a message.
     
    967968The API is simple:
    968969
    969970    * To create a new message, use
    970       ``user_obj.message_set.create(message='message_text')``.
     971      ``user_obj.message_set.create(message='message_text',
     972      category='message_category')``.
    971973    * To retrieve/delete messages, use ``user_obj.get_and_delete_messages()``,
    972974      which returns a list of ``Message`` objects in the user's queue (if any)
    973975      and deletes the messages from the queue.
     
    978980    def create_playlist(request, songs):
    979981        # Create the playlist with the given songs.
    980982        # ...
    981         request.user.message_set.create(message="Your playlist was added successfully.")
     983        request.user.message_set.create(message="Your playlist was added successfully.",
     984            category="information")
    982985        return render_to_response("playlists/create.html",
    983986            context_instance=RequestContext(request))
    984987
     
    989992    {% if messages %}
    990993    <ul>
    991994        {% for message in messages %}
    992         <li>{{ message }}</li>
     995        <li class="{{ message.category }}">{{ message }}</li>
    993996        {% endfor %}
    994997    </ul>
    995998    {% endif %}
Back to Top