This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
158 lines (115 loc) · 3.49 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
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
#!/usr/bin/env python
from google.appengine.dist import use_library
use_library('django', '1.2')
import cgi
import os
import string
import logging
import random
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext.db import djangoforms
from google.appengine.api.users import create_logout_url
from django import forms
from models.models import Link
class LinkForm(djangoforms.ModelForm):
error_css_class = 'error'
required_css_class = 'required'
url = forms.URLField(
max_length=200,
required=True,
label="URL",
help_text="The URL.",
widget = forms.TextInput(attrs={'size':'50'})
)
category = forms.CharField(
required=True,
label="Category",
widget=forms.Select(choices=(
('news', 'News'),
('picture', 'Picture'),
('story', 'Story'),
))
)
class Meta:
model = Link
class ListLinksHandler(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'templates/link_list.html')
links = Link.all()
template_values = {
'logout_url': create_logout_url('/admin/logout/'),
'links': links,
}
self.response.out.write(template.render(path, template_values))
class AddLinkHandler(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'templates/link_add.html')
template_values = {
'form': LinkForm(),
}
self.response.out.write(template.render(path, template_values))
def post(self):
form = LinkForm(data=self.request.POST)
if form.is_valid():
form.save()
self.redirect('/admin/links/')
else:
template_values = {
'form': form,
}
path = os.path.join(os.path.dirname(__file__), 'templates/link_add.html')
self.response.out.write(template.render(path, template_values))
class EditLinkHandler(webapp.RequestHandler):
def get(self, link_key):
link = Link.get(link_key)
path = os.path.join(os.path.dirname(__file__), 'templates/link_edit.html')
template_values = {
'link': link,
'form': LinkForm( instance=link ),
}
self.response.out.write(template.render(path, template_values))
def post(self, link_key):
link = Link.get(link_key)
form = LinkForm(data=self.request.POST, instance=link)
if form.is_valid():
form.save()
self.redirect('/admin/links/')
else:
path = os.path.join(os.path.dirname(__file__), 'templates/link_edit.html')
template_values = {
'link': link,
'form': form,
}
self.response.out.write(template.render(path, template_values))
class DeleteLinkHandler(webapp.RequestHandler):
def get(self, link_key):
path = os.path.join(os.path.dirname(__file__), 'templates/link_delete.html')
link = Link.get(link_key)
template_values = {
'link': link,
}
self.response.out.write(template.render(path, template_values))
def post(self, link_key):
if self.request.get('submit') == 'Delete':
link_key = self.request.get('link_key')
link = Link.get(link_key)
link.delete()
self.redirect('/admin/links/')
def main():
application = webapp.WSGIApplication(
[
('/admin/', ListLinksHandler),
('/admin/links/', ListLinksHandler),
('/admin/links/add/', AddLinkHandler),
('/admin/link/add/', AddLinkHandler),
('/admin/links/([a-zA-Z0-9]*)/delete/', DeleteLinkHandler),
('/admin/links/([a-zA-Z0-9]*)/edit/', EditLinkHandler),
],
debug=True
)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()