def render(self, context): posts = None t = template.loader.get_template(self.template) if isinstance(self.category, basestring): try: self.category = get_category_for_path(self.category, queryset=Category.objects.all()) posts = get_post_model().published.filter(categories=self.category) except: pass else: posts = get_post_model().published.filter(categories=self.category) if posts: context['posts'] = posts return t.render(context)
class PostView(YearMixin, MonthMixin, DayMixin, DateMixin, CategoryContentDetailView): model = get_post_model() date_field = 'date_modified' allow_future = True def get_object(self, queryset=None): """ Get the object this request displays. """ year = self.get_year() month = self.get_month() day = self.get_day() date = _date_from_string(year, self.get_year_format(), month, self.get_month_format(), day, self.get_day_format()) # Use a custom queryset if provided qs = self.get_queryset() if queryset is None else queryset if not self.get_allow_future() and date > datetime.date.today(): raise Http404(_( "Future %(verbose_name_plural)s not available because " "%(class_name)s.allow_future is False.") % { 'verbose_name_plural': qs.model._meta.verbose_name_plural, 'class_name': self.__class__.__name__, }, ) # Filter down a queryset from self.queryset using the date from the # URL. This'll get passed as the queryset to DetailView.get_object, # which'll handle the 404 lookup_kwargs = self._make_single_date_lookup(date) qs = qs.filter(**lookup_kwargs) return super(PostView, self).get_object(queryset=qs)
def render(self, context): posts = None t = template.loader.get_template(self.template) if isinstance(self.category, basestring): try: self.category = get_category_for_path( self.category, queryset=Category.objects.all()) posts = get_post_model().published.filter( categories=self.category) except: pass else: posts = get_post_model().published.filter(categories=self.category) if posts: context['posts'] = posts return t.render(context)
def clean(self): cleaned_data = super(PostForm, self).clean() """The slug + the date_modified must be unique together""" if 'date_modified' in cleaned_data and self.instance.pk is None: date_modified = cleaned_data['date_modified'] try: get_post_model().objects.get( slug=self.cleaned_data[('slug_' + get_language().replace('-', '_')) if settings.USE_TRANSLATION else 'slug'], date_modified__year=date_modified.year, date_modified__month=date_modified.month, date_modified__day=date_modified.day) raise forms.ValidationError( 'Please enter a different slug. The one you'\ 'entered is already being used for {0}'.format( date_modified.strftime("%Y-%b-%d"))) except get_post_model().DoesNotExist: pass return cleaned_data
def render(self, context): from datetime import timedelta, date enddate = date.today() startdate = enddate - timedelta(days=1) query = get_post_model().with_counter.filter(date_modified__range=[startdate, enddate]) if not query: startdate = enddate - timedelta(days=2) query = get_post_model().with_counter.filter(date_modified__range=[startdate, enddate]) if self.category is not None: self.category = self.category.resolve(context) query = query.filter(categories=self.category) if self.limit == -1: context[self.var_name] = query.all() else: context[self.var_name] = query.all()[:self.limit] return ''
def clean(self): cleaned_data = super(PostForm, self).clean() """The slug + the date_modified must be unique together""" if 'date_modified' in cleaned_data and self.instance.pk is None: date_modified = cleaned_data['date_modified'] try: get_post_model().objects.get( slug=self.cleaned_data[( 'slug_' + get_language().replace('-', '_') ) if settings.USE_TRANSLATION else 'slug'], date_modified__year=date_modified.year, date_modified__month=date_modified.month, date_modified__day=date_modified.day) raise forms.ValidationError( 'Please enter a different slug. The one you'\ 'entered is already being used for {0}'.format( date_modified.strftime("%Y-%b-%d"))) except get_post_model().DoesNotExist: pass return cleaned_data
def render(self, context): from datetime import timedelta, date enddate = date.today() startdate = enddate - timedelta(days=1) query = get_post_model().with_counter.filter( date_modified__range=[startdate, enddate]) if not query: startdate = enddate - timedelta(days=2) query = get_post_model().with_counter.filter( date_modified__range=[startdate, enddate]) if self.category is not None: self.category = self.category.resolve(context) query = query.filter(categories=self.category) if self.limit == -1: context[self.var_name] = query.all() else: context[self.var_name] = query.all()[:self.limit] return ''
def render(self, context): try: query = get_post_model().published.order_by("-date_modified") except: query = get_post_model().published.order_by("-date_created") if self.category_list is not None and self.category_list != []: if isinstance(self.category_list[0], template.Variable): self.category_list = [self.category_list[0].resolve(context)] query = query.filter(categories__in=self.category_list) if self.limit == -1: context[self.var_name] = query.all() else: context[self.var_name] = query.all()[:self.limit] if self.limit == 1: context[self.var_name] = context[self.var_name][0] return ''
def render(self, context): if self.category is None: context[self.var_name] = None return '' try: query = get_post_model().published.filter(categories__in=[self.category]) if self.random is True: query = query.order_by('?') else: query = query.order_by('-date_created') if self.limit == -1: context[self.var_name] = query.all() else: context[self.var_name] = query.all()[:self.limit] if self.limit == 1: context[self.var_name] = context[self.var_name][0] except: context[self.var_name] = None return ''
def render(self, context): if self.category is None: context[self.var_name] = None return '' try: query = get_post_model().published.filter( categories__in=[self.category]) if self.random is True: query = query.order_by('?') else: query = query.order_by('-date_created') if self.limit == -1: context[self.var_name] = query.all() else: context[self.var_name] = query.all()[:self.limit] if self.limit == 1: context[self.var_name] = context[self.var_name][0] except: context[self.var_name] = None return ''
from django.conf.urls import patterns, url from django.views.generic import ( YearArchiveView, MonthArchiveView, WeekArchiveView, DayArchiveView, TodayArchiveView ) from content_post import get_post_model from views import * from rest_framework.routers import DefaultRouter from api import PostViewSet # Create a router and register our viewsets with it. router = DefaultRouter() router.register(r'post', PostViewSet) info_dict = { 'queryset': get_post_model().published.all(), 'date_field': 'date_', 'allow_empty': True } urlpatterns = patterns('', # post detail url( r'^(?P<path>.+)/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', PostView.as_view(), name='category_post_detail' ), # news archive index url( r'^category/(?P<path>.+)/$',
class Meta: model = get_post_model() fields = "__all__"
from django.conf.urls import patterns, url from django.views.generic import (YearArchiveView, MonthArchiveView, WeekArchiveView, DayArchiveView, TodayArchiveView) from content_post import get_post_model from views import * from rest_framework.routers import DefaultRouter from api import PostViewSet # Create a router and register our viewsets with it. router = DefaultRouter() router.register(r'post', PostViewSet) info_dict = { 'queryset': get_post_model().published.all(), 'date_field': 'date_', 'allow_empty': True } urlpatterns = patterns( '', # post detail url(r'^(?P<path>.+)/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', PostView.as_view(), name='category_post_detail'), # news archive index url(r'^category/(?P<path>.+)/$', PostListView.as_view(), name='post_archive_index'),
from forms import PostForm class PostAdmin(CategoryContentAdmin): change_form_template = "admin/content_post/post/change_form.html" fieldsets = ( (None, { 'fields': ('title', 'slug'), }), (_('Content'), { 'fields': ('body',), 'classes': ('full-width',), }), ('Categories', { 'fields': ('categories',), }), (_('Post data'), { 'fields': ('tags', 'authors', 'non_staff_author', 'status', 'origin', 'allow_comments', 'allow_pings', 'is_sticky') }),) fieldsets = fieldsets + ((_('Advanced Options'), { 'fields': ('date_modified', 'site', ), 'classes': ('collapse',), }),) form = PostForm admin.site.register(get_post_model(), PostAdmin)
class PostListView(CategoryContentListView): model = get_post_model()
change_form_template = "admin/content_post/post/change_form.html" fieldsets = ( (None, { 'fields': ('title', 'slug'), }), (_('Content'), { 'fields': ('body', ), 'classes': ('full-width', ), }), ('Categories', { 'fields': ('categories', ), }), (_('Post data'), { 'fields': ('tags', 'authors', 'non_staff_author', 'status', 'origin', 'allow_comments', 'allow_pings', 'is_sticky') }), ) fieldsets = fieldsets + ((_('Advanced Options'), { 'fields': ( 'date_modified', 'site', ), 'classes': ('collapse', ), }), ) form = PostForm admin.site.register(get_post_model(), PostAdmin)