From 112cb677e93aa78c6848139597f8eb4955c482b9 Mon Sep 17 00:00:00 2001 From: Nick Sergeant Date: Thu, 24 May 2012 23:55:30 -0400 Subject: [PATCH] Blogs --- blogs/__init__.py | 0 blogs/middleware.py | 8 ++++++++ blogs/models.py | 3 +++ blogs/templates/blogs/list.html | 5 +++++ blogs/tests.py | 16 ++++++++++++++++ blogs/views.py | 1 + settings.py | 6 +++--- snipts/views.py | 21 +++++++++++++++++++++ 8 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 blogs/__init__.py create mode 100644 blogs/middleware.py create mode 100644 blogs/models.py create mode 100644 blogs/templates/blogs/list.html create mode 100644 blogs/tests.py create mode 100644 blogs/views.py diff --git a/blogs/__init__.py b/blogs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blogs/middleware.py b/blogs/middleware.py new file mode 100644 index 0000000..a7068bb --- /dev/null +++ b/blogs/middleware.py @@ -0,0 +1,8 @@ + +class SubdomainMiddleware: + def process_request(self, request): + request.subdomain = None + host = request.META.get('HTTP_HOST', '') + host_s = host.replace('www.', '').split('.') + if len(host_s) > 2: + request.subdomain = ''.join(host_s[:-2]) diff --git a/blogs/models.py b/blogs/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/blogs/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/blogs/templates/blogs/list.html b/blogs/templates/blogs/list.html new file mode 100644 index 0000000..35d23f7 --- /dev/null +++ b/blogs/templates/blogs/list.html @@ -0,0 +1,5 @@ +{{ user }} has some sniptz:

+{% for snipt in snipts %} + {{ snipt.title }}
+ {{ snipt.code }}

+{% endfor %} diff --git a/blogs/tests.py b/blogs/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/blogs/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/blogs/views.py b/blogs/views.py new file mode 100644 index 0000000..60f00ef --- /dev/null +++ b/blogs/views.py @@ -0,0 +1 @@ +# Create your views here. diff --git a/settings.py b/settings.py index d3415cd..8f50651 100644 --- a/settings.py +++ b/settings.py @@ -117,6 +117,7 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'pagination.middleware.PaginationMiddleware', + 'blogs.middleware.SubdomainMiddleware', ) ROOT_URLCONF = 'snipt.urls' @@ -151,6 +152,7 @@ INSTALLED_APPS = ( 'taggit', 'tastypie', + 'blogs', 'snipts', 'utils', ) @@ -199,14 +201,12 @@ LOGIN_URL = '/login/' LOGOUT_URL = '/logout/' ACCOUNT_ACTIVATION_DAYS = 0 -# Cookies -SESSION_COOKIE_DOMAIN = '.snipt.net' - # HTTPS if not DEBUG: USE_HTTPS = True SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True + SESSION_COOKIE_DOMAIN = '.snipt.net' # Messages MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage' diff --git a/snipts/views.py b/snipts/views.py index 5922a38..190b807 100644 --- a/snipts/views.py +++ b/snipts/views.py @@ -18,6 +18,10 @@ from haystack.query import EmptySearchQuerySet, SearchQuerySet RESULTS_PER_PAGE = getattr(settings, 'HAYSTACK_SEARCH_RESULTS_PER_PAGE', 20) def home(request): + + if request.subdomain: + return list_blog(request, request.subdomain) + if request.user.is_authenticated(): return HttpResponseRedirect('/%s/' % request.user.username) else: @@ -104,6 +108,23 @@ def list_user(request, username_or_custom_slug, tag_slug=None): return context +@render_to('blogs/list.html') +def list_blog(request, subdomain): + + user = get_object_or_404(User, username=subdomain) + snipts = Snipt.objects.filter(user=user, blog_post=True, public=True).order_by('-created') + + context = { + 'snipts': snipts, + 'user': user, + } + + if 'rss' in request.GET: + context['snipts'] = context['snipts'][:20] + return rss(request, context) + + return context + @render_to('snipts/detail.html') def detail(request, username, snipt_slug):