Whitespace-only validation of snippet fields #22

Merged
merged 2 commits into from Sep 19, 2012
View
@@ -8,7 +8,7 @@ class LanguageAdmin(admin.ModelAdmin):
class SnippetAdmin(admin.ModelAdmin):
- list_display = ('title', 'author', 'rating_score', 'pub_date')
+ list_display = ('id', 'title', 'author', 'rating_score', 'pub_date')
list_filter = ('language',)
date_hierarchy = 'pub_date'
search_fields = ('author__username', 'title', 'description', 'code',)
View
@@ -8,7 +8,25 @@
from registration.forms import RegistrationFormUniqueEmail
+def validate_non_whitespace_only_string(value):
+ """
+ Additionally to requiring a non-empty string, this validator also strips
+ the string to treat strings with only whitespaces in them as empty.
+ """
+ if not value or not value.strip():
+ raise forms.ValidationError(u'This field is required', code='required')
+
+
class SnippetForm(forms.ModelForm):
+ title = forms.CharField(
+ validators=[validate_non_whitespace_only_string])
+ description = forms.CharField(
+ validators=[validate_non_whitespace_only_string],
+ widget=forms.Textarea)
+ code = forms.CharField(
+ validators=[validate_non_whitespace_only_string],
+ widget=forms.Textarea)
+
class Meta:
model = Snippet
exclude = ('author', 'bookmark_count', 'rating_score',)
@@ -1,5 +1,5 @@
{% extends "base.html" %}
-
+{% load core_tags %}
{% block title %}All snippets{% if months %} last {{ months }} months{% endif %}{% endblock %}
{% block content_header %}All snippets{% if months %} last {{ months }} months{% endif %}{% endblock %}
@@ -15,18 +15,18 @@
<ul class="snippet_list">
{% for snippet in object_list %}
<li>
- <a href="{{ snippet.get_absolute_url }}">{{ snippet.title }}</a> by <a href="/users/{{ snippet.author.username }}/">{{ snippet.author.username }}</a>
+ <a href="{{ snippet.get_absolute_url }}">{% if not snippet.title|strip %}Untitled{% else %}{{ snippet.title }}{% endif %}</a> by <a href="/users/{{ snippet.author.username }}/">{{ snippet.author.username }}</a>
<p class="details">{{ snippet.pub_date|timesince }} ago, <span class="rating-{% if snippet.rating_score >= 0 %}positive{% else %}negative{% endif %}">{% if snippet.rating_score >= 0 %}+{% endif %}{{ snippet.rating_score }}</span></p>
</li>
{% endfor %}
</ul>
<p class="pagination">
{% if has_previous %}
- <a href="?page={{ previous }}{% if months %}&months={{ months }}{% endif %}">&lt; Previous {{ results_per_page }}</a>
+ <a href="?page={{ previous }}{% if months %}&amp;months={{ months }}{% endif %}">&lt; Previous {{ results_per_page }}</a>
{% endif %}
&nbsp;&nbsp;
{% if has_next %}
- <a href="?page={{ next }}{% if months %}&months={{ months }}{% endif %}">Next {{ results_per_page }} &gt;</a>
+ <a href="?page={{ next }}{% if months %}&amp;months={{ months }}{% endif %}">Next {{ results_per_page }} &gt;</a>
{% endif %}
</p>
{% else %}
@@ -19,18 +19,18 @@
<ul class="snippet_list">
{% for snippet in object_list %}
<li>
- <a href="{{ snippet.get_absolute_url }}">{{ snippet.title }}</a> by <a href="/users/{{ snippet.author.username }}/">{{ snippet.author.username }}</a>
+ <a href="{{ snippet.get_absolute_url }}">{% if not snippet.title|strip %}Untitled{% else %}{{ snippet.title }}{% endif %}</a> by <a href="/users/{{ snippet.author.username }}/">{{ snippet.author.username }}</a>
<p class="details">{{ snippet.pub_date|timesince }} ago, <span class="rating-{% if snippet.rating_score >= 0 %}positive{% else %}negative{% endif %}">{% if snippet.rating_score >= 0 %}+{% endif %}{{ snippet.rating_score }}</span></p>
</li>
{% endfor %}
</ul>
<p class="pagination">
{% if has_previous %}
- <a href="?page={{ previous }}{% if months %}&months={{ months }}{% endif %}">&lt; Previous {{ results_per_page }}</a>
+ <a href="?page={{ previous }}{% if months %}&amp;months={{ months }}{% endif %}">&lt; Previous {{ results_per_page }}</a>
{% endif %}
&nbsp;&nbsp;
{% if has_next %}
- <a href="?page={{ next }}{% if months %}&months={{ months }}{% endif %}">Next {{ results_per_page }} &gt;</a>
+ <a href="?page={{ next }}{% if months %}&amp;months={{ months }}{% endif %}">Next {{ results_per_page }} &gt;</a>
{% endif %}
</p>
{% else %}
@@ -40,3 +40,13 @@ def call_manager(model_or_obj, method):
manager = model_or_obj._default_manager
return getattr(manager, method)()
+
+
+@register.filter
+def strip(value):
+ """
+ Strips a string.
+ """
+ if value:
+ return value.strip()
+ return value