-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjournal.py
156 lines (113 loc) · 4.19 KB
/
journal.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
import datetime
from flask import Flask
from flask import request, redirect, url_for, render_template
from flask import json
from flask.ext.sqlalchemy import SQLAlchemy
import markdown2
app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///journal.db'
db = SQLAlchemy(app)
@app.route('/')
def index():
entries = Entry.query.order_by(Entry.date.desc()).limit(5).offset(0).all()
# render markdown for all entries in DB
for entry in entries:
entry.text_markdown = markdown2.markdown(entry.text)
total_entries = db.session.query(Entry.id).count()
return render_template('main.html',
total_entries=total_entries,
datetime=datetime.datetime,
entries=entries)
@app.route('/get/', methods=['GET'])
def get():
limit = request.args.get('limit', 5)
offset = request.args.get('offset', 0)
entries = Entry.query\
.order_by(Entry.date.desc())\
.limit(limit).offset(offset)\
.all()
# render markdown for all entries in DB
for entry in entries:
entry.text_markdown = markdown2.markdown(entry.text)
return render_template('entries.html',
datetime=datetime.datetime,
entries=entries)
@app.route('/get/<int:entry_id>', methods=['GET'])
def get_entry(entry_id):
entry = Entry.query.get(entry_id)
entry.text_markdown = markdown2.markdown(entry.text)
return render_template('single.html',
datetime=datetime.datetime,
entry=entry)
@app.route('/insert/', methods=['POST'])
def insert():
text = request.form['text']
if not text or len(text.strip()) == 0:
return redirect(url_for('index'))
entry = Entry(text)
if request.form['lat'] and request.form['lng']:
entry.lat = request.form['lat']
entry.lng = request.form['lng']
if request.form['date']:
entry.date = datetime.datetime.strptime((request.form['date']), '%Y-%m-%d %H:%M:%S')
db.session.add(entry)
db.session.commit()
return redirect(url_for('index'))
@app.route('/edit/', methods=['POST'])
def edit():
entry_id = request.form['entry_id']
edited_text = request.form['text']
edited_text_markdown = markdown2.markdown(edited_text)
entry = Entry.query.get(entry_id)
entry.text = edited_text
if request.form.get('lat') and request.form.get('lng'):
entry.lat = request.form['lat']
entry.lng = request.form['lng']
db.session.commit()
response = {'status': 'ok', 'code': 200, 'content': edited_text_markdown}
return json.dumps(response)
@app.route('/delete/', methods=['POST'])
def delete():
entry_id = request.form['entry_id']
entry = Entry.query.get(entry_id)
db.session.delete(entry)
db.session.commit()
response = {'status': 'ok', 'code': 200}
return json.dumps(response)
@app.route('/all_locations', methods=['GET'])
def all_locations():
entries = Entry.query.all()
json_locations = []
for e in entries:
if e.lat and e.lng:
loc = {'date': e.date.strftime('%a, %d. %B %Y'),
'id': e.id,
'lat': e.lat,
'lng': e.lng
}
json_locations.append(loc)
return json.dumps(json_locations)
class Entry(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, default=datetime.datetime.now)
text = db.Column(db.Text)
lat = db.Column(db.Float)
lng = db.Column(db.Float)
def __init__(self, text):
self.text = text
def __prep__(self):
return '<Entry %r>' % self.date
class Picture(db.Model):
id = db.Column(db.Integer, primary_key=True)
path = db.Column(db.String(128), unique=True)
mimetype = db.Column(db.String(64))
entry_id = db.Column(db.Integer, db.ForeignKey('entry.id'))
entry = db.relationship('Entry',
backref=db.backref('pictures', lazy='dynamic'))
def __init__(self, path):
self.path = path
def __repr__(self):
return '<Picture %r>' % self.path
if __name__ == '__main__':
app.run(host='0.0.0.0')