-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.py
180 lines (165 loc) · 8.28 KB
/
wiki.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
import webapp2
from main import BaseHandler
from database import Wiki_Entries
from google.appengine.ext import db
from google.appengine.api import memcache
import utils
class WikiPage(BaseHandler):
def get(self):
valid_cookie = self.request.cookies.get('user_id')
if valid_cookie:
import globals
if globals.users != None:
url = self.request.url.split('/')
build_edit_url = '/' + url[-2] + '/_edit/' + url[-1]
build_history_url = '/' + url[-2] + '/history/' + url[-1]
url_title = self.request.url.split('/')[-1]
if not url_title:
url_title = 'welcome'
stored = memcache.get(url_title)
if stored is not None:
self.render("wiki_page.html", user = globals.users, content = stored, build_url = build_edit_url, history_url = build_history_url)
else:
check_exist = utils.get_wiki_content()
content = None
for entry in check_exist:
title = entry['title']
if title == url_title:
content = entry['content']
if content:
content = content
utils.cache_wiki(url_title, content, True)
self.render("wiki_page.html", content = content, user = globals.users, build_url= build_edit_url, history_url = build_history_url)
else:
self.redirect('/wiki/_edit/' + url_title)
else:
get_user = utils.check_cookie(self)
globals.users = get_user
url = self.request.url.split('/')
build_edit_url = '/' + url[-2] + '/_edit/' + url[-1]
build_history_url = '/' + url[-2] + '/history/' + url[-1]
url_title = self.request.url.split('/')[-1]
if not url_title:
url_title = 'welcome'
stored = memcache.get(url_title)
if stored is not None:
self.render("wiki_page.html", user = globals.users,content = stored, build_url = build_edit_url, history_url = build_history_url)
else:
if url_title == 'welcome':
check_exist = utils.get_wiki_content()
content = None
for entry in check_exist:
title = entry['title']
if title == url_title:
content = entry['content']
if content:
content = content
utils.cache_wiki(url_title, content, True)
self.render("wiki_page.html", content = content, user = globals.users, build_url= build_edit_url, history_url = build_history_url)
else:
self.redirect('/wiki/_edit/')
else:
self.redirect('/wiki/_edit/' + url_title)
else:
self.redirect('/wiki/login')
class WikiEdit(BaseHandler):
def get(self):
valid_cookie = self.request.cookies.get('user_id')
if valid_cookie:
import globals
if globals.users != None:
url_title = self.request.url.split('/')[-1]
if not url_title:
url_title = 'welcome'
content = memcache.get(url_title)
if content:
self.render("wiki_edit.html", content = content, user = globals.users, current_url = url_title)
else:
self.render("wiki_edit.html", user = globals.users)
else:
self.redirect("/wiki/login")
def post(self):
url_title = self.request.url.split('/')[-1]
if not url_title:
url_title = 'welcome'
stored_content = memcache.get(url_title)
content = self.request.get("content")
if content and content != stored_content:
if not url_title:
title = 'welcome'
else:
title = url_title
if stored_content:
c = Wiki_Entries(parent = utils.wiki_key(title), content = content, title = title)
else:
version = 1
c = Wiki_Entries(key_name = title, content = content, title = title, version = version)
c.put()
history = utils.get_wiki_history(url_title)
utils.cache_history(url_title, history, update = True)
utils.cache_wiki(title, content, True)
if title == 'welcome':
self.redirect("/wiki/")
else:
self.redirect("/wiki/" + title)
else:
if title == 'welcome':
self.redirect('/wiki/')
else:
self.redirect('/wiki/' + title)
class Wiki_History(BaseHandler):
def get(self):
valid_cookie = self.request.cookies.get('user_id')
if valid_cookie:
import globals
if globals.users != None:
url_title = self.request.url.split('/')[-1]
if not url_title:
url_title = 'welcome'
history = memcache.get(url_title + '_history')
key = db.Key.from_path('Wiki_Entries', url_title)
if not history:
history = utils.get_wiki_history(url_title)
utils.cache_history(url_title, history, update = True)
# self.render("test.html", test = test)
self.render("history_main.html", history = history, user = globals.users)
else:
self.redirect("/wiki/login")
class Wiki_View_Edit(BaseHandler):
def get(self):
valid_cookie = self.request.cookies.get('user_id')
if valid_cookie:
import globals
if globals.users != None:
contents = []
# content = []
version = None
url = self.request.url.split('/')
url_title = self.request.url.split('/')[-2]
if url_title == 'view_edit':
url_title = url[-1]
else:
version = self.request.url.split('/')[-1]
history_cached = memcache.get(url_title + '_history')
for entry in history_cached:
if entry.key().id() is not None:
contents.append([int(entry.key().id()),entry.content])
else:
contents.append([entry.key().id(),entry.content])
for items in contents:
# content.append(items[0])
if version is not None:
if items[0] == int(version):
content = items[1]
else:
content = items[1]
# test = version
self.render("wiki_page.html", user = globals.users, content = content)
else:
self.redirect("/wiki/login")
PAGE_RE = r'(/(?:[a-zA-Z0-9_-]+/?)*)'
app = webapp2.WSGIApplication([('/wiki/_edit/(?:[a-zA-Z0-9_-]+/?)*', WikiEdit),
('/wiki/history/view_edit/(?:[a-zA-Z0-9_-]+/?)*', Wiki_View_Edit),
('/wiki/history/(?:[a-zA-Z0-9_-]+/?)*', Wiki_History),
('/wiki/(?:[a-zA-Z0-9_-]+/?)*', WikiPage)
], debug=True)