This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
110 lines (89 loc) · 3.53 KB
/
admin.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
from datetime import datetime
import wsgiref.handlers
from google.appengine.api import images
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
import settings
import utils
from handler import BaseHandler
from models import Page
from models import Photo
from models import PhotoCategory
from models import PhotoLocation
class Index(BaseHandler):
def get(self):
if users.is_current_user_admin():
self.render_html("admin_index", {'logout_url': users.create_logout_url("/")})
else:
self.redirect(users.create_login_url("/admin/"))
def get_photo_id(date_posted, slug):
return "%s-%s" % (date_posted.strftime("%Y-%m-%d"), slug)
class PhotoEdit(BaseHandler):
def get(self, id=None):
if id:
photo = Photo.gql("WHERE id = :1", id).get()
else:
photo = None
self.render_html("photo_edit", {'photo': photo})
def post(self, id=None):
if id:
photo = Photo.gql("WHERE id = :1", id).get()
else:
photo = Photo(content_type="image/jpg")
date_posted = utils.strptime_for_edit(self.request.get('date_posted'))
photo.title = self.request.get('title')
photo.id = get_photo_id(date_posted, self.request.get('slug'))
photo.location = self.request.get('location')
location_slug = utils.slug(photo.location)
location = db.Query(PhotoLocation).filter("id = ", location_slug).get()
if not location:
location = PhotoLocation(id=location_slug, name=photo.location)
location.put()
photo.location_ref = location
photo.categories = self.request.get('categories')
photo.categories_ref = []
for category_name in photo.categories.split(","):
category_name = category_name.strip()
category_slug = utils.slug(category_name)
category = db.Query(PhotoCategory).filter("id = ", category_slug).get()
if not category:
category = PhotoCategory(id=category_slug, name=category_name)
category.put()
photo.categories_ref.append(category.key())
photo.description = self.request.get('description')
photo.date_posted = date_posted
if self.request.get('file'):
photo.file = db.Blob(self.request.get('file'))
photo.thumbnail = images.resize(photo.file, 128, 128)
photo.put()
self.redirect(photo.get_url())
class PageEdit(BaseHandler):
def get(self, slug=None):
if slug:
page = Page.gql("WHERE slug = :1", slug).get()
else:
page = None
self.render_html("page_edit", {'page': page})
def post(self, slug=None):
if slug:
page = Page.gql("WHERE slug = :1", slug).get()
else:
page = Page()
page.title = self.request.get('title')
page.slug = self.request.get('slug')
page.content = self.request.get('content')
page.order_in_menu = int(self.request.get('order_in_menu'))
page.put()
self.redirect(page.get_url())
def main():
application = webapp.WSGIApplication(
[(r"^/admin/$", Index),
(r"^/admin/photo/add/$", PhotoEdit),
(r"^/admin/photo/(?P<id>[a-z0-9-]+)/edit/$", PhotoEdit),
(r"^/admin/page/add/$", PageEdit),
(r"^/admin/page/(?P<slug>[a-z0-9-]+)/edit/$", PageEdit)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()