From 24b760797fd26e80c1738c614408b02b50284d4d Mon Sep 17 00:00:00 2001 From: luxagraf Date: Wed, 4 Nov 2015 21:40:38 -0500 Subject: cleaned up the code in blog and refactored to PEP 8 and class based views --- app/blog/views.py | 184 +++++++++++++++++++++++++----------------------------- 1 file changed, 85 insertions(+), 99 deletions(-) (limited to 'app/blog/views.py') diff --git a/app/blog/views.py b/app/blog/views.py index c360ef3..7dd755b 100644 --- a/app/blog/views.py +++ b/app/blog/views.py @@ -1,103 +1,89 @@ -from django.shortcuts import render_to_response, get_object_or_404 -from django.template import RequestContext -from django.http import Http404 +from django.views.generic import ListView +from django.views.generic.detail import DetailView +from django.views.generic.dates import YearArchiveView, MonthArchiveView + from django.conf import settings -# from django.views.generic import ListView - -from blog.models import Entry, HomepageCurrator -from locations.models import Region, Country -from photos.models import Photo - - -def home(request): - obj = HomepageCurrator.objects.get(pk=1) - template = obj.template_name - recent = Entry.objects.filter(status__exact=1)[:4] - context = { - 'IMAGES_URL': settings.IMAGES_URL, - 'homepage': obj, - 'recent': recent, - } - return render_to_response(template, context, context_instance=RequestContext(request)) - - -def entry_detail(request, year, month, slug): - obj = get_object_or_404(Entry, slug__exact=slug) - photos = {} - # if obj.photo_gallery: - # photos = Photo.objects.filter(set__exact=obj.get().photo_gallery.id)[:9] - extra = {'photos': photos, } - return render_to_response('details/entry.html', {'object': obj, 'photos': photos}, context_instance=RequestContext(request)) - -""" -List of all writing -class BlogEntryListView(ListView): - template_name="archives/writing.html" - model = Entry + +from .models import Entry, HomepageCurrator + +from locations.models import Country + + +class EntryList(ListView): + """ + Return a list of Entries in reverse chronological order + """ + context_object_name = 'object_list' + queryset = Entry.objects.filter(status__exact=1).order_by('-pub_date').select_related() + template_name = "archives/writing.html" + + def dispatch(self, request, *args, **kwargs): + request.page_url = '/jrnl/%d/' + request.page = int(self.kwargs['page']) + return super(EntryList, self).dispatch(request, *args, **kwargs) + + +class EntryCountryList(ListView): + """ + Return a list of Entries by Country in reverse chronological order + """ + context_object_name = 'object_list' + template_name = "archives/writing.html" + + def dispatch(self, request, *args, **kwargs): + request.page_url = '/jrnl/' + self.kwargs['slug'] + '/%d/' + request.page = int(self.kwargs['page']) + return super(EntryCountryList, self).dispatch(request, *args, **kwargs) + def get_context_data(self, **kwargs): - context = super(BlogEntryListView, self).get_context_data(**kwargs) - context.update({ - 'page': self.kwargs['page'] - }) - self.request.page_url = '/writing/%d/' - self.request.page = self.kwargs['page'] + # Call the base implementation first to get a context + context = super(EntryCountryList, self).get_context_data(**kwargs) + context['region'] = Country.objects.get(slug__exact=self.kwargs['slug']) return context -""" - -def date_list(request, year, month=None): - import datetime - if month: - month_name = datetime.datetime.strptime(month,'%m').strftime('%B') - context = { - 'object_list': Entry.objects.filter(status__exact=1, pub_date__year=year, pub_date__month=month).order_by('pub_date'), - 'type': 'monthly', - 'year': year, - 'month': month_name - } - else: - context = { - 'object_list': Entry.objects.filter(status__exact=1, pub_date__year=year).order_by('-pub_date'), - 'type': 'year', - 'year': year, - } - return render_to_response("archives/writing_date.html", context, context_instance=RequestContext(request)) - - -def entry_list(request, page): - request.page_url = '/jrnl/%d/' - request.page = int(page) - try: - is_build = request.POST['builder'] - extra_context={ - 'page':page, - 'MEDIA_URL': settings.BAKED_MEDIA_URL - } - except: - extra_context={'page':page} - context = { - 'object_list': Entry.objects.filter(status__exact=1).order_by('-pub_date').select_related(), - 'page': page - } - return render_to_response("archives/writing.html", context, context_instance=RequestContext(request)) - - -def entry_list_by_area(request, slug, page): - """ Grabs entries by region or country""" - request.page_url = '/jrnl/' + slug + '/%d/' - request.page = int(page) - try: - region = Region.objects.get(slug__exact=slug) - qs = Entry.objects.filter(status__exact=1, location__state__country__lux_region=region.id).order_by('-pub_date') - except: - region = Country.objects.get(slug__exact=slug) - qs = Entry.objects.filter( - status__exact=1, location__state__country=region).order_by('-pub_date') - if not region: - raise Http404 - context = { - 'region': region, - 'object_list': qs, - 'page': page - } - return render_to_response("archives/writing.html", context, context_instance=RequestContext(request)) + def get_queryset(self): + country = Country.objects.get(slug__exact=self.kwargs['slug']) + return Entry.objects.filter( + status__exact=1, + location__state__country=country + ).order_by('-pub_date') + + +class EntryYearArchiveView(YearArchiveView): + queryset = Entry.objects.filter(status__exact=1).select_related() + date_field = "pub_date" + make_object_list = True + allow_future = True + template_name = "archives/writing_date.html" + + +class EntryMonthArchiveView(MonthArchiveView): + queryset = Entry.objects.filter(status__exact=1).select_related() + date_field = "pub_date" + allow_future = True + template_name = "archives/writing_date.html" + + +class EntryDetailView(DetailView): + model = Entry + template_name = "details/entry.html" + slug_field = "slug" + + +class HomepageList(ListView): + """ + Return a main entry and list of Entries in reverse chronological order + """ + context_object_name = 'recent' + queryset = Entry.objects.filter(status__exact=1)[:4] + + def get_template_names(self): + obj = HomepageCurrator.objects.get(pk=1) + return ['%s' % obj.template_name] + + def get_context_data(self, **kwargs): + # Call the base implementation first to get a context + context = super(HomepageList, self).get_context_data(**kwargs) + context['homepage'] = HomepageCurrator.objects.get(pk=1) + context['IMAGES_URL'] = settings.IMAGES_URL + return context -- cgit v1.2.3-70-g09d2