-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_server.py_prep_Python
More file actions
854 lines (627 loc) · 25.5 KB
/
app_server.py_prep_Python
File metadata and controls
854 lines (627 loc) · 25.5 KB
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
###########################################################
#
# Copyright (c) 2005, Southpaw Technology
# All Rights Reserved
#
# PROPRIETARY INFORMATION. This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
__all__ = ['AppServer', 'BaseAppServer', 'XmlrpcServer', 'get_site_import', 'get_main_tab_wdg']
import re, types
from pyasm.common import *
from pyasm.search import SObjectFactory, DbContainer, ExceptionLog, SearchType, Search, Sql, DatabaseException
from pyasm.security import *
from pyasm.biz import PrefSetting
from web_container import WebContainer
from widget import Widget, Html
from web_app import WebApp
from command_delegator import CommandDelegator
from event_container import EventContainer
from web_tools import *
from html_wdg import *
from url_security import *
from translation import *
import os, cStringIO
try:
import profile, pstats
except ImportError:
PROFILE = False
else:
PROFILE = True
class AppServerException(Exception):
pass
class BaseAppServer(Base):
'''The base application server class that handles the top level processing
of a given page. Different applications will derive off of this class
to implement how the resulting html will go to the server'''
ONLOAD_EVENT = "body_onload"
if PROFILE:
profile.object = None
def __init__(my):
my.top = None
my.hash = None
super(BaseAppServer,my).__init__()
def writeln(my, string):
my.buffer.write(string)
def get_display(my):
profile_flag = False
if profile_flag:
BaseAppServer.profile.object = my
if os.name == 'nt':
path = "C:/sthpw/profile"
else:
path = "/tmp/sthpw/temp/profile"
profile.run( "from pyasm.web.app_server import BaseAppServer; BaseAppServer.profile()", path)
p = pstats.Stats(path)
p.sort_stats('cumulative').print_stats(30)
print "*"*30
p.sort_stats('time').print_stats(30)
else:
my.execute()
value = WebContainer.get_buffer().getvalue()
WebContainer.clear_buffer()
return value
def profile():
my = BaseAppServer.profile.object
my.execute()
profile = staticmethod(profile)
def execute(my):
my.buffer = cStringIO.StringIO()
try:
try:
# clear the main container for this thread
Container.create()
# clear the buffer
WebContainer.clear_buffer()
# initialize the web environment object and register it
adapter = my.get_adapter()
WebContainer.set_web(adapter)
# get the display
my._get_display()
except SetupException, e:
'''Display setup exception in the interface'''
print "Setup exception: ", e.__str__()
DbContainer.rollback_all()
ExceptionLog.log(e)
my.writeln("<h3>Tactic Setup Error</h3>" )
my.writeln("<pre>" )
my.writeln(e.__str__() )
my.writeln("</pre>" )
except DatabaseException, e:
from tactic.ui.startup import DbConfigPanelWdg
config_wdg = DbConfigPanelWdg()
my.writeln("<pre>")
my.writeln(config_wdg.get_buffer_display())
my.writeln("</pre>")
except Exception, e:
stack_trace = ExceptionLog.get_stack_trace(e)
print stack_trace
my.writeln("<pre>")
my.writeln(stack_trace)
my.writeln("</pre>")
# it is possible that the security object was not set
security = Environment.get_security()
if not security:
security = Security()
WebContainer.set_security(security)
log = None
# ensure that database connections are rolled back
try:
DbContainer.rollback_all()
except Exception, e2:
print "Error: Could not rollback: ", e2.__str__()
my.writeln("Error: Could not rollback: '%s'" % e2.__str__() )
stack_trace = ExceptionLog.get_stack_trace(e2)
print stack_trace
my.writeln("<pre>")
my.writeln(stack_trace)
my.writeln("</pre>")
raise e
#return
try:
# WARNING: if this call causes an exception, the error
# will be obscure
log = ExceptionLog.log(e)
except Exception, e2:
print "Error: Could not log exception: ", e2.__str__()
my.writeln("Error '%s': Could not log exception" % e2.__str__() )
stack_trace = ExceptionLog.get_stack_trace(e2)
print stack_trace
my.writeln("<pre>")
my.writeln(stack_trace)
my.writeln("</pre>")
return
my.writeln("<pre>")
my.writeln("An Error has occurred. Please see your Tactic Administrator<br/>")
my.writeln( "Error Message: %s" % log.get_value("message") )
my.writeln("Error Id: %s" % log.get_id() )
my.writeln( log.get_value("stack_trace") )
my.writeln("</pre>")
finally:
# ensure that database connections are always closed
DbContainer.close_all()
# clear the container
Container.delete()
WebContainer.get_buffer().write( my.buffer.getvalue() )
def handle_not_logged_in(my, allow_change_admin=True):
from pyasm.widget import WebLoginWdg, BottomWdg
from tactic.ui.app import TitleTopWdg
from pyasm.biz import Project
from tactic.ui.panel import HashPanelWdg
web = WebContainer.get_web()
widget = Widget()
top = TitleTopWdg()
widget.add(top)
body = top.get_body()
body.add_gradient("background", "background", 5, -20)
body.add_color("color", "color")
reset_request = web.get_form_value('reset_request') =='true'
if reset_request:
from tactic.ui.widget import ResetPasswordWdg
top.add(ResetPasswordWdg())
else:
reset_msg = web.get_form_value('reset_msg')
if reset_msg:
web.set_form_value(WebLoginWdg.LOGIN_MSG, reset_msg)
sudo = Sudo()
try:
# get the project from the url because we are still
# in the admin project at this stage
current_project = web.get_context_name()
try:
if current_project != "default":
project = Project.get_by_code(current_project)
assert project
except Exception, e:
web_wdg = None
else:
# custom loginwidget
if not current_project or current_project == "default":
current_project = Config.get_value("install", "default_project")
if current_project and current_project != "default":
Project.set_project(current_project)
web_wdg = HashPanelWdg.get_widget_from_hash("/login", return_none=True)
if web_wdg:
web_wdg = web_wdg.get_buffer_display()
top.add(web_wdg)
else:
web_wdg = None
# display default web login
if not web_wdg:
top.add(WebLoginWdg(allow_change_admin=allow_change_admin) )
finally:
# sudo out of scope here
sudo.exit()
# create a web app and run it through the pipeline
web_app = WebApp()
web_app.get_display(widget)
return
def _get_display(my):
# set up the security object
from pyasm.security import Security, Sudo
from pyasm.biz import Project
from pyasm.web import WebContainer
web = WebContainer.get_web()
security = Security()
security = my.handle_security(security)
is_logged_in = security.is_logged_in()
# guest mode
#
allow_guest = Config.get_value("security", "allow_guest")
if allow_guest == 'true':
allow_guest = True
else:
allow_guest = False
guest_mode = Config.get_value("security", "guest_mode")
if not guest_mode:
guest_mode = 'restricted'
#allow_guest = True
#guest_mode = "full"
# if not logged in, then log in as guest
if not is_logged_in:
if not allow_guest:
return my.handle_not_logged_in()
else:
# login as guest
security = Security()
my.handle_guest_security(security)
# for here on, the user is logged in
login_name = Environment.get_user_name()
# check if the user has permission to see this project
project = web.get_context_name()
if project == 'default':
override_default = Config.get_value("install", "default_project")
if override_default:
project = override_default
if project != 'default':
security_version = get_security_version()
if security_version == 1:
default = "view"
access = security.check_access("project", project, "view", default="view")
else:
default = "deny"
key = { "code": project }
key2 = { "code": "*" }
#keys = [key]
keys = [key, key2]
access = security.check_access("project", keys, "allow", default=default)
else:
# you always have access to the default project
access = True
if not access:
if login_name == "guest":
from pyasm.widget import WebLoginWdg
msg = web.get_form_value(WebLoginWdg.LOGIN_MSG)
if not msg:
msg = "User [%s] is not allowed to see this project [%s]" % (login_name, project)
web.set_form_value(WebLoginWdg.LOGIN_MSG, msg)
return my.handle_not_logged_in(allow_change_admin=False)
else:
from pyasm.widget import WebLicenseWdg, BottomWdg, Error403Wdg
widget = Widget()
top = my.get_top_wdg()
widget.add( top )
widget.add( Error403Wdg() )
widget.add( BottomWdg() )
widget.get_display()
return
# some extra precautions in guest mode
if login_name == 'guest' and guest_mode != "full":
# show a restricted guest mode
from pyasm.widget import WebLoginWdg, BottomWdg
from tactic.ui.app import TitleTopWdg
from pyasm.biz import Project
from tactic.ui.panel import HashPanelWdg
web = WebContainer.get_web()
widget = Widget()
top = TitleTopWdg()
widget.add(top)
body = top.get_body()
body.add_gradient("background", "background", 5, -20)
body.add_color("color", "color")
# get the project from the url because we are still
# in the admin project at this stage
current_project = web.get_context_name()
try:
if current_project != "default":
project = Project.get_by_code(current_project)
assert project
except Exception, e:
web_wdg = None
else:
if not current_project or current_project == "default":
current_project = Config.get_value("install", "default_project")
if current_project and current_project != "default":
Project.set_project(current_project)
web_wdg = HashPanelWdg.get_widget_from_hash("/guest", return_none=True)
if web_wdg:
web_wdg = web_wdg.get_buffer_display()
top.add(web_wdg)
else:
web_wdg = None
if not web_wdg:
msg = "No widget for Guest defined"
web.set_form_value(WebLoginWdg.LOGIN_MSG, msg)
top.add(WebLoginWdg() )
# create a web app and run it through the pipeline
web_app = WebApp()
web_app.get_display(widget)
return
# Welcome message for first time run
is_first_run = Environment.is_first_run()
if is_first_run:
from pyasm.widget import WebLoginWdg, BottomWdg
top = my.get_top_wdg()
from tactic.ui.app import PageHeaderWdg
from tactic.ui.startup import DbConfigPanelWdg
widget = DivWdg()
widget.add( top )
widget.add( DbConfigPanelWdg() )
widget.add( BottomWdg() )
web_app = WebApp()
web_app.get_display(widget)
return
# handle licensing
license = security.get_license()
user_name = security.get_user_name()
is_licensed = license.is_licensed()
# handle url security
url_security = UrlSecurity()
html = url_security.get_display()
if html:
widget = Widget()
widget.add(html.getvalue())
widget.get_display()
return
web = WebContainer.get_web()
# FIXME: although this works, it should be cleaned up
# determine the type of request
if '/UploadServer' in web.get_request_url().to_string():
page_type = "upload"
elif web.get_form_value("ajax") != "":
page_type = "ajax"
elif web.get_form_value("dynamic_file") != "":
# this mode creates a file dynamically
page_type = "dynamic_file"
else:
page_type = "normal"
# TODO: the following could be combined into a page_init function
# provide the opportunity to set some templates
my.set_templates()
my.add_triggers()
my.init_web_container()
# install the language
#Translation.install()
widget = my.get_content(page_type)
# put an annoying alert if there is a problem with the licensed
if not is_licensed:
# to be sure, reread license. This gets around the problem
# of the extra error message when uploading a new license
license = security.reread_license()
is_licensed = license.is_licensed()
if not is_licensed:
widget.add("<script>alert('%s')</script>" % license.get_message())
# create a web app and run it through the pipeline
web_app = WebApp()
web_app.get_display(widget)
def handle_security(my, security):
# set the seucrity object
WebContainer.set_security(security)
# see if there is an override
web = WebContainer.get_web()
ticket_key = web.get_form_value("login_ticket")
# attempt to login in with a ticket
if not ticket_key:
ticket_key = web.get_cookie("login_ticket")
# We can define another place to look at ticket values and use
# that. ie: Drupal session key
session_key = Config.get_value("security", "session_key")
login = web.get_form_value("login")
password = web.get_form_value("password")
if session_key:
ticket_key = web.get_cookie(session_key)
if ticket_key:
security.login_with_session(ticket_key, add_access_rules=False)
elif login and password:
if login == "guest":
pass
else:
from pyasm.widget import WebLoginCmd
login_cmd = WebLoginCmd()
login_cmd.execute()
ticket_key = security.get_ticket_key()
elif ticket_key:
security.login_with_ticket(ticket_key, add_access_rules=False)
if not security.is_logged_in():
reset_password = web.get_form_value("reset_password") == 'true'
if reset_password:
from tactic.ui.widget import ResetPasswordCmd
reset_cmd = ResetPasswordCmd(reset=True)
try:
reset_cmd.execute()
except TacticException, e:
print "Reset failed. %s" %e.__str__()
else:
from pyasm.widget import WebLoginCmd
login_cmd = WebLoginCmd()
login_cmd.execute()
ticket_key = security.get_ticket_key()
if session_key:
web.set_cookie("login_ticket", ticket_key)
elif ticket_key:
web.set_cookie("login_ticket", ticket_key)
# set up default securities
my.set_default_security(security)
# for now apply the access rules after
security.add_access_rules()
return security
def handle_guest_security(my, security):
WebContainer.set_security(security)
security.login_as_guest()
ticket_key = security.get_ticket_key()
web = WebContainer.get_web()
web.set_cookie("login_ticket", ticket_key)
access_manager = security.get_access_manager()
xml = Xml()
xml.read_string('''
<rules>
<rule column="login" value="{$LOGIN}" search_type="sthpw/login" access="deny" op="!=" group="search_filter"/>
</rules>
''')
access_manager.add_xml_rules(xml)
def init_web_container(my):
# add the event container, initialization only
event_container = EventContainer()
WebContainer.set_event_container( event_container )
def get_content(my, request_type):
web = WebContainer.get_web()
# NOTE: is this needed anymore?
if request_type in ["upload", "dynamic_file"]:
print "DEPRECATED: dynamic file in app_server.py"
widget = Widget()
page = my.get_page_widget()
widget.add(page)
return widget
# find hash of url
my.custom_url = None
if my.hash:
hash = "/".join(my.hash)
hash = "/%s" % hash
from tactic.ui.panel import HashPanelWdg
my.custom_url = HashPanelWdg.get_url_from_hash(hash)
if my.custom_url:
content_type = my.custom_url.get_value("content_type", no_exception=True)
# TODO: we may want to handle this differently for content types
# other that text/html
return my.get_application_wdg()
def log_exception(my, exception):
import sys,traceback
tb = sys.exc_info()[2]
stacktrace = traceback.format_tb(tb)
stacktrace_str = "".join(stacktrace)
print "-"*50
print stacktrace_str
print str(exception)
print "-"*50
user_name = Environment.get_user_name()
exception_log = SObjectFactory.create("sthpw/exception_log")
exception_log.set_value("login", user_name)
exception_log.set_value("class", exception.__class__.__name__)
exception_log.set_value("message", str(exception) )
exception_log.set_value("stack_trace", stacktrace_str)
exception_log.commit()
del tb, stacktrace
#
# virtual functions
#
def set_default_security(my, security):
'''set a number of default security rules to be always implemented'''
rules = AppServerSecurityRules(security)
def get_application_wdg(my):
application = my.get_top_wdg()
# get the main page widget
# NOTE: this needs to happen after the body is put in a Container
page = my.get_page_widget()
page.set_as_top()
if type(page) in types.StringTypes:
page = StringWdg(page)
application.add(page, 'content')
return application
def get_page_widget(my):
'''get the content widget'''
return "No Content"
def get_top_wdg(my):
from tactic.ui.app import TopWdg
my.top = TopWdg()
return my.top
def get_single_widget(my, widget, minimal=True):
from pyasm.widget import BottomWdg
from tactic.ui.app import TitleTopWdg
if minimal:
top = TitleTopWdg()
else:
top = my.get_top_wdg()
container = Widget()
container.add( top )
top.add( widget )
container.add( BottomWdg() )
container.get_display()
return container
def add_triggers(my):
'''callback that enables a site to add custom triggers'''
pass
def set_templates(my):
'''callback where sobject templates can be set'''
pass
def add_onload_script(script):
''' this does not work on Login screen'''
event = WebContainer.get_event_container()
event.add_listener(BaseAppServer.ONLOAD_EVENT, script)
add_onload_script = staticmethod(add_onload_script)
class AppServerSecurityRules(object):
''' A set of rules applied at the start up of drawing of a page'''
def __init__(my, security):
assert security != None
my.security = security
my.access_manager = security.get_access_manager()
# we just need one instance of my.xml
my.xml = Xml()
my.execute()
def execute(my):
my.add_wdg_rules()
# the main administrator has no default restrictions
if 'admin' not in my.security.get_group_names():
my.add_default_rules()
def add_wdg_rules(my):
''' these rules are to be applied to all users'''
my.xml.read_string('''
<rules>
<rule category='secure_wdg' default='deny'/>
<rule category='public_wdg' access='edit'/>
</rules>
''')
my.access_manager.add_xml_rules(my.xml)
def add_default_rules(my):
''' these rules are to be applied to all but users in the admin group'''
security_version = get_security_version()
if security_version == 1:
my.xml.read_string('''
<rules>
<rule group='project' default='view'/>
<rule group='project' key='admin' access='deny'/>
<rule group='tab' key='Admin' access='deny'/>
</rules>
''')
my.access_manager.add_xml_rules(my.xml)
# NOTE: this function has to be declared after BaseAppServer
def get_app_server_class():
app_server = os.getenv('TACTIC_APP_SERVER')
if app_server == "webware":
from webware_adapter import get_app_server
elif app_server == "cherrypy":
import cherrypy
if cherrypy.__version__.startswith("3."):
from cherrypy30_adapter import get_app_server
else:
from cherrypy_adapter import get_app_server
elif app_server == "batch":
return object
else:
#raise AppServerException("Environment variable TACTIC_APP_SERVER not set")
# default to webware for now
#print "WARNING: Environment variable TACTIC_APP_SERVER not set"
#from webware_adapter import get_app_server
return object
return get_app_server()
def get_xmlrpc_server_class():
app_server = os.getenv('TACTIC_APP_SERVER')
if app_server == "webware":
from webware_adapter import get_xmlrpc_server
elif app_server == "cherrypy":
import cherrypy
if cherrypy.__version__.startswith("3."):
from cherrypy30_adapter import get_xmlrpc_server
else:
from cherrypy_adapter import get_xmlrpc_server
elif app_server == "batch":
return object
else:
#raise AppServerException("Environment variable TACTIC_APP_SERVER not set")
# default to webware for now
#print "WARNING: Environment variable TACTIC_APP_SERVER not set"
#from webware_adapter import get_xmlrpc_server
return object
return get_xmlrpc_server()
AppServer = get_app_server_class()
XmlrpcServer = get_xmlrpc_server_class()
def get_site_import(file):
'''function used by entry points into the AppServer to load the necessary
import function to see site specific modules
The following 2 lines should be included:
from pyasm.web import get_site_import
exec( get_site_import(__file__) )
'''
file = file.replace("\\","/")
p = re.compile( r'\/(\w+)\/context\/' )
m = p.search(file)
if not m:
raise Exception("Can't find context")
context = m.groups()[0]
if context == "":
context = "default"
if context in ['admin', 'default', 'template', 'unittest']:
return "from tactic_sites.%s.modules import *" % context
else:
return "from sites.%s.modules import *" % context
def get_main_tab_wdg():
from pyasm.biz import Project
project = Project.get()
type = project.get_base_type()
if type == "":
raise TacticException("Project: %s has no type" % project.get_code())
exec( "from pyasm.%s.site import MainTabWdg" % type )
tab = MainTabWdg()
return tab