Source

galaxy-central / lib / galaxy / webapps / galaxy / controllers / library_common.py

Full commit
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
import os, os.path, shutil, urllib, StringIO, re, gzip, tempfile, shutil, zipfile, copy, glob, string, urllib2
from galaxy.web.base.controller import *
from galaxy import util, jobs
from galaxy.datatypes import sniff
from galaxy.security import RBACAgent
from galaxy.util.json import to_json_string
from galaxy.tools.actions import upload_common
from galaxy.model.orm import *
from galaxy.util.streamball import StreamBall
from galaxy.util import inflector
from galaxy.web.form_builder import AddressField, CheckboxField, SelectField, TextArea, TextField, WorkflowField, WorkflowMappingField, HistoryField
import logging, tempfile, zipfile, tarfile, os, sys, operator
from galaxy.eggs import require
from galaxy.security import Action
# Whoosh is compatible with Python 2.5+ Try to import Whoosh and set flag to indicate whether tool search is enabled.
try:
    require( "Whoosh" )
    import whoosh.index
    from whoosh.fields import Schema, STORED, ID, KEYWORD, TEXT
    from whoosh.scoring import BM25F
    from whoosh.qparser import MultifieldParser
    whoosh_search_enabled = True
    # The following must be defined exactly like the
    # schema in ~/scripts/data_libraries/build_whoosh_index.py
    schema = Schema( id=STORED, name=TEXT, info=TEXT, dbkey=TEXT, message=TEXT )
except ImportError, e:
    whoosh_search_enabled = False
    schema = None

if sys.version_info[:2] < ( 2, 6 ):
    zipfile.BadZipFile = zipfile.error
if sys.version_info[:2] < ( 2, 5 ):
    zipfile.LargeZipFile = zipfile.error

log = logging.getLogger( __name__ )

# Test for available compression types
tmpd = tempfile.mkdtemp()
comptypes = []
for comptype in ( 'gz', 'bz2' ):
    tmpf = os.path.join( tmpd, 'compression_test.tar.' + comptype )
    try:
        archive = tarfile.open( tmpf, 'w:' + comptype )
        archive.close()
        comptypes.append( comptype )
    except tarfile.CompressionError:
        log.exception( "Compression error when testing %s compression.  This option will be disabled for library downloads." % comptype )
    try:
        os.unlink( tmpf )
    except OSError:
        pass
ziptype = '32'
tmpf = os.path.join( tmpd, 'compression_test.zip' )
try:
    archive = zipfile.ZipFile( tmpf, 'w', zipfile.ZIP_DEFLATED, True )
    archive.close()
    comptypes.append( 'zip' )
    ziptype = '64'
except RuntimeError:
    log.exception( "Compression error when testing zip compression. This option will be disabled for library downloads." )
except (TypeError, zipfile.LargeZipFile):
    # ZIP64 is only in Python2.5+.  Remove TypeError when 2.4 support is dropped
    log.warning( 'Max zip file size is 2GB, ZIP64 not supported' )
    comptypes.append( 'zip' )
try:
    os.unlink( tmpf )
except OSError:
    pass
os.rmdir( tmpd )

class LibraryCommon( BaseUIController, UsesFormDefinitionsMixin ):
    @web.json
    def library_item_updates( self, trans, ids=None, states=None ):
        # Avoid caching
        trans.response.headers['Pragma'] = 'no-cache'
        trans.response.headers['Expires'] = '0'
        # Create new HTML for any that have changed
        rval = {}
        if ids is not None and states is not None:
            ids = map( int, ids.split( "," ) )
            states = states.split( "," )
            for id, state in zip( ids, states ):
                data = trans.sa_session.query( self.app.model.LibraryDatasetDatasetAssociation ).get( id )
                if data.state != state:
                    job_ldda = data
                    while job_ldda.copied_from_library_dataset_dataset_association:
                        job_ldda = job_ldda.copied_from_library_dataset_dataset_association
                    force_history_refresh = False
                    rval[id] = {
                        "state": data.state,
                        "html": unicode( trans.fill_template( "library/common/library_item_info.mako", ldda=data ), 'utf-8' )
                        #"force_history_refresh": force_history_refresh
                    }
        return rval

    @web.expose
    def browse_library( self, trans, cntrller, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        # If use_panels is True, the library is being accessed via an external link
        # which did not originate from within the Galaxy instance, and the library will
        # be displayed correctly with the mast head.
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        library_id = params.get( 'id', None )
        if not library_id:
            # To handle bots
            message = "You must specify a library id."
            status = 'error'
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        try:
            library = trans.sa_session.query( trans.app.model.Library ).get( trans.security.decode_id( library_id ) )
        except:
            # Protect against attempts to phish for valid keys that return libraries
            library = None
        # Most security for browsing libraries is handled in the template, but do a basic check here.
        if not library or not ( is_admin or trans.app.security_agent.can_access_library( current_user_roles, library ) ):
            message = "Invalid library id ( %s ) specified." % str( library_id )
            status = 'error'
        else:
            show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
            created_ldda_ids = params.get( 'created_ldda_ids', '' )
            hidden_folder_ids = util.listify( params.get( 'hidden_folder_ids', '' ) )
            if created_ldda_ids and not message:
                message = "%d datasets are uploading in the background to the library '%s' (each is selected).  "  % \
                    ( len( created_ldda_ids.split( ',' ) ), library.name )
                message += "Don't navigate away from Galaxy or use the browser's \"stop\" or \"reload\" buttons (on this tab) until the "
                message += "message \"This job is running\" is cleared from the \"Information\" column below for each selected dataset."
                status = "info"
            comptypes = get_comptypes( trans )
            try:
                if self.app.config.new_lib_browse:
                    return trans.fill_template( 'library/common/browse_library_opt.mako',
                                                cntrller=cntrller,
                                                use_panels=use_panels,
                                                library=library,
                                                created_ldda_ids=created_ldda_ids,
                                                hidden_folder_ids=hidden_folder_ids,
                                                show_deleted=show_deleted,
                                                comptypes=comptypes,
                                                current_user_roles=current_user_roles,
                                                message=message,
                                                status=status )
                else:
                    return trans.fill_template( 'library/common/browse_library.mako',
                                                cntrller=cntrller,
                                                use_panels=use_panels,
                                                library=library,
                                                created_ldda_ids=created_ldda_ids,
                                                hidden_folder_ids=hidden_folder_ids,
                                                show_deleted=show_deleted,
                                                comptypes=comptypes,
                                                current_user_roles=current_user_roles,
                                                message=message,
                                                status=status )
            except Exception, e:
                message = 'Error attempting to display contents of library (%s): %s.' % ( str( library.name ), str( e ) )
                status = 'error'
        default_action = params.get( 'default_action', None )

        return trans.response.send_redirect( web.url_for( use_panels=use_panels,
                                                          controller=cntrller,
                                                          action='browse_libraries',
                                                          default_action=default_action,
                                                          message=util.sanitize_text( message ),
                                                          status=status ) )
    @web.expose
    def library_info( self, trans, cntrller, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        library_id = params.get( 'id', None )
        try:
            library = trans.sa_session.query( trans.app.model.Library ).get( trans.security.decode_id( library_id ) )
        except:
            library = None
        self._check_access( trans, cntrller, is_admin, library, current_user_roles, use_panels, library_id, show_deleted )
        if params.get( 'library_info_button', False ):
            self._check_modify( trans, cntrller, is_admin, library, current_user_roles, use_panels, library_id, show_deleted )
            old_name = library.name
            new_name = util.restore_text( params.get( 'name', 'No name' ) )
            if not new_name:
                message = 'Enter a valid name'
                status='error'
            else:
                new_description = util.restore_text( params.get( 'description', '' ) )
                new_synopsis = util.restore_text( params.get( 'synopsis', '' ) )
                if new_synopsis in [ None, 'None' ]:
                    new_synopsis = ''
                library.name = new_name
                library.description = new_description
                library.synopsis = new_synopsis
                # Rename the root_folder
                library.root_folder.name = new_name
                library.root_folder.description = new_description
                trans.sa_session.add_all( ( library, library.root_folder ) )
                trans.sa_session.flush()
                message = "Information updated for library '%s'." % library.name
                return trans.response.send_redirect( web.url_for( controller='library_common',
                                                                  action='library_info',
                                                                  cntrller=cntrller,
                                                                  use_panels=use_panels,
                                                                  id=trans.security.encode_id( library.id ),
                                                                  show_deleted=show_deleted,
                                                                  message=util.sanitize_text( message ),
                                                                  status='done' ) )
        # See if we have any associated templates
        info_association, inherited = library.get_info_association()
        widgets = library.get_template_widgets( trans )
        widget_fields_have_contents = self.widget_fields_have_contents( widgets )
        return trans.fill_template( '/library/common/library_info.mako',
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    library=library,
                                    widgets=widgets,
                                    widget_fields_have_contents=widget_fields_have_contents,
                                    current_user_roles=current_user_roles,
                                    show_deleted=show_deleted,
                                    info_association=info_association,
                                    inherited=inherited,
                                    message=message,
                                    status=status )
    @web.expose
    def library_permissions( self, trans, cntrller, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        library_id = params.get( 'id', None )
        try:
            library = trans.sa_session.query( trans.app.model.Library ).get( trans.security.decode_id( library_id ) )
        except:
            library = None
        self._check_access( trans, cntrller, is_admin, library, current_user_roles, use_panels, library_id, show_deleted )
        self._check_manage( trans, cntrller, is_admin, library, current_user_roles, use_panels, library_id, show_deleted )
        if params.get( 'update_roles_button', False ):
            # The user clicked the Save button on the 'Associate With Roles' form
            permissions = {}
            for k, v in trans.app.model.Library.permitted_actions.items():
                in_roles = [ trans.sa_session.query( trans.app.model.Role ).get( x ) for x in util.listify( params.get( k + '_in', [] ) ) ]
                permissions[ trans.app.security_agent.get_action( v.action ) ] = in_roles
            trans.app.security_agent.set_all_library_permissions( trans, library, permissions )
            trans.sa_session.refresh( library )
            # Copy the permissions to the root folder
            trans.app.security_agent.copy_library_permissions( trans, library, library.root_folder )
            message = "Permissions updated for library '%s'." % library.name
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='library_permissions',
                                                              cntrller=cntrller,
                                                              use_panels=use_panels,
                                                              id=trans.security.encode_id( library.id ),
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status='done' ) )
        roles = trans.app.security_agent.get_legitimate_roles( trans, library, cntrller )
        all_roles = trans.app.security_agent.get_all_roles( trans, cntrller )
        return trans.fill_template( '/library/common/library_permissions.mako',
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    library=library,
                                    current_user_roles=current_user_roles,
                                    roles=roles,
                                    all_roles=all_roles,
                                    show_deleted=show_deleted,
                                    message=message,
                                    status=status )
    @web.expose
    def create_folder( self, trans, cntrller, parent_id, library_id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        is_admin = trans.user_is_admin() and cntrller in ( 'library_admin', 'api' )
        current_user_roles = trans.get_current_user_roles()
        try:
            parent_folder = trans.sa_session.query( trans.app.model.LibraryFolder ).get( trans.security.decode_id( parent_id ) )
        except:
            parent_folder = None
        # Check the library which actually contains the user-supplied parent folder, not the user-supplied
        # library, which could be anything.
        if parent_folder:
            parent_library = parent_folder.parent_library
        self._check_access( trans, cntrller, is_admin, parent_folder, current_user_roles, use_panels, library_id, show_deleted )
        self._check_add( trans, cntrller, is_admin, parent_folder, current_user_roles, use_panels, library_id, show_deleted )
        if params.get( 'new_folder_button', False ) or cntrller == 'api':
            new_folder = trans.app.model.LibraryFolder( name=util.restore_text( params.name ),
                                                        description=util.restore_text( params.description ) )
            # We are associating the last used genome build with folders, so we will always
            # initialize a new folder with the first dbkey in util.dbnames which is currently
            # ?    unspecified (?)
            new_folder.genome_build = util.dbnames.default_value
            parent_folder.add_folder( new_folder )
            trans.sa_session.add( new_folder )
            trans.sa_session.flush()
            # New folders default to having the same permissions as their parent folder
            trans.app.security_agent.copy_library_permissions( trans, parent_folder, new_folder )
            # If we're creating in the API, we're done
            if cntrller == 'api':
                return 200, dict( created=new_folder )
            # If we have an inheritable template, redirect to the folder_info page so information
            # can be filled in immediately.
            widgets = []
            info_association, inherited = new_folder.get_info_association()
            if info_association and ( not( inherited ) or info_association.inheritable ):
                widgets = new_folder.get_template_widgets( trans )
            if info_association:
                message = "The new folder named '%s' has been added to the data library.  " % new_folder.name
                message += "Additional information about this folder may be added using the inherited template."
                return trans.fill_template( '/library/common/folder_info.mako',
                                            cntrller=cntrller,
                                            use_panels=use_panels,
                                            folder=new_folder,
                                            library_id=library_id,
                                            widgets=widgets,
                                            current_user_roles=current_user_roles,
                                            show_deleted=show_deleted,
                                            info_association=info_association,
                                            inherited=inherited,
                                            message=message,
                                            status='done' )
            # If not inheritable info_association, redirect to the library.
            message = "The new folder named '%s' has been added to the data library." % new_folder.name
            # SM: This is the second place where the API controller would
            # reference the library id:
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              use_panels=use_panels,
                                                              id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status='done' ) )
        # We do not render any template widgets on creation pages since saving the info_association
        # cannot occur before the associated item is saved.
        return trans.fill_template( '/library/common/new_folder.mako',
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    library_id=library_id,
                                    folder=parent_folder,
                                    show_deleted=show_deleted,
                                    message=message,
                                    status=status )
    @web.expose
    def folder_info( self, trans, cntrller, id, library_id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        try:
            folder = trans.sa_session.query( trans.app.model.LibraryFolder ).get( trans.security.decode_id( id ) )
        except:
            folder = None
        self._check_access( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
        if params.get( 'rename_folder_button', False ):
            self._check_modify( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
            old_name = folder.name
            new_name = util.restore_text( params.name )
            new_description = util.restore_text( params.description )
            if not new_name:
                message = 'Enter a valid name'
                status='error'
            else:
                folder.name = new_name
                folder.description = new_description
                trans.sa_session.add( folder )
                trans.sa_session.flush()
                message = "Information updated for folder '%s'." % folder.name
                return trans.response.send_redirect( web.url_for( controller='library_common',
                                                                  action='folder_info',
                                                                  cntrller=cntrller,
                                                                  use_panels=use_panels,
                                                                  id=id,
                                                                  library_id=library_id,
                                                                  show_deleted=show_deleted,
                                                                  message=util.sanitize_text( message ),
                                                                  status='done' ) )
        # See if we have any associated templates
        widgets = []
        widget_fields_have_contents = False
        info_association, inherited = folder.get_info_association()
        if info_association and ( not( inherited ) or info_association.inheritable ):
            widgets = folder.get_template_widgets( trans )
            widget_fields_have_contents = self.widget_fields_have_contents( widgets )
        return trans.fill_template( '/library/common/folder_info.mako',
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    folder=folder,
                                    library_id=library_id,
                                    widgets=widgets,
                                    widget_fields_have_contents=widget_fields_have_contents,
                                    current_user_roles=current_user_roles,
                                    show_deleted=show_deleted,
                                    info_association=info_association,
                                    inherited=inherited,
                                    message=message,
                                    status=status )
    @web.expose
    def folder_permissions( self, trans, cntrller, id, library_id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        try:
            folder = trans.sa_session.query( trans.app.model.LibraryFolder ).get( trans.security.decode_id( id ) )
        except:
            folder = None
        self._check_access( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
        self._check_manage( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
        if params.get( 'update_roles_button', False ):
            # The user clicked the Save button on the 'Associate With Roles' form
            permissions = {}
            for k, v in trans.app.model.Library.permitted_actions.items():
                if k != 'LIBRARY_ACCESS':
                    # LIBRARY_ACCESS is a special permission set only at the library level
                    # and it is not inherited.
                    in_roles = [ trans.sa_session.query( trans.app.model.Role ).get( int( x ) ) for x in util.listify( params.get( k + '_in', [] ) ) ]
                    permissions[ trans.app.security_agent.get_action( v.action ) ] = in_roles
            trans.app.security_agent.set_all_library_permissions( trans, folder, permissions )
            trans.sa_session.refresh( folder )
            message = "Permissions updated for folder '%s'." % folder.name
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='folder_permissions',
                                                              cntrller=cntrller,
                                                              use_panels=use_panels,
                                                              id=trans.security.encode_id( folder.id ),
                                                              library_id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status='done' ) )
        # If the library is public all roles are legitimate, but if the library
        # is restricted, only those roles associated with the LIBRARY_ACCESS
        # permission are legitimate.
        roles = trans.app.security_agent.get_legitimate_roles( trans, folder.parent_library, cntrller )
        return trans.fill_template( '/library/common/folder_permissions.mako',
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    folder=folder,
                                    library_id=library_id,
                                    current_user_roles=current_user_roles,
                                    roles=roles,
                                    show_deleted=show_deleted,
                                    message=message,
                                    status=status )
    @web.expose
    def ldda_edit_info( self, trans, cntrller, library_id, folder_id, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        try:
            ldda = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( trans.security.decode_id( id ) )
        except:
            ldda = None
        self._check_access( trans, cntrller, is_admin, ldda, current_user_roles, use_panels, library_id, show_deleted )
        self._check_modify( trans, cntrller, is_admin, ldda, current_user_roles, use_panels, library_id, show_deleted )
        dbkey = params.get( 'dbkey', '?' )
        if isinstance( dbkey, list ):
            dbkey = dbkey[0]
        file_formats = [ dtype_name for dtype_name, dtype_value in trans.app.datatypes_registry.datatypes_by_extension.iteritems() if dtype_value.allow_datatype_change ]
        file_formats.sort()
        # See if we have any associated templates
        widgets = []
        info_association, inherited = ldda.get_info_association()
        if info_association and ( not( inherited ) or info_association.inheritable ):
            widgets = ldda.get_template_widgets( trans )
        if params.get( 'change', False ):
            # The user clicked the Save button on the 'Change data type' form
            if ldda.datatype.allow_datatype_change and trans.app.datatypes_registry.get_datatype_by_extension( params.datatype ).allow_datatype_change:
                trans.app.datatypes_registry.change_datatype( ldda, params.datatype )
                trans.sa_session.flush()
                message = "Data type changed for library dataset '%s'." % ldda.name
                status = 'done'
            else:
                message = "You are unable to change datatypes in this manner. Changing %s to %s is not allowed." % ( ldda.extension, params.datatype )
                status = 'error'
        elif params.get( 'save', False ):
            # The user clicked the Save button on the 'Edit Attributes' form
            old_name = ldda.name
            new_name = util.restore_text( params.get( 'name', '' ) )
            new_info = util.restore_text( params.get( 'info', '' ) )
            new_message = util.restore_text( params.get( 'message', '' ) )
            if not new_name:
                message = 'Enter a valid name'
                status = 'error'
            else:
                ldda.name = new_name
                ldda.info = new_info
                ldda.message = new_message
                # The following for loop will save all metadata_spec items
                for name, spec in ldda.datatype.metadata_spec.items():
                    if spec.get("readonly"):
                        continue
                    optional = params.get( "is_" + name, None )
                    if optional and optional == 'true':
                        # optional element... == 'true' actually means it is NOT checked (and therefore ommitted)
                        setattr( ldda.metadata, name, None )
                    else:
                        setattr( ldda.metadata, name, spec.unwrap( params.get ( name, None ) ) )
                ldda.metadata.dbkey = dbkey
                ldda.datatype.after_setting_metadata( ldda )
                trans.sa_session.flush()
                message = "Attributes updated for library dataset '%s'." % ldda.name
                status = 'done'
        elif params.get( 'detect', False ):
            # The user clicked the Auto-detect button on the 'Edit Attributes' form
            for name, spec in ldda.datatype.metadata_spec.items():
                # We need to be careful about the attributes we are resetting
                if name not in [ 'name', 'info', 'dbkey' ]:
                    if spec.get( 'default' ):
                        setattr( ldda.metadata, name, spec.unwrap( spec.get( 'default' ) ) )
            ldda.datatype.set_meta( ldda )
            ldda.datatype.after_setting_metadata( ldda )
            trans.sa_session.flush()
            message = "Information updated for library dataset '%s'." % ldda.name
            status = 'done'
        if "dbkey" in ldda.datatype.metadata_spec and not ldda.metadata.dbkey:
            # Copy dbkey into metadata, for backwards compatability
            # This looks like it does nothing, but getting the dbkey
            # returns the metadata dbkey unless it is None, in which
            # case it resorts to the old dbkey.  Setting the dbkey
            # sets it properly in the metadata
            ldda.metadata.dbkey = ldda.dbkey
        return trans.fill_template( "/library/common/ldda_edit_info.mako",
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    ldda=ldda,
                                    library_id=library_id,
                                    file_formats=file_formats,
                                    widgets=widgets,
                                    current_user_roles=current_user_roles,
                                    show_deleted=show_deleted,
                                    info_association=info_association,
                                    inherited=inherited,
                                    message=message,
                                    status=status )
    @web.expose
    def ldda_info( self, trans, cntrller, library_id, folder_id, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        show_associated_hdas_and_lddas = util.string_as_bool( params.get( 'show_associated_hdas_and_lddas', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        ldda = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( trans.security.decode_id( id ) )
        self._check_access( trans, cntrller, is_admin, ldda, current_user_roles, use_panels, library_id, show_deleted )
        if is_admin and show_associated_hdas_and_lddas:
            # Get all associated hdas and lddas that use the same disk file.
            associated_hdas = trans.sa_session.query( trans.model.HistoryDatasetAssociation ) \
                                              .filter( and_( trans.model.HistoryDatasetAssociation.deleted == False,
                                                             trans.model.HistoryDatasetAssociation.dataset_id == ldda.dataset_id ) ) \
                                              .all()
            associated_lddas = trans.sa_session.query( trans.model.LibraryDatasetDatasetAssociation ) \
                                               .filter( and_( trans.model.LibraryDatasetDatasetAssociation.deleted == False,
                                                              trans.model.LibraryDatasetDatasetAssociation.dataset_id == ldda.dataset_id,
                                                              trans.model.LibraryDatasetDatasetAssociation.id != ldda.id ) ) \
                                               .all()
        else:
            associated_hdas = []
            associated_lddas = [] 
        # See if we have any associated templates
        widgets = []
        widget_fields_have_contents = False
        info_association, inherited = ldda.get_info_association()
        if info_association and ( not( inherited ) or info_association.inheritable ):
            widgets = ldda.get_template_widgets( trans )
            widget_fields_have_contents = self.widget_fields_have_contents( widgets )
        return trans.fill_template( '/library/common/ldda_info.mako',
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    ldda=ldda,
                                    library=ldda.library_dataset.folder.parent_library,
                                    show_associated_hdas_and_lddas=show_associated_hdas_and_lddas,
                                    associated_hdas=associated_hdas,
                                    associated_lddas=associated_lddas,
                                    show_deleted=show_deleted,
                                    widgets=widgets,
                                    widget_fields_have_contents=widget_fields_have_contents,
                                    current_user_roles=current_user_roles,
                                    info_association=info_association,
                                    inherited=inherited,
                                    message=message,
                                    status=status )
    @web.expose
    def ldda_permissions( self, trans, cntrller, library_id, folder_id, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        ids = util.listify( id )
        lddas = []
        libraries = []
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        for id in ids:
            try:
                ldda = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( trans.security.decode_id( id ) )
            except:
                ldda = None
            if ldda:
                library = ldda.library_dataset.folder.parent_library
            self._check_access( trans, cntrller, is_admin, ldda, current_user_roles, use_panels, library_id, show_deleted )
            lddas.append( ldda )
            libraries.append( library )
        library = libraries[0]
        if filter( lambda x: x != library, libraries ):
            message = "Library datasets specified span multiple libraries."
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              id=library_id,
                                                              cntrller=cntrller,
                                                              use_panels=use_panels,
                                                              message=util.sanitize_text( message ),
                                                              status='error' ) )
        # If access to the dataset is restricted, then use the roles associated with the DATASET_ACCESS permission to
        # determine the legitimate roles.  If the dataset is public, see if access to the library is restricted.  If
        # it is, use the roles associated with the LIBRARY_ACCESS permission to determine the legitimate roles.  If both
        # the dataset and the library are public, all roles are legitimate.  All of the datasets will have the same
        # permissions at this point.
        ldda = lddas[0]
        if trans.app.security_agent.dataset_is_public( ldda.dataset ):
            # The dataset is public, so check access to the library
            roles = trans.app.security_agent.get_legitimate_roles( trans, library, cntrller )
        else:
            roles = trans.app.security_agent.get_legitimate_roles( trans, ldda.dataset, cntrller )
        if params.get( 'update_roles_button', False ):
            # Dataset permissions
            access_action = trans.app.security_agent.get_action( trans.app.security_agent.permitted_actions.DATASET_ACCESS.action )
            manage_permissions_action = trans.app.security_agent.get_action( trans.app.security_agent.permitted_actions.DATASET_MANAGE_PERMISSIONS.action )
            permissions, in_roles, error, message = \
                trans.app.security_agent.derive_roles_from_access( trans, trans.app.security.decode_id( library_id ), cntrller, library=True, **kwd )
            # Keep roles for DATASET_MANAGE_PERMISSIONS on the dataset
            if not ldda.has_manage_permissions_roles( trans ):
                # Permission setting related to DATASET_MANAGE_PERMISSIONS was broken for a period of time,
                # so it is possible that some Datasets have no roles associated with the DATASET_MANAGE_PERMISSIONS
                # permission.  In this case, we'll reset this permission to the ldda user's private role.
                #dataset_manage_permissions_roles = [ trans.app.security_agent.get_private_user_role( ldda.user ) ]
                permissions[ manage_permissions_action ] = [ trans.app.security_agent.get_private_user_role( ldda.user ) ]
            else:
                permissions[ manage_permissions_action ] = ldda.get_manage_permissions_roles( trans )
            for ldda in lddas:
                # Set the DATASET permissions on the Dataset.
                if error:
                    # Keep the original role associations for the DATASET_ACCESS permission on the ldda.
                    permissions[ access_action ] = ldda.get_access_roles( trans )
                    status = 'error'
                else:
                    error = trans.app.security_agent.set_all_dataset_permissions( ldda.dataset, permissions )
                    if error:
                        message += error
                        status = 'error'
                    trans.sa_session.refresh( ldda.dataset )
            if not error:
                # Set the LIBRARY permissions on the LibraryDataset.  The LibraryDataset and
                # LibraryDatasetDatasetAssociation will be set with the same permissions.
                permissions = {}
                for k, v in trans.app.model.Library.permitted_actions.items():
                    if k != 'LIBRARY_ACCESS':
                        # LIBRARY_ACCESS is a special permission set only at the library level and it is not inherited.
                        in_roles = [ trans.sa_session.query( trans.app.model.Role ).get( x ) for x in util.listify( kwd.get( k + '_in', [] ) ) ]
                        permissions[ trans.app.security_agent.get_action( v.action ) ] = in_roles
                for ldda in lddas:
                    error = trans.app.security_agent.set_all_library_permissions( trans, ldda.library_dataset, permissions )
                    trans.sa_session.refresh( ldda.library_dataset )
                    if error:
                        message = error
                    else:
                        # Set the LIBRARY permissions on the LibraryDatasetDatasetAssociation
                        trans.app.security_agent.set_all_library_permissions( trans, ldda, permissions )
                        trans.sa_session.refresh( ldda )
                if len( lddas ) == 1:
                    message = "Permissions updated for dataset '%s'." % ldda.name
                else:
                    message = 'Permissions updated for %d datasets.' % len( lddas )
                status= 'done'
            return trans.fill_template( "/library/common/ldda_permissions.mako",
                                        cntrller=cntrller,
                                        use_panels=use_panels,
                                        lddas=lddas,
                                        library_id=library_id,
                                        roles=roles,
                                        show_deleted=show_deleted,
                                        message=message,
                                        status=status )
        if len( ids ) > 1:
            # Ensure that the permissions across all library items are identical, otherwise we can't update them together.
            check_list = []
            for ldda in lddas:
                permissions = []
                # Check the library level permissions - the permissions on the LibraryDatasetDatasetAssociation
                # will always be the same as the permissions on the associated LibraryDataset.
                for library_permission in trans.app.security_agent.get_permissions( ldda.library_dataset ):
                    if library_permission.action not in permissions:
                        permissions.append( library_permission.action )
                for dataset_permission in trans.app.security_agent.get_permissions( ldda.dataset ):
                    if dataset_permission.action not in permissions:
                        permissions.append( dataset_permission.action )
                permissions.sort()
                if not check_list:
                    check_list = permissions
                if permissions != check_list:
                    message = 'The datasets you selected do not have identical permissions, so they can not be updated together'
                    trans.response.send_redirect( web.url_for( controller='library_common',
                                                               action='browse_library',
                                                               cntrller=cntrller,
                                                               use_panels=use_panels,
                                                               id=library_id,
                                                               show_deleted=show_deleted,
                                                               message=util.sanitize_text( message ),
                                                               status='error' ) )
        # Display permission form, permissions will be updated for all lddas simultaneously.
        return trans.fill_template( "/library/common/ldda_permissions.mako",
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    lddas=lddas,
                                    library_id=library_id,
                                    roles=roles,
                                    show_deleted=show_deleted,
                                    message=message,
                                    status=status )
    @web.expose
    def upload_library_dataset( self, trans, cntrller, library_id, folder_id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        ldda_message = util.restore_text( params.get( 'ldda_message', '' ) )
        deleted = util.string_as_bool( params.get( 'deleted', False ) )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        replace_id = params.get( 'replace_id', None )
        replace_dataset = None
        upload_option = params.get( 'upload_option', 'upload_file' )
        if params.get( 'files_0|space_to_tab', False ):
            space_to_tab = params.get( 'files_0|space_to_tab', '' )
        else:
            space_to_tab = params.get( 'space_to_tab', '' )
        link_data_only = params.get( 'link_data_only', 'copy_files' )
        dbkey = params.get( 'dbkey', '?' )
        if isinstance( dbkey, list ):
            last_used_build = dbkey[0]
        else:
            last_used_build = dbkey
        roles = params.get( 'roles', '' )
        is_admin = trans.user_is_admin() and cntrller in ( 'library_admin', 'api' )
        current_user_roles = trans.get_current_user_roles()
        widgets = []
        widget_fields_have_contents = False
        info_association, inherited = None, None
        template_id = "None"
        if replace_id not in [ '', None, 'None' ]:
            replace_dataset = trans.sa_session.query( trans.app.model.LibraryDataset ).get( trans.security.decode_id( replace_id ) )
            self._check_access( trans, cntrller, is_admin, replace_dataset, current_user_roles, use_panels, library_id, show_deleted )
            self._check_modify( trans, cntrller, is_admin, replace_dataset, current_user_roles, use_panels, library_id, show_deleted )
            library = replace_dataset.folder.parent_library
            folder = replace_dataset.folder
            info_association, inherited = replace_dataset.library_dataset_dataset_association.get_info_association()
            if info_association and ( not( inherited ) or info_association.inheritable ):
                widgets = replace_dataset.library_dataset_dataset_association.get_template_widgets( trans )
                widget_fields_have_contents = self.widget_fields_have_contents( widgets )
            # The name is stored - by the time the new ldda is created, replace_dataset.name
            # will point to the new ldda, not the one it's replacing.
            replace_dataset_name = replace_dataset.name
            if not last_used_build:
                last_used_build = replace_dataset.library_dataset_dataset_association.dbkey
        else:
            folder = trans.sa_session.query( trans.app.model.LibraryFolder ).get( trans.security.decode_id( folder_id ) )
            self._check_access( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
            self._check_add( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
            library = folder.parent_library
        if folder and last_used_build in [ 'None', None, '?' ]:
            last_used_build = folder.genome_build
        if params.get( 'runtool_btn', False ) or params.get( 'ajax_upload', False ) or cntrller == 'api':
            error = False
            if upload_option == 'upload_paths' and not trans.app.config.allow_library_path_paste:
                error = True
                message = '"allow_library_path_paste" is not defined in the Galaxy configuration file'
            elif upload_option == 'upload_paths' and not is_admin:
                error = True
                message = 'Uploading files via filesystem paths can only be performed by administrators'
            elif roles:
                # Check to see if the user selected roles to associate with the DATASET_ACCESS permission
                # on the dataset that would cause accessibility issues.
                vars = dict( DATASET_ACCESS_in=roles )
                permissions, in_roles, error, message = \
                    trans.app.security_agent.derive_roles_from_access( trans, library.id, cntrller, library=True, **vars )
            if error:
                if cntrller == 'api':
                    return 400, message
                trans.response.send_redirect( web.url_for( controller='library_common',
                                                           action='upload_library_dataset',
                                                           cntrller=cntrller,
                                                           library_id=library_id,
                                                           folder_id=folder_id,
                                                           replace_id=replace_id,
                                                           upload_option=upload_option,
                                                           show_deleted=show_deleted,
                                                           message=util.sanitize_text( message ),
                                                           status='error' ) )
            else:
                # See if we have any inherited templates.
                if not info_association:
                    info_association, inherited = folder.get_info_association( inherited=True )
                if info_association and info_association.inheritable:
                    template_id = str( info_association.template.id )
                    widgets = folder.get_template_widgets( trans, get_contents=True )
                    processed_widgets = []
                    # The list of widgets may include an AddressField which we need to save if it is new
                    for index, widget_dict in enumerate( widgets ):
                        widget = widget_dict[ 'widget' ]
                        if isinstance( widget, AddressField ):
                            value = util.restore_text( params.get( widget.name, '' ) )
                            if value == 'new':
                                if self.field_param_values_ok( widget.name, 'AddressField', **kwd ):
                                    # Save the new address
                                    address = trans.app.model.UserAddress( user=trans.user )
                                    self.save_widget_field( trans, address, widget.name, **kwd )
                                    widget.value = str( address.id )
                                    widget_dict[ 'widget' ] = widget
                                    processed_widgets.append( widget_dict )
                                    # It is now critical to update the value of 'field_%i', replacing the string
                                    # 'new' with the new address id.  This is necessary because the upload_dataset()
                                    # method below calls the handle_library_params() method, which does not parse the
                                    # widget fields, it instead pulls form values from kwd.  See the FIXME comments in the
                                    # handle_library_params() method, and the CheckboxField code in the next conditional.
                                    kwd[ widget.name ] = str( address.id )
                                else:
                                    # The invalid address won't be saved, but we cannot display error
                                    # messages on the upload form due to the ajax upload already occurring.
                                    # When we re-engineer the upload process ( currently under way ), we
                                    # will be able to check the form values before the ajax upload occurs
                                    # in the background.  For now, we'll do nothing...
                                    pass
                        elif isinstance( widget, CheckboxField ):
                            # We need to check the value from kwd since util.Params would have munged the list if
                            # the checkbox is checked.
                            value = kwd.get( widget.name, '' )
                            if CheckboxField.is_checked( value ):
                                widget.value = 'true'
                                widget_dict[ 'widget' ] = widget
                                processed_widgets.append( widget_dict )
                                kwd[ widget.name ] = 'true'
                        else:
                            processed_widgets.append( widget_dict )
                    widgets = processed_widgets
                created_outputs_dict = trans.webapp.controllers[ 'library_common' ].upload_dataset( trans,
                                                                                                    cntrller=cntrller,
                                                                                                    library_id=trans.security.encode_id( library.id ),
                                                                                                    folder_id=trans.security.encode_id( folder.id ),
                                                                                                    template_id=template_id,
                                                                                                    widgets=widgets,
                                                                                                    replace_dataset=replace_dataset,
                                                                                                    **kwd )
                if created_outputs_dict:
                    if cntrller == 'api':
                        # created_outputs_dict can be a string only if cntrller == 'api'
                        if type( created_outputs_dict ) == str:
                            return 400, created_outputs_dict
                        elif type( created_outputs_dict ) == tuple:
                            return created_outputs_dict[0], created_outputs_dict[1]
                        return 200, created_outputs_dict
                    total_added = len( created_outputs_dict.keys() )
                    ldda_id_list = [ str( v.id ) for k, v in created_outputs_dict.items() ]
                    created_ldda_ids=",".join( ldda_id_list )
                    if replace_dataset:
                        message = "Added %d dataset versions to the library dataset '%s' in the folder '%s'." % ( total_added, replace_dataset_name, folder.name )
                    else:
                        if not folder.parent:
                            # Libraries have the same name as their root_folder
                            message = "Added %d datasets to the library '%s' (each is selected).  " % ( total_added, folder.name )
                        else:
                            message = "Added %d datasets to the folder '%s' (each is selected).  " % ( total_added, folder.name )
                        if cntrller == 'library_admin':
                            message += "Click the Go button at the bottom of this page to edit the permissions on these datasets if necessary."
                            status='done'
                        else:
                            # Since permissions on all LibraryDatasetDatasetAssociations must be the same at this point, we only need
                            # to check one of them to see if the current user can manage permissions on them.
                            check_ldda = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( ldda_id_list[0] )
                            if trans.app.security_agent.can_manage_library_item( current_user_roles, check_ldda ):
                                if replace_dataset:
                                    default_action = ''
                                else:
                                    message += "Click the Go button at the bottom of this page to edit the permissions on these datasets if necessary."
                                    default_action = 'manage_permissions'
                            else:
                                default_action = 'import_to_current_history'
                            trans.response.send_redirect( web.url_for( controller='library_common',
                                                                       action='browse_library',
                                                                       cntrller=cntrller,
                                                                       id=library_id,
                                                                       default_action=default_action,
                                                                       created_ldda_ids=created_ldda_ids,
                                                                       show_deleted=show_deleted,
                                                                       message=util.sanitize_text( message ), 
                                                                       status='done' ) )
                else:
                    created_ldda_ids = ''
                    message = "Upload failed"
                    status='error'
                    if cntrller == 'api':
                        return 400, message
                    response_code = 400
                trans.response.send_redirect( web.url_for( controller='library_common',
                                                           action='browse_library',
                                                           cntrller=cntrller,
                                                           id=library_id,
                                                           created_ldda_ids=created_ldda_ids,
                                                           show_deleted=show_deleted,
                                                           message=util.sanitize_text( message ),
                                                           status=status ) )
        # Note: if the upload form was submitted due to refresh_on_change for a form field, we cannot re-populate
        # the field for the selected file ( files_0|file_data ) if the user selected one.  This is because the value
        # attribute of the html input file type field is typically ignored by browsers as a security precaution. 
        
        # See if we have any inherited templates.
        if not info_association:
            info_association, inherited = folder.get_info_association( inherited=True )
            if info_association and info_association.inheritable:
                widgets = folder.get_template_widgets( trans, get_contents=True )
        if info_association:
            # Retain contents of widget fields when form was submitted via refresh_on_change.
            widgets = self.populate_widgets_from_kwd( trans, widgets, **kwd )
            template_id = str( info_association.template.id )
        
        # Send list of data formats to the upload form so the "extension" select list can be populated dynamically
        file_formats = trans.app.datatypes_registry.upload_file_formats
        dbkeys = trans.app.genomes.get_dbkeys_with_chrom_info( trans )
        # Send the current history to the form to enable importing datasets from history to library
        history = trans.get_history()
        if history is not None:
            trans.sa_session.refresh( history )
        if upload_option == 'upload_file' and trans.app.config.nginx_upload_path:
            # If we're using nginx upload, override the form action -
            # url_for is intentionally not used on the base URL here -
            # nginx_upload_path is expected to include the proxy prefix if the
            # administrator intends for it to be part of the URL.
            action = trans.app.config.nginx_upload_path + '?nginx_redir=' + web.url_for( controller='library_common', action='upload_library_dataset' )
        else:
            action = web.url_for( controller='library_common', action='upload_library_dataset' )
        do_not_display_options = []
        if replace_dataset:
            # TODO: Not sure why, but 'upload_paths' is not allowed if replacing a dataset.  See self.make_library_uploaded_dataset().
            do_not_display_options = [ 'upload_directory', 'upload_paths' ]
        upload_option_select_list = self._build_upload_option_select_list( trans, upload_option, is_admin, do_not_display_options )
        roles_select_list = self._build_roles_select_list( trans, cntrller, library, util.listify( roles ) )
        return trans.fill_template( '/library/common/upload.mako',
                                    cntrller=cntrller,
                                    upload_option_select_list=upload_option_select_list,
                                    upload_option=upload_option,
                                    action=action,
                                    library_id=library_id,
                                    folder_id=folder_id,
                                    replace_dataset=replace_dataset,
                                    file_formats=file_formats,
                                    dbkeys=dbkeys,
                                    last_used_build=last_used_build,
                                    roles_select_list=roles_select_list,
                                    history=history,
                                    widgets=widgets,
                                    template_id=template_id,
                                    space_to_tab=space_to_tab,
                                    link_data_only=link_data_only,
                                    show_deleted=show_deleted,
                                    ldda_message=ldda_message,
                                    message=message,
                                    status=status )
    def upload_dataset( self, trans, cntrller, library_id, folder_id, replace_dataset=None, **kwd ):
        # Set up the traditional tool state/params
        tool_id = 'upload1'
        tool = trans.app.toolbox.get_tool( tool_id )
        state = tool.new_state( trans )
        errors = tool.update_state( trans, tool.inputs_by_page[0], state.inputs, kwd )
        tool_params = state.inputs
        dataset_upload_inputs = []
        for input_name, input in tool.inputs.iteritems():
            if input.type == "upload_dataset":
                dataset_upload_inputs.append( input )
        # Library-specific params
        params = util.Params( kwd ) # is this filetoolparam safe?
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        server_dir = util.restore_text( params.get( 'server_dir', '' ) )
        if replace_dataset not in [ None, 'None' ]:
            replace_id = trans.security.encode_id( replace_dataset.id )
        else:
            replace_id = None
        upload_option = params.get( 'upload_option', 'upload_file' )
        response_code = 200
        if upload_option == 'upload_directory':
            if server_dir in [ None, 'None', '' ]:
                response_code = 400
            if cntrller == 'library_admin' or ( cntrller == 'api' and trans.user_is_admin ):
                import_dir = trans.app.config.library_import_dir
                import_dir_desc = 'library_import_dir'
                full_dir = os.path.join( import_dir, server_dir )
            else:
                import_dir = trans.app.config.user_library_import_dir
                import_dir_desc = 'user_library_import_dir'
                if server_dir == trans.user.email:
                    full_dir = os.path.join( import_dir, server_dir )
                else:
                    full_dir = os.path.join( import_dir, trans.user.email, server_dir )
            if import_dir:
                message = 'Select a directory'
            else:
                response_code = 403
                message = '"%s" is not defined in the Galaxy configuration file' % import_dir_desc
        elif upload_option == 'upload_paths':
            if not trans.app.config.allow_library_path_paste:
                response_code = 403
                message = '"allow_library_path_paste" is not defined in the Galaxy configuration file'
        # Some error handling should be added to this method.
        try:
            # FIXME: instead of passing params here ( which have been processed by util.Params(), the original kwd
            # should be passed so that complex objects that may have been included in the initial request remain.
            library_bunch = upload_common.handle_library_params( trans, params, folder_id, replace_dataset )
        except:
            response_code = 500
            message = "Unable to parse upload parameters, please report this error."
        # Proceed with (mostly) regular upload processing if we're still errorless
        if response_code == 200:
            precreated_datasets = upload_common.get_precreated_datasets( trans, tool_params, trans.app.model.LibraryDatasetDatasetAssociation, controller=cntrller )
            if upload_option == 'upload_file':
                tool_params = upload_common.persist_uploads( tool_params )
                uploaded_datasets = upload_common.get_uploaded_datasets( trans, cntrller, tool_params, precreated_datasets, dataset_upload_inputs, library_bunch=library_bunch )
            elif upload_option == 'upload_directory':
                uploaded_datasets, response_code, message = self.get_server_dir_uploaded_datasets( trans, cntrller, params, full_dir, import_dir_desc, library_bunch, response_code, message )
            elif upload_option == 'upload_paths':
                uploaded_datasets, response_code, message = self.get_path_paste_uploaded_datasets( trans, cntrller, params, library_bunch, response_code, message )
            upload_common.cleanup_unused_precreated_datasets( precreated_datasets )
            if upload_option == 'upload_file' and not uploaded_datasets:
                response_code = 400
                message = 'Select a file, enter a URL or enter text'
        if response_code != 200:
            if cntrller == 'api':
                return ( response_code, message )
            trans.response.send_redirect( web.url_for( controller='library_common',
                                                       action='upload_library_dataset',
                                                       cntrller=cntrller,
                                                       library_id=library_id,
                                                       folder_id=folder_id,
                                                       replace_id=replace_id,
                                                       upload_option=upload_option,
                                                       show_deleted=show_deleted,
                                                       message=util.sanitize_text( message ),
                                                       status='error' ) )
        json_file_path = upload_common.create_paramfile( trans, uploaded_datasets )
        data_list = [ ud.data for ud in uploaded_datasets ]
        job, output = upload_common.create_job( trans, tool_params, tool, json_file_path, data_list, folder=library_bunch.folder )
        # HACK: Prevent outputs_to_working_directory from overwriting inputs when "linking"
        job.add_parameter( 'link_data_only', to_json_string( kwd.get( 'link_data_only', 'copy_files' ) ) )
        trans.sa_session.add( job )
        trans.sa_session.flush()
        return output
    def make_library_uploaded_dataset( self, trans, cntrller, params, name, path, type, library_bunch, in_folder=None ):
        link_data_only = params.get( 'link_data_only', 'copy_files' )
        library_bunch.replace_dataset = None # not valid for these types of upload
        uploaded_dataset = util.bunch.Bunch()
        new_name = name
        # Remove compressed file extensions, if any, but only if
        # we're copying files into Galaxy's file space.
        if link_data_only == 'copy_files':
            if new_name.endswith( '.gz' ):
                new_name = new_name.rstrip( '.gz' )
            elif new_name.endswith( '.zip' ):
                new_name = new_name.rstrip( '.zip' )
        uploaded_dataset.name = new_name
        uploaded_dataset.path = path
        uploaded_dataset.type = type
        uploaded_dataset.ext = None
        uploaded_dataset.file_type = params.file_type
        uploaded_dataset.dbkey = params.dbkey
        uploaded_dataset.space_to_tab = params.space_to_tab
        if in_folder:
            uploaded_dataset.in_folder = in_folder
        uploaded_dataset.data = upload_common.new_upload( trans, cntrller, uploaded_dataset, library_bunch )
        uploaded_dataset.link_data_only = link_data_only
        if link_data_only == 'link_to_files':
            uploaded_dataset.data.file_name = os.path.abspath( path )
            # Since we are not copying the file into Galaxy's managed
            # default file location, the dataset should never be purgable.
            uploaded_dataset.data.dataset.purgable = False
            trans.sa_session.add_all( ( uploaded_dataset.data, uploaded_dataset.data.dataset ) )
            trans.sa_session.flush()
        return uploaded_dataset
    def get_server_dir_uploaded_datasets( self, trans, cntrller, params, full_dir, import_dir_desc, library_bunch, response_code, message ):
        files = []
        try:
            for entry in os.listdir( full_dir ):
                # Only import regular files
                path = os.path.join( full_dir, entry )
                link_data_only = params.get( 'link_data_only', 'copy_files' )
                if os.path.islink( full_dir ) and link_data_only == 'link_to_files':
                    # If we're linking instead of copying and the
                    # sub-"directory" in the import dir is actually a symlink,
                    # dereference the symlink, but not any of its contents.
                    link_path = os.readlink( full_dir )
                    if os.path.isabs( link_path ):
                        path = os.path.join( link_path, entry )
                    else:
                        path = os.path.abspath( os.path.join( link_path, entry ) )
                elif os.path.islink( path ) and os.path.isfile( path ) and link_data_only == 'link_to_files':
                    # If we're linking instead of copying and the "file" in the
                    # sub-directory of the import dir is actually a symlink,
                    # dereference the symlink (one dereference only, Vasili).
                    link_path = os.readlink( path )
                    if os.path.isabs( link_path ):
                        path = link_path
                    else:
                        path = os.path.abspath( os.path.join( os.path.dirname( path ), link_path ) )
                if os.path.isfile( path ):
                    files.append( path )
        except Exception, e:
            message = "Unable to get file list for configured %s, error: %s" % ( import_dir_desc, str( e ) )
            response_code = 500
            return None, response_code, message
        if not files:
            message = "The directory '%s' contains no valid files" % full_dir
            response_code = 400
            return None, response_code, message
        uploaded_datasets = []
        for file in files:
            name = os.path.basename( file )
            uploaded_datasets.append( self.make_library_uploaded_dataset( trans, cntrller, params, name, file, 'server_dir', library_bunch ) )
        return uploaded_datasets, 200, None
    def get_path_paste_uploaded_datasets( self, trans, cntrller, params, library_bunch, response_code, message ):
        if params.get( 'filesystem_paths', '' ) == '':
            message = "No paths entered in the upload form"
            response_code = 400
            return None, response_code, message
        preserve_dirs = util.string_as_bool( params.get( 'preserve_dirs', False ) )
        # locate files
        bad_paths = []
        uploaded_datasets = []
        for line in [ l.strip() for l in params.filesystem_paths.splitlines() if l.strip() ]:
            path = os.path.abspath( line )
            if not os.path.exists( path ):
                bad_paths.append( path )
                continue
            # don't bother processing if we're just going to return an error
            if not bad_paths:
                if os.path.isfile( path ):
                    name = os.path.basename( path )
                    uploaded_datasets.append( self.make_library_uploaded_dataset( trans, cntrller, params, name, path, 'path_paste', library_bunch ) )
                for basedir, dirs, files in os.walk( line ):
                    for file in files:
                        file_path = os.path.abspath( os.path.join( basedir, file ) )
                        if preserve_dirs:
                            in_folder = os.path.dirname( file_path.replace( path, '', 1 ).lstrip( '/' ) )
                        else:
                            in_folder = None
                        uploaded_datasets.append( self.make_library_uploaded_dataset( trans,
                                                                                      cntrller,
                                                                                      params,
                                                                                      file,
                                                                                      file_path,
                                                                                      'path_paste',
                                                                                      library_bunch,
                                                                                      in_folder ) )
        if bad_paths:
            message = "Invalid paths:<br><ul><li>%s</li></ul>" % "</li><li>".join( bad_paths )
            response_code = 400
            return None, response_code, message
        return uploaded_datasets, 200, None
    @web.expose
    def add_history_datasets_to_library( self, trans, cntrller, library_id, folder_id, hda_ids='', **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        ldda_message = util.restore_text( params.get( 'ldda_message', '' ) )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        replace_id = params.get( 'replace_id', None )
        replace_dataset = None
        upload_option = params.get( 'upload_option', 'import_from_history' )
        if params.get( 'files_0|space_to_tab', False ):
            space_to_tab = params.get( 'files_0|space_to_tab', '' )
        else:
            space_to_tab = params.get( 'space_to_tab', '' )
        link_data_only = params.get( 'link_data_only', 'copy_files' )
        dbkey = params.get( 'dbkey', '?' )
        if isinstance( dbkey, list ):
            last_used_build = dbkey[0]
        else:
            last_used_build = dbkey
        roles = params.get( 'roles', '' )
        is_admin = trans.user_is_admin() and cntrller in ( 'library_admin', 'api' )
        current_user_roles = trans.get_current_user_roles()
        widgets = []
        widget_fields_have_contents = False
        info_association, inherited = None, None
        template_id = "None"
        if replace_id not in [ None, 'None' ]:
            try:
                replace_dataset = trans.sa_session.query( trans.app.model.LibraryDataset ).get( trans.security.decode_id( replace_id ) )
            except:
                replace_dataset = None
            self._check_access( trans, cntrller, is_admin, replace_dataset, current_user_roles, use_panels, library_id, show_deleted )
            self._check_modify( trans, cntrller, is_admin, replace_dataset, current_user_roles, use_panels, library_id, show_deleted )
            library = replace_dataset.folder.parent_library
            folder = replace_dataset.folder
            last_used_build = replace_dataset.library_dataset_dataset_association.dbkey
            info_association, inherited = replace_dataset.library_dataset_dataset_association.get_info_association()
            if info_association and ( not( inherited ) or info_association.inheritable ):
                widgets = replace_dataset.library_dataset_dataset_association.get_template_widgets( trans )
                widget_fields_have_contents = self.widget_fields_have_contents( widgets )
                template_id = str( info_association.template.id )
        else:
            folder = trans.sa_session.query( trans.app.model.LibraryFolder ).get( trans.security.decode_id( folder_id ) )
            self._check_access( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
            self._check_add( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
            library = folder.parent_library
            last_used_build = folder.genome_build
        # See if the current history is empty
        history = trans.get_history()
        trans.sa_session.refresh( history )
        if not history.active_datasets:
            message = 'Your current history is empty'
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status='error' ) )
        if params.get( 'add_history_datasets_to_library_button', False ):
            hda_ids = util.listify( hda_ids )
            if hda_ids:
                dataset_names = []
                created_ldda_ids = ''
                for hda_id in hda_ids:
                    try:
                        hda = trans.sa_session.query( trans.app.model.HistoryDatasetAssociation ).get( trans.security.decode_id( hda_id ) )
                    except:
                        hda = None
                    self._check_access( trans, cntrller, is_admin, hda, current_user_roles, use_panels, library_id, show_deleted )
                    if roles:
                        role_ids = roles.split( ',' )
                        role_obj_list = [ trans.sa_session.query( trans.model.Role ).get( role_id ) for role_id in role_ids ]
                    else:
                        role_obj_list = []
                    ldda = hda.to_library_dataset_dataset_association( trans,
                                                                       target_folder=folder,
                                                                       replace_dataset=replace_dataset,
                                                                       roles=role_obj_list,
                                                                       ldda_message=ldda_message )
                    created_ldda_ids = '%s,%s' % ( created_ldda_ids, str( ldda.id ) )
                    dataset_names.append( ldda.name )
                    if not replace_dataset:
                        # If replace_dataset is None, the Library level permissions will be taken from the folder and applied to the new 
                        # LDDA and LibraryDataset.
                        trans.app.security_agent.copy_library_permissions( trans, folder, ldda )
                        trans.app.security_agent.copy_library_permissions( trans, folder, ldda.library_dataset )
                    else:
                        library_bunch = upload_common.handle_library_params( trans, params, folder_id, replace_dataset )
                        if library_bunch.template and library_bunch.template_field_contents:
                            # Since information templates are inherited, the template fields can be displayed on the upload form.
                            # If the user has added field contents, we'll need to create a new form_values and info_association
                            # for the new library_dataset_dataset_association object.
                            # Create a new FormValues object, using the template we previously retrieved
                            form_values = trans.app.model.FormValues( library_bunch.template, library_bunch.template_field_contents )
                            trans.sa_session.add( form_values )
                            trans.sa_session.flush()
                            # Create a new info_association between the current ldda and form_values
                            info_association = trans.app.model.LibraryDatasetDatasetInfoAssociation( ldda, library_bunch.template, form_values )
                            trans.sa_session.add( info_association )
                            trans.sa_session.flush()
                    # Make sure to apply any defined dataset permissions, allowing the permissions inherited from the folder to
                    # over-ride the same permissions on the dataset, if they exist.
                    dataset_permissions_dict = trans.app.security_agent.get_permissions( hda.dataset )
                    current_library_dataset_actions = [ permission.action for permission in ldda.library_dataset.actions ]
                    # The DATASET_MANAGE_PERMISSIONS permission on a dataset is a special case because if
                    # it exists, then we need to apply the LIBRARY_MANAGE permission to the library dataset.
                    dataset_manage_permissions_action = trans.app.security_agent.get_action( 'DATASET_MANAGE_PERMISSIONS' ).action
                    flush_needed = False
                    for action, dataset_permissions_roles in dataset_permissions_dict.items():
                        if isinstance( action, Action ):
                            action = action.action
                        if action == dataset_manage_permissions_action:
                            # Apply the LIBRARY_MANAGE permission to the library dataset.
                            action = trans.app.security_agent.get_action( 'LIBRARY_MANAGE' ).action
                        # Allow the permissions inherited from the folder to over-ride the same permissions on the dataset.
                        if action not in current_library_dataset_actions:
                            for ldp in [ trans.model.LibraryDatasetPermissions( action, ldda.library_dataset, role ) for role in dataset_permissions_roles ]:
                                trans.sa_session.add( ldp )
                                flush_needed = True
                    if flush_needed:
                        trans.sa_session.flush()
                    # Permissions must be the same on the LibraryDatasetDatasetAssociation and the associated LibraryDataset
                    trans.app.security_agent.copy_library_permissions( trans, ldda.library_dataset, ldda )
                if created_ldda_ids:
                    created_ldda_ids = created_ldda_ids.lstrip( ',' )
                    ldda_id_list = created_ldda_ids.split( ',' )
                    total_added = len( ldda_id_list )
                    if replace_dataset:
                        message = "Added %d dataset versions to the library dataset '%s' in the folder '%s'." % ( total_added, replace_dataset.name, folder.name )
                    else:
                        if not folder.parent:
                            # Libraries have the same name as their root_folder
                            message = "Added %d datasets to the library '%s' (each is selected).  " % ( total_added, folder.name )
                        else:
                            message = "Added %d datasets to the folder '%s' (each is selected).  " % ( total_added, folder.name )
                        if cntrller == 'library_admin':
                            message += "Click the Go button at the bottom of this page to edit the permissions on these datasets if necessary."
                        else:
                            # Since permissions on all LibraryDatasetDatasetAssociations must be the same at this point, we only need
                            # to check one of them to see if the current user can manage permissions on them.
                            check_ldda = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( ldda_id_list[0] )
                            if trans.app.security_agent.can_manage_library_item( current_user_roles, check_ldda ):
                                if replace_dataset:
                                    default_action = ''
                                else:
                                    message += "Click the Go button at the bottom of this page to edit the permissions on these datasets if necessary."
                                    default_action = 'manage_permissions'
                            else:
                                default_action = 'import_to_current_history'
                    return trans.response.send_redirect( web.url_for( controller='library_common',
                                                                      action='browse_library',
                                                                      cntrller=cntrller,
                                                                      id=library_id,
                                                                      created_ldda_ids=created_ldda_ids,
                                                                      show_deleted=show_deleted,
                                                                      message=util.sanitize_text( message ),
                                                                      status='done' ) )
            else:
                message = 'Select at least one dataset from the list of active datasets in your current history'
                status = 'error'
                upload_option = params.get( 'upload_option', 'import_from_history' )
                widgets = self._get_populated_widgets( folder )
                # Send list of data formats to the upload form so the "extension" select list can be populated dynamically
                file_formats = trans.app.datatypes_registry.upload_file_formats
                # Send list of genome builds to the form so the "dbkey" select list can be populated dynamically
                def get_dbkey_options( last_used_build ):
                    for dbkey, build_name in util.dbnames:
                        yield build_name, dbkey, ( dbkey==last_used_build )
                dbkeys = get_dbkey_options( last_used_build )
                # Send the current history to the form to enable importing datasets from history to library
                history = trans.get_history()
                trans.sa_session.refresh( history )
                action = 'add_history_datasets_to_library'
                upload_option_select_list = self._build_upload_option_select_list( trans, upload_option, is_admin )
                roles_select_list = self._build_roles_select_list( trans, cntrller, library, util.listify( roles ) )
                return trans.fill_template( "/library/common/upload.mako",
                                            cntrller=cntrller,
                                            upload_option_select_list=upload_option_select_list,
                                            upload_option=upload_option,
                                            action=action,
                                            library_id=library_id,
                                            folder_id=folder_id,
                                            replace_dataset=replace_dataset,
                                            file_formats=file_formats,
                                            dbkeys=dbkeys,
                                            last_used_build=last_used_build,
                                            roles_select_list=roles_select_list,
                                            history=history,
                                            widgets=widgets,
                                            template_id=template_id,
                                            space_to_tab=space_to_tab,
                                            link_data_only=link_data_only,
                                            show_deleted=show_deleted,
                                            ldda_message=ldda_message,
                                            message=message,
                                            status=status )
    def _build_roles_select_list( self, trans, cntrller, library, selected_role_ids=[] ):
        # Get the list of legitimate roles to display on the upload form.  If the library is public,
        # all active roles are legitimate.  If the library is restricted by the LIBRARY_ACCESS permission, only
        # the set of all roles associated with users that have that permission are legitimate.
        legitimate_roles = trans.app.security_agent.get_legitimate_roles( trans, library, cntrller )
        if legitimate_roles:
            # Build the roles multi-select list using the list of legitimate roles, making sure to select any that
            # were selected before refresh_on_change, if one occurred.
            roles_select_list = SelectField( "roles", multiple="true", size="5" )
            for role in legitimate_roles:
                selected = str( role.id ) in selected_role_ids
                roles_select_list.add_option( text=role.name, value=str( role.id ), selected=selected )
            return roles_select_list
        else:
            return None
    def _build_upload_option_select_list( self, trans, upload_option, is_admin, do_not_include_values=[] ):
        # Build the upload_option select list.  The do_not_include_values param can contain options that
        # should not be included in the list.  For example, the 'upload_directory' option should not be
        # included if uploading a new version of a librar dataset.
        upload_refresh_on_change_values = []
        for option_value, option_label in trans.model.LibraryDataset.upload_options:
            if option_value not in do_not_include_values:
                upload_refresh_on_change_values.append( option_value )
        upload_option_select_list = SelectField( 'upload_option', 
                                                 refresh_on_change=True, 
                                                 refresh_on_change_values=upload_refresh_on_change_values )
        for option_value, option_label in trans.model.LibraryDataset.upload_options:
            if option_value not in do_not_include_values:
                if option_value == 'upload_directory':
                    if is_admin and not trans.app.config.library_import_dir:
                        continue
                    elif not is_admin:
                        if not trans.app.config.user_library_import_dir:
                            continue
                        path = os.path.join( trans.app.config.user_library_import_dir, trans.user.email )
                        if not os.path.isdir( path ):
                            try:
                                os.makedirs( path )
                            except:
                                continue
                elif option_value == 'upload_paths':
                    if not is_admin or not trans.app.config.allow_library_path_paste:
                        continue
                upload_option_select_list.add_option( option_label, option_value, selected=option_value==upload_option )
        return upload_option_select_list
    def _get_populated_widgets( self, folder ):
        # See if we have any inherited templates.
        info_association, inherited = folder.get_info_association( inherited=True )
        if info_association and info_association.inheritable:
            widgets = folder.get_template_widgets( trans, get_contents=True )
            # Retain contents of widget fields when form was submitted via refresh_on_change.
            return self.populate_widgets_from_kwd( trans, widgets, **kwd )
        else:
            return []
    @web.expose
    def download_dataset_from_folder( self, trans, cntrller, id, library_id=None, **kwd ):
        """Catches the dataset id and displays file contents as directed"""
        show_deleted = util.string_as_bool( kwd.get( 'show_deleted', False ) )
        params = util.Params( kwd )        
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        try:
            ldda = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( trans.security.decode_id( id ) )
        except:
            ldda = None
        self._check_access( trans, cntrller, is_admin, ldda, current_user_roles, use_panels, library_id, show_deleted )
        composite_extensions = trans.app.datatypes_registry.get_composite_extensions( )
        ext = ldda.extension
        if ext in composite_extensions:
            # is composite - must return a zip of contents and the html file itself - ugh - should be reversible at upload!
            # use act_on_multiple_datasets( self, trans, cntrller, library_id, ldda_ids='', **kwd ) since it does what we need
            kwd['do_action'] = 'zip'
            return self.act_on_multiple_datasets( trans, cntrller, library_id, ldda_ids=[id,], **kwd )
        else:
            trans.response.set_content_type( ldda.get_mime() )
            fStat = os.stat( ldda.file_name )
            trans.response.headers[ 'Content-Length' ] = int( fStat.st_size )
            valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
            fname = ldda.name
            fname = ''.join( c in valid_chars and c or '_' for c in fname )[ 0:150 ]
            trans.response.headers[ "Content-Disposition" ] = 'attachment; filename="%s"' % fname
            try:
                return open( ldda.file_name )
            except: 
                message = 'This dataset contains no content'
        return trans.response.send_redirect( web.url_for( controller='library_common',
                                                          action='browse_library',
                                                          cntrller=cntrller,
                                                          use_panels=use_panels,
                                                          id=library_id,
                                                          show_deleted=show_deleted,
                                                          message=util.sanitize_text( message ),
                                                          status='error' ) )
    @web.expose
    def library_dataset_info( self, trans, cntrller, id, library_id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        try:
            library_dataset = trans.sa_session.query( trans.app.model.LibraryDataset ).get( trans.security.decode_id( id ) )
        except:
            library_dataset = None
        self._check_access( trans, cntrller, is_admin, library_dataset, current_user_roles, use_panels, library_id, show_deleted )
        if params.get( 'edit_attributes_button', False ):
            self._check_modify( trans, cntrller, is_admin, library_dataset, current_user_roles, use_panels, library_id, show_deleted )
            old_name = library_dataset.name
            new_name = util.restore_text( params.get( 'name', '' ) )
            new_info = util.restore_text( params.get( 'info', '' ) )
            if not new_name:
                message = 'Enter a valid name'
                status = 'error'
            else:
                library_dataset.name = new_name
                library_dataset.info = new_info
                trans.sa_session.add( library_dataset )
                trans.sa_session.flush()
                message = "Information updated for library dataset '%s'." % library_dataset.name
                status = 'done'
        # See if we have any associated templates
        widgets = []
        widget_fields_have_contents = False
        info_association, inherited = library_dataset.library_dataset_dataset_association.get_info_association()
        if info_association and ( not( inherited ) or info_association.inheritable ):
            widgets = library_dataset.library_dataset_dataset_association.get_template_widgets( trans )
            widget_fields_have_contents = self.widget_fields_have_contents( widgets )
        return trans.fill_template( '/library/common/library_dataset_info.mako',
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    library_dataset=library_dataset,
                                    library_id=library_id,
                                    current_user_roles=current_user_roles,
                                    info_association=info_association,
                                    inherited=inherited,
                                    widgets=widgets,
                                    widget_fields_have_contents=widget_fields_have_contents,
                                    show_deleted=show_deleted,
                                    message=message,
                                    status=status )
    @web.expose
    def library_dataset_permissions( self, trans, cntrller, id, library_id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        try:
            library_dataset = trans.sa_session.query( trans.app.model.LibraryDataset ).get( trans.security.decode_id( id ) )
        except:
            library_dataset = None
        self._check_access( trans, cntrller, is_admin, library_dataset, current_user_roles, use_panels, library_id, show_deleted )
        self._check_manage( trans, cntrller, is_admin, library_dataset, current_user_roles, use_panels, library_id, show_deleted )
        if params.get( 'update_roles_button', False ):
            # The user clicked the Save button on the 'Associate With Roles' form
            permissions = {}
            for k, v in trans.app.model.Library.permitted_actions.items():
                if k != 'LIBRARY_ACCESS':
                    # LIBRARY_ACCESS is a special permission set only at the library level
                    # and it is not inherited.
                    in_roles = [ trans.sa_session.query( trans.app.model.Role ).get( x ) for x in util.listify( kwd.get( k + '_in', [] ) ) ]
                    permissions[ trans.app.security_agent.get_action( v.action ) ] = in_roles
            # Set the LIBRARY permissions on the LibraryDataset
            # NOTE: the LibraryDataset and LibraryDatasetDatasetAssociation will be set with the same permissions
            error = trans.app.security_agent.set_all_library_permissions( trans, library_dataset, permissions )
            trans.sa_session.refresh( library_dataset )
            if error:
                message = error
                status = 'error'
            else:
                # Set the LIBRARY permissions on the LibraryDatasetDatasetAssociation
                trans.app.security_agent.set_all_library_permissions( trans, library_dataset.library_dataset_dataset_association, permissions )
                trans.sa_session.refresh( library_dataset.library_dataset_dataset_association )
                message = "Permisisons updated for library dataset '%s'." % library_dataset.name
                status = 'done'
        roles = trans.app.security_agent.get_legitimate_roles( trans, library_dataset, cntrller )
        return trans.fill_template( '/library/common/library_dataset_permissions.mako',
                                    cntrller=cntrller,
                                    use_panels=use_panels,
                                    library_dataset=library_dataset,
                                    library_id=library_id,
                                    roles=roles,
                                    current_user_roles=current_user_roles,
                                    show_deleted=show_deleted,
                                    message=message,
                                    status=status )
    @web.expose
    def make_library_item_public( self, trans, cntrller, library_id, item_type, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        current_user_roles = trans.get_current_user_roles()
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        if item_type == 'library':
            library = trans.sa_session.query( trans.model.Library ).get( trans.security.decode_id( id ) )
            self._check_access( trans, cntrller, is_admin, library, current_user_roles, use_panels, library_id, show_deleted )
            self._check_manage( trans, cntrller, is_admin, library, current_user_roles, use_panels, library_id, show_deleted )
            contents = util.string_as_bool( params.get( 'contents', 'False' ) )
            trans.app.security_agent.make_library_public( library, contents=contents )
            if contents:
                message = "The data library (%s) and all it's contents have been made publicly accessible." % library.name
            else:
                message = "The data library (%s) has been made publicly accessible, but access to it's contents has been left unchanged." % library.name
        elif item_type == 'folder':
            folder = trans.sa_session.query( trans.model.LibraryFolder ).get( trans.security.decode_id( id ) )
            self._check_access( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
            self._check_manage( trans, cntrller, is_admin, folder, current_user_roles, use_panels, library_id, show_deleted )
            trans.app.security_agent.make_folder_public( folder )
            message = "All of the contents of folder (%s) have been made publicly accessible." % folder.name
        elif item_type == 'ldda':
            ldda = trans.sa_session.query( trans.model.LibraryDatasetDatasetAssociation ).get( trans.security.decode_id( id ) )
            self._check_access( trans, cntrller, is_admin, ldda.library_dataset, current_user_roles, use_panels, library_id, show_deleted )
            self._check_manage( trans, cntrller, is_admin, ldda.library_dataset, current_user_roles, use_panels, library_id, show_deleted )
            trans.app.security_agent.make_dataset_public( ldda.dataset )
            message = "The libary dataset (%s) has been made publicly accessible." % ldda.name
        else:
            message = "Invalid item_type (%s) received." % str( item_type )
            status = 'error'
        return trans.response.send_redirect( web.url_for( controller='library_common',
                                                          action='browse_library',
                                                          cntrller=cntrller,
                                                          use_panels=use_panels,
                                                          id=library_id,
                                                          show_deleted=show_deleted,
                                                          message=util.sanitize_text( message ),
                                                          status=status ) )
    @web.expose
    def act_on_multiple_datasets( self, trans, cntrller, library_id=None, ldda_ids='', **kwd ):
        # This method is called from 1 of 3 places:
        # - this controller's download_dataset_from_folder() method
        # - he browse_library.mako template
        # - the library_dataset_search_results.mako template
        # In the last case above, we will not have a library_id
        class NgxZip( object ):
            def __init__( self, url_base ):
                self.files = {}
                self.url_base = url_base
            def add( self, file, relpath ):
                self.files[file] = relpath
            def __str__( self ):
                rval = ''
                for fname, relpath in self.files.items():
                    crc = '-'
                    size = os.stat( fname ).st_size
                    quoted_fname = urllib.quote_plus( fname, '/' )
                    rval += '%s %i %s%s %s\r\n' % ( crc, size, self.url_base, quoted_fname, relpath )
                return rval
        # Perform an action on a list of library datasets.
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        action = params.get( 'do_action', None )
        lddas = []
        error = False
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        current_user_roles = trans.get_current_user_roles()
        current_history = trans.get_history()
        if not ldda_ids:
            error = True
            message = 'You must select at least one dataset.'
        elif not action:
            error = True
            message = 'You must select an action to perform on the selected datasets.'
        else:
            if action in [ 'import_to_current_history', 'import_to_histories' ]:
                new_kwd = {}
                if action == 'import_to_current_history':
                    encoded_current_history_id = trans.security.encode_id( current_history.id )
                    selected_history_id = encoded_current_history_id
                    new_kwd[ 'do_action' ] = action
                    new_kwd[ 'target_history_ids' ] = encoded_current_history_id
                    new_kwd[ 'import_datasets_to_histories_button' ] = 'Import library datasets'
                else:
                    selected_history_id = ''
                return trans.response.send_redirect( web.url_for( controller='library_common',
                                                                  action='import_datasets_to_histories',
                                                                  cntrller=cntrller,
                                                                  library_id=library_id,
                                                                  selected_history_id=selected_history_id,
                                                                  ldda_ids=ldda_ids,
                                                                  use_panels=use_panels,
                                                                  show_deleted=show_deleted,
                                                                  message=message,
                                                                  status=status,
                                                                  **new_kwd ) )
            if action == 'move':
                if library_id in [ 'none', 'None', None ]:
                    source_library_id = ''
                else:
                    source_library_id = library_id
                return trans.response.send_redirect( web.url_for( controller='library_common',
                                                                  action='move_library_item',
                                                                  cntrller=cntrller,
                                                                  source_library_id=source_library_id,
                                                                  item_type='ldda',
                                                                  item_id=ldda_ids,
                                                                  use_panels=use_panels,
                                                                  show_deleted=show_deleted,
                                                                  message=message,
                                                                  status=status ) )
            ldda_ids = util.listify( ldda_ids )
            for ldda_id in ldda_ids:
                try:
                    ldda = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( trans.security.decode_id( ldda_id ) )
                    assert not ldda.dataset.purged
                    lddas.append( ldda )
                except:
                    ldda = None
                    message += "Invalid library dataset id (%s) specified.  " % str( ldda_id )
        if not error:
            if action == 'manage_permissions':
                valid_ldda_ids = []
                valid_lddas = []
                invalid_lddas = []
                for ldda in lddas:
                    if is_admin or trans.app.security_agent.can_manage_library_item( current_user_roles, ldda ):
                        valid_lddas.append( ldda )
                        valid_ldda_ids.append( ldda.id )
                    else:
                        invalid_lddas.append( ldda )
                if invalid_lddas:
                    message += "You are not authorized to manage permissions on %s: " % inflector.cond_plural( len( invalid_lddas ), "dataset" )
                    for ldda in invalid_lddas:
                        message += '(%s)' % ldda.name
                    message += '.  '
                if valid_ldda_ids:
                    encoded_ldda_ids = [ trans.security.encode_id( ldda_id ) for ldda_id in valid_ldda_ids ]
                    folder_id = trans.security.encode_id( valid_lddas[0].library_dataset.folder.id )
                    trans.response.send_redirect( web.url_for( controller='library_common',
                                                               action='ldda_permissions',
                                                               cntrller=cntrller,
                                                               use_panels=use_panels,
                                                               library_id=library_id,
                                                               folder_id=folder_id,
                                                               id=",".join( encoded_ldda_ids ),
                                                               show_deleted=show_deleted,
                                                               message=util.sanitize_text( message ),
                                                               status=status ) )
                else:
                    message = "You are not authorized to manage permissions on any of the selected datasets."
            elif action == 'delete':
                valid_lddas = []
                invalid_lddas = []
                for ldda in lddas:
                    if is_admin or trans.app.security_agent.can_modify_library_item( current_user_roles, ldda ):
                        valid_lddas.append( ldda )
                    else:
                        invalid_lddas.append( ldda )
                if invalid_lddas:
                    message += "You are not authorized to delete %s: " % inflector.cond_plural( len( invalid_lddas ), "dataset" )
                    for ldda in invalid_lddas:
                        message += '(%s)' % ldda.name
                    message += '.  '
                if valid_lddas:
                    for ldda in valid_lddas:
                        # Do not delete the association, just delete the library_dataset.  The
                        # cleanup_datasets.py script handles everything else.
                        ld = ldda.library_dataset
                        ld.deleted = True
                        trans.sa_session.add( ld )
                    trans.sa_session.flush()
                    num_valid_lddas = len( valid_lddas )
                    message += "Deleted %i %s." % ( num_valid_lddas, inflector.cond_plural( num_valid_lddas, "dataset" ) )
                else:
                    message = "You are not authorized to delete any of the selected datasets."
            elif action in [ 'zip','tgz','tbz','ngxzip' ]:
                error = False
                killme = string.punctuation + string.whitespace
                trantab = string.maketrans(killme,'_'*len(killme))
                try:
                    outext = 'zip'
                    if action == 'zip':
                        # Can't use mkstemp - the file must not exist first
                        tmpd = tempfile.mkdtemp()
                        util.umask_fix_perms( tmpd, trans.app.config.umask, 0777, self.app.config.gid )
                        tmpf = os.path.join( tmpd, 'library_download.' + action )
                        if ziptype == '64' and trans.app.config.upstream_gzip:
                            archive = zipfile.ZipFile( tmpf, 'w', zipfile.ZIP_STORED, True )
                        elif ziptype == '64':
                            archive = zipfile.ZipFile( tmpf, 'w', zipfile.ZIP_DEFLATED, True )
                        elif trans.app.config.upstream_gzip:
                            archive = zipfile.ZipFile( tmpf, 'w', zipfile.ZIP_STORED )
                        else:
                            archive = zipfile.ZipFile( tmpf, 'w', zipfile.ZIP_DEFLATED )
                        archive.add = lambda x, y: archive.write( x, y.encode('CP437') )
                    elif action == 'tgz':
                        if trans.app.config.upstream_gzip:
                            archive = util.streamball.StreamBall( 'w|' )
                            outext = 'tar'
                        else:
                            archive = util.streamball.StreamBall( 'w|gz' )
                            outext = 'tgz'
                    elif action == 'tbz':
                        archive = util.streamball.StreamBall( 'w|bz2' )
                        outext = 'tbz2'
                    elif action == 'ngxzip':
                        archive = NgxZip( trans.app.config.nginx_x_archive_files_base )
                except ( OSError, zipfile.BadZipfile ):
                    error = True
                    log.exception( "Unable to create archive for download" )
                    message = "Unable to create archive for download, please report this error"
                    status = 'error'
                except:
                     error = True
                     log.exception( "Unexpected error %s in create archive for download" % sys.exc_info()[0] )
                     message = "Unable to create archive for download, please report - %s" % sys.exc_info()[0]
                     status = 'error'
                if not error:
                    composite_extensions = trans.app.datatypes_registry.get_composite_extensions()
                    seen = []
                    for ldda in lddas:
                        if ldda.dataset.state in [ 'new', 'upload', 'queued', 'running', 'empty', 'discarded' ]:
                            continue
                        ext = ldda.extension
                        is_composite = ext in composite_extensions
                        path = ""
                        parent_folder = ldda.library_dataset.folder
                        while parent_folder is not None:
                            # Exclude the now-hidden "root folder"
                            if parent_folder.parent is None:
                                path = os.path.join( parent_folder.library_root[0].name, path )
                                break
                            path = os.path.join( parent_folder.name, path )
                            parent_folder = parent_folder.parent
                        path += ldda.name
                        while path in seen:
                            path += '_'
                        seen.append( path )
                        zpath = os.path.split(path)[-1] # comes as base_name/fname
                        outfname,zpathext = os.path.splitext(zpath)
                        if is_composite:
                            # need to add all the components from the extra_files_path to the zip
                            if zpathext == '':
                                zpath = '%s.html' % zpath # fake the real nature of the html file 
                            try:
                                archive.add(ldda.dataset.file_name,zpath) # add the primary of a composite set
                            except IOError:
                                error = True
                                log.exception( "Unable to add composite parent %s to temporary library download archive" % ldda.dataset.file_name)
                                message = "Unable to create archive for download, please report this error"
                                status = 'error'
                                continue                                
                            flist = glob.glob(os.path.join(ldda.dataset.extra_files_path,'*.*')) # glob returns full paths
                            for fpath in flist:
                                efp,fname = os.path.split(fpath)
                                if fname > '':
                                    fname = fname.translate(trantab)
                                try:
                                    archive.add( fpath,fname )
                                except IOError:
                                    error = True
                                    log.exception( "Unable to add %s to temporary library download archive %s" % (fname,outfname))
                                    message = "Unable to create archive for download, please report this error"
                                    status = 'error'
                                    continue
                        else: # simple case
                            try:
                                archive.add( ldda.dataset.file_name, path )
                            except IOError:
                                error = True
                                log.exception( "Unable to write %s to temporary library download archive" % ldda.dataset.file_name)
                                message = "Unable to create archive for download, please report this error"
                                status = 'error'                            
                    if not error:
                        if library_id:
                            lname = trans.sa_session.query( trans.app.model.Library ).get( trans.security.decode_id( library_id ) ).name
                        else:
                            # Request must have coe from the library_dataset_search_results page.
                            lname = 'selected_dataset'
                        fname = lname.replace( ' ', '_' ) + '_files'
                        if action == 'zip':
                            archive.close()
                            trans.response.set_content_type( "application/x-zip-compressed" )
                            trans.response.headers[ "Content-Disposition" ] = 'attachment; filename="%s.%s"' % (fname,outext)
                            archive = util.streamball.ZipBall(tmpf, tmpd)
                            archive.wsgi_status = trans.response.wsgi_status()
                            archive.wsgi_headeritems = trans.response.wsgi_headeritems()
                            return archive.stream
                        elif action == 'ngxzip':
                            trans.response.set_content_type( "application/zip" )
                            trans.response.headers[ "Content-Disposition" ] = 'attachment; filename="%s.%s"' % (fname,outext)
                            trans.response.headers[ "X-Archive-Files" ] = "zip"
                            return archive
                        else:
                            trans.response.set_content_type( "application/x-tar" )
                            trans.response.headers[ "Content-Disposition" ] = 'attachment; filename="%s.%s"' % (fname,outext)
                            archive.wsgi_status = trans.response.wsgi_status()
                            archive.wsgi_headeritems = trans.response.wsgi_headeritems()
                            return archive.stream
            else:
                status = 'error'
                message = 'Invalid action (%s) specified.' % str( action )
        if library_id:
            # If we have a library_id, browse the associated library
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              current_user_roles=current_user_roles,
                                                              use_panels=use_panels,
                                                              id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status=status ) )
        else:
            # We arrived here from the library_dataset_search_results page, so redirect there.
            search_term = params.get( 'search_term', '' )
            comptypes = get_comptypes( trans )
            return trans.fill_template( '/library/common/library_dataset_search_results.mako',
                                        cntrller=cntrller,
                                        current_user_roles=current_user_roles,
                                        search_term=search_term,
                                        comptypes=comptypes,
                                        lddas=lddas,
                                        show_deleted=show_deleted,
                                        use_panels=use_panels,
                                        message=message,
                                        status=status )
            
    @web.expose
    def import_datasets_to_histories( self, trans, cntrller, library_id='', folder_id='', ldda_ids='', target_history_id='', target_history_ids='', new_history_name='', **kwd ):
        # This method is called from one of the following places:
        # - a menu option for a library dataset ( ldda_ids is a single ldda id )
        # - a menu option for a library folder ( folder_id has a value )
        # - a select list option for acting on multiple selected datasets within a library 
        #   ( ldda_ids is a comma separated string of ldda ids )
        # - a menu option for a library dataset search result set ( ldda_ids is a comma separated string of ldda ids )
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        action = params.get( 'do_action', None )
        user = trans.get_user()
        current_history = trans.get_history()
        if library_id:
            library = trans.sa_session.query( trans.model.Library ).get( trans.security.decode_id( library_id ) )
        else:
            library = None
        if folder_id:
            folder = trans.sa_session.query( trans.model.LibraryFolder ).get( trans.security.decode_id( folder_id ) )
        else:
            folder = None
        ldda_ids = util.listify( ldda_ids )
        if ldda_ids:
            ldda_ids = map( trans.security.decode_id, ldda_ids )
        if target_history_ids:
            target_history_ids = util.listify( target_history_ids )
            target_history_ids = set( [ trans.security.decode_id( target_history_id ) for target_history_id in target_history_ids if target_history_id ] )
        elif target_history_id:
            target_history_ids = [ trans.security.decode_id( target_history_id ) ]
        if params.get( 'import_datasets_to_histories_button', False ):
            invalid_datasets = 0
            if not ldda_ids or not ( target_history_ids or new_history_name ):
                message = "You must provide one or more source library datasets and one or more target histories."
                status = 'error'
            else:
                if new_history_name:
                    new_history = trans.app.model.History()
                    new_history.name = new_history_name
                    new_history.user = user
                    trans.sa_session.add( new_history )
                    trans.sa_session.flush()
                    target_history_ids = [ new_history.id ]
                    target_histories = [ new_history ]
                elif user:
                    target_histories = [ hist for hist in map( trans.sa_session.query( trans.app.model.History ).get, target_history_ids ) if ( hist is not None and hist.user == user )]
                else:
                    target_histories = [ current_history ]
                if len( target_histories ) != len( target_history_ids ):
                    message += "You do not have permission to add datasets to %i requested histories.  " % ( len( target_history_ids ) - len( target_histories ) )
                    status = 'error'
                flush_needed = False
                for ldda in map( trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get, ldda_ids ):
                    if ldda is None:
                        message += "You tried to import a dataset that does not exist.  "
                        status = 'error'
                        invalid_datasets += 1
                    elif ldda.dataset.state not in [ trans.model.Dataset.states.OK, trans.model.Dataset.states.ERROR ]:
                        message += "You cannot import dataset '%s' since its state is '%s'.  " % ( ldda.name, ldda.dataset.state )
                        status = 'error'
                        invalid_datasets += 1
                    elif not ldda.has_data():
                        message += "You cannot import empty dataset '%s'.  " % ldda.name
                        status = 'error'
                        invalid_datasets += 1
                    else:
                        for target_history in target_histories:
                            hda = ldda.to_history_dataset_association( target_history=target_history, add_to_history=True )
                            if not flush_needed:
                                flush_needed = True
                if flush_needed:
                    trans.sa_session.flush()
                    hist_names_str = ", ".join( [ target_history.name for target_history in target_histories ] )
                    num_source = len( ldda_ids ) - invalid_datasets
                    num_target = len( target_histories )
                    message += "%i %s imported into %i %s: %s" % ( num_source,
                                                                   inflector.cond_plural( num_source, "dataset" ),
                                                                   num_target,
                                                                   inflector.cond_plural( num_target, "history" ),
                                                                   hist_names_str )
                trans.sa_session.refresh( current_history )
        current_user_roles = trans.get_current_user_roles()
        source_lddas = []
        if folder:
            for library_dataset in folder.datasets:
                ldda = library_dataset.library_dataset_dataset_association
                if not ldda.deleted and trans.app.security_agent.can_access_library_item( current_user_roles, ldda, trans.user ):
                    source_lddas.append( ldda )
        elif ldda_ids:
            for ldda_id in ldda_ids:
                # Secuirty access permiision chcck is not needed here since the current user had access
                # to the lddas in order for the menu optin  to be available.
                ldda = trans.sa_session.query( trans.model.LibraryDatasetDatasetAssociation ).get( ldda_id )
                source_lddas.append( ldda )
        if current_history is not None:
            target_histories = [ current_history ]
        else:
            target_histories = []
            message = 'You must have a history before you can import datasets.  You can do this by <a href="%s" target="_top">loading the analysis interface</a>.' % url_for(controller='root')
            status = 'error'
        if user:
           target_histories = user.active_histories
        if action == 'import_to_current_history' and library_id:
            # To streamline this as much as possible, go back to browsing the library.
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              id=library_id,
                                                              message=util.sanitize_text( message ),
                                                              status=status ) )
        return trans.fill_template( "/library/common/import_datasets_to_histories.mako",
                                    cntrller=cntrller,
                                    library=library,
                                    current_history=current_history,
                                    ldda_ids=ldda_ids,
                                    target_history_id=target_history_id,
                                    target_history_ids=target_history_ids,
                                    source_lddas=source_lddas,
                                    target_histories=target_histories,
                                    new_history_name=new_history_name,
                                    show_deleted=show_deleted,
                                    use_panels=use_panels,
                                    message=message,
                                    status=status )
    @web.expose
    def manage_template_inheritance( self, trans, cntrller, item_type, library_id, folder_id=None, ldda_id=None, **kwd ):
        params = util.Params( kwd )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        is_admin = ( trans.user_is_admin() and cntrller == 'library_admin' )
        current_user_roles = trans.get_current_user_roles()
        try:
            item, item_desc, action, id = self.get_item_and_stuff( trans,
                                                                   item_type=item_type,
                                                                   library_id=library_id,
                                                                   folder_id=folder_id,
                                                                   ldda_id=ldda_id,
                                                                   is_admin=is_admin )
        except ValueError:
            return None
        if not ( is_admin or trans.app.security_agent.can_modify_library_item( current_user_roles, item ) ):
            message = "You are not authorized to modify %s '%s'." % ( item_desc, item.name )
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status='error' ) )
        info_association, inherited = item.get_info_association( restrict=True )
        if info_association:
            if info_association.inheritable:
                message = "The template for this %s will no longer be inherited to contained folders and datasets." % item_desc
            else:
                message = "The template for this %s will now be inherited to contained folders and datasets." % item_desc
            info_association.inheritable = not( info_association.inheritable )
            trans.sa_session.add( info_association )
            trans.sa_session.flush()
        return trans.response.send_redirect( web.url_for( controller='library_common',
                                                          action=action,
                                                          cntrller=cntrller,
                                                          use_panels=use_panels,
                                                          library_id=library_id,
                                                          folder_id=folder_id,
                                                          id=id,
                                                          show_deleted=show_deleted,
                                                          message=util.sanitize_text( message ),
                                                          status='done' ) )
    @web.expose
    def move_library_item( self, trans, cntrller, item_type, item_id, source_library_id='', make_target_current=True, **kwd ):
        # This method is called from one of the following places:
        # - a menu option for a library dataset ( item_type is 'ldda' and item_id is a single ldda id )
        # - a menu option for a library folder ( item_type is 'folder' and item_id is a single folder id )
        # - a select list option for acting on multiple selected datasets within a library ( item_type is 
        #   'ldda' and item_id is a comma separated string of ldda ids )
        # - a menu option for a library dataset search result set ( item_type is 'ldda' and item_id is a 
        #   comma separated string of ldda ids )
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        show_deleted = util.string_as_bool( params.get( 'show_deleted', False ) )
        use_panels = util.string_as_bool( params.get( 'use_panels', False ) )
        make_target_current = util.string_as_bool( make_target_current )
        is_admin = trans.user_is_admin() and cntrller == 'library_admin'
        user = trans.get_user()
        current_user_roles = trans.get_current_user_roles()
        move_ldda_ids = []
        move_lddas = []
        move_folder_id = []
        move_folder = None
        if source_library_id:
            source_library = trans.sa_session.query( trans.model.Library ).get( trans.security.decode_id( source_library_id ) )
        else:
            # Request sent from the library_dataset_search_results page.
            source_library = None
        target_library_id = params.get( 'target_library_id', '' )
        if target_library_id not in [ '', 'none', None ]:
            target_library = trans.sa_session.query( trans.model.Library ).get( trans.security.decode_id( target_library_id ) )
        elif make_target_current:
            target_library = source_library
        else:
            target_library = None
        target_folder_id = params.get( 'target_folder_id', '' )
        if target_folder_id not in [ '', 'none', None ]:
            target_folder = trans.sa_session.query( trans.model.LibraryFolder ).get( trans.security.decode_id( target_folder_id ) )
            if target_library is None:
                target_library = target_folder.parent_library
        else:
            target_folder = None
        if item_type == 'ldda':
            # We've been called from a menu option for a library dataset search result set
            move_ldda_ids = util.listify( item_id )
            if move_ldda_ids:
                move_ldda_ids = map( trans.security.decode_id, move_ldda_ids )
        elif item_type == 'folder':
            move_folder_id = item_id
            move_folder = trans.sa_session.query( trans.model.LibraryFolder ).get( trans.security.decode_id( move_folder_id ) )
        if params.get( 'move_library_item_button', False ):
            if not ( move_ldda_ids or move_folder_id ) or target_folder_id in [ '', 'none', None ]:
                message = "You must select a source folder or one or more source datasets, and a target folder."
                status = 'error'
            else:
                valid_lddas = []
                invalid_lddas = []
                invalid_items = 0
                flush_required = False
                if item_type == 'ldda':
                    for ldda in map( trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get, move_ldda_ids ):
                        if ldda is None:
                            message += "You tried to move a dataset that does not exist.  "
                            status = 'error'
                            invalid_items += 1
                        elif ldda.dataset.state not in [ trans.model.Dataset.states.OK, trans.model.Dataset.states.ERROR ]:
                            message += "You cannot move dataset '%s' since its state is '%s'.  " % ( ldda.name, ldda.dataset.state )
                            status = 'error'
                            invalid_items += 1
                        elif not ldda.has_data():
                            message += "You cannot move empty dataset '%s'.  " % ldda.name
                            status = 'error'
                            invalid_items += 1
                        else:
                            if is_admin:
                                library_dataset = ldda.library_dataset
                                library_dataset.folder = target_folder
                                trans.sa_session.add( library_dataset )
                                flush_required = True
                            else:
                                if trans.app.security_agent.can_modify_library_item( current_user_roles, ldda ):
                                    valid_lddas.append( ldda )
                                    library_dataset = ldda.library_dataset
                                    library_dataset.folder = target_folder
                                    trans.sa_session.add( library_dataset )
                                    flush_required = True
                                else:
                                    invalid_items += 1
                                    invalid_lddas.append( ldda )
                    if not valid_lddas:
                        message = "You are not authorized to move any of the selected datasets."
                    elif invalid_lddas:
                        message += "You are not authorized to move %s: " % inflector.cond_plural( len( invalid_lddas ), "dataset" )
                        for ldda in invalid_lddas:
                            message += '(%s)' % ldda.name
                        message += '.  '
                    num_source = len( move_ldda_ids ) - invalid_items
                    message = "%i %s moved to folder (%s) within data library (%s)" % ( num_source,
                                                                                        inflector.cond_plural( num_source, "dataset" ),
                                                                                        target_folder.name,
                                                                                        target_library.name )
                elif item_type == 'folder':
                    move_folder = trans.sa_session.query( trans.app.model.LibraryFolder ) \
                                                  .get( trans.security.decode_id( move_folder_id ) )
                    if move_folder is None:
                        message += "You tried to move a folder that does not exist.  "
                        status = 'error'
                        invalid_items += 1
                    else:
                        move_folder.parent = target_folder
                        trans.sa_session.add( move_folder )
                        flush_required = True
                    message = "Moved folder (%s) to folder (%s) within data library (%s) " % ( move_folder.name,
                                                                                               target_folder.name,
                                                                                               target_library.name )
                if flush_required:
                    trans.sa_session.flush()
        if target_library:
            if is_admin:
                target_library_folders = target_library.get_active_folders( target_library.root_folder )
            else:
                folders_with_permission_to_add = []
                for folder in target_library.get_active_folders( target_library.root_folder ):
                    if trans.app.security_agent.can_add_library_item( current_user_roles, folder ):
                        folders_with_permission_to_add.append( folder )
                target_library_folders = folders_with_permission_to_add
        else:
            target_library_folders = []
        if item_type == 'ldda':
            for ldda_id in move_ldda_ids:
                # TODO: It is difficult to filter out undesired folders (e.g. the ldda's current
                # folder) if we have a list of lddas, but we may want to filter folders that
                # are easily handled.
                ldda = trans.sa_session.query( trans.model.LibraryDatasetDatasetAssociation ).get( ldda_id )
                move_lddas.append( ldda )
        elif item_type == 'folder':
            def __is_contained_in( folder1, folder2 ):
                # Return True if folder1 is contained in folder2
                if folder1.parent:
                    if folder1.parent == folder2:
                        return True
                    return __is_contained_in( folder1.parent, folder2 )
                return False
            filtered_folders = []
            for folder in target_library_folders:
                include = True
                if move_folder:
                    if __is_contained_in( folder, move_folder ):
                        # Don't allow moving a folder to one of it's sub-folders (circular issues in db)
                        include = False
                    if move_folder.id == folder.id:
                        # Don't allow moving a folder to itself
                        include = False
                    if move_folder.parent and move_folder.parent.id == folder.id:
                        # Don't allow moving a folder to it's current parent folder
                        include = False
                if include:
                    filtered_folders.append( folder )
            target_library_folders = filtered_folders
        def __build_target_library_id_select_field( trans, selected_value='none' ):
            # Get all the libraries for which the current user can add items.
            target_libraries = []
            if is_admin:
                for library in trans.sa_session.query( trans.model.Library ) \
                                               .filter( trans.model.Library.deleted == False ) \
                                               .order_by( trans.model.Library.table.c.name ):
                    if source_library is None or library.id != source_library.id:
                        target_libraries.append( library )
            else:
                for library in trans.app.security_agent.get_accessible_libraries( trans, user ):
                    if source_library is None:
                        if trans.app.security_agent.can_add_library_item( current_user_roles, library ):
                            target_libraries.append( library )
                    elif library.id != source_library.id:
                        if trans.app.security_agent.can_add_library_item( current_user_roles, library ):
                            target_libraries.append( library )
            # A refresh_on_change is required to display the selected library's folders
            return build_select_field( trans,
                                       objs=target_libraries,
                                       label_attr='name',
                                       select_field_name='target_library_id',
                                       selected_value=selected_value,
                                       refresh_on_change=True )
        def __build_target_folder_id_select_field( trans, folders, selected_value='none' ):
            for folder in folders:
                if not folder.parent:
                    folder.name = 'Data library root'
                    break
            return build_select_field( trans,
                                       objs=folders,
                                       label_attr='name',
                                       select_field_name='target_folder_id',
                                       selected_value=selected_value,
                                       refresh_on_change=False )
        if target_library:
            selected_value = target_library.id
        else:
            selected_value = 'none'
        target_library_id_select_field = __build_target_library_id_select_field( trans, selected_value=selected_value )
        target_folder_id_select_field = __build_target_folder_id_select_field( trans, target_library_folders )
        return trans.fill_template( "/library/common/move_library_item.mako",
                                    cntrller=cntrller,
                                    make_target_current=make_target_current,
                                    source_library=source_library,
                                    item_type=item_type,
                                    item_id=item_id,
                                    move_ldda_ids=move_ldda_ids,
                                    move_lddas=move_lddas,
                                    move_folder=move_folder,
                                    target_library=target_library,
                                    target_library_id_select_field=target_library_id_select_field,
                                    target_folder_id_select_field=target_folder_id_select_field,
                                    show_deleted=show_deleted,
                                    use_panels=use_panels,
                                    message=message,
                                    status=status )
    @web.expose
    def delete_library_item( self, trans, cntrller, library_id, item_id, item_type, **kwd ):
        # This action will handle deleting all types of library items.  State is saved for libraries and
        # folders ( i.e., if undeleted, the state of contents of the library or folder will remain, so previously
        # deleted / purged contents will have the same state ).  When a library or folder has been deleted for
        # the amount of time defined in the cleanup_datasets.py script, the library or folder and all of its
        # contents will be purged.  The association between this method and the cleanup_datasets.py script
        # enables clean maintenance of libraries and library dataset disk files.  This is also why the item_types
        # are not any of the associations ( the cleanup_datasets.py script handles everything ).
        status = kwd.get( 'status', 'done' )
        show_deleted = util.string_as_bool( kwd.get( 'show_deleted', False ) )
        item_types = { 'library': trans.app.model.Library,
                       'folder': trans.app.model.LibraryFolder,
                       'library_dataset': trans.app.model.LibraryDataset }
        is_admin = ( trans.user_is_admin() and cntrller == 'library_admin' )
        current_user_roles = trans.get_current_user_roles()
        if item_type not in item_types:
            message = 'Bad item_type specified: %s' % str( item_type )
            status = 'error'
        else:
            if item_type == 'library_dataset':
                item_desc = 'Dataset'
            else:
                item_desc = item_type.capitalize()
            library_item_ids = util.listify( item_id )
            valid_items = 0
            invalid_items = 0
            not_authorized_items = 0
            flush_needed = False
            message = ''
            for library_item_id in library_item_ids:
                try:
                    library_item = trans.sa_session.query( item_types[ item_type ] ).get( trans.security.decode_id( library_item_id ) )
                except:
                    library_item = None
                if not library_item or not ( is_admin or trans.app.security_agent.can_access_library_item( current_user_roles, library_item, trans.user ) ):
                    invalid_items += 1
                elif not ( is_admin or trans.app.security_agent.can_modify_library_item( current_user_roles, library_item ) ):
                    not_authorized_items += 1
                else:
                    valid_items += 1
                    library_item.deleted = True
                    trans.sa_session.add( library_item )
                    flush_needed = True
            if flush_needed:
                trans.sa_session.flush()
            if valid_items:
                message += "%d %s marked deleted.  " % ( valid_items, inflector.cond_plural( valid_items, item_desc ) )
            if invalid_items:
                message += '%d invalid %s specifield.  ' % ( invalid_items, inflector.cond_plural( invalid_items, item_desc ) )
                status = 'error'
            if not_authorized_items:
                message += 'You are not authorized to delete %d %s.  ' % ( not_authorized_items, inflector.cond_plural( not_authorized_items, item_desc ) )
                status = 'error'
        if item_type == 'library':
            return trans.response.send_redirect( web.url_for( controller=cntrller,
                                                              action='browse_libraries',
                                                              message=message,
                                                              status=status ) )
        else:
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=message,
                                                              status=status ) )
    @web.expose
    def undelete_library_item( self, trans, cntrller, library_id, item_id, item_type, **kwd ):
        # This action will handle undeleting all types of library items
        status = kwd.get( 'status', 'done' )
        show_deleted = util.string_as_bool( kwd.get( 'show_deleted', False ) )
        item_types = { 'library': trans.app.model.Library,
                       'folder': trans.app.model.LibraryFolder,
                       'library_dataset': trans.app.model.LibraryDataset }
        is_admin = ( trans.user_is_admin() and cntrller == 'library_admin' )
        current_user_roles = trans.get_current_user_roles()
        if item_type not in item_types:
            message = 'Bad item_type specified: %s' % str( item_type )
            status = 'error'
        else:
            if item_type == 'library_dataset':
                item_desc = 'Dataset'
            else:
                item_desc = item_type.capitalize()

            library_item_ids = util.listify( item_id )
            valid_items = 0
            invalid_items = 0
            purged_items = 0
            not_authorized_items = 0
            flush_needed = False
            message = ''
            for library_item_id in library_item_ids:
                try:
                    library_item = trans.sa_session.query( item_types[ item_type ] ).get( trans.security.decode_id( library_item_id ) )
                except:
                    library_item = None
                if not library_item or not ( is_admin or trans.app.security_agent.can_access_library_item( current_user_roles, library_item, trans.user ) ):
                    invalid_items += 1
                elif library_item.purged:
                    purged_items += 1
                elif not ( is_admin or trans.app.security_agent.can_modify_library_item( current_user_roles, library_item ) ):
                    not_authorized_items += 1
                else:
                    valid_items += 1
                    library_item.deleted = False
                    trans.sa_session.add( library_item )
                    flush_needed = True
            if flush_needed:
                trans.sa_session.flush()
            if valid_items:
                message += "%d %s marked undeleted.  " % ( valid_items, inflector.cond_plural( valid_items, item_desc ) )
            if invalid_items:
                message += '%d invalid %s specifield.  ' % ( invalid_items, inflector.cond_plural( invalid_items, item_desc ) )
                status = 'error'
            if not_authorized_items:
                message += 'You are not authorized to undelete %d %s.  ' % ( not_authorized_items, inflector.cond_plural( not_authorized_items, item_desc ) )
                status = 'error'  
            if purged_items:
                message += '%d %s marked purged, so cannot be undeleted.  ' % ( purged_items, inflector.cond_plural( purged_items, item_desc ) )
                status = 'error'  
        if item_type == 'library':
            return trans.response.send_redirect( web.url_for( controller=cntrller,
                                                              action='browse_libraries',
                                                              message=message,
                                                              status=status ) )
        else:
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=message,
                                                              status=status ) )
    def _check_access( self, trans, cntrller, is_admin, item, current_user_roles, use_panels, library_id, show_deleted ):
        can_access = True
        if isinstance( item, trans.model.HistoryDatasetAssociation ):
            # Make sure the user has the DATASET_ACCESS permission on the history_dataset_association.
            if not item:
                message = "Invalid history dataset (%s) specified." % str( item )
                can_access = False
            elif not trans.app.security_agent.can_access_dataset( current_user_roles, item.dataset ) and item.history.user==trans.user:
                message = "You do not have permission to access the history dataset with id (%s)." % str( item.id )
                can_access = False
        else:
            # Make sure the user has the LIBRARY_ACCESS permission on the library item.
            if not item:
                message = "Invalid library item (%s) specified." % str( item )
                can_access = False
            elif not ( is_admin or trans.app.security_agent.can_access_library_item( current_user_roles, item, trans.user ) ):
                if isinstance( item, trans.model.Library ):
                    item_type = 'data library'
                elif isinstance( item, trans.model.LibraryFolder ):
                    item_type = 'folder'
                else:
                    item_type = '(unknown item type)'
                message = "You do not have permission to access the %s with id (%s)." % ( item_type, str( item.id ) )
                can_access = False
        if not can_access:
            if cntrller == 'api':
                return 400, message
            if isinstance( item, trans.model.Library ):
                return trans.response.send_redirect( web.url_for( controller=cntrller,
                                                                  action='browse_libraries',
                                                                  cntrller=cntrller,
                                                                  use_panels=use_panels,
                                                                  message=util.sanitize_text( message ),
                                                                  status='error' ) )
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              use_panels=use_panels,
                                                              id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status='error' ) )
    def _check_add( self, trans, cntrller, is_admin, item, current_user_roles, use_panels, library_id, show_deleted ):
        # Deny access if the user is not an admin and does not have the LIBRARY_ADD permission.
        if not ( is_admin or trans.app.security_agent.can_add_library_item( current_user_roles, item ) ):
            message = "You are not authorized to add an item to (%s)." % item.name
            # Redirect to the real parent library since we know we have access to it.
            if cntrller == 'api':
                return 403, message
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              use_panels=use_panels,
                                                              id=library_id,
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status='error' ) )
    def _check_manage( self, trans, cntrller, is_admin, item, current_user_roles, use_panels, library_id, show_deleted ):
        if isinstance( item, trans.model.LibraryDataset ):
            # Deny access if the user is not an admin and does not have the LIBRARY_MANAGE and DATASET_MANAGE_PERMISSIONS permissions.
            if not ( is_admin or \
                     ( trans.app.security_agent.can_manage_library_item( current_user_roles, item ) and 
                       trans.app.security_agent.can_manage_dataset( current_user_roles, library_dataset.library_dataset_dataset_association.dataset ) ) ):
                message = "You are not authorized to manage permissions on library dataset (%s)." % library_dataset.name
                if cntrller == 'api':
                    return 403, message
                return trans.response.send_redirect( web.url_for( controller='library_common',
                                                                  action='browse_library',
                                                                  id=library_id,
                                                                  cntrller=cntrller,
                                                                  use_panels=use_panels,
                                                                  message=util.sanitize_text( message ),
                                                                  status='error' ) )
        # Deny access if the user is not an admin and does not have the LIBRARY_MANAGE permission.
        if not ( is_admin or trans.app.security_agent.can_manage_library_item( current_user_roles, item ) ):
            message = "You are not authorized to manage permissions on (%s)." % item.name
            if cntrller == 'api':
                return 403, message
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              id=library_id,
                                                              cntrller=cntrller,
                                                              use_panels=use_panels,
                                                              message=util.sanitize_text( message ),
                                                              status='error' ) )
    def _check_modify( self, trans, cntrller, is_admin, item, current_user_roles, use_panels, library_id, show_deleted ):
        # Deny modification if the user is not an admin and does not have the LIBRARY_MODIFY permission.
        if not ( is_admin or trans.app.security_agent.can_modify_library_item( current_user_roles, item ) ):
            message = "You are not authorized to modify (%s)." % item.name
            if cntrller == 'api':
                return 403, message
            return trans.response.send_redirect( web.url_for( controller='library_common',
                                                              action='browse_library',
                                                              cntrller=cntrller,
                                                              id=library_id,
                                                              use_panels=use_panels,
                                                              show_deleted=show_deleted,
                                                              message=util.sanitize_text( message ),
                                                              status='error' ) )

# ---- Utility methods -------------------------------------------------------

def active_folders( trans, folder ):
    # Much faster way of retrieving all active sub-folders within a given folder than the
    # performance of the mapper.  This query also eagerloads the permissions on each folder.
    return trans.sa_session.query( trans.app.model.LibraryFolder ) \
                           .filter_by( parent=folder, deleted=False ) \
                           .options( eagerload_all( "actions" ) ) \
                           .order_by( trans.app.model.LibraryFolder.table.c.name ) \
                           .all()
def activatable_folders( trans, folder ):
    return trans.sa_session.query( trans.app.model.LibraryFolder ) \
                           .filter_by( parent=folder, purged=False ) \
                           .options( eagerload_all( "actions" ) ) \
                           .order_by( trans.app.model.LibraryFolder.table.c.name ) \
                           .all()

def map_library_datasets_to_lddas( trans, lib_datasets ):
    '''
    Given a list of LibraryDatasets, return a map from the LibraryDatasets
    to their LDDAs. If an LDDA does not exist for a LibraryDataset, then
    there will be no entry in the return hash.
    '''
    # Get a list of the LibraryDatasets' ids so that we can pass it along to
    # a query to retrieve the LDDAs. This eliminates querying for each 
    # LibraryDataset.
    lib_dataset_ids = [ x.library_dataset_dataset_association_id for x in lib_datasets ]
    lddas = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ) \
                            .filter( trans.app.model.LibraryDatasetDatasetAssociation.id.in_( lib_dataset_ids ) ) \
                            .all()
    
    # Map the LibraryDataset to the returned LDDAs:
    ret_lddas = {}
    for ldda in lddas:
        ret_lddas[ldda.library_dataset_id] = ldda
    return ret_lddas

def datasets_for_lddas( trans, lddas ):
    '''
    Given a list of LDDAs, return a list of Datasets for them. 
    '''
    dataset_ids = [ x.dataset_id for x in lddas ]
    datasets = trans.sa_session.query( trans.app.model.Dataset ) \
                               .filter( trans.app.model.Dataset.id.in_( dataset_ids ) ) \
                               .all()
    return datasets 

def active_folders_and_library_datasets( trans, folder ):
    # SM: TODO: Eliminate timing code
    from datetime import datetime, timedelta
    query_start = datetime.now()
    folders = active_folders( trans, folder )
    library_datasets = trans.sa_session.query( trans.model.LibraryDataset ) \
                                       .filter( and_( trans.model.LibraryDataset.table.c.deleted == False,
                                                      trans.model.LibraryDataset.table.c.folder_id == folder.id ) ) \
                                       .order_by( trans.model.LibraryDataset.table.c._name ) \
                                       .all()
    query_end = datetime.now()
    query_delta = query_end - query_start
    #log.debug( "active_folders_and_library_datasets: %d.%.6d" % 
    #           ( query_delta.seconds, query_delta.microseconds ) )
    return folders, library_datasets

def activatable_folders_and_library_datasets( trans, folder ):
    folders = activatable_folders( trans, folder )
    from datetime import datetime, timedelta
    query_start = datetime.now()
    library_datasets = trans.sa_session.query( trans.model.LibraryDataset ) \
                                       .filter( trans.model.LibraryDataset.table.c.folder_id == folder.id ) \
                                       .join( ( trans.model.LibraryDatasetDatasetAssociation.table,
                                                trans.model.LibraryDataset.table.c.library_dataset_dataset_association_id == trans.model.LibraryDatasetDatasetAssociation.table.c.id ) ) \
                                       .join( ( trans.model.Dataset.table,
                                                trans.model.LibraryDatasetDatasetAssociation.table.c.dataset_id == trans.model.Dataset.table.c.id ) ) \
                                       .filter( trans.model.Dataset.table.c.deleted == False ) \
                                       .order_by( trans.model.LibraryDataset.table.c._name ) \
                                       .all()
    query_end = datetime.now()
    query_delta = query_end - query_start
    log.debug( "activatable_folders_and_library_datasets: %d.%.6d" % 
               ( query_delta.seconds, query_delta.microseconds ) )
    return folders, library_datasets
def branch_deleted( folder ):
    # Return True if a folder belongs to a branch that has been deleted
    if folder.deleted:
        return True
    if folder.parent:
        return branch_deleted( folder.parent )
    return False
def get_containing_library_from_library_dataset( trans, library_dataset ):
    """Given a library_dataset, get the containing library"""
    folder = library_dataset.folder
    while folder.parent:
        folder = folder.parent
    # We have folder set to the library's root folder, which has the same name as the library
    for library in trans.sa_session.query( trans.model.Library ) \
                                   .filter( and_( trans.model.Library.table.c.deleted == False,
                                                  trans.model.Library.table.c.name == folder.name ) ):
        # Just to double-check
        if library.root_folder == folder:
            return library
    return None
def get_comptypes( trans ):
    comptypes_t = comptypes
    if trans.app.config.nginx_x_archive_files_base:
        comptypes_t = ['ngxzip']
    for comptype in trans.app.config.disable_library_comptypes:
        # TODO: do this once, not every time (we're gonna raise an
        # exception every time after the first time)
        try:
            comptypes_t.remove( comptype )
        except:
            pass
    return comptypes_t
def get_sorted_accessible_library_items( trans, cntrller, items, sort_attr ):
    is_admin = trans.user_is_admin() and cntrller == 'library_admin'
    if is_admin:
        accessible_items = items
    else:
        # Enforce access permission settings
        current_user_roles = trans.get_current_user_roles()
        accessible_items = []
        for item in items:
            if trans.app.security_agent.can_access_library_item( current_user_roles, item, trans.user ):
                accessible_items.append( item )
    # Sort by name
    return sort_by_attr( [ item for item in accessible_items ], sort_attr )
def sort_by_attr( seq, attr ):
    """
    Sort the sequence of objects by object's attribute
    Arguments:
    seq  - the list or any sequence (including immutable one) of objects to sort.
    attr - the name of attribute to sort by
    """
    # Use the "Schwartzian transform"
    # Create the auxiliary list of tuples where every i-th tuple has form
    # (seq[i].attr, i, seq[i]) and sort it. The second item of tuple is needed not
    # only to provide stable sorting, but mainly to eliminate comparison of objects
    # (which can be expensive or prohibited) in case of equal attribute values.
    intermed = map( None, map( getattr, seq, ( attr, ) * len( seq ) ), xrange( len( seq ) ), seq )
    intermed.sort()
    return map( operator.getitem, intermed, ( -1, ) * len( intermed ) )
def lucene_search( trans, cntrller, search_term, search_url, **kwd ):
    """Return display of results from a full-text lucene search of data libraries."""
    params = util.Params( kwd )
    message = util.restore_text( params.get( 'message', ''  ) )
    status = params.get( 'status', 'done' )
    full_url = "%s/find?%s" % ( search_url, urllib.urlencode( { "kwd" : search_term } ) )
    response = urllib2.urlopen( full_url )
    ldda_ids = util.json.from_json_string( response.read() )[ "ids" ]
    response.close()
    lddas = [ trans.app.model.LibraryDatasetDatasetAssociation.get( ldda_id ) for ldda_id in ldda_ids ]
    return status, message, get_sorted_accessible_library_items( trans, cntrller, lddas, 'name' )
def whoosh_search( trans, cntrller, search_term, **kwd ):
    """Return display of results from a full-text whoosh search of data libraries."""
    params = util.Params( kwd )
    message = util.restore_text( params.get( 'message', ''  ) )
    status = params.get( 'status', 'done' )
    ok = True
    if whoosh_search_enabled:
        whoosh_index_dir = trans.app.config.whoosh_index_dir
        index_exists = whoosh.index.exists_in( whoosh_index_dir )
        if index_exists:
            index = whoosh.index.open_dir( whoosh_index_dir )
            # Set field boosts for searcher to place equal weight on all search fields.
            searcher = index.searcher( weighting=BM25F( field_B={ 'name_B' : 3.4,
                                                                  'info_B' : 3.2,
                                                                  'dbkey_B' : 3.3,
                                                                  'message_B' : 3.5 } ) )
            # Perform search
            parser = MultifieldParser( [ 'name', 'info', 'dbkey', 'message' ], schema=schema )
            # Search term with wildcards may be slow...
            results = searcher.search( parser.parse( '*' + search_term + '*' ), minscore=0.01 )
            ldda_ids = [ result[ 'id' ] for result in results ]
            lddas = []
            for ldda_id in ldda_ids:
                ldda = trans.app.model.LibraryDatasetDatasetAssociation.get( ldda_id )
                if ldda:
                    lddas.append( ldda )
            lddas = get_sorted_accessible_library_items( trans, cntrller, lddas, 'name' )
        else:
            message = "Tell your Galaxy administrator that the directory %s does not contain valid whoosh indexes" % str( whoosh_index_dir )
            ok = False
    else:
        message = "Whoosh is compatible with Python version 2.5 or greater.  Your Python verison is not compatible."
        ok = False
    if not ok:
        status = 'error'
        lddas = []
    return status, message, lddas