forked from diwakergupta/gaestebin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (85 loc) · 3.16 KB
/
main.py
File metadata and controls
95 lines (85 loc) · 3.16 KB
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
# Copyright 2012 Diwaker Gupta
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import cgi
import jinja2
import os
import random
import string
import webapp2
from google.appengine.api import memcache
from google.appengine.api import users
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class Paste(db.Model):
id = db.StringProperty()
content = db.TextProperty()
timestamp = date = db.DateTimeProperty(auto_now_add=True)
def gen_random_string(length):
chars = string.letters + string.digits
return ''.join(random.choice(chars) for i in xrange(length))
class SavePaste(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
paste = Paste()
paste.id = gen_random_string(8)
paste.content = self.request.get('content')
paste.put()
self.response.set_cookie('delid', str(paste.key()))
self.redirect('/' + paste.id)
class DelPaste(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
delid = self.request.get('delid')
if delid:
paste = Paste.get(delid)
if paste:
memcache.delete(paste.id)
paste.delete()
self.redirect('/')
class CreatePaste(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
template_values = {}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class ShowPaste(webapp2.RequestHandler):
def get(self, paste_id):
paste = memcache.get(paste_id)
if paste is None:
query = db.Query(Paste)
query.filter("id = ", paste_id)
entry = query.get()
if entry is None:
self.abort(404)
paste = entry.content
memcache.add(paste_id, paste)
template_values = {"content": cgi.escape(paste)}
if 'delid' in self.request.cookies:
template_values['delid'] = self.request.cookies.get('delid')
self.response.delete_cookie('delid')
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
app = webapp2.WSGIApplication([
(r'/', CreatePaste),
(r'/paste', SavePaste),
(r'/oops', DelPaste),
(r'/(\S+)', ShowPaste)
])