Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
145 lines (105 sloc) 4.82 KB

Comparing implementations

The following are simple, quite common views from a real world application.

Django 1.3 Class-based generic views:

class ArtistDetail(DetailView):
    context_object_name = 'artist'
    queryset = Artist.publ.all()

utkik.View:

class ArtistDetail(View):
    def get(self, slug):
        self.c.artist = get_object_or_404(Artist.publ, slug=slug)

Note

The following two list views are written a little differently regarding the context collection to reflect more real world usage.

Django 1.3 Class-based generic views:

class ArtistList(TemplateView):
    template_name = 'artists/artist_list.html'

    def get_context_data(self, slug=None, tag=None):
        genre = get_object_or_404(Genre.objects, slug=slug)
        artists = Artist.publ.filter(genres=genre).order_by('?')
        tags = Tag.objects\
            .filter(artist__genres=genre, artist__active=True)\
            .annotate(num_tags=Count('artist'))\
            .filter(num_tags__gte=2)\
            .order_by('-num_tags')\
            .distinct()
        return {
            'artists': artists,
            'tags': tags,
            'selected_genre': genre,
        }

utkik.View:

class ArtistList(View):
    def get(self, slug=None, tag=None):
        self.c.selected_genre = get_object_or_404(Genre.objects, slug=slug)
        self.c.artists = Artist.publ.filter(genres=self.c.selected_genre).order_by('?')
        self.c.tags = Tag.objects\
            .filter(artist__genres=self.c.selected_genre, artist__active=True)\
            .annotate(num_tags=Count('artist'))\
            .filter(num_tags__gte=2)\
            .order_by('-num_tags')\
            .distinct()

Django 1.3 Class-based generic views:

class MoreArtists(TemplateView):
    template_name = 'artists/inc/artists_in_focus.html'

    def dispatch(self, request, *args, **kwargs):
        if not request.is_ajax():
            return HttpResponseForbidden()
        return super(MoreArtists, self).dispatch(request, *args, **kwargs)

    def get_context_data(self):
        return { 'artists': Artist.publ.all().order_by('?')[:6] }

utkik.View:

class ArtistsInFocus(View):
    decorators = [ requires_ajax ]

    def get(self):
        self.c.artists = Artist.publ.all().order_by('?')[:6]

Django 1.3 Class-based generic views:

class ArtistLogin(FormView):
    form_class = ArtistLoginForm
    template_name = 'artists/artist_login.html'

    def get(self, request, *args, **kwargs):
        request.session.set_test_cookie()
        return super(ArtistLogin, self).get(request, *args, **kwargs)

    def form_valid(self, form):
        login(self.request, form.artist)
        return HttpResponseRedirect(reverse('artist_mypage'))

    def get_form_kwargs(self):
        kwargs = super(ArtistLogin, self).get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

utkik.View:

class ArtistLogin(View):
    def setup(self):
        self.c.form = ArtistLoginForm(
            request=self.request, data=self.request.POST or None)

    def get(self):
        self.request.session.set_test_cookie()

    def post(self):
        if self.c.form.is_valid():
            login(self.request, self.c.form.artist)
            return HttpResponseRedirect(reverse('artist_mypage'))

Django 1.3 Class-based generic views:

from django.conf.urls.defaults import *
from artists.views import ArtistDetail, ArtistList, ArtistTagList, MoreArtists, ArtistSearch, ArtistMyPage


urlpatterns = patterns('',
    url(r'^min-sida/$', ArtistMyPage.as_view(), name='artist_mypage'),
    url(r'^artister/$', MoreArtists.as_view(), name='artist_more'),
    url(r'^artister/sok/$', ArtistSearch.as_view(), name='artist_search'),
    url(r'^artister/(?P<slug>[-\w]+)/$', ArtistList.as_view(), name='artist_genre_list'),
    url(r'^artister/(?P<slug>[-\w]+)/(?P<tag>.*)/$', ArtistTagList.as_view(), name='artist_genre_tag_list'),
    url(r'^(?P<slug>[-\w]+)/$', ArtistDetail.as_view(), name='artist_detail'),
)

utkik.View:

from utkik.dispatch import *


urlpatterns = patterns('',
    url(r'^min-sida/$', 'artists.ArtistMyPage', name='artist_mypage'),
    url(r'^artister/$', 'artists.MoreArtists', name='artist_more'),
    url(r'^artister/sok/$', 'artists.ArtistSearch', name='artist_search'),
    url(r'^artister/(?P<slug>[-\w]+)/$', 'artists.ArtistList', name='artist_genre_list'),
    url(r'^artister/(?P<slug>[-\w]+)/(?P<tag>.*)/$', 'artists.ArtistTagList', name='artist_genre_tag_list'),
    url(r'^(?P<slug>[-\w]+)/$', 'artists.ArtistDetail', name='artist_detail'),
)

Note

You can of course use the utkik dispatcher for Django 1.3 Class-based generic views too.