-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreko247.py
129 lines (108 loc) · 4.19 KB
/
freko247.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
# -*- coding: utf-8 -*-
import jinja2
import os
import urllib
import webapp2
from google.appengine.ext import db
from google.appengine.api import users
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
DEFAULT_BLOG = 'freko247'
class Post(db.Model):
'''Models an individual Blog entry wtih author, content and date'''
author = db.StringProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
class Co_Writer(db.Model):
'''Blog co-writers'''
grantee = db.StringProperty()
writer = db.StringProperty()
def blog_key(blog_name=None):
'''Constructs a Datastore key for a Blog entity
with blog_name.
'''
return db.Key.from_path('Blog', blog_name or DEFAULT_BLOG)
def can_post(user, blog_name):
if user:
q = Co_Writer.all().ancestor(
blog_key(blog_name)).filter('writer =', user.nickname())
co_writer = q.get()
if co_writer or is_owner(user, blog_name):
return True
return False
def is_owner(user, blog_name):
if user:
if user.nickname() == blog_name:
return True
return False
class MainPage(webapp2.RedirectHandler):
def get(self):
user = users.get_current_user()
blog_name = self.request.get('blog_name') or \
self.request.cookies.get(u'blog_name')
if user:
if not blog_name:
blog_name = user.nickname()
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
if not blog_name:
blog_name = DEFAULT_BLOG
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
posts = Post.all().ancestor(
blog_key(blog_name)).order('-date')
co_writers = Co_Writer.all().ancestor(
blog_key(blog_name)).order('writer')
template_values = {
'posts': posts or None,
'url': url,
'url_linktext': url_linktext,
'can_post': can_post(user, blog_name),
'blog_name': blog_name,
'is_owner': is_owner(user, blog_name),
'co_writers': co_writers,
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.set_cookie('blog_name', blog_name)
self.response.write(template.render(template_values))
class Blog(webapp2.RedirectHandler):
def post(self):
user = users.get_current_user()
if user:
blog_name = self.request.cookies.get('blog_name')
post = Post(parent=blog_key(blog_name))
post.author = users.get_current_user().nickname()
post.content = self.request.get('content')
post.put()
# TODO: Don't reload entire page when submitting new post...
query_params = {'blog_name': blog_name}
self.redirect('/?' + urllib.urlencode(query_params))
self.redirect('/')
class Co_writerManager(webapp2.RedirectHandler):
def post(self):
user = users.get_current_user()
if user:
add = self.request.get('add')
remove = self.request.get('remove')
writers = self.request.get('co_writers', allow_multiple=True)
if add:
blog = user.nickname()
co_writer = Co_Writer(parent=blog_key(blog))
co_writer.writer = self.request.get('co_writer')
co_writer.grantee = blog
co_writer.put()
query_params = {'blog_name': blog}
self.redirect('/?' + urllib.urlencode(query_params))
elif remove and writers:
blog_name = self.request.cookies.get('blog_name')
q = Co_Writer.all().ancestor(
blog_key(blog_name)).filter('writer IN', writers)
co_writers = q.get()
db.delete(co_writers)
self.redirect('/')
app = webapp2.WSGIApplication([('/', MainPage),
('/new_post', Blog),
('/update_co_writers', Co_writerManager)],
debug=True
)