-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostcarding.py
99 lines (78 loc) · 3.05 KB
/
postcarding.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
import os
from flask import Flask
from flask import render_template, url_for, redirect, session, request, jsonify
import pymongo
import json
import datetime
import random
app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config['DEBUG'] = True
conn = pymongo.Connection(os.environ['OPENSHIFT_MONGODB_DB_URL'])
db = conn['postcards']
def get_index(alist, element):
return alist.index(element)
@app.route('/no_text', methods = ['GET', 'POST'])
def add_numbers():
if request.method == 'POST':
no_text = request.json['hasText']
card_id = request.json['cardId']
result = db['cards'].find_one({'img_id': card_id})
if result:
result = db['cards'].update({"img_id": card_id},{"$set": {"no_text": True}})
return 'Postkaart uuendatud'
else:
return 'Postkaari ei ole uuendatud'
else:
return ''
@app.route("/")
def front():
pages = []
pages_list = db['cards'].find({ "no_text" : { "$exists" : False }})
for page in pages_list:
pages.append(page['img_id'])
random_page = "00" + pages[random.randrange(len(pages))]
return render_template('front.html', random_page = random_page)
@app.route("/item/00<card_id>", methods=['GET', 'POST'])
def digar_cards(card_id):
pages = []
pages_list = db['cards'].find({ "no_text" : { "$exists" : False } })
for page in pages_list:
pages.append(page['img_id'])
random_page = random.randrange(len(pages))
result = db['cards'].find_one({'img_id': card_id})
if result and not 'no_text' in result:
postcard = {}
postcard['front_img'] = result['front']
postcard['back_img'] = result['back']
postcard['previous'] = "00" + pages[get_index(pages, card_id)-1]
postcard['next'] = "00" + pages[get_index(pages, card_id)+1]
postcard['text'] = ''
postcard['card_id'] = card_id
postcard['random_page'] = "00" + pages[random_page]
elif result and 'no_text' in result:
postcard = {}
postcard['front_img'] = result['front']
postcard['back_img'] = result['back']
postcard['text'] = ''
postcard['card_id'] = card_id
postcard['random_page'] = "00" + pages[random_page]
else:
return 'Vabandame, sellist postkaarti ei ole.'
commented = db['comments'].find({'img_id': card_id}).sort('date', pymongo.DESCENDING)
comments = []
if commented:
for i in commented:
comments.append(i['comment'])
if len(comments) > 0:
postcard['text'] = comments[-1]
if request.method == 'POST':
if len(request.form['comment']) > 0:
db['comments'].insert({'img_id': card_id, 'comment': request.form['comment'], 'time': datetime.datetime.utcnow()})
return redirect(url_for('digar_cards', card_id=card_id))
if result and not 'no_text' in result:
return render_template('index.html', postcard = postcard)
else:
return render_template('notext.html', postcard = postcard)
if __name__ == "__main__":
app.run()