changeset 139:b8e0bdc37e32

Hide snippets created by anonymous users Created a custom manager for Snippet, with a public() method for easily retrieving all the snippets that were created by registered users. This change makes the code a bit neater, as it's no longer to necessary to have to do the {% if snippet.author %} check every time.
author dellsystem <ilostwaldo@gmail.com>
date Sat, 29 Sep 2012 21:31:17 -0400
parents 4d358e1e3014
children 3c4dc3b15f3b
files apps/snippet/models.py apps/snippet/views.py templates/code.djhtml templates/snippet/explore.html templates/snippet/snippet_new.djhtml views.py
diffstat 6 files changed, 18 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/apps/snippet/models.py	Sat Sep 29 18:03:35 2012 -0400
+++ b/apps/snippet/models.py	Sat Sep 29 21:31:17 2012 -0400
@@ -14,7 +14,17 @@
 def generate_secret_id(length=4):
     return ''.join([random.choice(t) for i in range(length)])
 
+class SnippetManager(models.Manager):
+    def public(self):
+        """
+        Returns all the snippets that were created by registered users
+        and thus can be publicly listed.
+        """
+        return self.filter(author__isnull=False)
+
+
 class Snippet(models.Model):
+    objects = SnippetManager()
     secret_id = models.CharField(_(u'Secret ID'), max_length=4, blank=True)
     title = models.CharField(_(u'Title'), max_length=120, blank=True)
     author = models.ForeignKey(User, max_length=30, blank=True, null=True)
--- a/apps/snippet/views.py	Sat Sep 29 18:03:35 2012 -0400
+++ b/apps/snippet/views.py	Sat Sep 29 21:31:17 2012 -0400
@@ -22,7 +22,7 @@
 
 def snippet_explore(request):
     context = {
-        'recent_snippets': Snippet.objects.all()[:20]
+        'recent_snippets': Snippet.objects.public()[:20]
     }
 
     return render(request, 'snippet/explore.html', context)
@@ -46,7 +46,7 @@
     else:
         snippet_form = SnippetForm(request=request)
 
-    recent = Snippet.objects.all()[:10]
+    recent = Snippet.objects.public()[:10]
 
     context = {
         'snippet_form': snippet_form,
--- a/templates/code.djhtml	Sat Sep 29 18:03:35 2012 -0400
+++ b/templates/code.djhtml	Sat Sep 29 21:31:17 2012 -0400
@@ -24,13 +24,9 @@
                     <strong>{{ snippet.get_title }}</strong>
                 </a>
                 by
-                {% if snippet.author %}
                 <a href="{{ snippet.author.get_absolute_url }}">
                     {{ snippet.author }}
                 </a>
-                {% else %}
-                anonymous
-                {% endif %}
                 <br />
                 {{ snippet.published }}
             </li>
--- a/templates/snippet/explore.html	Sat Sep 29 18:03:35 2012 -0400
+++ b/templates/snippet/explore.html	Sat Sep 29 21:31:17 2012 -0400
@@ -27,13 +27,10 @@
             </a></td>
             <td>{{ snippet.get_lexer_display }}
             <td>N/A</td>
-            <td>{% if snippet.author %}
+            <td>
                 <a href="{{ snippet.author.get_absolute_url }}">
                     {{ snippet.author }}
                 </a>
-                {% else %}
-                {% trans "anonymous" %}
-                {% endif %}
             </td>
         </tr>
     </tbody>
--- a/templates/snippet/snippet_new.djhtml	Sat Sep 29 18:03:35 2012 -0400
+++ b/templates/snippet/snippet_new.djhtml	Sat Sep 29 21:31:17 2012 -0400
@@ -6,7 +6,7 @@
 {% block content %}
     <div id="non-sidebar">
         <h1>{% trans "Paste a new snippet" %}</h1>
-        <p class="hint">{% trans "Snippets provide a way to quickly share pieces of code, complete with line-numbering and syntax-highlighting." %} {% if not user.is_authenticated %}{% trans "Although registration is not required, only registered users can delete their own snippets after ending a session or have them linked to their profile." %}{% endif %}</p>
+        <p class="hint">{% trans "Snippets provide a way to quickly share pieces of code, complete with line-numbering and syntax-highlighting." %} {% if not user.is_authenticated %}{% trans "Although registration is not required, only registered users can delete their own snippets after ending a session or have them linked to their profile. Additionally, snippets made by guests will not be publicly listed." %}{% endif %}</p>
         {% include "snippet/snippet_form.djhtml" %}
     </div><div id="sidebar">
         <h2>Recent snippets</h2>
@@ -18,13 +18,9 @@
             </a>
             <br />
             by
-            {% if snippet.author %}
-                <a href="{{ snippet.author.get_absolute_url }}">
-                    {{ snippet.author }}
-                </a>
-            {% else %}
-            anonymous
-            {% endif %}
+            <a href="{{ snippet.author.get_absolute_url }}">
+                {{ snippet.author }}
+            </a>
         </p>
         {% endfor %}
         <p class="right-float"><a href="{% url snippet_explore %}">
--- a/views.py	Sat Sep 29 18:03:35 2012 -0400
+++ b/views.py	Sat Sep 29 21:31:17 2012 -0400
@@ -9,7 +9,7 @@
 
 def code(request):
     context = {
-        'snippets': Snippet.objects.all()[:5],
+        'snippets': Snippet.objects.public()[:5],
         'modules': None, # temp
         'forge': None, # temp
     }