Expose level_tag property on messages. #1891

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

Contributor
Sjord commented Nov 7, 2013

Exposing the level name (e.g. "info") makes it possible to prepend
something to the class name. For example, Twitter Bootstrap has
an alert-info class. This class can now be added to the message
using class="alert-{{ message.level_tag }}".
Because the level_tag was on the end of the tags property, it
could not be used in this fashion when extra_tags were given.

Owner

Hi, it looks like you've sent a pull request without filing a Trac ticket. Please file a ticket to suggest changes.

The patch would also require a documentation in order to be merged.

@timgraham timgraham closed this Nov 8, 2013
@timgraham timgraham reopened this Nov 8, 2013
Contributor
Sjord commented Nov 11, 2013
@bmispelon bmispelon commented on an outdated diff Nov 11, 2013
django/contrib/messages/storage/base.py
@@ -40,18 +40,21 @@ def __str__(self):
return force_text(self.message)
def _get_tags(self):
- label_tag = force_text(LEVEL_TAGS.get(self.level, ''),
- strings_only=True)
+ level_tag = self._get_level_tag()
bmispelon
bmispelon Nov 11, 2013 Member

Since you made it a property you can just use self.level_tag where label_tag was used before (no need to create a temporary variable).

@bmispelon bmispelon commented on an outdated diff Nov 11, 2013
django/contrib/messages/storage/base.py
return ''
tags = property(_get_tags)
+ def _get_level_tag(self):
+ return force_text(LEVEL_TAGS.get(self.level, ''), strings_only=True)
+ level_tag = property(_get_level_tag)
bmispelon
bmispelon Nov 11, 2013 Member

I think the @property syntax would be more readable here.

@bmispelon bmispelon commented on an outdated diff Nov 11, 2013
docs/ref/contrib/messages.txt
@@ -122,13 +122,16 @@ than this will be ignored.
Message tags
------------
-Message tags are a string representation of the message level plus any
-extra tags that were added directly in the view (see
-`Adding extra message tags`_ below for more details). Tags are stored in a
-string and are separated by spaces. Typically, message tags
-are used as CSS classes to customize message style based on message type. By
-default, each level has a single tag that's a lowercase version of its own
-constant:
+Each message has at least one tag, which is the string representation of the
bmispelon
bmispelon Nov 11, 2013 Member

I think you can remove the comma before "which"

@bmispelon bmispelon commented on an outdated diff Nov 11, 2013
docs/ref/contrib/messages.txt
@@ -122,13 +122,16 @@ than this will be ignored.
Message tags
------------
-Message tags are a string representation of the message level plus any
-extra tags that were added directly in the view (see
-`Adding extra message tags`_ below for more details). Tags are stored in a
-string and are separated by spaces. Typically, message tags
-are used as CSS classes to customize message style based on message type. By
-default, each level has a single tag that's a lowercase version of its own
-constant:
+Each message has at least one tag, which is the string representation of the
+message level. The `level_tag` property contains a string with only the
+name of the level. Additionaly, you can add extra tags by supplying the
+extra_tags argument (see `Adding extra message tags`_ below for more details).
bmispelon
bmispelon Nov 11, 2013 Member

"extra_tags" should be quoted

@bmispelon bmispelon commented on an outdated diff Nov 11, 2013
docs/ref/contrib/messages.txt
@@ -122,13 +122,16 @@ than this will be ignored.
Message tags
------------
-Message tags are a string representation of the message level plus any
-extra tags that were added directly in the view (see
-`Adding extra message tags`_ below for more details). Tags are stored in a
-string and are separated by spaces. Typically, message tags
-are used as CSS classes to customize message style based on message type. By
-default, each level has a single tag that's a lowercase version of its own
-constant:
+Each message has at least one tag, which is the string representation of the
+message level. The `level_tag` property contains a string with only the
bmispelon
bmispelon Nov 11, 2013 Member

sphinx uses double backticks for quoting

@bmispelon bmispelon commented on an outdated diff Nov 11, 2013
docs/ref/contrib/messages.txt
@@ -284,7 +287,7 @@ containing extra tags to any of the add methods::
messages.error(request, 'Email box full', extra_tags='email')
Extra tags are added before the default tag for that level and are space
-separated.
+separated. If you want only the default tag using the `level_tag` property.
bmispelon
bmispelon Nov 11, 2013 Member

I'm not sure this sentence belongs in this section since it's about adding extra tags from inside a view.

@bmispelon bmispelon commented on an outdated diff Nov 11, 2013
docs/ref/contrib/messages.txt
@@ -122,13 +122,16 @@ than this will be ignored.
Message tags
------------
-Message tags are a string representation of the message level plus any
-extra tags that were added directly in the view (see
-`Adding extra message tags`_ below for more details). Tags are stored in a
-string and are separated by spaces. Typically, message tags
-are used as CSS classes to customize message style based on message type. By
-default, each level has a single tag that's a lowercase version of its own
-constant:
+Each message has at least one tag, which is the string representation of the
+message level. The `level_tag` property contains a string with only the
+name of the level. Additionaly, you can add extra tags by supplying the
bmispelon
bmispelon Nov 11, 2013 Member

You can remove the double-spacing before "Additionaly". Also, it's spelled with a double-L: "additionally".

Member

The addition to the documentation looks good.
Make sure you remove the trailing whitespace too though.

The only thing that's missing now is a mention of the new feature in the docs/releases/1.7.txt file.

Member

Once you've made all these changes, can you also squash all the commits together into one (also make sure you follow the commit messages guidelines outlined on https://docs.djangoproject.com/en/1.6/internals/contributing/committing-code/#committing-guidelines)?

I'm available on the #django-dev channel on Freenode if you need some guidance.

Thanks.

Sjoerd Langkemper Fixed #21421 -- Added level_tag attribute on messages.
Exposing the level name (e.g. "info") makes it possible to prepend
something to the class name. For example, Twitter Bootstrap has
an alert-info class. This class can now be added to the message
using `class="alert-{{ message.level_tag }}".
Because the level_tag was on the end of the `tags` property, it
could not be used in this fashion when extra_tags were given.
bfae42d
Member

Merged in d871276.

Thanks!

@bmispelon bmispelon closed this Nov 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment