forked from sahana/eden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
executable file
·1142 lines (977 loc) · 43.7 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
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
# -*- coding: utf-8 -*-
"""
Admin Controllers
"""
module = request.controller
resourcename = request.function
# S3 framework functions
# -----------------------------------------------------------------------------
def index():
""" Module's Home Page """
module_name = settings.modules[module].name_nice
response.title = module_name
return dict(module_name=module_name)
# =============================================================================
@auth.s3_requires_membership(1)
def setting():
"""
Custom page to link to those Settings which can be edited through the web interface
"""
return dict()
# =============================================================================
# AAA
# =============================================================================
@auth.s3_requires_membership(1)
def role():
"""
Role Manager
"""
# ACLs as component of roles
s3db.add_components("auth_group",
**{auth.permission.TABLENAME: "group_id"}
)
def prep(r):
if r.representation != "html":
return False
handler = s3base.S3RoleManager()
modules = settings.modules
handler.controllers = Storage([(m, modules[m])
for m in modules
if modules[m].restricted])
# Configure REST methods
set_handler = r.set_handler
set_handler("users", handler)
set_handler("read", handler)
set_handler("list", handler)
set_handler("copy", handler)
set_handler("create", handler)
set_handler("update", handler)
set_handler("delete", handler)
return True
s3.prep = prep
s3.stylesheets.append( "S3/role.css" )
output = s3_rest_controller("auth", "group")
return output
# -----------------------------------------------------------------------------
def user():
""" RESTful CRUD controller """
table = auth.settings.table_user
if s3_has_role("ADMIN"):
# Needed as Admin has all roles
pe_ids = None
elif s3_has_role("ORG_ADMIN"):
if settings.get_security_policy() < 6:
# Filter users to just those belonging to the Org Admin's Org & Descendants
otable = s3db.org_organisation
pe_id = db(otable.id == auth.user.organisation_id).select(otable.pe_id,
limitby=(0, 1),
cache=s3db.cache,
).first().pe_id
pe_ids = s3db.pr_get_descendants(pe_id, entity_types="org_organisation")
pe_ids.append(pe_id)
s3.filter = (otable.pe_id.belongs(pe_ids)) & \
(table.organisation_id == otable.id)
else:
# Filter users to just those belonging to the Org Admin's Realms
pe_ids = auth.user.realms[auth.get_system_roles().ORG_ADMIN]
if pe_ids:
otable = s3db.org_organisation
s3.filter = (otable.pe_id.belongs(pe_ids)) & \
(table.organisation_id == otable.id)
else:
auth.permission.fail()
auth.configure_user_fields(pe_ids)
s3db.add_components("auth_user",
auth_membership = "user_id")
list_fields = ["first_name",
"last_name",
"email",
]
lappend = list_fields.append
if len(settings.get_L10n_languages()) > 1:
lappend("language")
if auth.s3_has_role("ADMIN"):
if settings.get_auth_admin_sees_organisation():
lappend("organisation_id")
elif settings.get_auth_registration_requests_organisation():
lappend("organisation_id")
if settings.get_auth_registration_requests_organisation_group():
lappend("org_group_id")
if settings.get_auth_registration_requests_site():
lappend("site_id")
link_user_to = settings.get_auth_registration_link_user_to()
if link_user_to and len(link_user_to) > 1 and settings.get_auth_show_link():
lappend("link_user_to")
lappend((T("Registration"), "created_on"))
table.created_on.represent = s3base.S3DateTime.date_represent
lappend((T("Roles"), "membership.group_id"))
s3db.configure("auth_user",
create_next = URL(c="admin", f="user", args=["[id]", "roles"]),
create_onaccept = lambda form: auth.s3_approve_user(form.vars),
list_fields = list_fields,
main = "first_name",
#update_onaccept = lambda form: auth.s3_link_user(form.vars),
)
def disable_user(r, **args):
if not r.id:
session.error = T("Can only disable 1 record at a time!")
redirect(URL(args=[]))
if r.id == session.auth.user.id: # we're trying to disable ourself
session.error = T("Cannot disable your own account!")
redirect(URL(args=[]))
db(table.id == r.id).update(registration_key = "disabled")
session.confirmation = T("User Account has been Disabled")
redirect(URL(args=[]))
def approve_user(r, **args):
if not r.id:
session.error = T("Can only approve 1 record at a time!")
redirect(URL(args=[]))
user = db(table.id == r.id).select(limitby=(0, 1)).first()
auth.s3_approve_user(user)
session.confirmation = T("User Account has been Approved")
redirect(URL(args=[r.id, "roles"]))
def link_user(r, **args):
if not r.id:
session.error = T("Can only update 1 record at a time!")
redirect(URL(args=[]))
user = db(table.id == r.id).select(limitby=(0, 1)).first()
auth.s3_link_user(user)
session.confirmation = T("User has been (re)linked to Person and Human Resource record")
redirect(URL(args=[]))
# Custom Methods
set_method = s3db.set_method
set_method("auth", "user", method="roles",
action=s3base.S3RoleManager())
set_method("auth", "user", method="disable",
action=disable_user)
set_method("auth", "user", method="approve",
action=approve_user)
set_method("auth", "user", method="link",
action=link_user)
# CRUD Strings
ADD_USER = T("Create User")
s3.crud_strings["auth_user"] = Storage(
label_create = ADD_USER,
title_display = T("User Details"),
title_list = T("Users"),
title_update = T("Edit User"),
title_upload = T("Import Users"),
label_list_button = T("List Users"),
label_delete_button = T("Delete User"),
msg_record_created = T("User added"),
msg_record_modified = T("User updated"),
msg_record_deleted = T("User deleted"),
msg_list_empty = T("No Users currently registered"))
def rheader(r):
if r.representation != "html":
return None
rheader = DIV()
if r.record:
id = r.id
registration_key = r.record.registration_key
if not registration_key:
btn = A(T("Disable"),
_class = "action-btn",
_title = "Disable User",
_href = URL(args=[id, "disable"])
)
rheader.append(btn)
if settings.get_auth_show_link():
btn = A(T("Link"),
_class = "action-btn",
_title = "Link (or refresh link) between User, Person & HR Record",
_href = URL(args=[id, "link"])
)
rheader.append(btn)
#elif registration_key == "pending":
# btn = A(T("Approve"),
# _class = "action-btn",
# _title = "Approve User",
# _href = URL(args=[id, "approve"])
# )
# rheader.append(btn)
else:
# Verify & Approve
btn = A(T("Approve"),
_class = "action-btn",
_title = "Approve User",
_href = URL(args=[id, "approve"])
)
rheader.append(btn)
tabs = [(T("User Details"), None),
(T("Roles"), "roles")
]
rheader_tabs = s3_rheader_tabs(r, tabs)
rheader.append(rheader_tabs)
return rheader
# Pre-processor
def prep(r):
if r.interactive:
s3db.configure(r.tablename,
addbtn = True,
# Password confirmation
create_onvalidation = user_create_onvalidation,
deletable = False,
# jquery.validate is clashing with dataTables so don't embed the create form in with the List
listadd = False,
sortby = [[2, "asc"], [1, "asc"]],
)
elif r.representation == "xls":
lappend((T("Status"), "registration_key"))
if r.method == "delete" and r.http == "GET":
if r.id == session.auth.user.id: # we're trying to delete ourself
get_vars.update({"user.id":str(r.id)})
r.id = None
s3db.configure(r.tablename,
delete_next = URL(c="default", f="user/logout"))
s3.crud.confirm_delete = T("You are attempting to delete your own account - are you sure you want to proceed?")
if r.http == "GET" and not r.method:
session.s3.cancel = r.url()
return True
s3.prep = prep
def postp(r, output):
if r.interactive:
# Only show the disable button if the user is not currently disabled
table = r.table
query = (table.registration_key == None) | \
(table.registration_key == "")
rows = db(query).select(table.id)
restrict = [str(row.id) for row in rows]
s3.actions = [dict(label=str(UPDATE), _class="action-btn",
url=URL(c="admin", f="user",
args=["[id]", "update"])),
dict(label=str(T("Roles")), _class="action-btn",
url=URL(c="admin", f="user",
args=["[id]", "roles"])),
dict(label=str(T("Disable")), _class="action-btn",
url=URL(c="admin", f="user",
args=["[id]", "disable"]),
restrict = restrict)
]
if settings.get_auth_show_link():
s3.actions.insert(1, dict(label=str(T("Link")),
_class="action-btn",
_title = str(T("Link (or refresh link) between User, Person & HR Record")),
url=URL(c="admin", f="user",
args=["[id]", "link"]),
restrict = restrict)
)
# Only show the approve button if the user is currently pending
query = (table.registration_key != "disabled") & \
(table.registration_key != None) & \
(table.registration_key != "")
rows = db(query).select(table.id)
restrict = [str(row.id) for row in rows]
s3.actions.append(
dict(label=str(T("Approve")), _class="action-btn",
url=URL(c="admin", f="user",
args=["[id]", "approve"]),
restrict = restrict)
)
# Add some highlighting to the rows
query = (table.registration_key.belongs(["disabled", "pending"]))
rows = db(query).select(table.id,
table.registration_key)
s3.dataTableStyleDisabled = s3.dataTableStyleWarning = [str(row.id) for row in rows if row.registration_key == "disabled"]
s3.dataTableStyleAlert = [str(row.id) for row in rows if row.registration_key == "pending"]
# Translate the status values
values = [dict(col=6, key="", display=str(T("Active"))),
dict(col=6, key="None", display=str(T("Active"))),
dict(col=6, key="pending", display=str(T("Pending"))),
dict(col=6, key="disabled", display=str(T("Disabled")))
]
s3.dataTableDisplay = values
# @ToDo: Merge these with the code in s3aaa.py and use S3SQLCustomForm to implement
form = output.get("form", None)
if not form:
create_url = URL(args=["create"])
output["showadd_btn"] = s3base.S3CRUD.crud_button(T("Create User"),
_href=create_url)
return output
# Assume formstyle callable
id = "auth_user_password_two__row"
label = "%s:" % T("Verify password")
widget = INPUT(_name="password_two",
_id="password_two",
_type="password",
_disabled="disabled",
)
comment = ""
row = s3_formstyle(id, label, widget, comment, hidden=True)
if isinstance(row, tuple):
# Formstyle with separate row for label (e.g. default Eden formstyle)
tuple_rows = True
form[0].insert(8, row)
else:
# Formstyle with just a single row (e.g. Bootstrap, Foundation or DRRPP)
tuple_rows = False
form[0].insert(4, row)
# @ToDo: Ensure this reads existing values & creates/updates when saved
#if settings.get_auth_registration_requests_mobile_phone():
# id = "auth_user_mobile__row"
# label = LABEL("%s:" % settings.get_ui_label_mobile_phone(),
# _for="mobile",
# )
# widget = INPUT(_name="mobile",
# _id="auth_user_mobile",
# _class="string",
# )
# comment = ""
# row = s3_formstyle(id, label, widget, comment)
# if tuple_rows:
# form[0].insert(-8, row)
# else:
# form[0].insert(-4, row)
# Add client-side validation
auth.s3_register_validation()
return output
s3.postp = postp
s3.import_prep = auth.s3_import_prep
output = s3_rest_controller("auth", "user",
csv_stylesheet = ("auth", "user.xsl"),
csv_template = ("auth", "user"),
rheader = rheader,
)
return output
# =============================================================================
def group():
"""
RESTful CRUD controller
- used by role_required autocomplete
"""
tablename = "auth_group"
if not auth.s3_has_role(ADMIN):
s3db.configure(tablename,
editable=False,
insertable=False,
deletable=False)
# CRUD Strings
ADD_ROLE = T("Create Role")
s3.crud_strings[tablename] = Storage(
label_create = ADD_ROLE,
title_display = T("Role Details"),
title_list = T("Roles"),
title_update = T("Edit Role"),
label_list_button = T("List Roles"),
msg_record_created = T("Role added"),
msg_record_modified = T("Role updated"),
msg_record_deleted = T("Role deleted"),
msg_list_empty = T("No Roles defined"))
s3db.configure(tablename, main="role")
return s3_rest_controller("auth", resourcename)
# -----------------------------------------------------------------------------
@auth.s3_requires_membership(1)
def organisation():
"""
RESTful CRUD controller
@ToDo: Prevent multiple records for the same domain
"""
module = "auth"
tablename = "auth_organisation"
table = s3db[tablename]
s3.crud_strings[tablename] = Storage(
label_create = T("Add Organization Domain"),
title_display = T("Organization Domain Details"),
title_list = T("Organization Domains"),
title_update = T("Edit Organization Domain"),
label_list_button = T("List Organization Domains"),
label_delete_button = T("Delete Organization Domain"),
msg_record_created = T("Organization Domain added"),
msg_record_modified = T("Organization Domain updated"),
msg_record_deleted = T("Organization Domain deleted"),
msg_list_empty = T("No Organization Domains currently registered")
)
output = s3_rest_controller(module, resourcename)
return output
# -----------------------------------------------------------------------------
def user_create_onvalidation (form):
""" Server-side check that Password confirmation field is valid """
if (form.request_vars.has_key("password_two") and \
form.request_vars.password != form.request_vars.password_two):
form.errors.password = T("Password fields don't match")
return True
# =============================================================================
@auth.s3_requires_membership(1)
def acl():
"""
Preliminary controller for ACLs
for testing purposes, not for production use!
"""
module = "s3"
name = "permission"
table = auth.permission.table
tablename = table._tablename
table.group_id.requires = IS_ONE_OF(db, "auth_group.id", "%(role)s")
table.group_id.represent = lambda opt: opt and db.auth_group[opt].role or opt
table.controller.requires = IS_EMPTY_OR(IS_IN_SET(settings.modules.keys(),
zero="ANY"))
table.controller.represent = lambda opt: opt and \
"%s (%s)" % (opt,
auth.permission.modules.get(opt, {}).get("name_nice", opt)) or "ANY"
table.function.represent = lambda val: val and val or T("ANY")
table.tablename.requires = IS_EMPTY_OR(IS_IN_SET([t._tablename for t in db],
zero=T("ANY")))
table.tablename.represent = lambda val: val and val or T("ANY")
table.uacl.label = T("All Resources")
table.uacl.widget = S3ACLWidget.widget
table.uacl.requires = IS_ACL(auth.permission.PERMISSION_OPTS)
table.uacl.represent = lambda val: acl_represent(val,
auth.permission.PERMISSION_OPTS)
table.oacl.label = T("Owned Resources")
table.oacl.widget = S3ACLWidget.widget
table.oacl.requires = IS_ACL(auth.permission.PERMISSION_OPTS)
table.oacl.represent = lambda val: acl_represent(val,
auth.permission.PERMISSION_OPTS)
s3db.configure(tablename,
create_next = URL(r=request),
update_next = URL(r=request))
if "_next" in request.vars:
next = request.vars._next
s3db.configure(tablename, delete_next=next)
output = s3_rest_controller(module, name)
return output
# -----------------------------------------------------------------------------
def acl_represent(acl, options):
"""
Represent ACLs in tables
for testing purposes, not for production use!
"""
values = []
for o in options.keys():
if o == 0 and acl == 0:
values.append("%s" % options[o][0])
elif acl and acl & o == o:
values.append("%s" % options[o][0])
else:
values.append("_")
return " ".join(values)
# =============================================================================
# Ticket viewing
# =============================================================================
@auth.s3_requires_membership(1)
def ticket():
""" Ticket handler """
import traceback
from gluon.restricted import RestrictedError
if len(request.args) != 2:
session.error = T("Invalid ticket")
redirect(URL(r=request))
app = request.args[0]
ticket = request.args[1]
e = RestrictedError()
e.load(request, app, ticket)
return dict(app=app,
ticket=ticket,
traceback=s3base.Traceback(e.traceback),
code=e.code,
layer=e.layer)
# -----------------------------------------------------------------------------
# Web2Py Ticket Viewer functions Borrowed from admin application of web2py
@auth.s3_requires_membership(1)
def errors():
""" Error handler """
from gluon.admin import apath
from gluon.fileutils import listdir
for item in request.vars:
if item[:7] == "delete_":
os.unlink(apath("%s/errors/%s" % (appname, item[7:]), r=request))
func = lambda p: os.stat(apath("%s/errors/%s" % (appname, p), r=request)).st_mtime
tickets = sorted(listdir(apath("%s/errors/" % appname, r=request), "^\w.*"),
key=func,
reverse=True)
return dict(app=appname, tickets=tickets)
# =============================================================================
# Management scripts
# =============================================================================
@auth.s3_requires_membership(1)
def clean():
"""
Run an external script to clean this instance & reset to default values
visudo
web2py ALL=(ALL)NOPASSWD:/usr/local/bin/clean
"""
from subprocess import check_call
instance = settings.get_instance_name()
try:
check_call(["sudo /usr/local/bin/clean %s" % instance], shell=True)
except:
import sys
error = sys.exc_info()[1]
status = current.xml.json_message(False, 400,
"Script cannot be run: %s" % error)
raise HTTP(400, body=status)
# =============================================================================
# Create portable app
# =============================================================================
@auth.s3_requires_membership(1)
def portable():
""" Portable app creator"""
from gluon.admin import apath
import os
from operator import itemgetter, attrgetter
uploadfolder=os.path.join(apath("%s" % appname, r=request), "cache")
web2py_source = None
web2py_source_exists = False
last_build_exists = False
for base, dirs, files in os.walk(uploadfolder):
for filename in files:
if "web2py_source" in filename:
web2py_source_exists = True
web2py_source = filename
break
for base, dirs, files in os.walk(uploadfolder):
for filename in files:
if "download.zip" in filename:
last_build_exists = True
break
web2py_form = SQLFORM.factory(
Field("web2py_source",
"upload",
uploadfolder=uploadfolder,
requires=IS_UPLOAD_FILENAME(extension="zip"),
),
table_name="web2py_source",
)
if web2py_form.accepts(request.vars, keepvalues=True, session=None):
# Make sure only one web2py source file exists
files_to_remove = {}
for base, dirs, files in os.walk(uploadfolder):
for filename in files:
if "web2py_source" in filename:
files_to_remove[filename] = os.stat(os.path.join(uploadfolder, filename)).st_mtime
sorted_files = sorted(files_to_remove.items(), key=itemgetter(1))
for i in range(0, len(sorted_files) - 1): # 1 indicates leave one file
os.remove(os.path.join(uploadfolder,sorted_files[i][0]))
web2py_source = sorted_files[len(sorted_files) - 1][0]
web2py_source_exists = True
session.flash = T("Web2py executable zip file found - Upload to replace the existing file")
else:
# Lets throw an error message if this the zip file isn't found
if not web2py_source_exists:
session.error = T("Web2py executable zip file needs to be uploaded to use this function.")
else:
session.flash = T("Web2py executable zip file found - Upload to replace the existing file")
# Since the 2nd form depends on having uploaded the zip
# in order to work we only show it if the upload was successfully
# completed.
if web2py_source_exists:
generator_form = SQLFORM.factory(
Field("copy_database", "boolean"),
Field("copy_uploads", "boolean"),
)
if generator_form.accepts(request.vars, keepvalues=True, session=None):
if web2py_source_exists:
create_portable_app(web2py_source=web2py_source,\
copy_database = request.vars.copy_database,\
copy_uploads = request.vars.copy_uploads)
else:
session.error = T("Web2py executable zip file needs to be uploaded first to use this function.")
else:
generator_form = None
if last_build_exists:
download_last_form = SQLFORM.factory()
if download_last_form.accepts(request.vars, keepvalues=True, session=None):
portable_app = os.path.join(cachedir, "download.zip")
response.headers["Content-Type"] = contenttype.contenttype(portable_app)
response.headers["Content-Disposition"] = \
"attachment; filename=portable-sahana.zip"
return response.stream(portable_app)
else:
download_last_form = None
return dict(
web2py_form=web2py_form,
generator_form=generator_form,
download_last_form=download_last_form
)
# -----------------------------------------------------------------------------
def create_portable_app(web2py_source, copy_database=False, copy_uploads=False):
"""Function to create the portable app based on the parameters"""
from gluon.admin import apath
import shutil,tempfile,os
import zipfile
import contenttype
cachedir = os.path.join(apath("%s" % appname, r=request), "cache")
tempdir = tempfile.mkdtemp("", "eden-", cachedir)
workdir = os.path.join(tempdir, "web2py")
if copy_uploads:
ignore = shutil.ignore_patterns("*.db", "*.log", "*.table", "errors", "sessions", "compiled" , "cache", ".bzr", "*.pyc")
else:
ignore = shutil.ignore_patterns("*.db", "*.log", "*.table", "errors", "sessions", "compiled" , "uploads", "cache", ".bzr", "*.pyc")
appdir = os.path.join(workdir, "applications", appname)
shutil.copytree(apath("%s" % appname, r=request),\
appdir, \
ignore = ignore)
os.mkdir(os.path.join(appdir, "errors"))
os.mkdir(os.path.join(appdir, "sessions"))
os.mkdir(os.path.join(appdir, "cache"))
if not copy_uploads:
os.mkdir(os.path.join(appdir, "uploads"))
shutil.copy(os.path.join(appdir, "deployment-templates", "cron", "crontab"),\
os.path.join(appdir, "cron", "crontab"))
if copy_database:
# Copy the db for the portable app
s3db.load_all_models() # Load all modules to copy everything
portable_db = DAL("sqlite://storage.db", folder=os.path.join(appdir, "databases"))
for table in db:
portable_db.define_table(table._tablename, *[field for field in table])
portable_db.commit()
temp_csv_file=tempfile.mkstemp()
db.export_to_csv_file(open(temp_csv_file[1], "wb"))
portable_db.import_from_csv_file(open(temp_csv_file[1], "rb"))
os.unlink(temp_csv_file[1])
portable_db.commit()
# Replace the following with a more specific config
config_template = open(os.path.join(appdir, "deployment-templates", "models", "000_config.py"), "r")
new_config = open(os.path.join(appdir, "models", "000_config.py"), "w")
# Replace first occurance of False with True
new_config.write(config_template.read().replace("False", "True", 1))
new_config.close()
# Embedded the web2py source with eden for download
shutil.copy(os.path.join(cachedir, web2py_source), os.path.join(cachedir, "download.zip"))
portable_app = os.path.join(cachedir, "download.zip")
zip = zipfile.ZipFile(portable_app, "a", zipfile.ZIP_DEFLATED)
tozip = os.path.join(tempdir, "web2py")
rootlen = len(tempdir) + 1
for base, dirs, files in os.walk(tozip):
for directory in dirs:
directory = os.path.join(base, directory)
zip.write(directory, directory[rootlen:]) # Create empty directories
for file in files:
fn = os.path.join(base, file)
zip.write(fn, fn[rootlen:])
zip.close()
shutil.rmtree(tempdir)
response.headers["Content-Type"] = contenttype.contenttype(portable_app)
response.headers["Content-Disposition"] = \
"attachment; filename=portable-sahana.zip"
return response.stream(portable_app)
# =============================================================================
# Translation Functionality
# =============================================================================
def translate():
"""
Translation controller to enable four major workflows :-
1) Select modules which require translation. The list of strings
belonging to selected modules can be exported in .xls or .po format
2) Upload csv file containing strings with their translations which
are then merged with existing language file
3) Display the percentage of translation for each module for a given
language file
4) Upload a text file containing a list of new-line separated strings
which are to be considered for translation in the future. These
strings are termed as "user-supplied" strings and are picked up by
the first workflow when preparing the spreadsheet for translation
Note : The above functionalities require a considerable amount of
main memory to execute successfully.
@ToDo: Move opts 1, 3 & 4 outside the REST Controller
- only opt 2 makes use of this so it's unnecessary overhead!
"""
opt = get_vars.get("opt", None)
if not opt:
# Show index page
return dict()
# For the one which actually uses CRUD (opt 2)
s3.crud.submit_button = T("Upload")
def postp(r, output):
# Create a custom form
form = FORM()
# Prevent redirection
r.next = None
# Remove the false error from the form
# error : "Invalid form (re-opened in another window?)"
if response.error and not output["form"]["error"]:
response.error = None
if opt == "1":
# Select modules for Translation
from math import ceil
from s3.s3translate import TranslateAPI, Strings
if form.accepts(request.vars, session):
modlist = []
# If only one module is selected
if type(form.request_vars.module_list) == str:
modlist.append(form.request_vars.module_list)
# If multiple modules are selected
else:
modlist = form.request_vars.module_list
# If no module is selected
if modlist is None:
modlist = []
# If "Select All" option is chosen
all_template_flag = 0
if "all" in modlist:
all_template_flag = 1
A = TranslateAPI()
modlist = A.get_modules()
if "core" in form.request_vars.module_list:
modlist.append("core")
# Obtaining the language file from the language code
code = form.request_vars.new_code
if code == "":
code = form.request_vars.code
code += ".py"
# Obtaining the type of file to export to
filetype = form.request_vars.filetype
# Generate the file to download
X = Strings()
output = X.export_file(code, modlist, [], filetype, all_template_flag)
return output
# Create a form with checkboxes for list of modules
A = TranslateAPI()
# Retrieve list of active modules
activemodlist = settings.modules.keys()
modlist = activemodlist
# Hiding core modules
hidden_modules = A.core_modules
for module in hidden_modules:
if module in modlist:
modlist.remove(module)
modlist.sort()
modcount = len(modlist)
langlist = A.get_langcodes()
langlist.sort()
table = TABLE(_class="translation_module_table")
table.append(BR())
# Set number of columns in the form
NO_OF_COLUMNS = 3
# Display "NO_OF_COLUMNS" modules per row so as to utilize the page completely
num = 0
max_rows = int(ceil(modcount / float(NO_OF_COLUMNS)))
modules = settings.modules
while num < max_rows:
check = "yes"
mod_name = modules[modlist[num]].name_nice
mod_name = "%s (%s)" %(mod_name, modlist[num])
row = TR(TD(num + 1),
TD(INPUT(_type="checkbox", _name="module_list",
_value=modlist[num], _checked = check)),
TD(mod_name),
)
for c in range(1, NO_OF_COLUMNS):
cmax_rows = num + (c * max_rows)
if cmax_rows < modcount:
mod_name = modules[modlist[cmax_rows]].name_nice
mod_name = "%s (%s)" % (mod_name, modlist[cmax_rows])
row.append(TD(cmax_rows + 1))
row.append(TD(INPUT(_type="checkbox",
_name="module_list",
_value=modlist[cmax_rows],
_checked = check)))
row.append(TD(mod_name))
num += 1
table.append(row)
div = DIV(table,
BR(),
)
row = TR(TD(INPUT(_type="checkbox", _name="module_list",
_value="core", _checked="yes")),
TD(T("Include core files")),
)
div.append(row)
div.append(BR())
row = TR(TD(INPUT(_type="checkbox", _name="module_list",
_value="all")),
TD(T("Select all templates (All modules included)")),
)
div.append(row)
div.append(BR())
# Provide option to choose export format
row = TR(TD("%s:" % T("Export as")),
TD(INPUT(_type="radio", _name="filetype",
_value="xls", _checked="checked")),
TD(".xls (Excel)"),
TD(INPUT(_type="radio", _name="filetype", _value="po")),
TD(".po (Pootle)"),
BR(),
BR(),
)
div.append(row)
# Drop-down for available language codes
lang_dropdown = SELECT(_name = "code")
for lang in langlist:
lang_dropdown.append(lang)
row = TR(TD("%s:" % T("Select language code")),
TD(lang_dropdown),
TD("%s:" % T("Or add a new language code")),
TD(INPUT(_type="text", _name="new_code")),
)
div.append(row)
div.append(BR())
div.append(BR())
div.append(INPUT(_type="submit", _value=T("Submit")))
form.append(div)
# Add the custom form to the output
output["form"] = form
output["title"] = T("Select the required modules")
elif opt == "2":
# Upload translated files
form = output["form"]
div = DIV(BR(),
T("Note: Make sure that all the text cells are quoted in the csv file before uploading"),
)
form.append(div)
output["form"] = form
elif opt == "3":
# View Translation Percentage
if form.accepts(request.vars, session):
# Retrieve the translation percentage for each module
from math import ceil
from s3.s3translate import TranslateReportStatus
code = form.request_vars.code
S = TranslateReportStatus()
if form.request_vars.update_master == "on":
S.create_master_file()
percent_dict = S.get_translation_percentages(code)
modlist = []
for mod in sorted(percent_dict.keys()):
if mod != "complete_file":
modlist.append(mod)
modcount = len(modlist)
table = TABLE(_class="translation_module_table")
table.append(BR())
# Set number of columns in the table
NO_OF_COLUMNS = 3
# Display "NO_OF_COLUMNS" modules per row so as to utilize the page completely
num = 0
max_rows = int(ceil(modcount / float(NO_OF_COLUMNS)))
while num < max_rows:
row = TR(TD(modlist[num]), TD(percent_dict[modlist[num]]))
for c in range(1, NO_OF_COLUMNS):
cmax_rows = num + (c * max_rows)
if cmax_rows < modcount:
row.append(TD(modlist[cmax_rows]))
row.append(TD(percent_dict[modlist[cmax_rows]]))
num += 1
table.append(row)
# Add the table to output to display it
div = DIV(table,
BR(),
TR(TD("Overall translation percentage of the file: "),
TD(percent_dict["complete_file"])),