# HG changeset patch # User dellsystem # Date 1348968677 14400 # Node ID b8e0bdc37e324bf76ea71767c149855984c5832f # Parent 4d358e1e301473229ebdd937f0887b193c88cc71 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. diff -r 4d358e1e3014 -r b8e0bdc37e32 apps/snippet/models.py --- 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) diff -r 4d358e1e3014 -r b8e0bdc37e32 apps/snippet/views.py --- 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, diff -r 4d358e1e3014 -r b8e0bdc37e32 templates/code.djhtml --- 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 @@ {{ snippet.get_title }} by - {% if snippet.author %} {{ snippet.author }} - {% else %} - anonymous - {% endif %}
{{ snippet.published }} diff -r 4d358e1e3014 -r b8e0bdc37e32 templates/snippet/explore.html --- 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 @@ {{ snippet.get_lexer_display }} N/A - {% if snippet.author %} + {{ snippet.author }} - {% else %} - {% trans "anonymous" %} - {% endif %} diff -r 4d358e1e3014 -r b8e0bdc37e32 templates/snippet/snippet_new.djhtml --- 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 %}

{% trans "Paste a new snippet" %}

-

{% 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 %}

+

{% 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 %}

{% include "snippet/snippet_form.djhtml" %}