mirror of
https://github.com/kennethreitz-archive/django-wordpress.git
synced 2026-06-05 15:40:19 +00:00
clean up wordpress views and models
This commit is contained in:
+6
-5
@@ -1,6 +1,6 @@
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import connection, models
|
||||
from django.db import connections, models
|
||||
from django.db.models import signals
|
||||
from django.http import HttpResponseRedirect
|
||||
import re
|
||||
@@ -154,11 +154,12 @@ class PostManager(models.Manager):
|
||||
def published(self, post_type='post'):
|
||||
return self._by_status('publish', post_type)
|
||||
|
||||
def term(self, term, taxonomy='post_tag'):
|
||||
def term(self, term, taxonomy='post_tag'):
|
||||
term = term.replace('-', ' ')
|
||||
tx = Taxonomy.objects.get(name=taxonomy, term__name=term)
|
||||
table = '%s_term_relationships' % TABLE_PREFIX
|
||||
sql = """SELECT object_id FROM """ + table + """ WHERE term_taxonomy_id = %s"""
|
||||
cursor = connection.cursor()
|
||||
cursor = connections['wordpress'].cursor()
|
||||
cursor.execute(sql, [tx.pk,])
|
||||
pids = [row[0] for row in cursor.fetchall()]
|
||||
return Post.objects.published().filter(pk__in=pids)
|
||||
@@ -223,7 +224,6 @@ class Post(WordPressModel):
|
||||
month = self.post_date.month
|
||||
day = self.post_date.day
|
||||
slug = self.slug
|
||||
print reverse('wp_object_detail', args=(year, month, day, slug))
|
||||
return reverse('wp_object_detail', args=(year, month, day, slug))
|
||||
|
||||
"""
|
||||
@@ -244,6 +244,7 @@ class Post(WordPressModel):
|
||||
"""
|
||||
|
||||
def tags(self):
|
||||
print self.get_absolute_url()
|
||||
if not self.tag_cache:
|
||||
taxonomy = "post_tag"
|
||||
self.tag_cache = self._get_terms(taxonomy)
|
||||
@@ -252,7 +253,7 @@ class Post(WordPressModel):
|
||||
def _get_terms(self, taxonomy):
|
||||
table = '%s_term_relationships' % TABLE_PREFIX
|
||||
sql = """SELECT term_taxonomy_id FROM """ + table + """ WHERE object_id = %s ORDER BY term_order"""
|
||||
cursor = connection.cursor()
|
||||
cursor = connections['wordpress'].cursor()
|
||||
cursor.execute(sql, [self.id,])
|
||||
ttids = [row[0] for row in cursor.fetchall()]
|
||||
return Term.objects.filter(taxonomies__name=taxonomy, taxonomies__pk__in=ttids)
|
||||
|
||||
@@ -1 +1 @@
|
||||
{{ latest }}
|
||||
{{ post_list }}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
from django.conf.urls.defaults import *
|
||||
|
||||
urlpatterns = patterns('wordpress.views',
|
||||
url(r'^taxonomy/(?P<taxonomy>term|category)/(?P<term>[\w-]+)/$', 'taxonomy', name='wp_taxonomy'),
|
||||
url(r'^taxonomy/(?P<taxonomy>term|category)/(?P<term>[\w\-\.]+)/$', 'taxonomy', name='wp_taxonomy'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', name='wp_object_detail'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$', 'archive_day', name='wp_archive_day'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/$', 'archive_month', name='wp_archive_month'),
|
||||
|
||||
+14
-6
@@ -1,8 +1,11 @@
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render_to_response
|
||||
from django.views.generic import date_based
|
||||
from django.views.generic import list_detail, date_based
|
||||
from wordpress.models import Post
|
||||
|
||||
PER_PAGE = getattr(settings, 'WP_PER_PAGE', 10)
|
||||
|
||||
TAXONOMIES = {
|
||||
'term': 'post_tag',
|
||||
'category': 'category',
|
||||
@@ -12,7 +15,7 @@ TAXONOMIES = {
|
||||
def object_detail(request, year, month, day, slug):
|
||||
return date_based.object_detail(request, queryset=Post.objects.published(),
|
||||
date_field='post_date', year=year, month=month, month_format="%m",
|
||||
day=day, slug=slug, slug_field='slug', template_object_name='post')
|
||||
day=day, slug=slug, template_object_name='post', allow_future=True)
|
||||
|
||||
def archive_day(request, year, month, day):
|
||||
return date_based.archive_day(request, queryset=Post.objects.published(),
|
||||
@@ -33,11 +36,16 @@ def archive_index(request):
|
||||
if p:
|
||||
post = Post.objects.get(pk=p)
|
||||
return HttpResponseRedirect(post.get_absolute_url())
|
||||
return date_based.archive_index(request,
|
||||
queryset=Post.objects.published(), date_field='post_date')
|
||||
posts = Post.objects.published().select_related()
|
||||
return list_detail.object_list(request, queryset=posts,
|
||||
paginate_by=10, template_name='wordpress/post_archive.html',
|
||||
template_object_name='post', allow_empty=True)
|
||||
|
||||
def taxonomy(request, taxonomy, term):
|
||||
taxonomy = TAXONOMIES.get(taxonomy, None)
|
||||
if taxonomy:
|
||||
posts = Post.objects.term(term, taxonomy=taxonomy)
|
||||
return render_to_response('wordpress/post_term.html', {'post_list': posts})
|
||||
posts = Post.objects.term(term, taxonomy=taxonomy).select_related()
|
||||
return list_detail.object_list(request, queryset=posts,
|
||||
paginate_by=10, template_name='wordpress/post_term.html',
|
||||
template_object_name='post', allow_empty=True,
|
||||
extra_context={'term': term})
|
||||
Reference in New Issue
Block a user