-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1219 lines (1018 loc) · 49.9 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import logging
import random
import time
import urllib
import wsgiref.handlers
import zipfile
import re
from cStringIO import StringIO
from datetime import datetime
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import images
import ec
import servertest
class MainHandler(webapp.RequestHandler):
def get(self):
results = db.GqlQuery("Select * FROM Enterprise")
templateValues = {
'enterprises' : results
}
path = os.path.join(os.path.dirname(__file__), 'html/index.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class About(webapp.RequestHandler):
def get(self):
templateValues = {}
path = os.path.join(os.path.dirname(__file__), 'html/about.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class Media(webapp.RequestHandler):
def get(self):
templateValues = {}
path = os.path.join(os.path.dirname(__file__), 'html/media.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class Help(webapp.RequestHandler):
def get(self):
templateValues = {}
path = os.path.join(os.path.dirname(__file__), 'html/iPhone-help.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class Tutorial(webapp.RequestHandler):
def get(self):
templateValues = {}
path = os.path.join(os.path.dirname(__file__), 'html/tutorial.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class Template(webapp.RequestHandler):
def get(self):
templateValues = {}
path = os.path.join(os.path.dirname(__file__), 'html/template.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class CreateProject(webapp.RequestHandler):
def post(self):
error = ''
projectName = self.request.get("projectName")
protect = self.request.get("protect") == "true"
if projectName == None or len(projectName.strip()) < 4: #just show the form, don't process anything
self.response.out.write("Error: Project names must be at least 4 characters long.")
return
if projectName.find(" ") >= 0 or projectName.find("\t") >= 0 or projectName.find("\n") >= 0 or projectName.find(",") >= 0 or projectName.find("&") >= 0 or projectName.find(".") >= 0 or projectName.find("-") >= 0:
self.response.out.write("Error: Spaces, tabs, commas, hyphens, dots and/or ampersands are not allowed in project names.")
return
try:
asciiString = str(projectName)
except UnicodeEncodeError:
self.response.out.write("Error: Project names must consist only of ASCII characters; accented or other special characters are not (yet) allowed.")
return
results = db.GqlQuery("SELECT * FROM Project WHERE name = :1 ", projectName).fetch(1)
if len(results) > 0:
self.response.out.write("Error: The name " + projectName + " is already in use by another project.")
return
try:
logging.info("Creating project named " + projectName)
newProject = ec.Project(name = projectName, description = "", imagePath = "", protected = protect)
newProject.put()
self.response.out.write("Success")
except db.BadValueError, reason:
self.response.out.write("Error: Bad value error - " + str(reason))
except Exception, reason:
self.response.out.write("Error: Unexpected exception - " + str(reason))
class EditProject(webapp.RequestHandler):
def post(self):
projectKey = self.request.get("projectKey")
projectName = self.request.get("projectName")
if projectKey != None and len(projectKey.strip()) > 0: #just show the form, don't process anything
project = db.get(projectKey)
projectName = project.name
logging.info("updating project " + project.name)
elif projectName != None and len(projectName.strip()) > 0: #just show the form, don't process anything
results = db.GqlQuery("SELECT * FROM Project WHERE name = :1 ", projectName).fetch(1)
if len(results) > 0:
project = results[0]
if project is None:
templateValues = { "error" : "Could not find project with key " + str(projectKey) + " and/or name " + str(projectName) }
self.response.out.write(unicode(templateValues))
return
project.description = self.request.get("projectDescription")
#project.image = db.Blob(self.request.get("projectImg"))
logging.info(self.request.get("removeImg"))
if self.request.get("removeImg") != "":
project.removeImage()
if self.request.get("projectImg") != "":
img_tmp = images.Image(self.request.get("projectImg"))
logging.info("image width = " + str(img_tmp.width) + " image height = " + str(img_tmp.height))
if (img_tmp.width > 300) | (img_tmp.height > 200):
logging.info("resizing image")
img_tmp = images.resize(self.request.get("projectImg"), 300, 200)
else :
logging.info("not resizing image")
img_tmp = self.request.get("projectImg")
project.saveImage('image', db.Blob(img_tmp))
project.put()
self.redirect("/project.html?key=" + str(project.key()))
return
class ProjectImage(webapp.RequestHandler):
def get(self):
logging.info("Getting Project Image")
projectKey = self.request.get("projectKey")
projectName = self.request.get("projectName")
if projectKey != None and len(projectKey.strip()) > 0: #just show the form, don't process anything
project = db.get(projectKey)
projectName = project.name
logging.info("updating project " + project.name)
elif projectName != None and len(projectName.strip()) > 0: #just show the form, don't process anything
results = db.GqlQuery("SELECT * FROM Project WHERE name = :1 ", projectName).fetch(1)
if len(results) > 0:
project = results[0]
image = project.getImage()
if image is not None:
self.response.headers['Content-Type'] = 'image/gif'
self.response.out.write(str(image))
class ProjectMenu(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("key")
projectName = self.request.get("name")
templateValues = {}
project = None
if projectKey != None and len(projectKey.strip()) > 0:
project = db.get(projectKey)
projectName = project.name
elif projectName != None and len(projectName.strip()) > 0: #just show the form, don't process anything
results = db.GqlQuery("SELECT * FROM Project WHERE name = :1 ", projectName).fetch(1)
if len(results) > 0:
project = results[0]
if project is None:
templateValues = { "error" : "Could not find project with key " + str(projectKey) + " and/or name " + str(projectName) + ", <a href=\"http://www.epicollect.net/create1.html\">Create a new project?</a>" }
else:
form = project.getLatestForm()
templateValues["projectName"] = project.name
if users.get_current_user() is None:
templateValues["loginLink"] = " | <a href=\"" + users.create_login_url(self.request.uri) + "\" >Login</a>"
else :
templateValues["loginLink"] = " | <a href=\"" + users.create_logout_url(self.request.uri) + "\" >Logout</a>"
if project.protected and form.user != users.get_current_user():
templateValues = { "error" : "You do not have permission to view this project " + ("<a href=\"" + users.create_login_url('/project.html?name=' + projectName) + "\">Login</a>" if users.get_current_user() is None else ""), "projectName" : str(project.name), "projectKey" : "", "description" : "", "hasImage" : ""}
else :
if project.image is not None:
templateValues["projectImage"] = "<img src= \"/projectImage?projectKey=" + str(project.key()) + "\" />"
if form is not None:
templateValues["projectDescription"] = project.description
templateValues["getFormLink"] = "<a href=\"/getForm?projectKey=" + str(project.key()) + "\" >View form definition</a>"
templateValues["userMenu"] = "<tr><td colspan=\"2\" bgcolor=\"#CCCCCC\"><strong>View Data</strong></td></tr><tr><td width=\"50%\" align=\"center\"><a href=\"/listEntries?projectKey=" + str(project.key()) + "\"><img src=\"http://www.epicollect.net/website/images/projecthome/form_view.png\" alt=\"form\" width=\"98\" height=\"70\"></a></td><td width=\"50%\" align=\"center\"><a href=\"/showMap?projectKey=" + str(project.key()) + "\"><img src=\"http://www.epicollect.net/website/images/projecthome/map.png\" alt=\"map\" width=\"98\" height=\"70\"></a></td></tr><tr><td width=\"50%\" align=\"center\"><a id=\"listEntries\" href=\"/listEntries?projectKey=" + str(project.key()) + "\">Browse Project Data</a></td><td width=\"50%\" align=\"center\"><a id=\"showMap\" href=\"/showMap?projectKey=" + str(project.key()) + "\">Show Map </tr>"
else:
templateValues["projectDescription"] = "<p>The url of this project's homepage is <a href=\"http://www.epicollect.net/project.html?name=" + project.name + "\">http://www.epicollect.net/project.html?name=" + project.name + "</a><p>Congratulations, you have now created your project wesbite. Before submitting data you must create a Form - to do this click the 'create/Edit Project Form Icon below. For instructions please click the link at the top left of this page.</p><p>You can also add some explanatory text and upload an image which will appear here by clicking the Update Project description and picture icon.</p>"
if form is None or form.user == users.get_current_user() :
templateValues["adminMenu"] = "<tr><td colspan=\"2\" bgcolor=\"#CCCCCC\"><strong>Project Administration</strong></td></tr><tr><td width=\"50%\" align=\"center\"><a href=\"/createOrEditForm.html?projectKey=" + str(project.key()) + "\"><img src=\"http://www.epicollect.net/website/images/projecthome/form_small.png\" alt=\"form\" width=\"98\" height=\"70\"></a></td><td width=\"50%\" align=\"center\"><a href=\"/ProjectDescription.html?projectKey=" + str(project.key()) + "\"><img src=\"http://www.epicollect.net/website/images/projecthome/homepage_update.png\" alt=\"map\" width=\"98\" height=\"70\"></a></td></tr><tr><td width=\"50%\" align=\"center\"><a id=\"listEntries\" href=\"/createOrEditForm.html?projectKey=" + str(project.key()) + "\">Create or Edit Project Form</a></td><td width=\"50%\" align=\"center\"><a id=\"updateProject\" href=\"/ProjectDescription.html?projectKey=" + str(project.key()) + "\">Update Homepage description and picture</tr>"
path = os.path.join(os.path.dirname(__file__), 'html/project.html')
content = template.render(path, templateValues)
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(unicode(content))
class ProjectDescription(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("projectKey")
projectName = self.request.get("projectName")
templateValues = {}
project = None
if projectKey != None and len(projectKey.strip()) > 0: #just show the form, don't process anything
project = db.get(projectKey)
projectName = project.name
elif projectName != None and len(projectName.strip()) > 0: #just show the form, don't process anything
results = db.GqlQuery("SELECT * FROM Project WHERE name = :1 ", projectName).fetch(1)
if len(results) > 0:
project = results[0]
if project is None:
templateValues = { "error" : "Could not find project with key " + str(projectKey) + " and/or name " + str(projectName) }
return
form = project.getLatestForm()
templateValues["projectName"] = project.name
if project.protected and form.user != users.get_current_user():
templateValues = { "error" : "You do not have permission to view this project " + ("<a href=\"" + users.create_login_url('/project.html?name=' + projectName) + "\">Login</a>" if users.get_current_user() is None else ""), "projectName" : str(project.name), "projectKey" : "", "description" : "", "hasImage" : ""}
else :
templateValues["projectDescription"] = project.description
templateValues["getFormLink"] = "<a href=\"/getForm?projectKey=" + str(project.key()) + "\" >View form definition</a>"
if project.image is not None:
templateValues["projectImage"] = "<img style=\"float: right;\" src= \"/projectImage?projectKey=" + str(project.key()) + "\" />"
path = os.path.join(os.path.dirname(__file__), 'html/ProjectDescription.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class ProjectHome(webapp.RequestHandler):
def post(self):
projectKey = self.request.get("projectKey")
projectName = self.request.get("projectName")
templateValues = {}
project = None
if projectKey != None and len(projectKey.strip()) > 0: #just show the form, don't process anything
project = db.get(projectKey)
projectName = project.name
elif projectName != None and len(projectName.strip()) > 0: #just show the form, don't process anything
results = db.GqlQuery("SELECT * FROM Project WHERE name = :1 ", projectName).fetch(1)
if len(results) > 0:
project = results[0]
if project is None:
templateValues = { "error" : "Could not find project with key " + str(projectKey) + " and/or name " + str(projectName) }
self.response.out.write(unicode(templateValues))
return
form = project.getLatestForm()
if not project.protected or form is None or (project.protected and form.user == users.get_current_user()) :
templateValues = { "error" : "none", "projectName" : str(project.name), "projectKey" : str(project.key()), "description" : str(project.description), "hasImage" : str(project.hasImage())}
if form is None:
templateValues["hasForm"] = "false"
else:
templateValues["hasForm"] = "true"
formHandler = ec.FormHandler()
#TODO: check that the user is OK
logging.info("Returning project-home values as JSON: " + str(templateValues))
templateValues["loginUrl"] = users.create_login_url(self.request.uri.replace('project.asp', 'project.html?name=' + projectName));
else:
templateValues = { "error" : "You do not have permission to view this project " + ("<a href=\"" + users.create_login_url('/project.html?name=' + projectName) + "\">Login</a>" if users.get_current_user() is None else ""), "projectName" : str(project.name), "projectKey" : "", "description" : "", "hasImage" : ""}
self.response.out.write(unicode(templateValues))
return
class DoCreateOrEditForm(webapp.RequestHandler):
def post(self):
#ticketHandler=ec.TicketHandler()
#ticketError = ticketHandler.validate(self.request.get("ticket"), self.request.get("projectKey"))
#if ticketError is not None:
# self.response.out.write("Error: "+ticketError)
# return
if users.get_current_user() is None:
self.response.out.write(unicode("Error: You are not logged in <a href=\"" + users.create_login_url('/project.html?name=' + projectName) + "\">Login</a>"))
return
projectKey = self.request.get("projectKey")
projectName = self.request.get("projectName")
formKey = self.request.get("formKey")
formName = self.request.get("formName")
versionNumber = self.request.get("formVersion")
allowDownloadEdits = self.request.get("allowDownloadEdits")
xml = self.request.get("inputXML")
logging.info("form xml is " + str(xml))
project = None
form = None
if formKey != None and len(formKey.strip()) > 0:
form = db.get(formKey)
project = form.project
else:
if projectKey != None and len(projectKey.strip()) > 0:
project = db.get(projectKey)
projectName = project.name
elif projectName != None and len(projectName.strip()) > 0:
projects = db.GqlQuery("SELECT * FROM Project WHERE name = :1", projectName).fetch(1)
if len(projects) > 0:
project = projects[0]
success = ''
formHandler = ec.FormHandler()
error = formHandler.validateNameAndVersion(form, formName, versionNumber)
#If 'XML' button checked, test that the XML is minimally valid
if error != None:
self.response.out.write(unicode("Error: " + error))
return
error = formHandler.validateXML(xml)
if error != None:
self.response.out.write(unicode("XML validation Error: " + error))
return
form = formHandler.saveXML(xml, formName, versionNumber, project, allowDownloadEdits, sendEmail = True)
self.response.out.write(unicode(form.key()))
class GetFormXML(webapp.RequestHandler):
def get(self):
formKey = self.request.get("formKey")
formName = self.request.get("name")
formVersion = self.request.get("version")
projectKey = self.request.get("projectKey")
projectName = self.request.get("projectName")
formHandler = ec.FormHandler()
xml = formHandler.getFormXML(formKey, formName, formVersion, projectKey, projectName)
logging.info("Responding to form request with xml:\n" + xml)
self.response.headers['Content-Type'] = "text/xml"
self.response.out.write(unicode(xml))
class ShowFormHelp(webapp.RequestHandler):
def get(self):
templateValues = {}
path = os.path.join(os.path.dirname(__file__), 'html/formBuilderHelp.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class GetEntries(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("projectKey")
project = db.get(projectKey)
#logging.info("getting for for " + projectKey + " " + project.name)
form = project.getLatestForm()
if project.protected and form.user != users.get_current_user():
self.response.out.write("[]")
return;
formHandler = ec.FormHandler()
offset = int(self.request.get("offset"))
num = int(self.request.get("num"))
if form is not None:
#logging.info("got form")
fieldNames = formHandler.getFields(form)
#logging.info("form has " + str(len(fieldNames)) + " fields")
entries = db.GqlQuery("Select * FROM Entry WHERE projectName = :1", project.name)
entryHandler = ec.EntryHandler()
i = 0
count = 0
self.response.out.write("[")
for ent in entries:
j = 0
if count == num :
#self.response.out.write("break")
break
if i < offset :
i += 1
#self.response.out.write("continue %s" % i)
continue
photoUrl = "http://epicollectserver.appspot.com/static/no_image.png"
if ent.hasPhoto():
if re.match("^[A-Za-z0-9=_-]*$", ent.photoPath) is None:
photoUrl = 'showImageWithKey?imageKey=' + str(ent.key())
else:
logging.info("getting blobstore image")
photoUrl = 'showImageWithKey?imageKey=' + str(ent.photoPath)
if count > 0:
self.response.out.write(",")
self.response.out.write("{")
self.response.out.write("\"key\" : \"" + str(ent.key()) + "\",\"title\" : \"" + entryHandler.getTitleStringFor(ent).replace("\n", "<br />").replace("'", "`").replace("\r", "").replace("\"", "\\\"").replace("\t", " ") + "\",")
self.response.out.write("\"dateCreated\" : \"" + str(ent.timeCreated) + "\",\"latitude\" : \"" + str(ent.location.lat) + "\",")
self.response.out.write("\"longitude\" : \"" + str(ent.location.lon) + "\",")
self.response.out.write("\"altitude\" : \"" + str(ent.altitude) + "\",")
self.response.out.write("\"photo\" : \"" + photoUrl + "\",")
self.response.out.write("\"image\" : \"<img src=\\\"" + photoUrl + "\\\" height=\\\"32\\\"/>\",")
self.response.out.write("\"deviceId\" : \"" + str(ent.deviceID) + "\",")
self.response.out.write("\"entryId\" : \"" + str(ent.entryID) + "\",")
self.response.out.write("\"lastEdited\" : \"" + str(ent.lastEdited) + "\",")
self.response.out.write("\"timeUploaded\" : \"" + str(ent.timeUploaded) + "\",")
self.response.out.write("\"enterpriseName\" : \"" + str(ent.enterpriseName) + "\",")
self.response.out.write("\"projectName\" : \"" + ent.projectName + "\",")
vals = dict(zip(ent.fieldNames, ent.fieldValues))
j = 0
while j < len(fieldNames):
field = unicode(fieldNames[j])
if(j > 0):
self.response.out.write(",")
if vals.has_key(field):
self.response.out.write("\"%s\":\"%s\"" % (field, vals[field].replace("\n", "<br />").replace("'", "`").replace("\r", "").replace("\"", "\\\"").replace("\t", " ")))
else:
self.response.out.write("\"%s\":\"\"" % (field))
j += 1
self.response.out.write("}")
i += 1
count += 1
# while j<len(fieldNames):
# k=0
# added = False
# while k<len(entries[i].fieldNames):
# logging.info(entries[i].fieldNames[k] +" " + fieldNames[j])
# if entries[i].fieldNames[k] == fieldNames[j]:
# strOut += "\"" + fieldNames[j] + "\":\"" + entries[i].fieldValues[k].replace("\n", "<br />").replace("'", "\\'").replace("\r", "").replace("\"", "\\\"").replace("\t", " ") + "\""
# added=True
# k+=1
# if not added:
# strOut += "\"" + fieldNames[j] + "\":\"\""
# if j < len(fieldNames) - 1:
# strOut += ","
# j+=1
# strOut += "}"
# if i < len(entries) - 1:
# strOut += ","
# i+=1
self.response.out.write("]")
class MiniDomTest(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("projectKey")
project = db.get(projectKey)
#logging.info("getting for for " + projectKey + " " + project.name)
form = project.getLatestForm()
formHandler = ec.FormHandler()
self.response.out.write(unicode(formHandler.getFullFields(form)))
class ListEntries(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("projectKey")
project = db.get(projectKey)
#logging.info("getting for for " + projectKey + " " + project.name)
form = project.getLatestForm()
formHandler = ec.FormHandler()
fields = []
if form is not None:
#logging.info("got form")
fields = formHandler.getFullFields(form)
isOwner = False
if users.get_current_user() and form.user:
logging.info("List Entries : current user " + users.get_current_user().nickname() + " form user " + form.user.nickname())
isOwner = users.get_current_user() == form.user
upload_url = blobstore.create_upload_url('/upload_photo')
templateValues = {
'project' : project,
'fields' : fields,
'isOwner' : isOwner,
'projectKey' : projectKey,
'uploadurl' : upload_url
}
path = os.path.join(os.path.dirname(__file__), 'html/listEntries.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class ListEntriesCSV(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("projectKey")
project = db.get(projectKey)
#logging.info("getting for for " + projectKey + " " + project.name)
form = project.getLatestForm()
formHandler = ec.FormHandler()
results = []
self.response.headers['Content-Type'] = 'text/csv'
n = int(self.request.get("limit")) if self.request.get("limit") != '' else None #number of entries
o = int(self.request.get("offset")) if self.request.get("offset") != '' else 0 #how many entries to skip
if n is not None:
n = n + o;
i = 0 #iterator
if form is not None:
#logging.info("got form")
fieldNames = formHandler.getFields(form)
#logging.info("form has " + str(len(fieldNames)) + " fields")
entries = db.GqlQuery("Select * FROM Entry WHERE projectName = :1", project.name)
entryHandler = ec.EntryHandler()
j = 0
self.response.out.write('key,dateCreated,latitude,longitude,altitude,deviceId,entryId,lastEdited,timeUploaded,projectName,photo,')
while j < len(fieldNames):
self.response.out.write(fieldNames[j] + ',')
j += 1
self.response.out.write('\r\n')
for ent in entries:
if o is not None and i < o:
#self.response.out.write('skip - offset\r\n')
i += 1
continue
if n is not None and i >= n :
#self.response.out.write('skip - limit\r\n')
i += 1
break
self.response.out.write(getString(ent.key()) + ',' + getString(ent.timeCreated) +
',' + getString(ent.location.lat) + ',' + getString(ent.location.lon) + ',' + getString(ent.altitude) +
',' + getString(ent.deviceID) + ',' + getString(ent.entryID) + ',' + getString(ent.lastEdited) + ',' + getString(ent.timeUploaded) +
',' + getString(ent.projectName) + ',"http://epicollectserver.appspot.com/showImageWithKey?imageKey=' + getString(ent.key()) + '",')
vals = dict(zip(ent.fieldNames, ent.fieldValues))
j = 0
while j < len(fieldNames):
field = unicode(fieldNames[j])
if vals.has_key(field):
self.response.out.write('"%s",' % vals[field].replace('\n', ' ').replace('"', '`'))
else:
self.response.out.write('"",')
j += 1
self.response.out.write('\r\n')
#if i == 100:
# p+=1
# entries = db.GqlQuery("Select * FROM Entry WHERE projectName = :1", project.name).fetch(100, 100 * p)
# if len(entries) > 0:
# i = 0;
i += 1
def getString(obj):
if obj is None:
return ' '
else:
try:
return str(obj).replace('"', '""')
except:
return ' '
class EntryDetails(webapp.RequestHandler):
def get(self):
entryKey = self.request.get("key")
entry = db.get(entryKey)
fields = []
i = 0
while i < len(entry.fieldNames):
value = entry.fieldValues[i]
if value is not None and len(value) > 0:
aDict = { 'name' : entry.fieldNames[i], 'value' : value }
fields.append(aDict)
i += 1
i = 0
while i < len(entry.longFieldNames):
aDict = { 'name' : entry.longFieldNames[i], 'value' : entry.longFieldValues[i] }
fields.append(aDict)
i += 1
templateValues = {
'title' : entry.timeCreated,
'entry' : entry,
'fields' : fields,
}
path = os.path.join(os.path.dirname(__file__), 'html/entryDetails.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class SaveWebEntry(webapp.RequestHandler):
def get(self):
self.post();
def post(self):
argsVals = {}
myArgs = self.request.arguments()
for arg in myArgs:
val = self.request.get(arg)
argsVals[arg] = val
#logging.info("Received entry data via web: "+str(argsVals))
entryHandler = ec.EntryHandler()
responseText = entryHandler.saveFromWeb(argsVals)
self.response.out.write(responseText)
class SaveEntry(webapp.RequestHandler):
def get(self):
self.post();
def post(self):
argsVals = {}
myArgs = self.request.arguments()
for arg in myArgs:
val = self.request.get(arg)
argsVals[unicode(arg)] = unicode(val)
logging.info("Received entry data: " + str(argsVals))
entryHandler = ec.EntryHandler()
responseText = entryHandler.saveEntry(argsVals)
self.response.out.write(responseText)
class GetSavedEntries(webapp.RequestHandler):
def get(self):
enterpriseName = self.request.get("enterprise")
projectName = self.request.get("project")
if projectName is None or len(projectName) == 0:
projectName = self.request.get("application") # legacy naming support
entryHandler = ec.EntryHandler()
xml = entryHandler.getSavedEntriesXML(self.request.host_url, enterpriseName, projectName)
logging.info("Returning entry data: " + xml)
self.response.headers['Content-Type'] = 'text/xml'
self.response.out.write(xml)
class RemoveEntry(webapp.RequestHandler):
def get(self):
self.post()
def post(self):
curUser = users.get_current_user()
if curUser:
projectKey = self.request.get("projectKey")
project = db.get(projectKey)
#logging.info("getting for for " + projectKey + " " + project.name)
form = project.getLatestForm()
if curUser == form.user or curUser.is_current_user_admin():
if self.request.get("entryKey"):
entry = ec.Entry.get(self.request.get("entryKey"))
entry.delete()
self.response.out.write({"success":True, "msg":""})
else:
self.response.out.write({"success":False, "msg":"You must specify and entry key to remove an entry"})
else:
self.response.out.write({"success":False, "msg":"You do not have permission to remove entries from this project"})
else:
self.response.out.write({"success":False, "msg":"You must be logged in to remove an entry"})
class StoreImage(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('photo-path')
blob_info = upload_files[0]
self.redirect('/imageStored?key=' + str(blob_info.key()))
class ImageStored(webapp.RequestHandler):
def get(self):
self.response.out.write('{"success": true, "msg" :"' + str(self.request.get("key")) + '", "newUrl":"' + blobstore.create_upload_url('/upload_photo') + '"}')
class DisplayImage(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
class SaveImage(webapp.RequestHandler):
def post(self):
entryHandler = ec.EntryHandler()
logging.info("Saving image with arguments " + str(self.request.arguments()))
for arg in self.request.arguments():
file = self.request.get(arg)
response = entryHandler.saveImage(arg, file)
self.response.out.write(response)
class CountImages(webapp.RequestHandler):
def get(self):
query = ec.Entry.all()
query.filter("image !=", None)
results = query.fetch()
self.response.out.write("Found " + str(len(results)) + " images")
class ListImages(webapp.RequestHandler):
def get(self):
query = ec.Entry.all()
query.filter("image !=", None)
for entry in query:
imageCount += 1
self.response.out.write('<a href="' + entry.getImageUrlPath() + '">' + str(imageCount) + "</a>\n")
class ShowImageWithKey(webapp.RequestHandler):
def get(self):
key = self.request.get('imageKey').replace('%3D', '=')
entryHandler = ec.EntryHandler()
try:
image = entryHandler.getImageByEntryKey(key)
try:
if self.request.get('width') is not None:
width = int(self.request.get('width'))
else:
width = 300
if self.request.get('height') is not None:
height = int(self.request.get('height'))
else:
height = 200
img = images.resize(image, width, height)
if image is not None:
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(img)
except:
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(str(image))
except:
self.redirect('/displayImage/' + key)
class ShowImageNamed(webapp.RequestHandler):
def get(self):
fileName = self.request.get('fileName')
entryHandler = ec.EntryHandler()
image = entryHandler.getImageByImageName(fileName)
if image is not None:
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(image)
else:
self.response.out.write("Image with file name " + str(fileName) + " not found")
class ShowMap(webapp.RequestHandler):
def get(self):
project = None
projectKey = self.request.get("projectKey")
projectName = self.request.get("projectName")
if projectKey != None and len(projectKey.strip()) > 0:
project = db.get(projectKey)
if project is not None:
projectName = project.name
elif projectName != None and len(projectName.strip()) > 0:
projects = db.GqlQuery("SELECT * FROM Project WHERE name = :1", projectName).fetch(1)
if len(projects) > 0:
project = projects[0]
if project is None:
self.response.out.write("Error: no valid project key or name passed in - cannot show map")
return
form = project.getLatestForm()
formKey = self.request.get("formKey")
if formKey != None and len(formKey.strip()) > 0:
form = db.get(formKey)
formHandler = ec.FormHandler()
doTest = len(self.request.get("doTest")) > 0
if form is None:
if doTest:
form = formHandler.getTestForm()
else:
self.response.out.write("Error: no form found for project - cannot show map")
return
if doTest:
entryHandler = ec.EntryHandler()
entries = entryHandler.generateTestEntriesFor(form, 50)
fields = []
fields.append("time")
fields.extend(formHandler.getFields(form))
chartables, markerField = formHandler.getChartables(form)
logging.info("Chartables: " + str(chartables))
templateValues = {
"project" : project,
"fields" : fields,
"title" : formHandler.getTitle(form),
"subtitle" : formHandler.getSubtitle(form),
"chartables" : chartables,
"markerField" : markerField,
}
if doTest:
templateValues["doTestUrlSuffix"] = "&doTest=true"
path = os.path.join(os.path.dirname(__file__), 'html/showMap.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class GetMapXML(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("projectKey")
if projectKey != None and len(projectKey.strip()) > 0:
project = db.get(projectKey)
handler = ec.EntryHandler()
number = self.request.get("number")
offset = self.request.get("offset")
#logging.info("number = " + str(number) + " offset = " + str(offset))
try:
number = int(number)
except (TypeError, ValueError):
#logging.warning("Number could not be parsed")
number = 1000
try:
offset = int(offset)
except (TypeError, ValueError):
#logging.warning("Offset could not be parsed")
offset = 0
#logging.info("number = " + str(number) + " offset = " + str(offset))
doTest = len(self.request.get("doTest")) > 0
#xml = handler.getMapXML(project, self.request.host_url, number, offset, testFlag = doTest
self.response.headers['Content-Type'] = "text/xml"
entries = db.GqlQuery("Select * FROM Entry WHERE projectName = '%s' LIMIT %s OFFSET %s" % (project.name, number, offset))
self.response.out.write('<epicollect>')
for entry in entries:
xml="<Record>"
xml+="<id>"+entry.entryID+"</id>"
xml+="<f_time>"+str(entry.timeCreated)+"</f_time>"
if entry.timeCreated is not None:
xml+="<timeMillis>"+repr((time.mktime(entry.timeCreated.timetuple()) * 1000) + (entry.timeCreated.microsecond / 1000))+"</timeMillis>"
else:
xml+="<timeMillis>0</timeMillis>"
xml+="<lastEdited>"+str(entry.lastEdited)+"</lastEdited>"
if entry.lastEdited is not None:
xml+="<lastEditedMillis>"+repr((time.mktime(entry.lastEdited.timetuple()) * 1000) + (entry.lastEdited.microsecond / 1000))+"</lastEditedMillis>"
else:
xml+="<lastEditedMillis>0</lastEditedMillis>"
xml+="<latitude>"+str(entry.location.lat)+"</latitude>"
xml+="<longitude>"+str(entry.location.lon)+"</longitude>"
xml+="<altitude>"+str(entry.altitude)+"</altitude>"
xml+="<ecPhotoPath>"+entry.getImageUrlPath(self.request.host_url)+"</ecPhotoPath>"
if entry.userEmail is not None:
xml+="<userEmail>"+entry.userEmail+"</userEmail>"
i = 0
for fieldName in entry.fieldNames:
fn = fieldName.replace(' ', '_').replace('?','')
xml+="<f_"+fn+">"+entry.fieldValues[i].replace('&', '&')+"</f_"+fn+">"
i+=1
self.response.out.write(xml)
self.response.out.write('</Record>')
self.response.out.write('</epicollect>')
#self.response.out.write(xml)
else:
self.response.out.write("No project indicated - you must pass in a project key")
class GetMapKML(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("projectKey")
if projectKey != None and len(projectKey.strip()) > 0:
project = db.get(projectKey)
handler = ec.EntryHandler()
doTest = len(self.request.get("doTest")) > 0
kml = handler.getMapKML(project, self.request.host_url, testFlag = doTest)
self.response.headers['Content-Type'] = "application/vnd.google-earth.kml+xml"
self.response.headers['Content-Disposition'] = 'attachment; filename=' + str(time.time()) + '.kml'
self.response.out.write(kml)
else:
self.response.out.write("No project indicated - you must pass in a project key")
class GetKMLFeed(webapp.RequestHandler):
def get(self):
projectKey = self.request.get("projectKey")
if projectKey != None and len(projectKey.strip()) > 0:
self.response.headers['Content-Type'] = "application/vnd.google-earth.kml+xml"
self.response.headers['Content-Disposition'] = 'attachment; filename=' + str(time.time()) + '.kml'
self.response.out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\"><NetworkLink><name>EpiCollect</name><open>1</open><Link><href>" + self.request.url.replace("getKmlFeed", "getMapKml") + "</href><refreshMode>onInterval</refreshMode><refreshInterval>30</refreshInterval></Link></NetworkLink></kml>")
else:
self.response.out.write("No project indicated - you must pass in a project key")
class ValidateTicket(webapp.RequestHandler):
def get(self):
#if the user's logged in, that's OK too
logging.info(self.request.get("projectKey"))
if self.request.get("projectKey") == "":
projects = db.GqlQuery("SELECT * FROM Project WHERE name = :1", self.request.get("projectName")).fetch(1)
if len(projects) > 0:
project = projects[0]
else:
self.response.set_status(401, "No Project found")
else:
project = db.get(self.request.get("projectKey"))
ticketHandler = ec.TicketHandler()
ticketError = ticketHandler.validate(self.request.get("ticket"), project.key())
if ticketError is None:
self.response.out.write("ticket validated")
else:
self.response.set_status(401, ticketError)
self.response.out.write(ticketError)
class LoginIframe(webapp.RequestHandler):
def get(self):
ticketString = self.request.get("ticket")
ticket = ec.Ticket(id = ticketString, user = users.User())
ticket.put()
logging.info("Created ticket with ID " + ticket.id + " for user " + str(ticket.user))
self.redirect(self.request.host_url + "/login-successful.html?destination=" + self.request.get("destination") + "&ticket=" + ticketString + "&projectKey=" + self.request.get("projectKey") + "&projectName=" + self.request.get("projectName"))
class RunServerTest(webapp.RequestHandler):
def get(self):
tester = servertest.ECServerTester()
message = tester.runServerTest()
self.response.out.write(message)
class RunLoadTest(webapp.RequestHandler):
def get(self):
tester = servertest.ECServerTester()
message = tester.runLoadTest()
self.response.out.write(message)
class DoTweak(webapp.RequestHandler):
def get(self):
forms = db.GqlQuery("SELECT * FROM Form WHERE name = :1", "UrbanBeasts").fetch(1)
form = forms[0]
formHandler = ec.FormHandler()
requestError = formHandler.initiateApkRequest(form)
self.response.out.write(requestError)
class AddEnterprise(webapp.RequestHandler):
def get(self):
error = ''
newName = self.request.get("enterpriseName")
existing = db.GqlQuery("Select * FROM Enterprise WHERE name = :1", newName).fetch(1)
if len(existing) > 0:
error = "Sorry - the name " + newName + " is already in use."
elif len(newName.strip()) == 0:
error = "You must enter a name."
else:
newEnt = ec.Enterprise(name = newName)
newEnt.put()
results = db.GqlQuery("Select * FROM Enterprise")
templateValues = {
'error' : error,
'enterprises' : results,
}
path = os.path.join(os.path.dirname(__file__), 'html/index.html')
self.response.out.write(unicode(template.render(path, templateValues)))
class AddProject(webapp.RequestHandler):
def get(self):
error = ''
enterpriseKey = self.request.get("enterpriseKey")
enterprise = db.get(enterpriseKey)
protected = self.request.get("protect") == "true"
newName = self.request.get("projectName")
existing = db.GqlQuery("Select * FROM Project WHERE enterprise = :1 AND name = :2", enterprise, newName).fetch(1)
if len(existing) > 0:
error = "Sorry - the project name " + newName + " is already in use for this enterprise."
elif len(newName.strip()) == 0:
error = "You must enter a name."
else:
newProj = ec.Project(enterprise = enterprise, name = newName, protected = protected)
newProj.put()
results = db.GqlQuery("Select * FROM Project WHERE enterprise = :1", enterprise)
templateValues = {
'error' : error,
'enterprise' : enterprise,
'projects' : results
}
path = os.path.join(os.path.dirname(__file__), 'html/listProjects.html')