-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviews.py
278 lines (234 loc) · 9.66 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import functools
# https://docs.djangoproject.com/en/1.4/topics/generic-views-migration/
# http://ccbv.co.uk/projects/Django/1.5/django.views.generic.list/MultipleObjectMixin/
# http://ccbv.co.uk/projects/Django/1.5/django.views.generic.dates/ArchiveIndexView/
from django.contrib.syndication.views import Feed
# from django.utils.feedgenerator import Atom1Feed
# from django.views.generic.list import MultipleObjectMixin
from django.views.generic import ArchiveIndexView, MonthArchiveView, YearArchiveView, DetailView, ListView
from django.core.urlresolvers import reverse
from django.contrib.flatpages.models import FlatPage
from .models import Entry
from taggit.models import Tag
# =home, =list views ===================================
# def new(request):
# """Show new posts"""
# return object_list(request,
# queryset=Entry.objects.filter(status=IS_PUBLIC).order_by('-created_at')[:300],
# template_name='new.html',
# template_object_name='post',
# extra_context= {"profile": get_profiles}
# )
# http://stackoverflow.com/questions/8547880/listing-object-with-specific-tag-using-django-taggit
# http://stackoverflow.com/a/7382708/412329
class LogbookView(ArchiveIndexView):
"""
Logbook Homepage
"""
model = Entry
date_field = 'pub_date'
template_name = 'hth/logbook.html'
queryset = Entry.objects.filter(
is_active=True).order_by('-pub_date', 'title')
allow_future = False
def get_dated_items(self):
date_list, items, extra_context = super(LogbookView, self).get_dated_items()
return (date_list, items[:30], extra_context)
# class LogbookView(ArchiveIndexView):
# """
# Logbook Homepage
# Extends the ArchiveIndexView view to add entries to the context
# """
# model = Entry
# date_field = 'pub_date'
# template_name = 'hth/logbook.html'
# allow_future = False
# # queryset = Entry.objects.filter(
# # is_active=True).order_by('-pub_date', 'title')
# # paginate_by = 30
# queryset = Entry.objects.filter(
# is_active=True).order_by('-pub_date', 'title')[:30]
# # https://docs.djangoproject.com/en/1.5/topics/class-based-views/generic-display/#adding-extra-context
# # def get_context_data(self, **kwargs):
# # context = super(LogbookView, self).get_context_data(**kwargs)
# # context['latest'] = Entry.objects.filter(
# # is_active=True).order_by('-pub_date', 'title')[:30]
# # return context
class LinkedListView(ArchiveIndexView):
"""
Linked List (kind == Link)
Extends the ArchiveIndexView view to add entries to the context
"""
model = Entry
date_field = 'pub_date'
template_name = 'hth/linked.html'
allow_future = False
queryset = Entry.objects.filter(
is_active=True, kind="L").order_by('-pub_date', 'title')
# =feeds ===================================
# http://stackoverflow.com/a/250373/412329
# def smart_truncate(content, length=200, suffix='...'):
# if len(content) <= length:
# return content
# else:
# # return ' '.join(content[:length+1].split(' ')[0:-1]) + suffix
# return content[:length].rsplit(' ', 1)[0]+suffix
class RssLogbookFeed(Feed):
title = "Hypertexthero"
link = "http://hypertexthero.com/logbook/"
feed_url = 'http://hypertexthero.com/logbook/rss/'
description = "Writing and links on web, design and simplicity by Simon Griffee"
# description_template = "hth/feed_description.html" # using default for now
def items(self):
return Entry.objects.filter(is_active=True).order_by('-pub_date')[:15]
def item_title(self, item):
return item.title
def item_pubdate(self, item):
return item.pub_date
def item_description(self, item):
# return smart_truncate(item.body_html)
return item.body_html
# class AtomLogbookFeed(RssLogbookFeed):
# feed_type = Atom1Feed
# feed_url = 'http://hypertexthero.com/logbook/rss/'
# link = "http://hypertexthero.com/logbook/"
# subtitle = RssLogbookFeed.description
# =single pages ===================================
class LogbookDetailView(DetailView):
""" Article permalink page """
model = Entry
class LinkedDetailView(DetailView):
""" Link permalink page """
model = Entry
class LogbookTagsDetailView(DetailView):
""" Tag detail page """
model = Entry
# class TaggedList(ListView):
# """ Get all content tagged with tag """
# queryset = Entry.objects.all()
# template_name = "hth/tagged.html"
# tagname =
#
# def get_queryset(self):
# return Entry.objects.filter(tags__name__in=[self.kwargs['tag']])
# def get_context_data(self, **kwargs):
# # Call the base implementation first to get a context
# context = super(TaggedList, self).get_context_data(**kwargs)
# # Add in the tag
# context['tag'] = self.tag
# return context
# =archives ===================================
class LogbookArchiveView(ArchiveIndexView):
"""
Archive of original entries (kind == Article) -
Extends the ArchiveIndexView view to add entries to the context
"""
model = Entry
date_field = 'pub_date'
template_name = 'hth/archive.html'
allow_future = False
queryset = Entry.objects.filter(
is_active=True, kind='A').order_by('-pub_date', 'title')
class LogbookTagsView(ArchiveIndexView):
"""
Tags - lets see if this works.
"""
model = Entry
date_field = 'pub_date'
template_name = 'hth/tags.html'
allow_future = False
queryset = Entry.objects.filter(
is_active=True).order_by('-pub_date', 'title')
class LinkedListMonthArchive(MonthArchiveView):
"""Linked List monthly archives"""
model = Entry
date_field = 'pub_date'
month_format='%m'
template_name = 'hth/linked_archive.html'
allow_future = False
queryset = Entry.objects.filter(
is_active=True, kind='L').order_by('-pub_date', 'title')
class LogbookMonthArchive(MonthArchiveView):
"""Monthly archives of articles"""
model = Entry
date_field = 'pub_date'
month_format='%m'
template_name = 'hth/archive_month.html'
allow_future = False
queryset = Entry.objects.filter(
is_active=True, kind='A').order_by('-pub_date', 'title')
class LogbookYearArchive(YearArchiveView):
"""Yearly archives of articles"""
model = Entry
date_field = 'pub_date'
year_format='%Y'
make_object_list=True,
template_name = 'hth/archive_year.html'
allow_future = False
queryset = Entry.objects.filter(
is_active=True, kind='A').order_by('-pub_date', 'title')
# =Search ===================================
# from django.http import HttpResponse
# from django.template import loader, Context
#
# def search(request):
# query = request.GET['q']
# results = Note.objects.filter(content_html__icontains=query)
# template = loader.get_template('hth/search.html')
# context = Context({ 'query': query, 'results': results })
# response = template.render(context)
# return HttpResponse(response)
# or use django's render_to_response shortcut:
from django.shortcuts import render_to_response
from django.db.models import Q
# http://stackoverflow.com/a/5478944/412329
from django.template import RequestContext
# def search(request):
# query = request.GET['q']
# return render_to_response('hth/search.html',
# {'query': query,
# 'results': Note.objects.filter(content_html__icontains=query) })
# rewritten so /search/ URL can be accessed directly:
# http://stackoverflow.com/questions/744424/django-models-how-to-filter-out-duplicate-values-by-pk-after-the-fact
from itertools import chain
def Search(request):
query = request.GET.get('q', '') # both /search/ and /search/?q=query work
entry_list = []
work_list = []
result_list = list(chain(entry_list, work_list))
# http://stackoverflow.com/a/4338108/412329 -
# passing the user variable into the context
user = request.user
if query:
# INSTEAD OF THIS:
# title_results = Note.objects.filter(title__icontains=query)
# results = Note.objects.filter(content_html__icontains=query)
# DO THIS avoid duplicate results when query word is both in title
# and content_html:
# http://stackoverflow.com/questions/744424/django-models-how-to-filter-out-duplicate-values-by-pk-after-the-fact
# http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view
entry_list = Entry.objects.filter(Q(title__icontains=query)|Q(
body_html__icontains=query), is_active=True).distinct()
work_list = FlatPage.objects.filter(Q(title__icontains=query)|Q(
content__icontains=query)).distinct()
result_list = sorted(
chain(entry_list, work_list),
key=lambda instance: instance)
# key=lambda instance: instance.pub_date)
return render_to_response('hth/search.html',
{'query': query,
'results': result_list,
'user': user
# http://stackoverflow.com/a/5478944/412329
}, context_instance=RequestContext(request))
# =Static Generation of files on server ===================================
# https://github.com/hypertexthero/django-staticgenerator/
# from django.dispatch import dispatcher
# from django.db.models import signals
# from staticgenerator import quick_delete
#
# def delete(sender, instance):
# quick_delete(instance, '/')
#
# dispatcher.connect(delete, sender=Entry, signal=signals.post_save)
# dispatcher.connect(delete, sender=FlatPage, signal=signals.post_save)