Django

Code

Ticket #3995 (new)

Opened 2 years ago

Last modified 1 year ago

Give the Message model a type (success/failure/generic)

Reported by: joe4444 Assigned to: PJCrosier
Milestone: Component: Core framework
Version: SVN Keywords: auth message
Cc: django@apolloner.eu, ross@rossp.org Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The message system is just a bit too light for me. Rather than assume all messages indicate a successful action I think they should also be able to point out errors (unrelated to form validation obviously) or just display generic notices. This patch makes it possible to create 3 types of messages: success (default), failure, and generic. Normal messages will remain the same (always success). For a failure/error or generic message you simply pass an extra argument like so:

    user.message_set.create(message=u'Something went wrong!', category='F')    # failure or error
    user.message_set.create(message=u'This is just a notice', category='G')    # generic message
    user.message_set.create(message=u'It worked!', category='S')    # explicit success
    user.message_set.create(message=u'It worked!')    # implicit success


Now in your template you can distinguish messages by type, and then use CSS to style them differently.

{% if messages %}
  <ul id="messages">
{% for message in messages %}
    <li class="{{ message.get_category_display }}">{{ message }}</li>
{% endfor %}
  </ul>
{% endif %}

Attachments

message_type.diff (0.9 kB) - added by joe4444 on 04/10/07 11:27:12.
Modify the Message model to categorize messages
message_type.2.diff (1.7 kB) - added by joe4444 on 04/10/07 11:42:34.
Same code; appended Message model docstring
message_type.3.diff (1.7 kB) - added by joe4444 on 04/12/07 03:16:18.
I think the others only worked for models.py from 0.96
3995.django.contrib.auth.diff (3.8 kB) - added by PJCrosier on 12/20/07 17:21:43.
Updated messages with a type, with tests and documentation

Change History

04/10/07 11:27:12 changed by joe4444

  • attachment message_type.diff added.

Modify the Message model to categorize messages

04/10/07 11:41:40 changed by joe4444

  • needs_better_patch changed.
  • version changed from SVN to 0.96.
  • needs_tests changed.
  • needs_docs changed.

Probably doesn't make a difference, but I patched 0.96 not trunk.

04/10/07 11:42:34 changed by joe4444

  • attachment message_type.2.diff added.

Same code; appended Message model docstring

04/10/07 11:48:28 changed by joe4444

  • needs_better_patch set to 1.
  • needs_tests set to 1.
  • needs_docs set to 1.

Using a CharField? with choices might not be the ideal solution. Any other suggestions?

04/11/07 05:26:47 changed by Simon G. <dev@simon.net.nz>

  • version changed from 0.96 to SVN.
  • stage changed from Unreviewed to Design decision needed.

Interesting idea Joe, please bring it up on Django-Developers to get some discussion going (this is a backwards incompat. issue after all).

04/11/07 05:27:08 changed by Simon G. <dev@simon.net.nz>

  • summary changed from Give the Message model a type to Give the Message model a type (success/failure/generic).

04/11/07 16:18:11 changed by joe4444

Yeah I guess you're right. I was thinking since code for creating new messages doesn't have to change then it is backwards compatible. But changing the Message model requires an update on the db table auth_message.

04/12/07 03:16:18 changed by joe4444

  • attachment message_type.3.diff added.

I think the others only worked for models.py from 0.96

09/16/07 13:57:05 changed by ubernostrum

I'd be -1 on this because of the backwards incompatibility, but there have been various proposals to tie messages into sessions (for messages to anonymous users), and if we ever do that we should revisit this issue.

12/01/07 11:32:32 changed by jacob

  • stage changed from Design decision needed to Accepted.

I think this is basically a good idea, but there needs to be a transition plan. I'd like to see a post-syncdb handler that automatically upgrades the table (if needed), and I'd like the type to just be a string field (instead of choices). And optional, of course.

12/01/07 11:43:54 changed by xian

I think type as a slugfield makes sense. That way you can use them as css classes and deal with them in the templates a bit easier. And it would be good to be able to get to them separately in the templates {{ messages }} all messages. {{ messages.warning }} just the messages with type='warning'.

12/09/07 05:44:13 changed by anonymous

  • cc set to django@apolloner.eu.

12/17/07 22:25:31 changed by anonymous

  • cc changed from django@apolloner.eu to django@apolloner.eu, ross@rossp.org.

12/20/07 17:03:51 changed by PJCrosier

  • owner changed from nobody to PJCrosier.
  • needs_better_patch deleted.
  • needs_tests deleted.
  • needs_docs deleted.

Patch attached as per Jacob's suggestion - just using a string (limited to 30 chars) rather than choices. Ignoring this patch, I think all calls to user.message_set.create need to use (message="blah") rather than just ("blah") - so I think it's backwards compatible (the type is optional in the patch)?

12/20/07 17:21:43 changed by PJCrosier

  • attachment 3995.django.contrib.auth.diff added.

Updated messages with a type, with tests and documentation

12/20/07 17:24:31 changed by PJCrosier

Quick patch update to use "category" rather than "type" attached, thanks to oebfare for pointing out problems and that the patch is indeed backwards incompatible without doing something post-syncdb.


Add/Change #3995 (Give the Message model a type (success/failure/generic))




Change Properties
Action