Skip to content

Commit

Permalink
refactoring. user generic views
Browse files Browse the repository at this point in the history
  • Loading branch information
d1ffuz0r committed Oct 22, 2011
1 parent 62e8296 commit 7d1cd4c
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 61 deletions.
1 change: 1 addition & 0 deletions homesite/models.py
Expand Up @@ -112,6 +112,7 @@ class Blog(models.Model):
class Meta: class Meta:
verbose_name = u'Post' verbose_name = u'Post'
verbose_name_plural = u'Blog' verbose_name_plural = u'Blog'
ordering = ["-id"]


def get_absolute_url(self): def get_absolute_url(self):
return "/blog/post/%i/" % self.id return "/blog/post/%i/" % self.id
Expand Down
10 changes: 5 additions & 5 deletions homesite/tests.py
@@ -1,6 +1,6 @@
from django.test import TestCase from django.test import TestCase
from django.test.client import Client from django.test.client import Client
from homesite.models import Blog, QuickMessages, About, Portfolio, Services, Settings from homesite.models import Blog, QuickMessages, About, Settings


class HomesiteTest(TestCase): class HomesiteTest(TestCase):
def setUp(self): def setUp(self):
Expand Down Expand Up @@ -33,13 +33,13 @@ def setUp(self):
#main tests #main tests
def testHome(self): def testHome(self):
request = self.client.get('/') request = self.client.get('/')
self.assertEqual(request.context['page'], 'home') self.assertContains(request, text="portfolio")


def testAbout(self): def testAbout(self):
self.assertEqual(self.about.description, 'test') self.assertEqual(self.about.description, 'test')


request = self.client.get('/about/') request = self.client.get('/about/')
self.assertEqual(request.context['page'], 'about') self.assertContains(request, text="about")
self.assertDictEqual( self.assertDictEqual(
request.context['latest_posts'].values()[0], request.context['latest_posts'].values()[0],
{'text': u'test', 'id': 1, 'title': u'test'}, {'text': u'test', 'id': 1, 'title': u'test'},
Expand All @@ -59,11 +59,11 @@ def testBlog(self):


def testPortfolio(self): def testPortfolio(self):
request = self.client.get('/portfolio/') request = self.client.get('/portfolio/')
self.assertEqual(request.context['page'], 'portfolio') self.assertContains(request, text="portfolio")


def testServices(self): def testServices(self):
request = self.client.get('/services/') request = self.client.get('/services/')
self.assertEqual(request.context['page'], 'services') self.assertContains(request, text="services")


# other tests # other tests
def testMessages(self): def testMessages(self):
Expand Down
29 changes: 2 additions & 27 deletions homesite/views.py
@@ -1,40 +1,15 @@
from django.core.paginator import Paginator, InvalidPage, EmptyPage from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from models import Services, Portfolio, Settings, About, Blog from models import Blog
from homesite.forms import QuickContactForm from homesite.forms import QuickContactForm
from django.http import HttpResponse from django.http import HttpResponse
from decorators import render_to from decorators import render_to


_ = lambda x: x _ = lambda x: x


@render_to('home.html')
def home(request):
settings = Settings.objects.get()
return {'page': 'home', 'settings': settings}

@render_to('about.html')
def about(request):
about = About.objects.get()
return {'page': 'about', 'about': about}

@render_to('portfolio.html')
def portfolio(request):
works = Portfolio.objects.all()
return {'page': 'portfolio', 'works': works}

@render_to('services.html')
def services(request):
services = Services.objects.all()
return {'page': 'services', 'services': services}

@render_to('contacts.html')
def contacts(request):
settings = Settings.objects.get()
return {'page': 'contacts', 'settings': settings}

@render_to('blog.html') @render_to('blog.html')
def blog_list(request): def blog_list(request):
posts_list = Blog.objects.order_by('-id').all() posts_list = Blog.objects.all()
paginator = Paginator(posts_list,5) paginator = Paginator(posts_list,5)
try: try:
page = int(request.GET.get('page', '1')) page = int(request.GET.get('page', '1'))
Expand Down
3 changes: 1 addition & 2 deletions public/templates/about.html
@@ -1,5 +1,4 @@
{% extends 'base.html' %} {% extends 'base.html' %}{% block title %}About me{% endblock %}{% block page_title %}About me{% endblock %}
{% block page_title %}About me{% endblock %}
{% block content %}<div id="about"> {% block content %}<div id="about">
<div class="about-left"> <div class="about-left">
{{ about.description }} {{ about.description }}
Expand Down
14 changes: 7 additions & 7 deletions public/templates/base.html
Expand Up @@ -2,25 +2,25 @@
"http://www.w3.org/TR/html4/loose.dtd"> "http://www.w3.org/TR/html4/loose.dtd">
<html> <html>
<head> <head>
<title>{{ page|capfirst }}</title> <title>{% block title %}{% endblock %}</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="author" content="d1ffuz0r, d1fffuz0r@gmail.com" /> <meta name="author" content="d1ffuz0r, d1fffuz0r@gmail.com" />
<meta name="keywords" content="d1ffuz0r, blog, contacts, portfolio, services, python, django, tornado, jQuery" /> <meta name="keywords" content="d1ffuz0r, blog, contacts, portfolio, services, python, django, tornado, jQuery" />
<meta name="description" content="d1ffuz0r personal site, blog, portfolio" /> <meta name="description" content="d1ffuz0r personal site, blog, portfolio" />
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" type="text/css" media="screen" title="no title" charset="utf-8"> <link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script type="text/javascript" src="{{ STATIC_URL }}js/jquery-1.6.2.min.js" charset="utf-8"></script> <script type="text/javascript" src="{{ STATIC_URL }}js/jquery-1.6.2.min.js" charset="utf-8"></script>
<script type="text/javascript" src="{{ STATIC_URL }}js/site.js" charset="utf-8"></script> <script type="text/javascript" src="{{ STATIC_URL }}js/site.js" charset="utf-8"></script>
{% block head %}{% endblock %}</head> </head>
<body> <body>
<div id="head"> <div id="head">
<div id="menu"> <div id="menu">
<ul> <ul>
<li><a href="/"><button class="{% ifequal page 'home' %}active{% else %}button{% endifequal %}">{{ _('home')|capfirst }}</button></a></li> <li><a href="/"><button class="{% if home %}active{% else %}button{% endif %}">{{ _('home')|capfirst }}</button></a></li>
<li><a href="/about/"><button class="{% ifequal page 'about' %} active{% else %}button{% endifequal %}">{{ _('about me')|capfirst }}</button></a></li> <li><a href="/about/"><button class="{% if about %} active{% else %}button{% endif %}">{{ _('about me')|capfirst }}</button></a></li>
<li><a href="/blog/"><button class="{% ifequal page 'blog' %} active{% else %}button{% endifequal %}">{{ _('blog')|capfirst }}</button></a></li> <li><a href="/blog/"><button class="{% ifequal page 'blog' %} active{% else %}button{% endifequal %}">{{ _('blog')|capfirst }}</button></a></li>
<li><a href="/portfolio/"><button class="{% ifequal page 'portfolio' %} active{% else %}button{% endifequal %}">{{ _('portfolio')|capfirst }}</button></a></li> <li><a href="/portfolio/"><button class="{% if portfolio %} active{% else %}button{% endif %}">{{ _('portfolio')|capfirst }}</button></a></li>
<li><a href="/services/"><button class="{% ifequal page 'services' %} active{% else %}button{% endifequal %}">{{ _('services')|capfirst }}</button></a></li> <li><a href="/services/"><button class="{% if services %} active{% else %}button{% endif %}">{{ _('services')|capfirst }}</button></a></li>
<li><a href="/contacts/"><button class="{% ifequal page 'contacts' %} active{% else %}button{% endifequal %}">{{ _('contacts')|capfirst }}</button></a></li> <li><a href="/contacts/"><button class="{% if contacts %} active{% else %}button{% endif %}">{{ _('contacts')|capfirst }}</button></a></li>
</ul> </ul>
</div> </div>
</div> </div>
Expand Down
2 changes: 1 addition & 1 deletion public/templates/blog.html
@@ -1,4 +1,4 @@
{% extends 'base.html' %}{% block page_title %}Blog{% endblock %} {% extends 'base.html' %}{% block title %}Blog{% endblock %}{% block page_title %}Blog{% endblock %}
{% block content %}<div id="blog"> {% block content %}<div id="blog">
{% if posts %}{% for post in posts.object_list %}<ul class="posts"> {% if posts %}{% for post in posts.object_list %}<ul class="posts">
<li class="blog-post"> <li class="blog-post">
Expand Down
8 changes: 4 additions & 4 deletions public/templates/contacts.html
@@ -1,15 +1,15 @@
{% extends 'base.html' %} {% extends 'base.html' %}{% block title %}Contacts{% endblock %}
{% block page_title %}Contacts{% endblock %}{% block content %}<div id="contacts"> {% block page_title %}Contacts{% endblock %}{% block content %}<div id="contacts">
<div class="contact jabber"> <div class="contact jabber">
<span>Jabber</span> <span>Jabber</span>
<div class="desc">{{ settings.jabber }}</div> <div class="desc">{{ contacts.jabber }}</div>
</div> </div>
<div class="contact icq"> <div class="contact icq">
<span>ICQ</span> <span>ICQ</span>
<div class="desc">{{ settings.icq }}</div> <div class="desc">{{ contacts.icq }}</div>
</div> </div>
<div class="contact email"> <div class="contact email">
<span>E-mail</span> <span>E-mail</span>
<div class="desc">{{ settings.email }}</div> <div class="desc">{{ contacts.email }}</div>
</div> </div>
</div>{% endblock %} </div>{% endblock %}
2 changes: 1 addition & 1 deletion public/templates/home.html
@@ -1,4 +1,4 @@
{% extends 'base.html' %}{% block content %}<div id="home"> {% extends 'base.html' %}{% block title %}Home{% endblock %}{% block content %}<div id="home">
<div id="home-links"> <div id="home-links">
<ul> <ul>
<li class="about"><a href="/about/">{{ _('about me') }}</a></li> <li class="about"><a href="/about/">{{ _('about me') }}</a></li>
Expand Down
2 changes: 1 addition & 1 deletion public/templates/portfolio.html
@@ -1,4 +1,4 @@
{% extends 'base.html' %} {% extends 'base.html' %}{% block title %}Portfolio{% endblock %}
{% block page_title %}Portfolio{% endblock %}{% block content %}<div id="portfolio"> {% block page_title %}Portfolio{% endblock %}{% block content %}<div id="portfolio">
{% if works %}{% for work in works %}<ul> {% if works %}{% for work in works %}<ul>
<li class="portfolio-item"> <li class="portfolio-item">
Expand Down
2 changes: 1 addition & 1 deletion public/templates/post.html
@@ -1,4 +1,4 @@
{% extends 'base.html' %} {% extends 'base.html' %}{% block title %}Blog - {{ post.title|capfirst }}{% endblock %}
{% block page_title %}{{ post.title|capfirst }}{% endblock %}{% block content %}<div id="post"> {% block page_title %}{{ post.title|capfirst }}{% endblock %}{% block content %}<div id="post">
{{ post.text|safe|linebreaksbr }} {{ post.text|safe|linebreaksbr }}
</div>{% endblock %} </div>{% endblock %}
2 changes: 1 addition & 1 deletion public/templates/services.html
@@ -1,4 +1,4 @@
{% extends 'base.html' %} {% extends 'base.html' %}{% block title %}Services{% endblock %}
{% block page_title %}Services{% endblock %}{% block content %}<div id="services"> {% block page_title %}Services{% endblock %}{% block content %}<div id="services">
<div class="services-items"> <div class="services-items">
<ul>{% if services %}{% for service in services %} <ul>{% if services %}{% for service in services %}
Expand Down
34 changes: 23 additions & 11 deletions urls.py
@@ -1,25 +1,37 @@
from homesite.models import About, Portfolio, Services, Settings
from django.conf.urls.defaults import patterns, url, include from django.conf.urls.defaults import patterns, url, include
from django.contrib import admin from django.views.generic.list import ListView
from homesite.feed import BlogRss from homesite.feed import BlogRss
from django.contrib import admin
import settings import settings


admin.autodiscover() admin.autodiscover()


urlpatterns = patterns('', urlpatterns = patterns('',
# Examples: url(r'^$', ListView.as_view(queryset=Settings.objects.all()[0],
url(r'^$', 'homesite.views.home'), context_object_name="home",
url(r'^about/', 'homesite.views.about'), template_name="home.html")),
url(r'^portfolio/', 'homesite.views.portfolio'),
url(r'^services/', 'homesite.views.services'), url(r'^about/', ListView.as_view(queryset=About.objects.all()[0],
url(r'^contacts/', 'homesite.views.contacts'), context_object_name="about",
template_name="about.html")),

url(r'^portfolio/', ListView.as_view(queryset=Portfolio.objects.all(),
context_object_name="portfolio",
template_name="portfolio.html")),

url(r'^services/', ListView.as_view(queryset=Services.objects.all(),
context_object_name="services",
template_name="services.html")),

url(r'^contacts/', ListView.as_view(queryset=Settings.objects.all()[0],
context_object_name="contacts",
template_name="contacts.html")),
url(r'^blog/rss/$', BlogRss()), url(r'^blog/rss/$', BlogRss()),
url(r'^blog/post/(\d+)', 'homesite.views.blog_post'), url(r'^blog/post/(\d+)', 'homesite.views.blog_post'),
url(r'^blog/', 'homesite.views.blog_list'), url(r'^blog/', 'homesite.views.blog_list'),
url(r'^ajax/quick-form', 'homesite.views.quick_form'), url(r'^ajax/quick-form', 'homesite.views.quick_form'),
url(r'^comments/', include('django.contrib.comments.urls')),
url(r'^admin/jsi18n/', 'django.views.i18n.javascript_catalog'),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
url(r'^admin/', include(admin.site.urls)), url(r'^admin/', include(admin.site.urls)),
) )

0 comments on commit 7d1cd4c

Please sign in to comment.