-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysite.py
198 lines (160 loc) · 5.9 KB
/
mysite.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
from __future__ import with_statement
from contextlib import closing
from functools import wraps
import uuid
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash, jsonify
from flaskext.sqlalchemy import SQLAlchemy
from db_utils import *
import os
import simplejson
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation
from datetime import datetime
DEBUG = False
SQLALCHEMY_DATABASE_URI = 'sqlite:////home/psanwald/paulsanwald/blog.db'
SECRET_KEY = 'development key'
USERNAME = 'paul'
PASSWORD = 'default'
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('MYSITE_SETTINGS',silent=True)
app.config['THEME'] = 'halloween.json'
db = SQLAlchemy(app)
post_tags = db.Table('post_tags',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)
class Post(db.Model):
id = db.Column(db.Integer,primary_key=True)
date = db.Column(db.DateTime)
text = db.Column(db.Text)
title = db.Column(db.String(100))
tags = db.relationship('Tag', secondary=post_tags,
backref=db.backref('posts',lazy='dynamic'))
def __init__(self,title=None,text=None,date=datetime.now()):
self.title = title
self.text = text
self.date = date
def __repr__(self):
return '<Post %r>' % (self.title)
def display_date(self):
return self.date.strftime("%m.%d.%Y")
def display_tags(self):
if len(self.tags) == 0:
return ""
tag_names = [tag.name for tag in self.tags]
return "Categories: "+ ", ".join(tag_names)
def tag_ids(self):
return [tag.id for tag in self.tags]
class Tag(db.Model):
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(100))
def __init__(self,name):
self.name = name
def __repr__(self):
return '<Tag %r %r>' % (self.id,self.name)
# This section handles routing for the basic site pages
def login_required(f):
@wraps(f)
def decorated_function(*args,**kwargs):
# TODO: look at storing the user in g.user instead of session
# if g.user is None:
if not session.get('logged_in'):
return redirect(url_for('login',next=request.url))
return f(*args, **kwargs)
return decorated_function
@app.route('/index.html')
def index():
return render_template('about.html')
@app.route('/')
def slash():
return redirect(url_for('index'))
@app.route('/music.html')
def music():
posts = db.session.query(Post).order_by(desc(Post.date)).filter(Post.tags.any(Tag.id==1)).all()
return render_template('music.html',posts=posts)
@app.route('/software.html')
def software():
posts = db.session.query(Post).order_by(desc(Post.date)).filter(Post.tags.any(Tag.id==3)).all()
return render_template('software.html',posts=posts)
@app.route('/color.css')
def color_css():
with app.open_resource('public/themes/%s' % (app.config['THEME'])) as defaultCSS:
theme = simplejson.load(defaultCSS)
return render_template('color.css',theme=theme)
@app.route('/color.js')
def color_js():
return render_template('color.js',default_theme=app.config['THEME'])
"""
Blogging section
"""
@app.route('/blog.html')
def blog():
return render_template('blog.html',posts=get_posts())
@app.route('/blog/<int:post_id>.html')
def blog_post(post_id):
return render_template('blog.html',posts=[Post.query.get(post_id)])
@app.route('/feed.rss')
def basic_rss():
return render_template('rss.xml',posts=get_posts())
@app.route('/blog/category/<tag_id>.rss')
def rss_category(tag_id):
posts = db.session.query(Post).filter(Post.tags.any(Tag.id==tag_id)).all()
return render_template('rss.xml',posts=posts)
@app.route('/blog/category/<tag_id>.html')
def blog_category(tag_id):
posts = db.session.query(Post).filter(Post.tags.any(Tag.id==tag_id)).all()
return render_template('blog.html',posts=posts)
@app.route('/admin/post/<int:post_id>/delete')
@login_required
def delete_post(post_id):
db.session.delete(Post.query.get(post_id))
db.session.commit()
return redirect(url_for('add_post'))
@app.route('/admin/post/<int:post_id>/edit',methods=['GET','POST'])
@login_required
def edit_post(post_id):
if request.method == 'GET':
return render_template('admin/post.html',post=Post.query.get(post_id),posts=get_posts(),tags=Tag.query.all())
elif request.method == 'POST':
post = Post.query.get(post_id)
post.title = request.form['title']
post.text = request.form['text']
post.tags = [Tag.query.get(tag_id) for tag_id in request.form.getlist('tags')]
db.session.commit()
return redirect(url_for('add_post'))
@app.route('/admin/post.html',methods=['GET','POST'])
@login_required
def add_post():
if request.method == 'POST':
post = Post(request.form['title'],request.form['text'])
post.tags = [Tag.query.get(tag_id) for tag_id in request.form.getlist('tags')]
db.session.add(post)
db.session.commit()
return redirect(url_for('add_post'))
else:
return render_template('admin/post.html',post=Post('',''),posts=get_posts(),tags=Tag.query.all())
def get_posts():
return Post.query.order_by(desc('date')).all()
"""
login/logout functions
"""
@app.route('/admin/login.html',methods=['GET','POST'])
def login():
error = None
if request.method == 'POST':
if (request.form['username'] != app.config['USERNAME']
or request.form['password'] != app.config['PASSWORD']):
error = "Wrong credentials"
else:
session['logged_in'] = True
return redirect(url_for('add_post'))
return render_template('admin/login.html',error=error)
@app.route('/admin/logout.html')
def logout():
session.pop('logged_in',None)
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(port=5555)