Commits

Anonymous committed b94f368 Merge

Merged in H3n0xek/django-diario (pull request #2)

Comments (0)

Files changed (4)

diario/urls/entries.py

 
 from django.conf.urls.defaults import *
 from diario.settings import DIARIO_NUM_LATEST
+from diario.views.entries import EntryList, EntryDetail, EntryArchiveDay, \
+                                EntryArchiveMonth, EntryArchiveYear
 
 info_dict = {
     'template_object_name': 'entry',
 
 entry_detail = url(
     regex  = '^(?P<year>\d{4})/(?P<month>[0-9]{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
-    view   = 'diario.views.entries.entry_detail',
-    kwargs = dict(info_dict, month_format='%m'),
+    view   = EntryDetail.as_view(),    
     name   = 'diario-entry'
 )
 
 archive_day = url(
     regex  = '^(?P<year>\d{4})/(?P<month>[0-9]{2})/(?P<day>\d{2})/$',
-    view   = 'diario.views.entries.entry_archive_day',
-    kwargs = dict(info_dict, month_format='%m'),
+    view   = EntryArchiveDay.as_view(),    
     name   = 'diario-archive-day'
 )
 
 archive_month = url(
     regex  = '^(?P<year>\d{4})/(?P<month>[0-9]{2})/$',
-    view   = 'diario.views.entries.entry_archive_month',
-    kwargs = dict(info_dict, month_format='%m'),
+    view   = EntryArchiveMonth.as_view(),    
     name   = 'diario-archive-month'
 )
 
 archive_year = url(
     regex  = '^(?P<year>\d{4})/$',
-    view   = 'diario.views.entries.entry_archive_year',
-    kwargs = info_dict,
+    view   = EntryArchiveYear.as_view(),    
     name   = 'diario-archive-year'
 )
 
 entry_list = url(
     regex  = '^$',
-    view   = 'diario.views.entries.entry_list',
-    kwargs = dict(info_dict, paginate_by=DIARIO_NUM_LATEST),
+    view   = EntryList.as_view(),    
     name   = 'diario-entry-list'
 )
 

diario/urls/entries_by_author.py

 
 from django.conf.urls.defaults import *
 from diario.settings import DIARIO_NUM_LATEST
-
-info_dict = {
-    'paginate_by': DIARIO_NUM_LATEST,
-    'template_name': 'diario/entry_list_by_author.html',
-    'template_object_name': 'entry',
-}
+from diario.views.entries_by_author import EntryListByAuthor
 
 entry_list_by_author = url(        # entries by author
     regex  = '^(?P<author>[^/]+)/$',
-    view   = 'diario.views.entries_by_author.entry_list_by_author',
-    kwargs = info_dict,
+    view   = EntryListByAuthor.as_view(),    
     name   = 'diario-author-entry-list',
 )
 

diario/views/entries.py

 #  Software Foundation. See the file README for copying conditions.
 #
 
-from django.views.generic import date_based, list_detail
 from diario.models import Entry
+from django.views.generic.list import ListView
+from django.views.generic.dates import DateDetailView, YearArchiveView, \
+                                        MonthArchiveView, DayArchiveView
+from diario.settings import DIARIO_NUM_LATEST
 
-def entry_detail(request, *args, **kwargs):
-    """
-    A wrapper around ``django.views.generic.date_based.object_detail``
-    which creates a ``QuerySet`` containing drafts and future entries
-    if user has permission to change entries
-    (``diario.change_entry``).
 
-    This is useful for preview entries with your own templates and
-    CSS.
+class EntryDetail(DateDetailView):    
+    slug_field = 'slug'
+    date_field = 'pub_date'
+    queryset = Entry.published_on_site.all()
+    month_format = '%m'
+    
+    
+    def dispatch(self, request, *args, **kwargs):
+        if request.user.has_perm('diario.change_entry'):
+            self.allow_future = True
+            self.queryset = Entry.on_site.all()
+        else:
+            self.allow_future = False
+        return super(EntryDetail, self).dispatch(request, *args, **kwargs)
+    
 
-    Tip: Uses the *View on site* button in Admin interface to access
-    yours drafts and entries in future.
-    """
-    kwargs['date_field'] = 'pub_date'
-    kwargs['slug_field'] = 'slug'
-    if request.user.has_perm('diario.change_entry'):
-        kwargs['allow_future'] = True
-        kwargs['queryset'] = Entry.on_site.all()
-    else:
-        kwargs['allow_future'] = False
-        if 'queryset' not in kwargs:
-            kwargs['queryset'] = Entry.published_on_site.all()
-    return date_based.object_detail(request, *args, **kwargs)
+    
+class EntryArchiveYear(YearArchiveView):
+    queryset = Entry.published_on_site.all()
+    date_field = 'pub_date'
+    
 
-def entry_archive_year(request, *args, **kwargs):
-    """
-    A thin wrapper around ``django.views.generic.date_based.archive_year``.
-    """
-    kwargs['date_field'] = 'pub_date'
-    if 'queryset' not in kwargs:
-        kwargs['queryset'] = Entry.published_on_site.all()
-    return date_based.archive_year(request, *args, **kwargs)
 
-def entry_archive_month(request, *args, **kwargs):
-    """
-    A thin wrapper around ``django.views.generic.date_based.archive_month``.
-    """
-    kwargs['date_field'] = 'pub_date'
-    if 'queryset' not in kwargs:
-        kwargs['queryset'] = Entry.published_on_site.all()
-    return date_based.archive_month(request, *args, **kwargs)
+class EntryArchiveMonth(MonthArchiveView):
+    month_format = '%m'
+    date_field = 'pub_date'
+    queryset = Entry.published_on_site.all()
+    
 
-def entry_archive_day(request, *args, **kwargs):
-    """
-    A thin wrapper around ``django.views.generic.date_based.archive_day``.
-    """
-    kwargs['date_field'] = 'pub_date'
-    if 'queryset' not in kwargs:
-        kwargs['queryset'] = Entry.published_on_site.all()
-    return date_based.archive_day(request, *args, **kwargs)
+class EntryArchiveDay(DayArchiveView):
+    month_format = '%m'
+    date_field = 'pub_date'
+    queryset = Entry.published_on_site.all()
 
-def entry_list(request, *args, **kwargs):
-    """
-    A thin wrapper around ``django.views.generic.list_detail.object_list``.
-    """
-    if 'queryset' not in kwargs:
-        kwargs['queryset'] = Entry.published_on_site.all()
-    return list_detail.object_list(request, *args, **kwargs)
+
+class EntryList(ListView):
+    queryset = Entry.published_on_site.all()
+    paginate_by = DIARIO_NUM_LATEST
+

diario/views/entries_by_author.py

 from django.contrib.auth.models import User
 from django.http import Http404
 from django.utils.translation import ugettext as _
-from django.views.generic import list_detail
+from django.views.generic.list import ListView
+from diario.settings import DIARIO_NUM_LATEST
 
 from diario.models import Entry
 
-def entry_list_by_author(request, author=None, *args, **kwargs):
-    """
-    A thin wrapper around ``django.views.generic.list_detail.object_list``.
-
-    In addition to the context variables set up by ``object_list``, a
-    ``author`` context variable will contain the ``User`` instance for the
-    entry author.
-    """
-    if author is None:
-        try:
-            author = kwargs.pop('author')
-        except KeyError:
+class EntryListByAuthor(ListView):
+    paginate_by = DIARIO_NUM_LATEST
+    template_name = 'diario/entry_list_by_author.html'
+    
+    def dispatch(self, request, *args, **kwargs):
+        self.author = kwargs.get('author', None)
+        if not self.author:
             raise AttributeError(_('entry_list_by_author must be called with '
                                    'a author.'))
-    try:
-        user_instance = User.objects.get(username=author)
-    except User.DoesNotExist:
-        raise Http404(_('No User found matching "%s".') % author)
-    if 'queryset' not in kwargs:
-        kwargs['queryset'] = Entry.published_on_site
-    kwargs['queryset'] = kwargs['queryset'].filter(author=user_instance)
-    if 'extra_context' not in kwargs:
-        kwargs['extra_context'] = {}
-    kwargs['extra_context']['author'] = user_instance
-    return list_detail.object_list(request, *args, **kwargs)
+        return super(EntryListByAuthor, self).dispatch(request, *args, **kwargs)
+    
+    def get_queryset(self):
+        try:
+            self.user = User.objects.get(username=self.author)
+        except User.DoesNotExist:
+            raise Http404(_('No User found matching "%s".') % self.author)
+        return Entry.published_on_site.filter(author=self.user)
+    
+    def get_context_data(self, **kwargs):
+        context = super(EntryListByAuthor, self).get_context_data(**kwargs)
+        context['author'] = self.user
+        return context
+  
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.