-
Notifications
You must be signed in to change notification settings - Fork 1
/
helloworld.py
195 lines (134 loc) · 5.86 KB
/
helloworld.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import cgi
import logging
import os
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms
specializations = set(['Neurology,', 'General', 'Psychology', 'Dental']);
hours = ["%02d" % h for h in range(1,13)]
mins = ["%02d" % h for h in range(0,60)]
#TODO: Doctor deletion
#TODO: What should be the default values for time boxes?
#TODO: Calculating the time difference
# Time difference should be stored as float with following procedure
# time_to_store = hour + min/60
# if pm:
# time_to_store += 12
def time_as_number(t):
temp = t.split(':')
h = float(temp[0])
if h == 12 and temp[2].lower() == 'am':
h = 0
if h < 12 and temp[2].lower() == 'pm':
h += 12
h = h + float(temp[1])/60.0
return h
def time_as_string(num):
h = int(num)
m = round((num - int(num)) * 60.0)
ampm = 'AM'
if h >= 12:
h -= 12
ampm = 'PM'
return "%02d:%02d:%s" % (h,m,ampm)
#FIXME: Timings must be datetime.datetime
class Doctor(db.Model):
name = db.StringProperty('Name', required=True)
specialization = db.StringProperty('Specialization',
default='General', required=True,
choices=specializations)
sits_from = db.FloatProperty('From')
sits_upto = db.FloatProperty('Up to')
address = db.PostalAddressProperty('Address')
fee = db.IntegerProperty('Fee')
phone = db.PhoneNumberProperty('Phone')
email = db.EmailProperty('Email')
rating = db.RatingProperty('Rating')
def sits_from_string(self, time_as_str=None):
if time_as_str is None:
return time_as_string(self.sits_from)
else:
self.sits_from = time_as_number(time_as_str)
def sits_upto_string(self, time_as_str=None):
if time_as_str is None:
return time_as_string(self.sits_upto)
else:
self.sits_upto = time_as_number(time_as_str)
# This is just temporary as we will not be generating the form
# own our own.
class DoctorForm(djangoforms.ModelForm):
class Meta:
model = Doctor
exclude = ['rating']
#TODO: The form should have a client side validation.
class DoctorsListings(webapp.RequestHandler):
def get(self):
doctors = db.GqlQuery("SELECT * FROM Doctor ORDER BY rating DESC LIMIT 10");
template_values = {'doctors': doctors,
'specializations' : specializations,
'hours': hours,
'mins': mins
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
#self.response.out.write(DoctorForm())
#TODO: This should do validation and should put all the errors in a dictionary
# that would be forwarded to the main page where it would be visible on the
# form with relvant error messges.
class RegisterDoctor(webapp.RequestHandler):
def post(self):
doctor= Doctor(name=self.request.get('name'), specialization=self.request.get('specialization'))
from_time = "%s:%s:%s" % (self.request.get('sits_from_hour'),
self.request.get('sits_from_min'),
self.request.get('sits_from_ampm'))
upto_time = "%s:%s:%s" % (self.request.get('sits_upto_hour'),
self.request.get('sits_upto_min'),
self.request.get('sits_upto_ampm'))
doctor.sits_from = float(time_as_number(from_time))
doctor.sits_upto = float(time_as_number(upto_time))
doctor.address = db.PostalAddress(self.request.get('address'))
doctor.fee = int(self.request.get('fee'))
doctor.phone = db.PhoneNumber(self.request.get('phone'))
doctor.email = db.Email(self.request.get('email'))
doctor.put()
self.redirect('/')
#FIXME: Remove this stuff.
class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write("""<html><head><title>Guest book</title></head><body>""")
greetings = db.GqlQuery("SELECT * FROM Greeting ORDER BY date DESC LIMIT 10")
for greeting in greetings:
if greeting.author:
self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname())
else:
self.response.out.write('An unknown person wrote')
self.response.out.write("<blockquote>%s</blockquote>" % cgi.escape(greeting.content))
# Write the page footer.
self.response.out.write("""
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>
""")
class GuestBook(webapp.RequestHandler):
def post(self):
greeting = Greeting()
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get('content')
greeting.put()
self.redirect('/')
application = webapp.WSGIApplication([
('/', DoctorsListings),
('/newdoctor', RegisterDoctor),
('/sign', GuestBook)], debug=True)
def main():
logging.getLogger().setLevel(logging.DEBUG)
run_wsgi_app(application)
if __name__ == "__main__":
main()