/
views.py
175 lines (150 loc) · 5.13 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
from urlparse import urljoin
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
import re
from datetime import date, datetime
from werkzeug.contrib.atom import AtomFeed
from app import app, psql_db
from auth import auth
from models import User, Entry, Page, Recipe
from admin import admin
from math import ceil
#create db connections for functions
@app.before_request
def _db_connect():
psql_db.connect()
@app.teardown_request
def _db_close(exc):
if not psql_db.is_closed():
psql_db.close()
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html')
@app.route('/')
def show_entries(page=1):
pagecount, perpage = calc_entries()
entries = Entry.select().order_by(-Entry.publishdate).paginate(int(page), perpage)
for entry in entries:
entry.year = entry.publishdate.year
if len(str(entry.publishdate.month))==1:
entry.month="0"+str(entry.publishdate.month)
else:
entry.month=entry.publishdate.month
if len(str(entry.publishdate.day))==1:
entry.day="0"+str(entry.publishdate.day)
else:
entry.day=entry.publishdate.day
entry.twitter = User.get(User.id==entry.user).twitter
page=int(page)
if page>1:
entries.newer=page-1
else:
entries.newer=None
if pagecount>page:
entries.older=page+1
else:
entries.older=None
return render_template('show_entries.html', entries=entries)
def calc_entries():
paginate=10
entrycount = Entry.select().count()
if entrycount>paginate:
pagecount = ceil(entrycount/float(paginate))
else:
pagecount=1
return pagecount, paginate
@app.route('/entry-list/<pagenum>/')
def new_page(pagenum):
if pagenum=='admin':
return redirect(url_for(admin))
else:
pagenum=int(pagenum)
return show_entries(page=pagenum)
@app.route('/entries/<year>/<month>/<slug>.html')
def show_entry(slug, year, month):
entry = Entry.select().where(Entry.publishdate.year==year).where(Entry.publishdate.month==month).where(Entry.slug==slug).get()
entry.year = entry.publishdate.year
if len(str(entry.publishdate.month))==1:
entry.month="0"+str(entry.publishdate.month)
else:
entry.month=entry.publishdate.month
if len(str(entry.publishdate.day))==1:
entry.day="0"+str(entry.publishdate.day)
else:
entry.day=entry.publishdate.day
entry.twitter = User.get(User.id==entry.user).twitter
if entry is None:
abort(404)
else:
return render_template('entry.html', entry=entry)
@app.route('/recipes/')
def show_recipes(page=1):
pagecount, perpage=calc_recipes()
recipes = Recipe.select().order_by(Recipe.title).paginate(int(page), perpage)
page=int(page)
if page>1:
recipes.newer=page-1
else:
recipes.newer=None
if pagecount>page:
recipes.older=page+1
else:
recipes.older=None
return render_template('show_recipes.html', recipes=recipes)
def calc_recipes():
paginate=25
recipecount = Recipe.select().count()
if recipecount>paginate:
pagecount = ceil(recipecount/float(paginate))
else:
pagecount=1
return pagecount, paginate
@app.route('/recipe-list/<pagenum>/')
def new_recipe_page(pagenum):
if pagenum=='admin':
return redirect(url_for(admin))
else:
pagenum=int(pagenum)
return show_recipes(page=pagenum)
@app.route('/recipes/<slug>.html')
def show_recipe(slug):
recipe = Recipe.select().where(Recipe.slug==slug).get()
recipe.year = recipe.added_date.year
if len(str(recipe.added_date.month))==1:
recipe.month="0"+str(recipe.added_date.month)
else:
recipe.month=recipe.added_date.month
if len(str(recipe.added_date.day))==1:
recipe.day="0"+str(recipe.added_date.day)
else:
recipe.day=recipe.added_date.day
if recipe is None:
abort(404)
else:
return render_template('recipe.html', recipe=recipe)
@app.route('/<pagename>.html')
def show_page(pagename):
try:
page = Page.get(Page.slug==str(pagename))
if page is None:
abort(404)
else:
return render_template('page.html', page=page)
except:
abort(404)
@app.route('/recent.atom')
def recent_feed():
feed=AtomFeed('Recent Posts',
feed_url=request.url, url=request.url_root)
posts = Entry.select().order_by(-Entry.publishdate).limit(15)
for post in posts:
post.year = post.publishdate.year
if len(str(post.publishdate.month))==1:
post.month="0"+str(post.publishdate.month)
else:
post.month=post.publishdate.month
feed.add(post.title, unicode(post.text), content_type='html',
author = post.user.user_first+' '+post.user.user_last,
url = 'http://carboy.tommeagher.com/entries/{0}/{1}/{2}.html'.format(post.year, post.month, post.slug),
published = datetime.combine(post.publishdate, datetime.min.time()), updated=datetime.combine(post.publishdate, datetime.min.time()))
return feed.get_response()