galaxy-central / lib / galaxy / webapps / community / controllers / repository.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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
import os, logging, tempfile, shutil, ConfigParser
from time import gmtime, strftime
from datetime import date, datetime
from galaxy import util
from galaxy.util.odict import odict
from galaxy.web.base.controller import *
from galaxy.web.form_builder import CheckboxField
from galaxy.webapps.community import model
from galaxy.webapps.community.model import directory_hash_id
from galaxy.web.framework.helpers import time_ago, iff, grids
from galaxy.util.json import from_json_string, to_json_string
from galaxy.model.orm import and_
import galaxy.util.shed_util_common as suc
from galaxy.tool_shed import encoding_util
import common

from galaxy import eggs
eggs.require('mercurial')
from mercurial import hg, ui, patch, commands
eggs.require('markupsafe')
from markupsafe import escape as escape_html

log = logging.getLogger( __name__ )

VALID_REPOSITORYNAME_RE = re.compile( "^[a-z0-9\_]+$" )
    
class CategoryGrid( grids.Grid ):
    class NameColumn( grids.TextColumn ):
        def get_value( self, trans, grid, category ):
            return category.name
    class DescriptionColumn( grids.TextColumn ):
        def get_value( self, trans, grid, category ):
            return category.description
    class RepositoriesColumn( grids.TextColumn ):
        def get_value( self, trans, grid, category ):
            if category.repositories:
                viewable_repositories = 0
                for rca in category.repositories:
                    if not rca.repository.deleted and not rca.repository.deprecated:
                        viewable_repositories += 1
                return viewable_repositories
            return 0
    title = "Categories"
    model_class = model.Category
    template='/webapps/community/category/grid.mako'
    default_sort_key = "name"
    columns = [
        NameColumn( "Name",
                    key="Category.name",
                    link=( lambda item: dict( operation="repositories_by_category", id=item.id ) ),
                    attach_popup=False ),
        DescriptionColumn( "Description",
                           key="Category.description",
                           attach_popup=False ),
        RepositoriesColumn( "Repositories",
                            model_class=model.Repository,
                            attach_popup=False )
    ]
    # Override these
    default_filter = {}
    global_actions = []
    operations = []
    standard_filters = []
    num_rows_per_page = 50
    preserve_state = False
    use_paging = True

class ValidCategoryGrid( CategoryGrid ):
    class RepositoriesColumn( grids.TextColumn ):
        def get_value( self, trans, grid, category ):
            if category.repositories:
                viewable_repositories = 0
                for rca in category.repositories:
                    repository = rca.repository
                    if not repository.deprecated and repository.downloadable_revisions:
                        viewable_repositories += 1
                return viewable_repositories
            return 0
    title = "Categories of valid repositories"
    model_class = model.Category
    template='/webapps/community/category/valid_grid.mako'
    default_sort_key = "name"
    columns = [
        CategoryGrid.NameColumn( "Name",
                                 key="Category.name",
                                 link=( lambda item: dict( operation="valid_repositories_by_category", id=item.id ) ),
                                 attach_popup=False ),
        CategoryGrid.DescriptionColumn( "Description",
                                        key="Category.description",
                                        attach_popup=False ),
        # Columns that are valid for filtering but are not visible.
        RepositoriesColumn( "Valid repositories",
                            model_class=model.Repository,
                            attach_popup=False )
    ]
    # Override these
    default_filter = {}
    global_actions = []
    operations = []
    standard_filters = []
    num_rows_per_page = 50
    preserve_state = False
    use_paging = True

class RepositoryGrid( grids.Grid ):
    class NameColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository ):
            return escape_html( repository.name )
    class MetadataRevisionColumn( grids.GridColumn ):
        def __init__( self, col_name ):
            grids.GridColumn.__init__( self, col_name )
        def get_value( self, trans, grid, repository ):
            """Display a SelectField whose options are the changeset_revision strings of all metadata revisions of this repository."""
            # A repository's metadata revisions may not all be installable, as some may contain only invalid tools.
            select_field = build_changeset_revision_select_field( trans, repository, downloadable=False )
            if len( select_field.options ) > 1:
                return select_field.get_html()
            elif len( select_field.options ) == 1:
                return select_field.options[ 0 ][ 0 ]
            return ''
    class TipRevisionColumn( grids.GridColumn ):
        def __init__( self, col_name ):
            grids.GridColumn.__init__( self, col_name )
        def get_value( self, trans, grid, repository ):
            """Display the repository tip revision label."""
            return escape_html( repository.revision( trans.app ) )
    class DescriptionColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository ):
            return escape_html( repository.description )
    class CategoryColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository ):
            rval = '<ul>'
            if repository.categories:
                for rca in repository.categories:
                    rval += '<li><a href="browse_repositories?operation=repositories_by_category&id=%s">%s</a></li>' \
                        % ( trans.security.encode_id( rca.category.id ), rca.category.name )
            else:
                rval += '<li>not set</li>'
            rval += '</ul>'
            return rval
    class RepositoryCategoryColumn( grids.GridColumn ):
        def filter( self, trans, user, query, column_filter ):
            """Modify query to filter by category."""
            if column_filter == "All":
                return query
            return query.filter( model.Category.name == column_filter )
    class UserColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository ):
            if repository.user:
                return escape_html( repository.user.username )
            return 'no user'
    class EmailColumn( grids.TextColumn ):
        def filter( self, trans, user, query, column_filter ):
            if column_filter == 'All':
                return query
            return query.filter( and_( model.Repository.table.c.user_id == model.User.table.c.id,
                                       model.User.table.c.email == column_filter ) )
    class EmailAlertsColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository ):
            if trans.user and repository.email_alerts and trans.user.email in from_json_string( repository.email_alerts ):
                return 'yes'
            return ''
    class DeprecatedColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository ):
            if repository.deprecated:
                return 'yes'
            return ''
    # Grid definition
    title = "Repositories"
    model_class = model.Repository
    template='/webapps/community/repository/grid.mako'
    default_sort_key = "name"
    columns = [
        NameColumn( "Name",
                    key="name",
                    link=( lambda item: dict( operation="view_or_manage_repository", id=item.id ) ),
                    attach_popup=True ),
        DescriptionColumn( "Synopsis",
                           key="description",
                           attach_popup=False ),
        MetadataRevisionColumn( "Metadata Revisions" ),
        TipRevisionColumn( "Tip Revision" ),
        CategoryColumn( "Category",
                        model_class=model.Category,
                        key="Category.name",
                        attach_popup=False ),
        UserColumn( "Owner",
                     model_class=model.User,
                     link=( lambda item: dict( operation="repositories_by_user", id=item.id ) ),
                     attach_popup=False,
                     key="User.username" ),
        # Columns that are valid for filtering but are not visible.
        EmailColumn( "Email",
                     model_class=model.User,
                     key="email",
                     visible=False ),
        RepositoryCategoryColumn( "Category",
                                  model_class=model.Category,
                                  key="Category.name",
                                  visible=False )
    ]
    columns.append( grids.MulticolFilterColumn( "Search repository name, description", 
                                                cols_to_filter=[ columns[0], columns[1] ],
                                                key="free-text-search",
                                                visible=False,
                                                filterable="standard" ) )
    operations = [ grids.GridOperation( "Receive email alerts",
                                        allow_multiple=False,
                                        condition=( lambda item: not item.deleted ),
                                        async_compatible=False ) ]
    standard_filters = []
    default_filter = dict( deleted="False" )
    num_rows_per_page = 50
    preserve_state = False
    use_paging = True
    def build_initial_query( self, trans, **kwd ):
        return trans.sa_session.query( model.Repository ) \
                               .filter( and_( model.Repository.table.c.deleted == False,
                                              model.Repository.table.c.deprecated == False ) ) \
                               .join( model.User.table ) \
                               .outerjoin( model.RepositoryCategoryAssociation.table ) \
                               .outerjoin( model.Category.table )

class RepositoriesIOwnGrid( RepositoryGrid ):
    title = "Repositories I own"
    columns = [
        RepositoryGrid.NameColumn( "Name",
                                   key="name",
                                   link=( lambda item: dict( operation="view_or_manage_repository", id=item.id ) ),
                                   attach_popup=True ),
        RepositoryGrid.MetadataRevisionColumn( "Metadata Revisions" ),
        RepositoryGrid.TipRevisionColumn( "Tip Revision" ),
        RepositoryGrid.CategoryColumn( "Category",
                                       model_class=model.Category,
                                       key="Category.name",
                                       attach_popup=False ),
        RepositoryGrid.DeprecatedColumn( "Deprecated" )
    ]
    columns.append( grids.MulticolFilterColumn( "Search repository name", 
                                                cols_to_filter=[ columns[0] ],
                                                key="free-text-search",
                                                visible=False,
                                                filterable="standard" ) )
    operations = [ grids.GridOperation( "Mark as deprecated",
                                        allow_multiple=False,
                                        condition=( lambda item: not item.deleted and not item.deprecated ),
                                        async_compatible=False ),
                   grids.GridOperation( "Mark as not deprecated",
                                        allow_multiple=False,
                                        condition=( lambda item: not item.deleted and item.deprecated ),
                                        async_compatible=False ) ]
    def build_initial_query( self, trans, **kwd ):
        return trans.sa_session.query( model.Repository ) \
                               .filter( and_( model.Repository.table.c.deleted == False,
                                              model.Repository.table.c.user_id == trans.user.id ) ) \
                               .join( model.User.table ) \
                               .outerjoin( model.RepositoryCategoryAssociation.table ) \
                               .outerjoin( model.Category.table )

class DeprecatedRepositoriesIOwnGrid( RepositoriesIOwnGrid ):
    title = "Deprecated repositories I own"
    columns = [
        RepositoriesIOwnGrid.NameColumn( "Name",
                                         key="name",
                                         link=( lambda item: dict( operation="view_or_manage_repository", id=item.id ) ),
                                         attach_popup=True ),
        RepositoriesIOwnGrid.MetadataRevisionColumn( "Metadata Revisions" ),
        RepositoriesIOwnGrid.TipRevisionColumn( "Tip Revision" ),
        RepositoriesIOwnGrid.CategoryColumn( "Category",
                                             model_class=model.Category,
                                             key="Category.name",
                                             attach_popup=False ),
    ]
    columns.append( grids.MulticolFilterColumn( "Search repository name", 
                                                cols_to_filter=[ columns[0] ],
                                                key="free-text-search",
                                                visible=False,
                                                filterable="standard" ) )
    def build_initial_query( self, trans, **kwd ):
        return trans.sa_session.query( model.Repository ) \
                               .filter( and_( model.Repository.table.c.deleted == False,
                                              model.Repository.table.c.user_id == trans.user.id,
                                              model.Repository.table.c.deprecated == True ) ) \
                               .join( model.User.table ) \
                               .outerjoin( model.RepositoryCategoryAssociation.table ) \
                               .outerjoin( model.Category.table )

class EmailAlertsRepositoryGrid( RepositoryGrid ):
    columns = [
        RepositoryGrid.NameColumn( "Name",
                                   key="name",
                                   link=( lambda item: dict( operation="view_or_manage_repository", id=item.id ) ),
                                   attach_popup=False ),
        RepositoryGrid.DescriptionColumn( "Synopsis",
                                          key="description",
                                          attach_popup=False ),
        RepositoryGrid.UserColumn( "Owner",
                                   model_class=model.User,
                                   link=( lambda item: dict( operation="repositories_by_user", id=item.id ) ),
                                   attach_popup=False,
                                   key="User.username" ),
        RepositoryGrid.EmailAlertsColumn( "Alert", attach_popup=False ),
        # Columns that are valid for filtering but are not visible.
        grids.DeletedColumn( "Deleted",
                             key="deleted",
                             visible=False,
                             filterable="advanced" )
    ]
    operations = [ grids.GridOperation( "Receive email alerts",
                                        allow_multiple=True,
                                        condition=( lambda item: not item.deleted ),
                                        async_compatible=False ) ]
    global_actions = [
            grids.GridAction( "User preferences", dict( controller='user', action='index', cntrller='repository' ) )
        ]

class MyWritableRepositoriesGrid( RepositoryGrid ):
    # This grid filters out repositories that have been marked as deprecated.
    columns = [
        RepositoryGrid.NameColumn( "Name",
                                   key="name",
                                   link=( lambda item: dict( operation="view_or_manage_repository", id=item.id ) ),
                                   attach_popup=True ),
        RepositoryGrid.MetadataRevisionColumn( "Metadata Revisions" ),
        RepositoryGrid.TipRevisionColumn( "Tip Revision" ),
        RepositoryGrid.UserColumn( "Owner",
                                   model_class=model.User,
                                   link=( lambda item: dict( operation="repositories_by_user", id=item.id ) ),
                                   attach_popup=False,
                                   key="User.username" ),
        RepositoryGrid.EmailAlertsColumn( "Alert", attach_popup=False ),
        # Columns that are valid for filtering but are not visible.
        RepositoryGrid.EmailColumn( "Email",
                                    model_class=model.User,
                                    key="email",
                                    visible=False ),
        RepositoryGrid.RepositoryCategoryColumn( "Category",
                                                 model_class=model.Category,
                                                 key="Category.name",
                                                 visible=False )
    ]
    columns.append( grids.MulticolFilterColumn( "Search repository name", 
                                                cols_to_filter=[ columns[0] ],
                                                key="free-text-search",
                                                visible=False,
                                                filterable="standard" ) )
    operations = [ grids.GridOperation( "Receive email alerts",
                                        allow_multiple=False,
                                        condition=( lambda item: not item.deleted ),
                                        async_compatible=False ) ]
    def build_initial_query( self, trans, **kwd ):
        # TODO: improve performance by adding a db table associating users with repositories for which they have write access.
        username = trans.user.username
        clause_list = []
        for repository in trans.sa_session.query( model.Repository ) \
                                          .filter( and_( model.Repository.table.c.deprecated == False,
                                                         model.Repository.table.c.deleted == False ) ):
            allow_push = repository.allow_push( trans.app )
            if allow_push:
                allow_push_usernames = allow_push.split( ',' )
                if username in allow_push_usernames:
                    clause_list.append( model.Repository.table.c.id == repository.id )
        if clause_list:
            return trans.sa_session.query( model.Repository ) \
                                   .filter( or_( *clause_list ) ) \
                                   .join( model.User.table ) \
                                   .outerjoin( model.RepositoryCategoryAssociation.table ) \
                                   .outerjoin( model.Category.table )
        # Return an empty query.
        return trans.sa_session.query( model.Repository ) \
                               .filter( model.Repository.table.c.id < 0 )

class ValidRepositoryGrid( RepositoryGrid ):
    # This grid filters out repositories that have been marked as deprecated.
    class CategoryColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository ):
            rval = '<ul>'
            if repository.categories:
                for rca in repository.categories:
                    rval += '<li><a href="browse_repositories?operation=valid_repositories_by_category&id=%s">%s</a></li>' \
                        % ( trans.security.encode_id( rca.category.id ), rca.category.name )
            else:
                rval += '<li>not set</li>'
            rval += '</ul>'
            return rval
    class RepositoryCategoryColumn( grids.GridColumn ):
        def filter( self, trans, user, query, column_filter ):
            """Modify query to filter by category."""
            if column_filter == "All":
                return query
            return query.filter( model.Category.name == column_filter )
    class RevisionColumn( grids.GridColumn ):
        def __init__( self, col_name ):
            grids.GridColumn.__init__( self, col_name )
        def get_value( self, trans, grid, repository ):
            """Display a SelectField whose options are the changeset_revision strings of all download-able revisions of this repository."""
            select_field = build_changeset_revision_select_field( trans, repository, downloadable=True )
            if len( select_field.options ) > 1:
                return select_field.get_html()
            elif len( select_field.options ) == 1:
                return select_field.options[ 0 ][ 0 ]
            return ''
    title = "Valid repositories"
    columns = [
        RepositoryGrid.NameColumn( "Name",
                                   key="name",
                                   attach_popup=True ),
        RepositoryGrid.DescriptionColumn( "Synopsis",
                                          key="description",
                                          attach_popup=False ),
        RevisionColumn( "Installable Revisions" ),
        RepositoryGrid.UserColumn( "Owner",
                                   model_class=model.User,
                                   attach_popup=False ),
        # Columns that are valid for filtering but are not visible.
        RepositoryCategoryColumn( "Category",
                                  model_class=model.Category,
                                  key="Category.name",
                                  visible=False )
    ]
    columns.append( grids.MulticolFilterColumn( "Search repository name, description", 
                                                cols_to_filter=[ columns[0], columns[1] ],
                                                key="free-text-search",
                                                visible=False,
                                                filterable="standard" ) )
    operations = []
    def build_initial_query( self, trans, **kwd ):
        if 'id' in kwd:
            # The user is browsing categories of valid repositories, so filter the request by the received id, which is a category id.
            return trans.sa_session.query( model.Repository ) \
                                   .filter( and_( model.Repository.table.c.deleted == False,
                                                  model.Repository.table.c.deprecated == False ) ) \
                                   .join( model.RepositoryMetadata.table ) \
                                   .join( model.User.table ) \
                                   .join( model.RepositoryCategoryAssociation.table ) \
                                   .join( model.Category.table ) \
                                   .filter( and_( model.Category.table.c.id == trans.security.decode_id( kwd[ 'id' ] ),
                                                  model.RepositoryMetadata.table.c.downloadable == True ) )
        # The user performed a free text search on the ValidCategoryGrid.
        return trans.sa_session.query( model.Repository ) \
                               .filter( and_( model.Repository.table.c.deleted == False,
                                              model.Repository.table.c.deprecated == False ) ) \
                               .join( model.RepositoryMetadata.table ) \
                               .join( model.User.table ) \
                               .outerjoin( model.RepositoryCategoryAssociation.table ) \
                               .outerjoin( model.Category.table ) \
                               .filter( model.RepositoryMetadata.table.c.downloadable == True )

class MatchedRepositoryGrid( grids.Grid ):
    # This grid filters out repositories that have been marked as deprecated.
    class NameColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository_metadata ):
            return repository_metadata.repository.name
    class DescriptionColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository_metadata ):
            return repository_metadata.repository.description
    class RevisionColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository_metadata ):
            return repository_metadata.changeset_revision
    class UserColumn( grids.TextColumn ):
        def get_value( self, trans, grid, repository_metadata ):
            if repository_metadata.repository.user:
                return repository_metadata.repository.user.username
            return 'no user'
    # Grid definition
    title = "Matching repositories"
    model_class = model.RepositoryMetadata
    template='/webapps/community/repository/grid.mako'
    default_sort_key = "Repository.name"
    columns = [
        NameColumn( "Repository name",
                    link=( lambda item: dict( operation="view_or_manage_repository", id=item.id ) ),
                    attach_popup=True ),
        DescriptionColumn( "Synopsis",
                           attach_popup=False ),
        RevisionColumn( "Revision" ),
        UserColumn( "Owner",
                     model_class=model.User,
                     attach_popup=False )
    ]
    operations = []
    standard_filters = []
    default_filter = {}
    num_rows_per_page = 50
    preserve_state = False
    use_paging = True
    def build_initial_query( self, trans, **kwd ):
        match_tuples = kwd.get( 'match_tuples', [] )
        clause_list = []
        if match_tuples:
            for match_tuple in match_tuples:
                repository_id, changeset_revision = match_tuple
                clause_list.append( "%s=%d and %s='%s'" % ( model.RepositoryMetadata.table.c.repository_id,
                                                            int( repository_id ),
                                                            model.RepositoryMetadata.table.c.changeset_revision,
                                                            changeset_revision ) )
            return trans.sa_session.query( model.RepositoryMetadata ) \
                                   .join( model.Repository ) \
                                   .filter( and_( model.Repository.table.c.deleted == False,
                                                  model.Repository.table.c.deprecated == False ) ) \
                                   .join( model.User.table ) \
                                   .filter( or_( *clause_list ) ) \
                                   .order_by( model.Repository.name )
        # Return an empty query
        return trans.sa_session.query( model.RepositoryMetadata ) \
                               .filter( model.RepositoryMetadata.id < 0 )

class InstallMatchedRepositoryGrid( MatchedRepositoryGrid ):
    columns = [ col for col in MatchedRepositoryGrid.columns ]
    # Override the NameColumn
    columns[ 0 ] = MatchedRepositoryGrid.NameColumn( "Name",
                                                     link=( lambda item: dict( operation="view_or_manage_repository", id=item.id ) ),
                                                     attach_popup=False )

class RepositoryController( BaseUIController, common.ItemRatings ):

    install_matched_repository_grid = InstallMatchedRepositoryGrid()
    matched_repository_grid = MatchedRepositoryGrid()
    valid_repository_grid = ValidRepositoryGrid()
    repository_grid = RepositoryGrid()
    email_alerts_repository_grid = EmailAlertsRepositoryGrid()
    category_grid = CategoryGrid()
    valid_category_grid = ValidCategoryGrid()
    my_writable_repositories_grid = MyWritableRepositoriesGrid()
    repositories_i_own_grid = RepositoriesIOwnGrid()
    deprecated_repositories_i_own_grid = DeprecatedRepositoriesIOwnGrid()

    @web.expose
    def browse_categories( self, trans, **kwd ):
        # The request came from the tool shed.
        if 'f-free-text-search' in kwd:
            # Trick to enable searching repository name, description from the CategoryGrid.  What we've done is rendered the search box for the
            # RepositoryGrid on the grid.mako template for the CategoryGrid.  See ~/templates/webapps/community/category/grid.mako.  Since we
            # are searching repositories and not categories, redirect to browse_repositories().
            if 'id' in kwd and 'f-free-text-search' in kwd and kwd[ 'id' ] == kwd[ 'f-free-text-search' ]:
                # The value of 'id' has been set to the search string, which is a repository name.  We'll try to get the desired encoded repository
                # id to pass on.
                try:
                    repository = common.get_repository_by_name( trans, kwd[ 'id' ] )
                    kwd[ 'id' ] = trans.security.encode_id( repository.id )
                except:
                    pass
            return self.browse_repositories( trans, **kwd )
        if 'operation' in kwd:
            operation = kwd['operation'].lower()
            if operation in [ "repositories_by_category", "repositories_by_user" ]:
                # Eliminate the current filters if any exist.
                for k, v in kwd.items():
                    if k.startswith( 'f-' ):
                        del kwd[ k ]
                return trans.response.send_redirect( web.url_for( controller='repository',
                                                                  action='browse_repositories',
                                                                  **kwd ) )
        return self.category_grid( trans, **kwd )
    @web.expose
    def browse_invalid_tools( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        cntrller = params.get( 'cntrller', 'repository' )
        is_admin = trans.user_is_admin()
        invalid_tools_dict = odict()
        if is_admin and cntrller == 'admin':
            for repository in trans.sa_session.query( trans.model.Repository ) \
                                              .filter( trans.model.Repository.table.c.deleted == False ) \
                                              .order_by( trans.model.Repository.table.c.name ):
                # A repository's metadata_revisions are those that ignore the value of the repository_metadata.downloadable column.
                for downloadable_revision in repository.metadata_revisions:
                    metadata = downloadable_revision.metadata
                    invalid_tools = metadata.get( 'invalid_tools', [] )
                    for invalid_tool_config in invalid_tools:
                        invalid_tools_dict[ invalid_tool_config ] = ( repository.id,
                                                                      repository.name,
                                                                      repository.user.username,
                                                                      downloadable_revision.changeset_revision )
        else:
            for repository in trans.sa_session.query( trans.model.Repository ) \
                                              .filter( and_( trans.model.Repository.table.c.deleted == False,
                                                             trans.model.Repository.table.c.user_id == trans.user.id ) ) \
                                              .order_by( trans.model.Repository.table.c.name ):
                for downloadable_revision in repository.metadata_revisions:
                    metadata = downloadable_revision.metadata
                    invalid_tools = metadata.get( 'invalid_tools', [] )
                    for invalid_tool_config in invalid_tools:
                        invalid_tools_dict[ invalid_tool_config ] = ( repository.id,
                                                                      repository.name,
                                                                      repository.user.username,
                                                                      downloadable_revision.changeset_revision )
        return trans.fill_template( '/webapps/community/repository/browse_invalid_tools.mako',
                                    cntrller=cntrller,
                                    invalid_tools_dict=invalid_tools_dict,
                                    message=message,
                                    status=status )
    @web.expose
    def browse_repositories( self, trans, **kwd ):
        # We add params to the keyword dict in this method in order to rename the param with an "f-" prefix, simulating filtering by clicking a search
        # link.  We have to take this approach because the "-" character is illegal in HTTP requests.
        if 'operation' in kwd:
            operation = kwd['operation'].lower()
            if operation == "view_or_manage_repository":
                return trans.response.send_redirect( web.url_for( controller='repository',
                                                                  action='view_or_manage_repository',
                                                                  **kwd ) )
            elif operation == "edit_repository":
                return trans.response.send_redirect( web.url_for( controller='repository',
                                                                  action='edit_repository',
                                                                  **kwd ) )
            elif operation == "repositories_by_user":
                # Eliminate the current filters if any exist.
                for k, v in kwd.items():
                    if k.startswith( 'f-' ):
                        del kwd[ k ]
                if 'user_id' in kwd:
                    user = common.get_user( trans, kwd[ 'user_id' ] )
                    kwd[ 'f-email' ] = user.email
                    del kwd[ 'user_id' ]
                else:
                    # The received id is the repository id, so we need to get the id of the user that uploaded the repository.
                    repository_id = kwd.get( 'id', None )
                    repository = suc.get_repository_in_tool_shed( trans, repository_id )
                    kwd[ 'f-email' ] = repository.user.email
            elif operation == "repositories_i_own":
                # Eliminate the current filters if any exist.
                for k, v in kwd.items():
                    if k.startswith( 'f-' ):
                        del kwd[ k ]
                return self.repositories_i_own_grid( trans, **kwd )
            elif operation == "deprecated_repositories_i_own":
                # Eliminate the current filters if any exist.
                for k, v in kwd.items():
                    if k.startswith( 'f-' ):
                        del kwd[ k ]
                return self.deprecated_repositories_i_own_grid( trans, **kwd )
            elif operation in [ 'mark as deprecated', 'mark as not deprecated' ]:
                # Eliminate the current filters if any exist.
                for k, v in kwd.items():
                    if k.startswith( 'f-' ):
                        del kwd[ k ]
                kwd[ 'mark_deprecated' ] = operation == 'mark as deprecated'
                return trans.response.send_redirect( web.url_for( controller='repository',
                                                                  action='deprecate',
                                                                  **kwd ) )
            elif operation == "reviewed_repositories_i_own":
                return trans.response.send_redirect( web.url_for( controller='repository_review',
                                                                  action='reviewed_repositories_i_own' ) )
            elif operation == "my_writable_repositories":
                return self.my_writable_repositories_grid( trans, **kwd )
            elif operation == "repositories_by_category":
                # Eliminate the current filters if any exist.
                for k, v in kwd.items():
                    if k.startswith( 'f-' ):
                        del kwd[ k ]
                category_id = kwd.get( 'id', None )
                category = common.get_category( trans, category_id )
                kwd[ 'f-Category.name' ] = category.name
            elif operation == "receive email alerts":
                if trans.user:
                    if kwd[ 'id' ]:
                        kwd[ 'caller' ] = 'browse_repositories'
                        return trans.response.send_redirect( web.url_for( controller='repository',
                                                                          action='set_email_alerts',
                                                                          **kwd ) )
                else:
                    kwd[ 'message' ] = 'You must be logged in to set email alerts.'
                    kwd[ 'status' ] = 'error'
                    del kwd[ 'operation' ]
        # The changeset_revision_select_field in the RepositoryGrid performs a refresh_on_change
        # which sends in request parameters like changeset_revison_1, changeset_revision_2, etc.  One
        # of the many select fields on the grid performed the refresh_on_change, so we loop through 
        # all of the received values to see which value is not the repository tip.  If we find it, we
        # know the refresh_on_change occurred, and we have the necessary repository id and change set
        # revision to pass on.
        for k, v in kwd.items():
            changset_revision_str = 'changeset_revision_'
            if k.startswith( changset_revision_str ):
                repository_id = trans.security.encode_id( int( k.lstrip( changset_revision_str ) ) )
                repository = suc.get_repository_in_tool_shed( trans, repository_id )
                if repository.tip( trans.app ) != v:
                    return trans.response.send_redirect( web.url_for( controller='repository',
                                                                      action='browse_repositories',
                                                                      operation='view_or_manage_repository',
                                                                      id=trans.security.encode_id( repository.id ),
                                                                      changeset_revision=v ) )
        return self.repository_grid( trans, **kwd )
    @web.expose
    def browse_repository( self, trans, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        commit_message = util.restore_text( params.get( 'commit_message', 'Deleted selected files' ) )
        repository = suc.get_repository_in_tool_shed( trans, id )
        repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) )
        # Update repository files for browsing.
        suc.update_repository( repo )
        is_malicious = common.changeset_is_malicious( trans, id, repository.tip( trans.app ) )
        metadata = self.get_metadata( trans, id, repository.tip( trans.app ) )
        return trans.fill_template( '/webapps/community/repository/browse_repository.mako',
                                    repository=repository,
                                    metadata=metadata,
                                    commit_message=commit_message,
                                    is_malicious=is_malicious,
                                    message=message,
                                    status=status )
    @web.expose
    def browse_valid_categories( self, trans, **kwd ):
        # The request came from Galaxy, so restrict category links to display only valid repository changeset revisions.
        galaxy_url = kwd.get( 'galaxy_url', None )
        if galaxy_url:
            trans.set_cookie( galaxy_url, name='toolshedgalaxyurl' )
        if 'f-free-text-search' in kwd:
            if kwd[ 'f-free-text-search' ] == 'All':
                # The user performed a search, then clicked the "x" to eliminate the search criteria.
                new_kwd = {}
                return self.valid_category_grid( trans, **new_kwd )
            # Since we are searching valid repositories and not categories, redirect to browse_valid_repositories().
            if 'id' in kwd and 'f-free-text-search' in kwd and kwd[ 'id' ] == kwd[ 'f-free-text-search' ]:
                # The value of 'id' has been set to the search string, which is a repository name.
                # We'll try to get the desired encoded repository id to pass on.
                try:
                    name = kwd[ 'id' ]
                    repository = common.get_repository_by_name( trans, name )
                    kwd[ 'id' ] = trans.security.encode_id( repository.id )
                except:
                    pass
            return self.browse_valid_repositories( trans, **kwd )
        if 'operation' in kwd:
            operation = kwd['operation'].lower()
            if operation in [ "valid_repositories_by_category", "valid_repositories_by_user" ]:
                # Eliminate the current filters if any exist.
                for k, v in kwd.items():
                    if k.startswith( 'f-' ):
                        del kwd[ k ]
                return trans.response.send_redirect( web.url_for( controller='repository',
                                                                  action='browse_valid_repositories',
                                                                  **kwd ) )
        return self.valid_category_grid( trans, **kwd )
    @web.expose
    def browse_valid_repositories( self, trans, **kwd ):
        galaxy_url = kwd.get( 'galaxy_url', None )
        if 'f-free-text-search' in kwd:
            if 'f-Category.name' in kwd:
                # The user browsed to a category and then entered a search string, so get the category associated with it's value.
                category_name = kwd[ 'f-Category.name' ]
                category = common.get_category_by_name( trans, category_name )
                # Set the id value in kwd since it is required by the ValidRepositoryGrid.build_initial_query method.
                kwd[ 'id' ] = trans.security.encode_id( category.id )
        if galaxy_url:
            trans.set_cookie( galaxy_url, name='toolshedgalaxyurl' )
        if 'operation' in kwd:
            operation = kwd[ 'operation' ].lower()
            if operation == "preview_tools_in_changeset":
                repository_id = kwd.get( 'id', None )
                repository = suc.get_repository_in_tool_shed( trans, repository_id )
                repository_metadata = common.get_latest_repository_metadata( trans, repository.id )
                latest_installable_changeset_revision = repository_metadata.changeset_revision
                return trans.response.send_redirect( web.url_for( controller='repository',
                                                                  action='preview_tools_in_changeset',
                                                                  repository_id=repository_id,
                                                                  changeset_revision=latest_installable_changeset_revision ) )
            elif operation == "valid_repositories_by_category":
                # Eliminate the current filters if any exist.
                for k, v in kwd.items():
                    if k.startswith( 'f-' ):
                        del kwd[ k ]
                category_id = kwd.get( 'id', None )
                category = common.get_category( trans, category_id )
                kwd[ 'f-Category.name' ] = category.name
        # The changeset_revision_select_field in the ValidRepositoryGrid performs a refresh_on_change which sends in request parameters like
        # changeset_revison_1, changeset_revision_2, etc.  One of the many select fields on the grid performed the refresh_on_change, so we loop
        # through all of the received values to see which value is not the repository tip.  If we find it, we know the refresh_on_change occurred
        # and we have the necessary repository id and change set revision to pass on.
        repository_id = None
        for k, v in kwd.items():
            changset_revision_str = 'changeset_revision_'
            if k.startswith( changset_revision_str ):
                repository_id = trans.security.encode_id( int( k.lstrip( changset_revision_str ) ) )
                repository = suc.get_repository_in_tool_shed( trans, repository_id )
                if repository.tip( trans.app ) != v:
                    return trans.response.send_redirect( web.url_for( controller='repository',
                                                                      action='preview_tools_in_changeset',
                                                                      repository_id=trans.security.encode_id( repository.id ),
                                                                      changeset_revision=v ) )
        url_args = dict( action='browse_valid_repositories',
                         operation='preview_tools_in_changeset',
                         repository_id=repository_id )
        self.valid_repository_grid.operations = [ grids.GridOperation( "Preview and install",
                                                                            url_args=url_args,
                                                                            allow_multiple=False,
                                                                            async_compatible=False ) ]
        return self.valid_repository_grid( trans, **kwd )
    def __build_allow_push_select_field( self, trans, current_push_list, selected_value='none' ):
        options = []
        for user in trans.sa_session.query( trans.model.User ):
            if user.username not in current_push_list:
                options.append( user )
        return build_select_field( trans,
                                   objs=options,
                                   label_attr='username',
                                   select_field_name='allow_push',
                                   selected_value=selected_value,
                                   refresh_on_change=False,
                                   multiple=True )
    def __change_repository_name_in_hgrc_file( self, hgrc_file, new_name ):
        config = ConfigParser.ConfigParser()
        config.read( hgrc_file )
        config.read( hgrc_file )
        config.set( 'web', 'name', new_name )
        new_file = open( hgrc_file, 'wb' )
        config.write( new_file )
        new_file.close()
    @web.expose
    def check_for_updates( self, trans, **kwd ):
        """Handle a request from a local Galaxy instance."""
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        # If the request originated with the UpdateManager, it will not include a galaxy_url.
        galaxy_url = kwd.get( 'galaxy_url', '' )
        name = params.get( 'name', None )
        owner = params.get( 'owner', None )
        changeset_revision = params.get( 'changeset_revision', None )
        repository = suc.get_repository_by_name_and_owner( trans, name, owner )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        # Default to the current changeset revision.
        update_to_ctx = suc.get_changectx_for_changeset( repo, changeset_revision )
        latest_changeset_revision = changeset_revision
        from_update_manager = kwd.get( 'from_update_manager', False )
        if from_update_manager:
            update = 'true'
            no_update = 'false'
        else:
            # Start building up the url to redirect back to the calling Galaxy instance.            
            url = suc.url_join( galaxy_url,
                                'admin_toolshed/update_to_changeset_revision?tool_shed_url=%s&name=%s&owner=%s&changeset_revision=%s&latest_changeset_revision=' % \
                                ( url_for( '/', qualified=True ), repository.name, repository.user.username, changeset_revision ) )
        if changeset_revision == repository.tip( trans.app ):
            # If changeset_revision is the repository tip, there are no additional updates.
            if from_update_manager:
                return no_update
            # Return the same value for changeset_revision and latest_changeset_revision.
            url += latest_changeset_revision
        else:
            repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, 
                                                                                     trans.security.encode_id( repository.id ),
                                                                                     changeset_revision )
            if repository_metadata:
                # If changeset_revision is in the repository_metadata table for this repository, there are no additional updates.
                if from_update_manager:
                    return no_update
                else:
                    # Return the same value for changeset_revision and latest_changeset_revision.
                    url += latest_changeset_revision
            else:
                # The changeset_revision column in the repository_metadata table has been updated with a new changeset_revision value since the
                # repository was installed.  We need to find the changeset_revision to which we need to update.
                update_to_changeset_hash = None
                for changeset in repo.changelog:
                    changeset_hash = str( repo.changectx( changeset ) )
                    ctx = suc.get_changectx_for_changeset( repo, changeset_hash )
                    if update_to_changeset_hash:
                        if changeset_hash == repository.tip( trans.app ):
                            update_to_ctx = suc.get_changectx_for_changeset( repo, changeset_hash )
                            latest_changeset_revision = changeset_hash
                            break
                        else:
                            repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans,
                                                                                                     trans.security.encode_id( repository.id ),
                                                                                                     changeset_hash )
                            if repository_metadata:
                                # We found a RepositoryMetadata record.
                                update_to_ctx = suc.get_changectx_for_changeset( repo, changeset_hash )
                                latest_changeset_revision = changeset_hash
                                break
                            else:
                                update_to_changeset_hash = changeset_hash
                    else:
                        if changeset_hash == changeset_revision:
                            # We've found the changeset in the changelog for which we need to get the next update.
                            update_to_changeset_hash = changeset_hash
                if from_update_manager:
                    if latest_changeset_revision == changeset_revision:
                        return no_update
                    return update
                url += str( latest_changeset_revision )
        url += '&latest_ctx_rev=%s' % str( update_to_ctx.rev() )
        return trans.response.send_redirect( url )
    @web.expose
    def contact_owner( self, trans, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        repository = suc.get_repository_in_tool_shed( trans, id )
        metadata = self.get_metadata( trans, id, repository.tip( trans.app ) )
        if trans.user and trans.user.email:
            return trans.fill_template( "/webapps/community/repository/contact_owner.mako",
                                        repository=repository,
                                        metadata=metadata,
                                        message=message,
                                        status=status )
        else:
            # Do all we can to eliminate spam.
            return trans.show_error_message( "You must be logged in to contact the owner of a repository." )
    def __create_hgrc_file( self, trans, repository ):
        # At this point, an entry for the repository is required to be in the hgweb.config file so we can call repository.repo_path( trans.app ).
        # Since we support both http and https, we set push_ssl to False to override the default (which is True) in the mercurial api.  The hg
        # purge extension purges all files and directories not being tracked by mercurial in the current repository.  It'll remove unknown files
        # and empty directories.  This is not currently used because it is not supported in the mercurial API.
        repo = hg.repository( suc.get_configured_ui(), path=repository.repo_path( trans.app ) )
        fp = repo.opener( 'hgrc', 'wb' )
        fp.write( '[paths]\n' )
        fp.write( 'default = .\n' )
        fp.write( 'default-push = .\n' )
        fp.write( '[web]\n' )
        fp.write( 'allow_push = %s\n' % repository.user.username )
        fp.write( 'name = %s\n' % repository.name )
        fp.write( 'push_ssl = false\n' )
        fp.write( '[extensions]\n' )
        fp.write( 'hgext.purge=' )
        fp.close()
    @web.expose
    def create_repository( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        categories = common.get_categories( trans )
        if not categories:
            message = 'No categories have been configured in this instance of the Galaxy Tool Shed.  ' + \
                'An administrator needs to create some via the Administrator control panel before creating repositories.',
            status = 'error'
            return trans.response.send_redirect( web.url_for( controller='repository',
                                                              action='browse_repositories',
                                                              message=message,
                                                              status=status ) )
        name = util.restore_text( params.get( 'name', '' ) )
        description = util.restore_text( params.get( 'description', '' ) )
        long_description = util.restore_text( params.get( 'long_description', '' ) )
        category_ids = util.listify( params.get( 'category_id', '' ) )
        selected_categories = [ trans.security.decode_id( id ) for id in category_ids ]
        if params.get( 'create_repository_button', False ):
            error = False
            message = self.__validate_repository_name( name, trans.user )
            if message:
                error = True
            if not description:
                message = 'Enter a description.'
                error = True
            if not error:
                # Add the repository record to the db
                repository = trans.app.model.Repository( name=name,
                                                         description=description,
                                                         long_description=long_description,
                                                         user_id=trans.user.id )
                # Flush to get the id
                trans.sa_session.add( repository )
                trans.sa_session.flush()
                # Determine the repository's repo_path on disk
                dir = os.path.join( trans.app.config.file_path, *directory_hash_id( repository.id ) )
                # Create directory if it does not exist
                if not os.path.exists( dir ):
                    os.makedirs( dir )
                # Define repo name inside hashed directory
                repository_path = os.path.join( dir, "repo_%d" % repository.id )
                # Create local repository directory
                if not os.path.exists( repository_path ):
                    os.makedirs( repository_path )
                # Create the local repository
                repo = hg.repository( suc.get_configured_ui(), repository_path, create=True )
                # Add an entry in the hgweb.config file for the local repository.
                lhs = "repos/%s/%s" % ( repository.user.username, repository.name )
                trans.app.hgweb_config_manager.add_entry( lhs, repository_path )
                # Create a .hg/hgrc file for the local repository
                self.__create_hgrc_file( trans, repository )
                flush_needed = False
                if category_ids:
                    # Create category associations
                    for category_id in category_ids:
                        category = trans.app.model.Category.get( trans.security.decode_id( category_id ) )
                        rca = trans.app.model.RepositoryCategoryAssociation( repository, category )
                        trans.sa_session.add( rca )
                        flush_needed = True
                if flush_needed:
                    trans.sa_session.flush()
                message = "Repository '%s' has been created." % repository.name
                trans.response.send_redirect( web.url_for( controller='repository',
                                                           action='view_repository',
                                                           message=message,
                                                           id=trans.security.encode_id( repository.id ) ) )
        return trans.fill_template( '/webapps/community/repository/create_repository.mako',
                                    name=name,
                                    description=description,
                                    long_description=long_description,
                                    selected_categories=selected_categories,
                                    categories=categories,
                                    message=message,
                                    status=status )
    @web.expose
    @web.require_login( "deprecate repository" )
    def deprecate( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        repository_id = params.get( 'id', None )
        repository = suc.get_repository_in_tool_shed( trans, repository_id )
        mark_deprecated = util.string_as_bool( params.get( 'mark_deprecated', False ) )
        repository.deprecated = mark_deprecated
        trans.sa_session.add( repository )
        trans.sa_session.flush()
        if mark_deprecated:
            message = 'The repository <b>%s</b> has been marked as deprecated.' % repository.name
        else:
            message = 'The repository <b>%s</b> has been marked as not deprecated.' % repository.name        
        trans.response.send_redirect( web.url_for( controller='repository',
                                                   action='browse_repositories',
                                                   operation='repositories_i_own',
                                                   message=message,
                                                   status=status ) )
    @web.expose
    def display_tool( self, trans, repository_id, tool_config, changeset_revision, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        repository, tool, message = common.load_tool_from_changeset_revision( trans, repository_id, changeset_revision, tool_config )
        if message:
            status = 'error'
        tool_state = self.__new_state( trans )
        is_malicious = common.changeset_is_malicious( trans, repository_id, repository.tip( trans.app ) )
        metadata = self.get_metadata( trans, repository_id, changeset_revision )
        try:
            return trans.fill_template( "/webapps/community/repository/tool_form.mako",
                                        repository=repository,
                                        metadata=metadata,
                                        changeset_revision=changeset_revision,
                                        tool=tool,
                                        tool_state=tool_state,
                                        is_malicious=is_malicious,
                                        message=message,
                                        status=status )
        except Exception, e:
            message = "Error displaying tool, probably due to a problem in the tool config.  The exception is: %s." % str( e )
        if trans.webapp.name == 'galaxy':
            return trans.response.send_redirect( web.url_for( controller='repository',
                                                              action='preview_tools_in_changeset',
                                                              repository_id=repository_id,
                                                              changeset_revision=changeset_revision,
                                                              message=message,
                                                              status='error' ) )
        return trans.response.send_redirect( web.url_for( controller='repository',
                                                          action='browse_repositories',
                                                          operation='view_or_manage_repository',
                                                          id=repository_id,
                                                          changeset_revision=changeset_revision,
                                                          message=message,
                                                          status='error' ) )
    @web.expose
    def download( self, trans, repository_id, changeset_revision, file_type, **kwd ):
        # Download an archive of the repository files compressed as zip, gz or bz2.
        params = util.Params( kwd )
        repository = suc.get_repository_in_tool_shed( trans, repository_id )
        # Allow hgweb to handle the download.  This requires the tool shed
        # server account's .hgrc file to include the following setting:
        # [web]
        # allow_archive = bz2, gz, zip
        if file_type == 'zip':
            file_type_str = '%s.zip' % changeset_revision
        elif file_type == 'bz2':
            file_type_str = '%s.tar.bz2' % changeset_revision
        elif file_type == 'gz':
            file_type_str = '%s.tar.gz' % changeset_revision
        repository.times_downloaded += 1
        trans.sa_session.add( repository )
        trans.sa_session.flush()
        download_url = '/repos/%s/%s/archive/%s' % ( repository.user.username, repository.name, file_type_str )
        return trans.response.send_redirect( download_url )
    @web.expose
    def find_tools( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', '' ) )
        status = params.get( 'status', 'done' )
        galaxy_url = kwd.get( 'galaxy_url', None )
        if galaxy_url:
            trans.set_cookie( galaxy_url, name='toolshedgalaxyurl' )
        if 'operation' in kwd:
            item_id = kwd.get( 'id', '' )
            if item_id:
                operation = kwd[ 'operation' ].lower()
                is_admin = trans.user_is_admin()
                if operation == "view_or_manage_repository":
                    # The received id is a RepositoryMetadata id, so we have to get the repository id.
                    repository_metadata = common.get_repository_metadata_by_id( trans, item_id )
                    repository_id = trans.security.encode_id( repository_metadata.repository.id )
                    repository = suc.get_repository_in_tool_shed( trans, repository_id )
                    kwd[ 'id' ] = repository_id
                    kwd[ 'changeset_revision' ] = repository_metadata.changeset_revision
                    if trans.webapp.name == 'community' and ( is_admin or repository.user == trans.user ):
                        a = 'manage_repository'
                    else:
                        a = 'view_repository'
                    return trans.response.send_redirect( web.url_for( controller='repository',
                                                                      action=a,
                                                                      **kwd ) )
                if operation == "install":
                    # We've received a list of RepositoryMetadata ids, so we need to build a list of associated Repository ids.
                    encoded_repository_ids = []
                    changeset_revisions = []
                    for repository_metadata_id in util.listify( item_id ):
                        repository_metadata = common.get_repository_metadata_by_id( trans, repository_metadata_id )
                        encoded_repository_ids.append( trans.security.encode_id( repository_metadata.repository.id ) )
                        changeset_revisions.append( repository_metadata.changeset_revision )
                    new_kwd[ 'repository_ids' ] = encoded_repository_ids
                    new_kwd[ 'changeset_revisions' ] = changeset_revisions
                    return trans.response.send_redirect( web.url_for( controller='repository',
                                                                      action='install_repositories_by_revision',
                                                                      **new_kwd ) )
            else:
                # This can only occur when there is a multi-select grid with check boxes and an operation,
                # and the user clicked the operation button without checking any of the check boxes.
                return trans.show_error_message( "No items were selected." )
        tool_ids = [ item.lower() for item in util.listify( kwd.get( 'tool_id', '' ) ) ]
        tool_names = [ item.lower() for item in util.listify( kwd.get( 'tool_name', '' ) ) ]
        tool_versions = [ item.lower() for item in util.listify( kwd.get( 'tool_version', '' ) ) ]
        exact_matches = params.get( 'exact_matches', '' )
        exact_matches_checked = CheckboxField.is_checked( exact_matches )
        match_tuples = []
        ok = True
        if tool_ids or tool_names or tool_versions:
            ok, match_tuples = self.__search_repository_metadata( trans, exact_matches_checked, tool_ids=tool_ids, tool_names=tool_names, tool_versions=tool_versions )
            if ok:
                kwd[ 'match_tuples' ] = match_tuples
                # Render the list view
                if trans.webapp.name == 'galaxy':
                    # Our initial request originated from a Galaxy instance.
                    global_actions = [ grids.GridAction( "Browse valid repositories",
                                                         dict( controller='repository', action='browse_valid_categories' ) ),
                                       grids.GridAction( "Search for valid tools",
                                                         dict( controller='repository', action='find_tools' ) ),
                                       grids.GridAction( "Search for workflows",
                                                         dict( controller='repository', action='find_workflows' ) ) ]
                    self.install_matched_repository_grid.global_actions = global_actions
                    install_url_args = dict( controller='repository', action='find_tools' )
                    operations = [ grids.GridOperation( "Install", url_args=install_url_args, allow_multiple=True, async_compatible=False ) ]
                    self.install_matched_repository_grid.operations = operations
                    return self.install_matched_repository_grid( trans, **kwd )
                else:
                    kwd[ 'message' ] = "tool id: <b>%s</b><br/>tool name: <b>%s</b><br/>tool version: <b>%s</b><br/>exact matches only: <b>%s</b>" % \
                        ( self.__stringify( tool_ids ), self.__stringify( tool_names ), self.__stringify( tool_versions ), str( exact_matches_checked ) )
                    self.matched_repository_grid.title = "Repositories with matching tools"
                    return self.matched_repository_grid( trans, **kwd )
            else:
                message = "No search performed - each field must contain the same number of comma-separated items."
                status = "error"
        exact_matches_check_box = CheckboxField( 'exact_matches', checked=exact_matches_checked )
        return trans.fill_template( '/webapps/community/repository/find_tools.mako',
                                    tool_id=self.__stringify( tool_ids ),
                                    tool_name=self.__stringify( tool_names ),
                                    tool_version=self.__stringify( tool_versions ),
                                    exact_matches_check_box=exact_matches_check_box,
                                    message=message,
                                    status=status )
    @web.expose
    def find_workflows( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', '' ) )
        status = params.get( 'status', 'done' )
        galaxy_url = kwd.get( 'galaxy_url', None )
        if galaxy_url:
            trans.set_cookie( galaxy_url, name='toolshedgalaxyurl' )
        if 'operation' in kwd:
            item_id = kwd.get( 'id', '' )
            if item_id:
                operation = kwd[ 'operation' ].lower()
                is_admin = trans.user_is_admin()
                if operation == "view_or_manage_repository":
                    # The received id is a RepositoryMetadata id, so we have to get the repository id.
                    repository_metadata = common.get_repository_metadata_by_id( trans, item_id )
                    repository_id = trans.security.encode_id( repository_metadata.repository.id )
                    repository = suc.get_repository_in_tool_shed( trans, repository_id )
                    kwd[ 'id' ] = repository_id
                    kwd[ 'changeset_revision' ] = repository_metadata.changeset_revision
                    if trans.webapp.name == 'community' and ( is_admin or repository.user == trans.user ):
                        a = 'manage_repository'
                    else:
                        a = 'view_repository'
                    return trans.response.send_redirect( web.url_for( controller='repository',
                                                                      action=a,
                                                                      **kwd ) )
                if operation == "install":
                    # We've received a list of RepositoryMetadata ids, so we need to build a list of associated Repository ids.
                    encoded_repository_ids = []
                    changeset_revisions = []
                    for repository_metadata_id in util.listify( item_id ):
                        repository_metadata = common.get_repository_metadata_by_id( trans, item_id )
                        encoded_repository_ids.append( trans.security.encode_id( repository_metadata.repository.id ) )
                        changeset_revisions.append( repository_metadata.changeset_revision )
                    new_kwd = {}
                    new_kwd[ 'repository_ids' ] = encoded_repository_ids
                    new_kwd[ 'changeset_revisions' ] = changeset_revisions
                    return trans.response.send_redirect( web.url_for( controller='repository',
                                                                      action='install_repositories_by_revision',
                                                                      **new_kwd ) )
            else:
                # This can only occur when there is a multi-select grid with check boxes and an operation,
                # and the user clicked the operation button without checking any of the check boxes.
                return trans.show_error_message( "No items were selected." )
        if 'find_workflows_button' in kwd:
            workflow_names = [ item.lower() for item in util.listify( kwd.get( 'workflow_name', '' ) ) ]
            exact_matches = params.get( 'exact_matches', '' )
            exact_matches_checked = CheckboxField.is_checked( exact_matches )
            match_tuples = []
            ok = True
            if workflow_names:
                ok, match_tuples = self.__search_repository_metadata( trans, exact_matches_checked, workflow_names=workflow_names )
            else:
                ok, match_tuples = self.__search_repository_metadata( trans, exact_matches_checked, workflow_names=[], all_workflows=True )
            if ok:
                kwd[ 'match_tuples' ] = match_tuples
                if trans.webapp.name == 'galaxy':
                    # Our initial request originated from a Galaxy instance.
                    global_actions = [ grids.GridAction( "Browse valid repositories",
                                                         dict( controller='repository', action='browse_valid_repositories' ) ),
                                       grids.GridAction( "Search for valid tools",
                                                         dict( controller='repository', action='find_tools' ) ),
                                       grids.GridAction( "Search for workflows",
                                                         dict( controller='repository', action='find_workflows' ) ) ]
                    self.install_matched_repository_grid.global_actions = global_actions
                    install_url_args = dict( controller='repository', action='find_workflows' )
                    operations = [ grids.GridOperation( "Install", url_args=install_url_args, allow_multiple=True, async_compatible=False ) ]
                    self.install_matched_repository_grid.operations = operations
                    return self.install_matched_repository_grid( trans, **kwd )
                else:
                    kwd[ 'message' ] = "workflow name: <b>%s</b><br/>exact matches only: <b>%s</b>" % \
                        ( self.__stringify( workflow_names ), str( exact_matches_checked ) )
                    self.matched_repository_grid.title = "Repositories with matching workflows"
                    return self.matched_repository_grid( trans, **kwd )
            else:
                message = "No search performed - each field must contain the same number of comma-separated items."
                status = "error"
        else:
            exact_matches_checked = False
            workflow_names = []
        exact_matches_check_box = CheckboxField( 'exact_matches', checked=exact_matches_checked )
        return trans.fill_template( '/webapps/community/repository/find_workflows.mako',
                                    workflow_name=self.__stringify( workflow_names ),
                                    exact_matches_check_box=exact_matches_check_box,
                                    message=message,
                                    status=status )
    @web.expose
    def get_changeset_revision_and_ctx_rev( self, trans, **kwd ):
        """Handle a request from a local Galaxy instance to retrieve the changeset revision hash to which an installed repository can be updated."""
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        galaxy_url = kwd.get( 'galaxy_url', '' )
        name = params.get( 'name', None )
        owner = params.get( 'owner', None )
        changeset_revision = params.get( 'changeset_revision', None )
        repository = suc.get_repository_by_name_and_owner( trans, name, owner )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        # Default to the received changeset revision and ctx_rev.
        update_to_ctx = suc.get_changectx_for_changeset( repo, changeset_revision )
        ctx_rev = str( update_to_ctx.rev() )
        latest_changeset_revision = changeset_revision
        update_dict = dict( changeset_revision=changeset_revision, ctx_rev=ctx_rev )
        if changeset_revision == repository.tip( trans.app ):
            # If changeset_revision is the repository tip, there are no additional updates.
            return encoding_util.tool_shed_encode( update_dict )
        else:
            repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, 
                                                                                     trans.security.encode_id( repository.id ),
                                                                                     changeset_revision )
            if repository_metadata:
                # If changeset_revision is in the repository_metadata table for this repository, there are no additional updates.
                return encoding_util.tool_shed_encode( update_dict )
            else:
                # The changeset_revision column in the repository_metadata table has been updated with a new changeset_revision value since the
                # repository was installed.  We need to find the changeset_revision to which we need to update.
                update_to_changeset_hash = None
                for changeset in repo.changelog:
                    changeset_hash = str( repo.changectx( changeset ) )
                    ctx = suc.get_changectx_for_changeset( repo, changeset_hash )
                    if update_to_changeset_hash:
                        if suc.get_repository_metadata_by_changeset_revision( trans, trans.security.encode_id( repository.id ), changeset_hash ):
                            # We found a RepositoryMetadata record.
                            if changeset_hash == repository.tip( trans.app ):
                                # The current ctx is the repository tip, so use it.
                                update_to_ctx = suc.get_changectx_for_changeset( repo, changeset_hash )
                                latest_changeset_revision = changeset_hash
                            else:
                                update_to_ctx = suc.get_changectx_for_changeset( repo, update_to_changeset_hash )
                                latest_changeset_revision = update_to_changeset_hash
                            break
                    elif not update_to_changeset_hash and changeset_hash == changeset_revision:
                        # We've found the changeset in the changelog for which we need to get the next update.
                        update_to_changeset_hash = changeset_hash
                update_dict[ 'changeset_revision' ] = str( latest_changeset_revision )
        update_dict[ 'ctx_rev' ] = str( update_to_ctx.rev() )
        return encoding_util.tool_shed_encode( update_dict )
    @web.expose
    def get_ctx_rev( self, trans, **kwd ):
        """Given a repository and changeset_revision, return the correct ctx.rev() value."""
        repository_name = kwd[ 'name' ]
        repository_owner = kwd[ 'owner' ]
        changeset_revision = kwd[ 'changeset_revision' ]
        repository = suc.get_repository_by_name_and_owner( trans, repository_name, repository_owner )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        ctx = suc.get_changectx_for_changeset( repo, changeset_revision )
        if ctx:
            return str( ctx.rev() )
        return ''
    @web.json
    def get_file_contents( self, trans, file_path ):
        # Avoid caching
        trans.response.headers['Pragma'] = 'no-cache'
        trans.response.headers['Expires'] = '0'
        return suc.get_repository_file_contents( file_path )
    def get_file_from_changeset_revision( self, repo_files_dir, changeset_revision, file_name, dir ):
        """Return file_name from the received changeset_revision of the repository manifest."""
        stripped_file_name = suc.strip_path( file_name )
        repo = hg.repository( suc.get_configured_ui(), repo_files_dir )
        ctx = suc.get_changectx_for_changeset( repo, changeset_revision )
        named_tmp_file = suc.get_named_tmpfile_from_ctx( ctx, file_name, dir )
        return named_tmp_file
    def get_metadata( self, trans, repository_id, changeset_revision ):
        repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision )
        if repository_metadata and repository_metadata.metadata:
            return repository_metadata.metadata
        return None
    @web.json
    def get_readme_files( self, trans, **kwd ):
        """
        This method is called when installing or re-installing a single repository into a Galaxy instance.  If the received changeset_revision 
        includes one or more readme files, return them in a dictionary.
        """
        repository_name = kwd[ 'name' ]
        repository_owner = kwd[ 'owner' ]
        changeset_revision = kwd[ 'changeset_revision' ]
        repository = suc.get_repository_by_name_and_owner( trans, repository_name, repository_owner )
        repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, trans.security.encode_id( repository.id ), changeset_revision )        
        return suc.build_readme_files_dict( repository_metadata )
    @web.json
    def get_repository_dependencies( self, trans, **kwd ):
        """Return an encoded dictionary of all repositories upon which the contents of the received repository depends."""
        params = util.Params( kwd )
        name = params.get( 'name', None )
        owner = params.get( 'owner', None )
        changeset_revision = params.get( 'changeset_revision', None )
        repository = suc.get_repository_by_name_and_owner( trans, name, owner )
        repository_id = trans.security.encode_id( repository.id )
        repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision )
        if repository_metadata:
            metadata = repository_metadata.metadata
            if metadata:
                repository_dependencies = suc.get_repository_dependencies_for_changeset_revision( trans=trans,
                                                                                                  repository=repository,
                                                                                                  repository_metadata=repository_metadata,
                                                                                                  toolshed_base_url=str( url_for( '/', qualified=True ) ).rstrip( '/' ),
                                                                                                  key_rd_dicts_to_be_processed=None,
                                                                                                  all_repository_dependencies=None,
                                                                                                  handled_key_rd_dicts=None,
                                                                                                  circular_repository_dependencies=None )
                if repository_dependencies:
                    return encoding_util.tool_shed_encode( repository_dependencies )
        return ''
    @web.json
    def get_repository_information( self, trans, repository_ids, changeset_revisions, **kwd ):
        """
        Generate a list of dictionaries, each of which contains the information about a repository that will be necessary for installing
        it into a local Galaxy instance.
        """
        includes_tools = False
        includes_repository_dependencies = False
        includes_tool_dependencies = False
        repo_info_dicts = []
        for tup in zip( util.listify( repository_ids ), util.listify( changeset_revisions ) ):
            repository_id, changeset_revision = tup
            repository = suc.get_repository_in_tool_shed( trans, repository_id )
            repository_clone_url = suc.generate_clone_url_for_repository_in_tool_shed( trans, repository )
            repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision )
            metadata = repository_metadata.metadata
            if not includes_tools and 'tools' in metadata:
                includes_tools = True
            if not includes_repository_dependencies and 'repository_dependencies' in metadata:
                includes_repository_dependencies = True
            if not includes_tool_dependencies and 'tool_dependencies' in metadata:
                includes_tool_dependencies = True
            repo_dir = repository.repo_path( trans.app )
            repo = hg.repository( suc.get_configured_ui(), repo_dir )
            ctx = suc.get_changectx_for_changeset( repo, changeset_revision )
            repo_info_dict = suc.create_repo_info_dict( trans=trans,
                                                        repository_clone_url=repository_clone_url,
                                                        changeset_revision=changeset_revision,
                                                        ctx_rev=str( ctx.rev() ),
                                                        repository_owner=repository.user.username,
                                                        repository_name=repository.name,
                                                        repository=repository,
                                                        metadata=None,
                                                        repository_metadata=repository_metadata )      
            repo_info_dicts.append( encoding_util.tool_shed_encode( repo_info_dict ) )
        return dict( includes_tools=includes_tools,
                     includes_repository_dependencies=includes_repository_dependencies,
                     includes_tool_dependencies=includes_tool_dependencies,
                     repo_info_dicts=repo_info_dicts )
    @web.json
    def get_required_repo_info_dict( self, trans, encoded_str ):
        """Retrive a list of dictionaries that each contain all of the information needed to install the list of repositories defined by encoded_str."""
        encoded_required_repository_str = encoding_util.tool_shed_decode( encoded_str )
        encoded_required_repository_tups = encoded_required_repository_str.split( encoding_util.encoding_sep2 )
        decoded_required_repository_tups = []
        for encoded_required_repository_tup in encoded_required_repository_tups:
            decoded_required_repository_tups.append( encoded_required_repository_tup.split( encoding_util.encoding_sep ) )
        encoded_repository_ids = []
        changeset_revisions = []
        for required_repository_tup in decoded_required_repository_tups:
            tool_shed, name, owner, changeset_revision = required_repository_tup
            repository = suc.get_repository_by_name_and_owner( trans, name, owner )
            encoded_repository_ids.append( trans.security.encode_id( repository.id ) )
            changeset_revisions.append( changeset_revision )
        if encoded_repository_ids and changeset_revisions:
            repo_info_dict = from_json_string( self.get_repository_information( trans, encoded_repository_ids, changeset_revisions ) )
        else:
            repo_info_dict = {}
        return repo_info_dict
    @web.expose
    def get_tool_dependencies( self, trans, **kwd ):
        """Handle a request from the InstallManager of a local Galaxy instance."""
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        # If the request originated with the UpdateManager, it will not include a galaxy_url.
        galaxy_url = kwd.get( 'galaxy_url', '' )
        name = params.get( 'name', None )
        owner = params.get( 'owner', None )
        changeset_revision = params.get( 'changeset_revision', None )
        repository = suc.get_repository_by_name_and_owner( trans, name, owner )
        for downloadable_revision in repository.downloadable_revisions:
            if downloadable_revision.changeset_revision == changeset_revision:
                break
        metadata = downloadable_revision.metadata
        tool_dependencies = metadata.get( 'tool_dependencies', '' )
        from_install_manager = kwd.get( 'from_install_manager', False )
        if from_install_manager:
            if tool_dependencies:
                return encoding_util.tool_shed_encode( tool_dependencies )
        return ''
    @web.expose
    def get_tool_versions( self, trans, **kwd ):
        """
        For each valid /downloadable change set (up to the received changeset_revision) in the repository's change log, append the change
        set's tool_versions dictionary to the list that will be returned.
        """
        name = kwd[ 'name' ]
        owner = kwd[ 'owner' ]
        changeset_revision = kwd[ 'changeset_revision' ]
        repository = suc.get_repository_by_name_and_owner( trans, name, owner )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        tool_version_dicts = []
        for changeset in repo.changelog:
            current_changeset_revision = str( repo.changectx( changeset ) )
            repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, trans.security.encode_id( repository.id ), current_changeset_revision )
            if repository_metadata and repository_metadata.tool_versions:
                tool_version_dicts.append( repository_metadata.tool_versions )
                if current_changeset_revision == changeset_revision:
                    break
        if tool_version_dicts:
            return to_json_string( tool_version_dicts )
        return ''
    def get_versions_of_tool( self, trans, repository, repository_metadata, guid ):
        """Return the tool lineage in descendant order for the received guid contained in the received repsitory_metadata.tool_versions."""
        encoded_id = trans.security.encode_id( repository.id )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        # Initialize the tool lineage
        tool_guid_lineage = [ guid ]
        # Get all ancestor guids of the received guid.
        current_child_guid = guid
        for changeset in suc.reversed_upper_bounded_changelog( repo, repository_metadata.changeset_revision ):
            ctx = repo.changectx( changeset )
            rm = suc.get_repository_metadata_by_changeset_revision( trans, encoded_id, str( ctx ) )
            if rm:
                parent_guid = rm.tool_versions.get( current_child_guid, None )
                if parent_guid:
                    tool_guid_lineage.append( parent_guid )
                    current_child_guid = parent_guid
        # Get all descendant guids of the received guid.
        current_parent_guid = guid
        for changeset in suc.reversed_lower_upper_bounded_changelog( repo, repository_metadata.changeset_revision, repository.tip( trans.app ) ):
            ctx = repo.changectx( changeset )
            rm = suc.get_repository_metadata_by_changeset_revision( trans, encoded_id, str( ctx ) )
            if rm:
                tool_versions = rm.tool_versions
                for child_guid, parent_guid in tool_versions.items():
                    if parent_guid == current_parent_guid:
                        tool_guid_lineage.insert( 0, child_guid )
                        current_parent_guid = child_guid
                        break
        return tool_guid_lineage
    @web.expose
    def help( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        return trans.fill_template( '/webapps/community/repository/help.mako', message=message, status=status, **kwd )
    def __in_tool_dict( self, tool_dict, exact_matches_checked, tool_id=None, tool_name=None, tool_version=None ):
        found = False
        if tool_id and not tool_name and not tool_version:
            tool_dict_tool_id = tool_dict[ 'id' ].lower()
            found = ( tool_id == tool_dict_tool_id ) or \
                    ( not exact_matches_checked and tool_dict_tool_id.find( tool_id ) >= 0 )
        elif tool_name and not tool_id and not tool_version:
            tool_dict_tool_name = tool_dict[ 'name' ].lower()
            found = ( tool_name == tool_dict_tool_name ) or \
                    ( not exact_matches_checked and tool_dict_tool_name.find( tool_name ) >= 0 )
        elif tool_version and not tool_id and not tool_name:
            tool_dict_tool_version = tool_dict[ 'version' ].lower()
            found = ( tool_version == tool_dict_tool_version ) or \
                    ( not exact_matches_checked and tool_dict_tool_version.find( tool_version ) >= 0 )
        elif tool_id and tool_name and not tool_version:
            tool_dict_tool_id = tool_dict[ 'id' ].lower()
            tool_dict_tool_name = tool_dict[ 'name' ].lower()
            found = ( tool_id == tool_dict_tool_id and tool_name == tool_dict_tool_name ) or \
                    ( not exact_matches_checked and tool_dict_tool_id.find( tool_id ) >= 0 and tool_dict_tool_name.find( tool_name ) >= 0 )
        elif tool_id and tool_version and not tool_name:
            tool_dict_tool_id = tool_dict[ 'id' ].lower()
            tool_dict_tool_version = tool_dict[ 'version' ].lower()
            found = ( tool_id == tool_dict_tool_id and tool_version == tool_dict_tool_version ) or \
                    ( not exact_matches_checked and tool_dict_tool_id.find( tool_id ) >= 0 and tool_dict_tool_version.find( tool_version ) >= 0 )
        elif tool_version and tool_name and not tool_id:
            tool_dict_tool_version = tool_dict[ 'version' ].lower()
            tool_dict_tool_name = tool_dict[ 'name' ].lower()
            found = ( tool_version == tool_dict_tool_version and tool_name == tool_dict_tool_name ) or \
                    ( not exact_matches_checked and tool_dict_tool_version.find( tool_version ) >= 0 and tool_dict_tool_name.find( tool_name ) >= 0 )
        elif tool_version and tool_name and tool_id:
            tool_dict_tool_version = tool_dict[ 'version' ].lower()
            tool_dict_tool_name = tool_dict[ 'name' ].lower()
            tool_dict_tool_id = tool_dict[ 'id' ].lower()
            found = ( tool_version == tool_dict_tool_version and \
                      tool_name == tool_dict_tool_name and \
                      tool_id == tool_dict_tool_id ) or \
                    ( not exact_matches_checked and \
                      tool_dict_tool_version.find( tool_version ) >= 0 and \
                      tool_dict_tool_name.find( tool_name ) >= 0 and \
                      tool_dict_tool_id.find( tool_id ) >= 0 )
        return found
    def __in_workflow_dict( self, workflow_dict, exact_matches_checked, workflow_name ):
        workflow_dict_workflow_name = workflow_dict[ 'name' ].lower()
        return ( workflow_name == workflow_dict_workflow_name ) or \
               ( not exact_matches_checked and workflow_dict_workflow_name.find( workflow_name ) >= 0 )
    @web.expose
    def index( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        # See if there are any RepositoryMetadata records since menu items require them.
        repository_metadata = trans.sa_session.query( model.RepositoryMetadata ).first()
        current_user = trans.user
        has_reviewed_repositories = False
        has_deprecated_repositories = False
        if current_user:
            # See if the current user owns any repositories that have been reviewed.
            for repository in current_user.active_repositories:
                if repository.reviews:
                    has_reviewed_repositories = True
                    break
            # See if the current user has any repositories that have been marked as deprecated.
            for repository in current_user.active_repositories:
                if repository.deprecated:
                    has_deprecated_repositories = True
                    break
        return trans.fill_template( '/webapps/community/index.mako',
                                    repository_metadata=repository_metadata,
                                    has_reviewed_repositories=has_reviewed_repositories,
                                    has_deprecated_repositories=has_deprecated_repositories,
                                    message=message,
                                    status=status )
    @web.expose
    def install_repositories_by_revision( self, trans, **kwd ):
        """
        Send the list of repository_ids and changeset_revisions to Galaxy so it can begin the installation process.  If the value of
        repository_ids is not received, then the name and owner of a single repository must be received to install a single repository.
        """
        repository_ids = kwd.get( 'repository_ids', None )
        changeset_revisions = kwd.get( 'changeset_revisions', None )
        name = kwd.get( 'name', None )
        owner = kwd.get( 'owner', None )
        galaxy_url = kwd.get( 'galaxy_url', None )
        if not repository_ids:
            repository = suc.get_repository_by_name_and_owner( trans, name, owner )
            repository_ids = trans.security.encode_id( repository.id )
        if not galaxy_url:
            # If galaxy_url is not in the request, it had to have been stored in a cookie by the tool shed.
            galaxy_url = trans.get_cookie( name='toolshedgalaxyurl' )
        # Redirect back to local Galaxy to perform install.
        url = suc.url_join( galaxy_url,
                            'admin_toolshed/prepare_for_install?tool_shed_url=%s&repository_ids=%s&changeset_revisions=%s' % \
                            ( url_for( '/', qualified=True ), ','.join( util.listify( repository_ids ) ), ','.join( util.listify( changeset_revisions ) ) ) )
        return trans.response.send_redirect( url )
    @web.expose
    def load_invalid_tool( self, trans, repository_id, tool_config, changeset_revision, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'error' )
        repository, tool, error_message = common.load_tool_from_changeset_revision( trans, repository_id, changeset_revision, tool_config )
        tool_state = self.__new_state( trans )
        is_malicious = common.changeset_is_malicious( trans, repository_id, repository.tip( trans.app ) )
        invalid_file_tups = []
        if tool:
            invalid_file_tups = suc.check_tool_input_params( trans.app,
                                                             repository.repo_path( trans.app ),
                                                             tool_config,
                                                             tool,
                                                             [] )
        if invalid_file_tups:
            message = suc.generate_message_for_invalid_tools( trans, invalid_file_tups, repository, {}, as_html=True, displaying_invalid_tool=True )
        elif error_message:
            message = error_message
        try:
            return trans.fill_template( "/webapps/community/repository/tool_form.mako",
                                        repository=repository,
                                        changeset_revision=changeset_revision,
                                        tool=tool,
                                        tool_state=tool_state,
                                        is_malicious=is_malicious,
                                        message=message,
                                        status='error' )
        except Exception, e:
            message = "Exception thrown attempting to display tool: %s." % str( e )
        if trans.webapp.name == 'galaxy':
            return trans.response.send_redirect( web.url_for( controller='repository',
                                                              action='preview_tools_in_changeset',
                                                              repository_id=repository_id,
                                                              changeset_revision=changeset_revision,
                                                              message=message,
                                                              status='error' ) )
        return trans.response.send_redirect( web.url_for( controller='repository',
                                                          action='browse_repositories',
                                                          operation='view_or_manage_repository',
                                                          id=repository_id,
                                                          changeset_revision=changeset_revision,
                                                          message=message,
                                                          status='error' ) )
    def __make_same_length( self, list1, list2 ):
        # If either list is 1 item, we'll append to it until its length is the same as the other.
        if len( list1 ) == 1:
            for i in range( 1, len( list2 ) ):
                list1.append( list1[ 0 ] )
        elif len( list2 ) == 1:
            for i in range( 1, len( list1 ) ):
                list2.append( list2[ 0 ] )
        return list1, list2
    @web.expose
    @web.require_login( "manage email alerts" )
    def manage_email_alerts( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        new_repo_alert = params.get( 'new_repo_alert', '' )
        new_repo_alert_checked = CheckboxField.is_checked( new_repo_alert )
        user = trans.user
        if params.get( 'new_repo_alert_button', False ):
            user.new_repo_alert = new_repo_alert_checked
            trans.sa_session.add( user )
            trans.sa_session.flush()
            if new_repo_alert_checked:
                message = 'You will receive email alerts for all new valid tool shed repositories.'
            else:
                message = 'You will not receive any email alerts for new valid tool shed repositories.'
        checked = new_repo_alert_checked or ( user and user.new_repo_alert )
        new_repo_alert_check_box = CheckboxField( 'new_repo_alert', checked=checked )
        email_alert_repositories = []
        for repository in trans.sa_session.query( trans.model.Repository ) \
                                          .filter( and_( trans.model.Repository.table.c.deleted == False,
                                                         trans.model.Repository.table.c.email_alerts != None ) ) \
                                          .order_by( trans.model.Repository.table.c.name ):
            if user.email in repository.email_alerts:
                email_alert_repositories.append( repository )
        return trans.fill_template( "/webapps/community/user/manage_email_alerts.mako",
                                    new_repo_alert_check_box=new_repo_alert_check_box,
                                    email_alert_repositories=email_alert_repositories,
                                    message=message,
                                    status=status )
    @web.expose
    @web.require_login( "manage repository" )
    def manage_repository( self, trans, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        cntrller = params.get( 'cntrller', 'repository' )
        repository = suc.get_repository_in_tool_shed( trans, id )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        repo_name = util.restore_text( params.get( 'repo_name', repository.name ) )
        changeset_revision = util.restore_text( params.get( 'changeset_revision', repository.tip( trans.app ) ) )
        description = util.restore_text( params.get( 'description', repository.description ) )
        long_description = util.restore_text( params.get( 'long_description', repository.long_description ) )
        avg_rating, num_ratings = self.get_ave_item_rating_data( trans.sa_session, repository, webapp_model=trans.model )
        display_reviews = util.string_as_bool( params.get( 'display_reviews', False ) )
        alerts = params.get( 'alerts', '' )
        alerts_checked = CheckboxField.is_checked( alerts )
        category_ids = util.listify( params.get( 'category_id', '' ) )
        if repository.email_alerts:
            email_alerts = from_json_string( repository.email_alerts )
        else:
            email_alerts = []
        allow_push = params.get( 'allow_push', '' )
        error = False
        user = trans.user
        if params.get( 'edit_repository_button', False ):
            flush_needed = False
            # TODO: add a can_manage in the security agent.
            if not ( user.email == repository.user.email or trans.user_is_admin() ):
                message = "You are not the owner of this repository, so you cannot manage it."
                return trans.response.send_redirect( web.url_for( controller='repository',
                                                                  action='view_repository',
                                                                  id=id,
                                                                  message=message,
                                                                  status='error' ) )
            if description != repository.description:
                repository.description = description
                flush_needed = True
            if long_description != repository.long_description:
                repository.long_description = long_description
                flush_needed = True
            if repository.times_downloaded == 0 and repo_name != repository.name:
                message = self.__validate_repository_name( repo_name, user )
                if message:
                    error = True
                else:
                    # Change the entry in the hgweb.config file for the repository.
                    old_lhs = "repos/%s/%s" % ( repository.user.username, repository.name )
                    new_lhs = "repos/%s/%s" % ( repository.user.username, repo_name )
                    trans.app.hgweb_config_manager.change_entry( old_lhs, new_lhs, repo_dir )
                    # Change the entry in the repository's hgrc file.
                    hgrc_file = os.path.join( repo_dir, '.hg', 'hgrc' )
                    self.__change_repository_name_in_hgrc_file( hgrc_file, repo_name )
                    repository.name = repo_name
                    flush_needed = True
            elif repository.times_downloaded != 0 and repo_name != repository.name:
                message = "Repository names cannot be changed if the repository has been cloned.  "
            if flush_needed:
                trans.sa_session.add( repository )
                trans.sa_session.flush()
                message += "The repository information has been updated."
        elif params.get( 'manage_categories_button', False ):
            flush_needed = False
            # Delete all currently existing categories.
            for rca in repository.categories:
                trans.sa_session.delete( rca )
                trans.sa_session.flush()
            if category_ids:
                # Create category associations
                for category_id in category_ids:
                    category = trans.app.model.Category.get( trans.security.decode_id( category_id ) )
                    rca = trans.app.model.RepositoryCategoryAssociation( repository, category )
                    trans.sa_session.add( rca )
                    trans.sa_session.flush()
            message = "The repository information has been updated."
        elif params.get( 'user_access_button', False ):
            if allow_push not in [ 'none' ]:
                remove_auth = params.get( 'remove_auth', '' )
                if remove_auth:
                    usernames = ''
                else:
                    user_ids = util.listify( allow_push )
                    usernames = []
                    for user_id in user_ids:
                        user = trans.sa_session.query( trans.model.User ).get( trans.security.decode_id( user_id ) )
                        usernames.append( user.username )
                    usernames = ','.join( usernames )
                repository.set_allow_push( trans.app, usernames, remove_auth=remove_auth )
            message = "The repository information has been updated."
        elif params.get( 'receive_email_alerts_button', False ):
            flush_needed = False
            if alerts_checked:
                if user.email not in email_alerts:
                    email_alerts.append( user.email )
                    repository.email_alerts = to_json_string( email_alerts )
                    flush_needed = True
            else:
                if user.email in email_alerts:
                    email_alerts.remove( user.email )
                    repository.email_alerts = to_json_string( email_alerts )
                    flush_needed = True
            if flush_needed:
                trans.sa_session.add( repository )
                trans.sa_session.flush()
            message = "The repository information has been updated."
        if error:
            status = 'error'
        current_allow_push = repository.allow_push( trans.app )
        if current_allow_push:
            current_allow_push_list = current_allow_push.split( ',' )
        else:
            current_allow_push_list = []
        allow_push_select_field = self.__build_allow_push_select_field( trans, current_allow_push_list )
        checked = alerts_checked or user.email in email_alerts
        alerts_check_box = CheckboxField( 'alerts', checked=checked )
        changeset_revision_select_field = build_changeset_revision_select_field( trans,
                                                                                 repository,
                                                                                 selected_value=changeset_revision,
                                                                                 add_id_to_name=False,
                                                                                 downloadable=False )
        revision_label = common.get_revision_label( trans, repository, repository.tip( trans.app ) )
        repository_metadata = None
        repository_metadata_id = None
        metadata = None
        is_malicious = False
        repository_dependencies = None
        if changeset_revision != suc.INITIAL_CHANGELOG_HASH:
            repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, id, changeset_revision )
            if repository_metadata:
                revision_label = common.get_revision_label( trans, repository, changeset_revision )
                repository_metadata_id = trans.security.encode_id( repository_metadata.id )
                metadata = repository_metadata.metadata
                is_malicious = repository_metadata.malicious
            else:
                # There is no repository_metadata defined for the changeset_revision, so see if it was defined in a previous changeset in the changelog.
                previous_changeset_revision = suc.get_previous_downloadable_changset_revision( repository, repo, changeset_revision )
                if previous_changeset_revision != suc.INITIAL_CHANGELOG_HASH:
                    repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, id, previous_changeset_revision )
                    if repository_metadata:
                        revision_label = common.get_revision_label( trans, repository, previous_changeset_revision )
                        repository_metadata_id = trans.security.encode_id( repository_metadata.id )
                        metadata = repository_metadata.metadata
                        is_malicious = repository_metadata.malicious
            if repository_metadata:
                # Get a dictionary of all repositories upon which the contents of the current repository_metadata record depend.
                repository_dependencies = suc.get_repository_dependencies_for_changeset_revision( trans=trans,
                                                                                                  repository=repository,
                                                                                                  repository_metadata=repository_metadata,
                                                                                                  toolshed_base_url=str( url_for( '/', qualified=True ) ).rstrip( '/' ),
                                                                                                  key_rd_dicts_to_be_processed=None,
                                                                                                  all_repository_dependencies=None,
                                                                                                  handled_key_rd_dicts=None )
        if is_malicious:
            if trans.app.security_agent.can_push( trans.app, trans.user, repository ):
                message += common.malicious_error_can_push
            else:
                message += common.malicious_error
            status = 'error'
        malicious_check_box = CheckboxField( 'malicious', checked=is_malicious )
        categories = common.get_categories( trans )
        selected_categories = [ rca.category_id for rca in repository.categories ]
        # Determine if the current changeset revision has been reviewed by the current user.
        reviewed_by_user = common.changeset_revision_reviewed_by_user( trans, trans.user, repository, changeset_revision )
        if reviewed_by_user:
            review = common.get_review_by_repository_id_changeset_revision_user_id( trans, id, changeset_revision, trans.security.encode_id( trans.user.id ) )
            review_id = trans.security.encode_id( review.id )
        else:
            review_id = None
        containers_dict = suc.build_repository_containers_for_tool_shed( repository, changeset_revision, repository_dependencies, repository_metadata )
        return trans.fill_template( '/webapps/community/repository/manage_repository.mako',
                                    cntrller=cntrller,
                                    repo_name=repo_name,
                                    description=description,
                                    long_description=long_description,
                                    current_allow_push_list=current_allow_push_list,
                                    allow_push_select_field=allow_push_select_field,
                                    repo=repo,
                                    repository=repository,
                                    containers_dict=containers_dict,
                                    repository_metadata_id=repository_metadata_id,
                                    changeset_revision=changeset_revision,
                                    reviewed_by_user=reviewed_by_user,
                                    review_id=review_id,
                                    changeset_revision_select_field=changeset_revision_select_field,
                                    revision_label=revision_label,
                                    selected_categories=selected_categories,
                                    categories=categories,
                                    metadata=metadata,
                                    avg_rating=avg_rating,
                                    display_reviews=display_reviews,
                                    num_ratings=num_ratings,
                                    alerts_check_box=alerts_check_box,
                                    malicious_check_box=malicious_check_box,
                                    is_malicious=is_malicious,
                                    message=message,
                                    status=status )
    @web.expose
    @web.require_login( "review repository revision" )
    def manage_repository_reviews_of_revision( self, trans, mine=False, **kwd ):
        return trans.response.send_redirect( web.url_for( controller='repository_review',
                                                          action='manage_repository_reviews_of_revision',
                                                          **kwd ) )
    @web.expose
    @web.require_login( "multi select email alerts" )
    def multi_select_email_alerts( self, trans, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        if 'operation' in kwd:
            operation = kwd['operation'].lower()
            if operation == "receive email alerts":
                if trans.user:
                    if kwd[ 'id' ]:
                        kwd[ 'caller' ] = 'multi_select_email_alerts'
                        return trans.response.send_redirect( web.url_for( controller='repository',
                                                                          action='set_email_alerts',
                                                                          **kwd ) )
                else:
                    kwd[ 'message' ] = 'You must be logged in to set email alerts.'
                    kwd[ 'status' ] = 'error'
                    del kwd[ 'operation' ]
        self.email_alerts_repository_grid.title = "Set email alerts for repository changes"
        return self.email_alerts_repository_grid( trans, **kwd )
    def __new_state( self, trans, all_pages=False ):
        """
        Create a new `DefaultToolState` for this tool. It will not be initialized
        with default values for inputs. 
        
        Only inputs on the first page will be initialized unless `all_pages` is
        True, in which case all inputs regardless of page are initialized.
        """
        state = DefaultToolState()
        state.inputs = {}
        return state
    @web.json
    def open_folder( self, trans, folder_path ):
        # Avoid caching
        trans.response.headers['Pragma'] = 'no-cache'
        trans.response.headers['Expires'] = '0'
        return suc.open_repository_files_folder( trans, folder_path )
    @web.expose
    def preview_tools_in_changeset( self, trans, repository_id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', '' ) )
        status = params.get( 'status', 'done' )
        repository = suc.get_repository_in_tool_shed( trans, repository_id )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        changeset_revision = util.restore_text( params.get( 'changeset_revision', repository.tip( trans.app ) ) )
        repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision )
        if repository_metadata:
            repository_metadata_id = trans.security.encode_id( repository_metadata.id ),
            metadata = repository_metadata.metadata
            # Get a dictionary of all repositories upon which the contents of the current repository_metadata record depend.
            repository_dependencies = suc.get_repository_dependencies_for_changeset_revision( trans=trans,
                                                                                              repository=repository,
                                                                                              repository_metadata=repository_metadata,
                                                                                              toolshed_base_url=str( url_for( '/', qualified=True ) ).rstrip( '/' ),
                                                                                              key_rd_dicts_to_be_processed=None,
                                                                                              all_repository_dependencies=None,
                                                                                              handled_key_rd_dicts=None )
        else:
            repository_metadata_id = None
            metadata = None
            repository_dependencies = None
        revision_label = common.get_revision_label( trans, repository, changeset_revision )
        changeset_revision_select_field = build_changeset_revision_select_field( trans,
                                                                                 repository,
                                                                                 selected_value=changeset_revision,
                                                                                 add_id_to_name=False,
                                                                                 downloadable=False )
        containers_dict = suc.build_repository_containers_for_tool_shed( repository, changeset_revision, repository_dependencies, repository_metadata )
        return trans.fill_template( '/webapps/community/repository/preview_tools_in_changeset.mako',
                                    repository=repository,
                                    containers_dict=containers_dict,
                                    repository_metadata_id=repository_metadata_id,
                                    changeset_revision=changeset_revision,
                                    revision_label=revision_label,
                                    changeset_revision_select_field=changeset_revision_select_field,
                                    metadata=metadata,
                                    message=message,
                                    status=status )
    @web.expose
    def previous_changeset_revisions( self, trans, **kwd ):
        """
        Handle a request from a local Galaxy instance.  This method will handle the case where the repository was previously installed using an
        older changeset_revsion, but later the repository was updated in the tool shed and the Galaxy admin is trying to install the latest
        changeset revision of the same repository instead of updating the one that was previously installed.
        """
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        # If the request originated with the UpdateManager, it will not include a galaxy_url.
        galaxy_url = kwd.get( 'galaxy_url', '' )
        name = params.get( 'name', None )
        owner = params.get( 'owner', None )
        changeset_revision = params.get( 'changeset_revision', None )
        repository = suc.get_repository_by_name_and_owner( trans, name, owner )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        # Get the lower bound changeset revision 
        lower_bound_changeset_revision = suc.get_previous_downloadable_changset_revision( repository, repo, changeset_revision )
        # Build the list of changeset revision hashes.
        changeset_hashes = []
        for changeset in suc.reversed_lower_upper_bounded_changelog( repo, lower_bound_changeset_revision, changeset_revision ):
            changeset_hashes.append( str( repo.changectx( changeset ) ) )
        if changeset_hashes:
            changeset_hashes_str = ','.join( changeset_hashes )
            return changeset_hashes_str
        return ''
    @web.expose
    @web.require_login( "rate repositories" )
    def rate_repository( self, trans, **kwd ):
        """ Rate a repository and return updated rating data. """
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        id = params.get( 'id', None )
        if not id:
            return trans.response.send_redirect( web.url_for( controller='repository',
                                                              action='browse_repositories',
                                                              message='Select a repository to rate',
                                                              status='error' ) )
        repository = suc.get_repository_in_tool_shed( trans, id )
        repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) )
        if repository.user == trans.user:
            return trans.response.send_redirect( web.url_for( controller='repository',
                                                              action='browse_repositories',
                                                              message="You are not allowed to rate your own repository",
                                                              status='error' ) )
        if params.get( 'rate_button', False ):
            rating = int( params.get( 'rating', '0' ) )
            comment = util.restore_text( params.get( 'comment', '' ) )
            rating = self.rate_item( trans, trans.user, repository, rating, comment )
        avg_rating, num_ratings = self.get_ave_item_rating_data( trans.sa_session, repository, webapp_model=trans.model )
        display_reviews = util.string_as_bool( params.get( 'display_reviews', False ) )
        rra = self.get_user_item_rating( trans.sa_session, trans.user, repository, webapp_model=trans.model )
        is_malicious = common.changeset_is_malicious( trans, id, repository.tip( trans.app ) )
        metadata = self.get_metadata( trans, id, repository.tip( trans.app ) )
        return trans.fill_template( '/webapps/community/repository/rate_repository.mako', 
                                    repository=repository,
                                    metadata=metadata,
                                    avg_rating=avg_rating,
                                    display_reviews=display_reviews,
                                    num_ratings=num_ratings,
                                    rra=rra,
                                    is_malicious=is_malicious,
                                    message=message,
                                    status=status )
    @web.expose
    def reset_all_metadata( self, trans, id, **kwd ):
        # This method is called only from the ~/templates/webapps/community/repository/manage_repository.mako template.
        # It resets all metadata on the complete changelog for a single repository in the tool shed.
        invalid_file_tups, metadata_dict = suc.reset_all_metadata_on_repository_in_tool_shed( trans, id, **kwd )
        if invalid_file_tups:
            repository = suc.get_repository_in_tool_shed( trans, id )
            message = suc.generate_message_for_invalid_tools( trans, invalid_file_tups, repository, metadata_dict )
            status = 'error'
        else:
            message = "All repository metadata has been reset."
            status = 'done'
        return trans.response.send_redirect( web.url_for( controller='repository',
                                                          action='manage_repository',
                                                          id=id,
                                                          message=message,
                                                          status=status ) )
    def __search_ids_names( self, tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_ids, tool_names ):
        for i, tool_id in enumerate( tool_ids ):
            tool_name = tool_names[ i ]
            if self.__in_tool_dict( tool_dict, exact_matches_checked, tool_id=tool_id, tool_name=tool_name ):
                match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
        return match_tuples
    def __search_ids_versions( self, tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_ids, tool_versions ):
        for i, tool_id in enumerate( tool_ids ):
            tool_version = tool_versions[ i ]
            if self.__in_tool_dict( tool_dict, exact_matches_checked, tool_id=tool_id, tool_version=tool_version ):
                match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
        return match_tuples
    def __search_names_versions( self, tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_names, tool_versions ):
        for i, tool_name in enumerate( tool_names ):
            tool_version = tool_versions[ i ]
            if self.__in_tool_dict( tool_dict, exact_matches_checked, tool_name=tool_name, tool_version=tool_version ):
                match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
        return match_tuples
    def __search_repository_metadata( self, trans, exact_matches_checked, tool_ids='', tool_names='', tool_versions='', workflow_names='', all_workflows=False ):
        match_tuples = []
        ok = True
        for repository_metadata in trans.sa_session.query( model.RepositoryMetadata ):
            metadata = repository_metadata.metadata
            if tool_ids or tool_names or tool_versions:
                if 'tools' in metadata:
                    tools = metadata[ 'tools' ]
                else:
                    tools = []
                for tool_dict in tools:
                    if tool_ids and not tool_names and not tool_versions:
                        for tool_id in tool_ids:
                            if self.__in_tool_dict( tool_dict, exact_matches_checked, tool_id=tool_id ):
                                match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
                    elif tool_names and not tool_ids and not tool_versions:
                        for tool_name in tool_names:
                            if self.__in_tool_dict( tool_dict, exact_matches_checked, tool_name=tool_name ):
                                match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
                    elif tool_versions and not tool_ids and not tool_names:
                        for tool_version in tool_versions:
                            if self.__in_tool_dict( tool_dict, exact_matches_checked, tool_version=tool_version ):
                                match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
                    elif tool_ids and tool_names and not tool_versions:
                        if len( tool_ids ) == len( tool_names ):
                            match_tuples = self.__search_ids_names( tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_ids, tool_names )
                        elif len( tool_ids ) == 1 or len( tool_names ) == 1:
                            tool_ids, tool_names = self.__make_same_length( tool_ids, tool_names )
                            match_tuples = self.__search_ids_names( tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_ids, tool_names )
                        else:
                            ok = False
                    elif tool_ids and tool_versions and not tool_names:
                        if len( tool_ids )  == len( tool_versions ):
                            match_tuples = self.__search_ids_versions( tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_ids, tool_versions )
                        elif len( tool_ids ) == 1 or len( tool_versions ) == 1:
                            tool_ids, tool_versions = self.__make_same_length( tool_ids, tool_versions )
                            match_tuples = self.__search_ids_versions( tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_ids, tool_versions )
                        else:
                            ok = False
                    elif tool_versions and tool_names and not tool_ids:
                        if len( tool_versions ) == len( tool_names ):
                            match_tuples = self.__search_names_versions( tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_names, tool_versions )
                        elif len( tool_versions ) == 1 or len( tool_names ) == 1:
                            tool_versions, tool_names = self.__make_same_length( tool_versions, tool_names )
                            match_tuples = self.__search_names_versions( tool_dict, exact_matches_checked, match_tuples, repository_metadata, tool_names, tool_versions )
                        else:
                            ok = False
                    elif tool_versions and tool_names and tool_ids:
                        if len( tool_versions ) == len( tool_names ) and len( tool_names ) == len( tool_ids ):
                            for i, tool_version in enumerate( tool_versions ):
                                tool_name = tool_names[ i ]
                                tool_id = tool_ids[ i ]
                                if self.__in_tool_dict( tool_dict, exact_matches_checked, tool_id=tool_id, tool_name=tool_name, tool_version=tool_version ):
                                    match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
                        else:
                            ok = False
            elif workflow_names:
                if 'workflows' in metadata:
                    # metadata[ 'workflows' ] is a list of tuples where each contained tuple is
                    # [ <relative path to the .ga file in the repository>, <exported workflow dict> ]
                    workflow_tups = metadata[ 'workflows' ]
                    workflows = [ workflow_tup[1] for workflow_tup in workflow_tups ]
                else:
                    workflows = []
                for workflow_dict in workflows:
                    for workflow_name in workflow_names:
                        if self.__in_workflow_dict( workflow_dict, exact_matches_checked, workflow_name ):
                            match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
            elif all_workflows and 'workflows' in metadata:
                match_tuples.append( ( repository_metadata.repository_id, repository_metadata.changeset_revision ) )
        return ok, match_tuples
    @web.expose
    def select_files_to_delete( self, trans, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', '' ) )
        status = params.get( 'status', 'done' )
        commit_message = util.restore_text( params.get( 'commit_message', 'Deleted selected files' ) )
        repository = suc.get_repository_in_tool_shed( trans, id )
        repo_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_dir )
        selected_files_to_delete = util.restore_text( params.get( 'selected_files_to_delete', '' ) )
        if params.get( 'select_files_to_delete_button', False ):
            if selected_files_to_delete:
                selected_files_to_delete = selected_files_to_delete.split( ',' )
                # Get the current repository tip.
                tip = repository.tip( trans.app )
                for selected_file in selected_files_to_delete:
                    try:
                        commands.remove( repo.ui, repo, selected_file, force=True )
                    except Exception, e:
                        log.debug( "Error removing files using the mercurial API, so trying a different approach, the error was: %s" % str( e ))
                        relative_selected_file = selected_file.split( 'repo_%d' % repository.id )[1].lstrip( '/' )
                        repo.dirstate.remove( relative_selected_file )
                        repo.dirstate.write()
                        absolute_selected_file = os.path.abspath( selected_file )
                        if os.path.isdir( absolute_selected_file ):
                            try:
                                os.rmdir( absolute_selected_file )
                            except OSError, e:
                                # The directory is not empty
                                pass
                        elif os.path.isfile( absolute_selected_file ):
                            os.remove( absolute_selected_file )
                            dir = os.path.split( absolute_selected_file )[0]
                            try:
                                os.rmdir( dir )
                            except OSError, e:
                                # The directory is not empty
                                pass
                # Commit the change set.
                if not commit_message:
                    commit_message = 'Deleted selected files'
                commands.commit( repo.ui, repo, repo_dir, user=trans.user.username, message=commit_message )
                common.handle_email_alerts( trans, repository )
                # Update the repository files for browsing.
                suc.update_repository( repo )
                # Get the new repository tip.
                repo = hg.repository( suc.get_configured_ui(), repo_dir )
                if tip == repository.tip( trans.app ):
                    message += 'No changes to repository.  '
                    kwd[ 'message' ] = message
                    
                else:
                    message += 'The selected files were deleted from the repository.  '
                    kwd[ 'message' ] = message
                    common.set_repository_metadata_due_to_new_tip( trans, repository, **kwd )
            else:
                message = "Select at least 1 file to delete from the repository before clicking <b>Delete selected files</b>."
                status = "error"
        is_malicious = common.changeset_is_malicious( trans, id, repository.tip( trans.app ) )
        return trans.fill_template( '/webapps/community/repository/browse_repository.mako',
                                    repo=repo,
                                    repository=repository,
                                    commit_message=commit_message,
                                    is_malicious=is_malicious,
                                    message=message,
                                    status=status )
    @web.expose
    def send_to_owner( self, trans, id, message='' ):
        repository = suc.get_repository_in_tool_shed( trans, id )
        if not message:
            message = 'Enter a message'
            status = 'error'
        elif trans.user and trans.user.email:
            smtp_server = trans.app.config.smtp_server
            from_address = trans.app.config.email_from
            if smtp_server is None or from_address is None:
                return trans.show_error_message( "Mail is not configured for this Galaxy tool shed instance" )
            to_address = repository.user.email
            # Get the name of the server hosting the tool shed instance.
            host = trans.request.host
            # Build the email message
            body = string.Template( common.contact_owner_template ) \
                .safe_substitute( username=trans.user.username,
                                  repository_name=repository.name,
                                  email=trans.user.email,
                                  message=message,
                                  host=host )
            subject = "Regarding your tool shed repository named %s" % repository.name
            # Send it
            try:
                util.send_mail( from_address, to_address, subject, body, trans.app.config )
                message = "Your message has been sent"
                status = "done"
            except Exception, e:
                message = "An error occurred sending your message by email: %s" % str( e )
                status = "error"
        else:
            # Do all we can to eliminate spam.
            return trans.show_error_message( "You must be logged in to contact the owner of a repository." )
        return trans.response.send_redirect( web.url_for( controller='repository',
                                                          action='contact_owner',
                                                          id=id,
                                                          message=message,
                                                          status=status ) )
    @web.expose
    @web.require_login( "set email alerts" )
    def set_email_alerts( self, trans, **kwd ):
        # Set email alerts for selected repositories
        # This method is called from multiple grids, so
        # the caller must be passed.
        caller = kwd[ 'caller' ]
        user = trans.user
        if user:
            repository_ids = util.listify( kwd.get( 'id', '' ) )
            total_alerts_added = 0
            total_alerts_removed = 0
            flush_needed = False
            for repository_id in repository_ids:
                repository = suc.get_repository_in_tool_shed( trans, repository_id )
                if repository.email_alerts:
                    email_alerts = from_json_string( repository.email_alerts )
                else:
                    email_alerts = []
                if user.email in email_alerts:
                    email_alerts.remove( user.email )
                    repository.email_alerts = to_json_string( email_alerts )
                    trans.sa_session.add( repository )
                    flush_needed = True
                    total_alerts_removed += 1
                else:
                    email_alerts.append( user.email )
                    repository.email_alerts = to_json_string( email_alerts )
                    trans.sa_session.add( repository )
                    flush_needed = True
                    total_alerts_added += 1
            if flush_needed:
                trans.sa_session.flush()
            message = 'Total alerts added: %d, total alerts removed: %d' % ( total_alerts_added, total_alerts_removed )
            kwd[ 'message' ] = message
            kwd[ 'status' ] = 'done'
        del kwd[ 'operation' ]
        return trans.response.send_redirect( web.url_for( controller='repository',
                                                          action=caller,
                                                          **kwd ) )
    @web.expose
    @web.require_login( "set repository as malicious" )
    def set_malicious( self, trans, id, ctx_str, **kwd ):
        malicious = kwd.get( 'malicious', '' )
        if kwd.get( 'malicious_button', False ):
            repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, id, ctx_str )
            malicious_checked = CheckboxField.is_checked( malicious )
            repository_metadata.malicious = malicious_checked
            trans.sa_session.add( repository_metadata )
            trans.sa_session.flush()
            if malicious_checked:
                message = "The repository tip has been defined as malicious."
            else:
                message = "The repository tip has been defined as <b>not</b> malicious."
            status = 'done'
        return trans.response.send_redirect( web.url_for( controller='repository',
                                                          action='manage_repository',
                                                          id=id,
                                                          changeset_revision=ctx_str,
                                                          malicious=malicious,
                                                          message=message,
                                                          status=status ) )
    def __stringify( self, list ):
        if list:
            return ','.join( list )
        return ''
    def __validate_repository_name( self, name, user ):
        # Repository names must be unique for each user, must be at least four characters
        # in length and must contain only lower-case letters, numbers, and the '_' character.
        if name in [ 'None', None, '' ]:
            return 'Enter the required repository name.'
        for repository in user.active_repositories:
            if repository.name == name:
                return "You already have a repository named '%s', so choose a different name." % name
        if len( name ) < 4:
            return "Repository names must be at least 4 characters in length."
        if len( name ) > 80:
            return "Repository names cannot be more than 80 characters in length."
        if not( VALID_REPOSITORYNAME_RE.match( name ) ):
            return "Repository names must contain only lower-case letters, numbers and underscore '_'."
        return ''
    @web.expose
    def view_changelog( self, trans, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        repository = suc.get_repository_in_tool_shed( trans, id )
        repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) )
        changesets = []
        for changeset in repo.changelog:
            ctx = repo.changectx( changeset )
            if suc.get_repository_metadata_by_changeset_revision( trans, id, str( ctx ) ):
                has_metadata = True
            else:
                has_metadata = False
            t, tz = ctx.date()
            date = datetime( *gmtime( float( t ) - tz )[:6] )
            display_date = date.strftime( "%Y-%m-%d" )
            change_dict = { 'ctx' : ctx,
                            'rev' : str( ctx.rev() ),
                            'date' : date,
                            'display_date' : display_date,
                            'description' : ctx.description(),
                            'files' : ctx.files(),
                            'user' : ctx.user(),
                            'parent' : ctx.parents()[0],
                            'has_metadata' : has_metadata }
            # Make sure we'll view latest changeset first.
            changesets.insert( 0, change_dict )
        is_malicious = common.changeset_is_malicious( trans, id, repository.tip( trans.app ) )
        metadata = self.get_metadata( trans, id, repository.tip( trans.app ) )
        return trans.fill_template( '/webapps/community/repository/view_changelog.mako', 
                                    repository=repository,
                                    metadata=metadata,
                                    changesets=changesets,
                                    is_malicious=is_malicious,
                                    message=message,
                                    status=status )
    @web.expose
    def view_changeset( self, trans, id, ctx_str, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        repository = suc.get_repository_in_tool_shed( trans, id )
        repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) )
        ctx = suc.get_changectx_for_changeset( repo, ctx_str )
        if ctx is None:
            message = "Repository does not include changeset revision '%s'." % str( ctx_str )
            status = 'error'
            return trans.response.send_redirect( web.url_for( controller='repository',
                                                              action='view_changelog',
                                                              id=id,
                                                              message=message,
                                                              status=status ) )
        ctx_parent = ctx.parents()[0]
        modified, added, removed, deleted, unknown, ignored, clean = repo.status( node1=ctx_parent.node(), node2=ctx.node() )
        anchors = modified + added + removed + deleted + unknown + ignored + clean
        diffs = []
        for diff in patch.diff( repo, node1=ctx_parent.node(), node2=ctx.node() ):
            diffs.append( suc.to_safe_string( diff, to_html=True ) )
        is_malicious = common.changeset_is_malicious( trans, id, repository.tip( trans.app ) )
        metadata = self.get_metadata( trans, id, ctx_str )
        return trans.fill_template( '/webapps/community/repository/view_changeset.mako', 
                                    repository=repository,
                                    metadata=metadata,
                                    ctx=ctx,
                                    anchors=anchors,
                                    modified=modified,
                                    added=added,
                                    removed=removed,
                                    deleted=deleted,
                                    unknown=unknown,
                                    ignored=ignored,
                                    clean=clean,
                                    diffs=diffs,
                                    is_malicious=is_malicious,
                                    message=message,
                                    status=status )
    @web.expose
    def view_or_manage_repository( self, trans, **kwd ):
        repository = suc.get_repository_in_tool_shed( trans, kwd[ 'id' ] )
        if trans.user_is_admin() or repository.user == trans.user:
            return trans.response.send_redirect( web.url_for( controller='repository',
                                                              action='manage_repository',
                                                              **kwd ) )
        else:
            return trans.response.send_redirect( web.url_for( controller='repository',
                                                              action='view_repository',
                                                              **kwd ) )
    @web.expose
    def view_repository( self, trans, id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        cntrller = params.get( 'cntrller', 'repository' )
        repository = suc.get_repository_in_tool_shed( trans, id )
        repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) )
        avg_rating, num_ratings = self.get_ave_item_rating_data( trans.sa_session, repository, webapp_model=trans.model )
        changeset_revision = util.restore_text( params.get( 'changeset_revision', repository.tip( trans.app ) ) )
        display_reviews = util.string_as_bool( params.get( 'display_reviews', False ) )
        alerts = params.get( 'alerts', '' )
        alerts_checked = CheckboxField.is_checked( alerts )
        if repository.email_alerts:
            email_alerts = from_json_string( repository.email_alerts )
        else:
            email_alerts = []
        repository_dependencies = None
        user = trans.user
        if user and params.get( 'receive_email_alerts_button', False ):
            flush_needed = False
            if alerts_checked:
                if user.email not in email_alerts:
                    email_alerts.append( user.email )
                    repository.email_alerts = to_json_string( email_alerts )
                    flush_needed = True
            else:
                if user.email in email_alerts:
                    email_alerts.remove( user.email )
                    repository.email_alerts = to_json_string( email_alerts )
                    flush_needed = True
            if flush_needed:
                trans.sa_session.add( repository )
                trans.sa_session.flush()
        checked = alerts_checked or ( user and user.email in email_alerts )
        alerts_check_box = CheckboxField( 'alerts', checked=checked )
        changeset_revision_select_field = build_changeset_revision_select_field( trans,
                                                                                 repository,
                                                                                 selected_value=changeset_revision,
                                                                                 add_id_to_name=False,
                                                                                 downloadable=False )
        revision_label = common.get_revision_label( trans, repository, changeset_revision )
        repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, id, changeset_revision )
        if repository_metadata:
            repository_metadata_id = trans.security.encode_id( repository_metadata.id )
            metadata = repository_metadata.metadata
            # Get a dictionary of all repositories upon which the contents of the current repository_metadata record depend.
            repository_dependencies = suc.get_repository_dependencies_for_changeset_revision( trans=trans,
                                                                                              repository=repository,
                                                                                              repository_metadata=repository_metadata,
                                                                                              toolshed_base_url=str( url_for( '/', qualified=True ) ).rstrip( '/' ),
                                                                                              key_rd_dicts_to_be_processed=None,
                                                                                              all_repository_dependencies=None,
                                                                                              handled_key_rd_dicts=None )
        else:
            repository_metadata_id = None
            metadata = None
        is_malicious = common.changeset_is_malicious( trans, id, repository.tip( trans.app ) )
        if is_malicious:
            if trans.app.security_agent.can_push( trans.app, trans.user, repository ):
                message += common.malicious_error_can_push
            else:
                message += common.malicious_error
            status = 'error'
        # Determine if the current changeset revision has been reviewed by the current user.
        reviewed_by_user = common.changeset_revision_reviewed_by_user( trans, trans.user, repository, changeset_revision )
        if reviewed_by_user:
            review = common.get_review_by_repository_id_changeset_revision_user_id( trans, id, changeset_revision, trans.security.encode_id( trans.user.id ) )
            review_id = trans.security.encode_id( review.id )
        else:
            review_id = None
        containers_dict = suc.build_repository_containers_for_tool_shed( repository, changeset_revision, repository_dependencies, repository_metadata )
        return trans.fill_template( '/webapps/community/repository/view_repository.mako',
                                    cntrller=cntrller,
                                    repo=repo,
                                    repository=repository,
                                    repository_metadata_id=repository_metadata_id,
                                    metadata=metadata,
                                    containers_dict=containers_dict,
                                    avg_rating=avg_rating,
                                    display_reviews=display_reviews,
                                    num_ratings=num_ratings,
                                    alerts_check_box=alerts_check_box,
                                    changeset_revision=changeset_revision,
                                    reviewed_by_user=reviewed_by_user,
                                    review_id=review_id,
                                    changeset_revision_select_field=changeset_revision_select_field,
                                    revision_label=revision_label,
                                    is_malicious=is_malicious,
                                    message=message,
                                    status=status )
    @web.expose
    def view_tool_metadata( self, trans, repository_id, changeset_revision, tool_id, **kwd ):
        params = util.Params( kwd )
        message = util.restore_text( params.get( 'message', ''  ) )
        status = params.get( 'status', 'done' )
        repository = suc.get_repository_in_tool_shed( trans, repository_id )
        repo_files_dir = repository.repo_path( trans.app )
        repo = hg.repository( suc.get_configured_ui(), repo_files_dir )
        tool_metadata_dict = {}
        tool_lineage = []
        tool = None
        guid = None
        original_tool_data_path = trans.app.config.tool_data_path
        revision_label = common.get_revision_label( trans, repository, changeset_revision )
        repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision )
        if repository_metadata:
            metadata = repository_metadata.metadata
            if metadata:
                if 'tools' in metadata:
                    for tool_metadata_dict in metadata[ 'tools' ]:
                        if tool_metadata_dict[ 'id' ] == tool_id:
                            work_dir = tempfile.mkdtemp()
                            relative_path_to_tool_config = tool_metadata_dict[ 'tool_config' ]
                            guid = tool_metadata_dict[ 'guid' ]
                            full_path_to_tool_config = os.path.abspath( relative_path_to_tool_config )
                            full_path_to_dir, tool_config_filename = os.path.split( full_path_to_tool_config )
                            can_use_disk_file = common.can_use_tool_config_disk_file( trans, repository, repo, full_path_to_tool_config, changeset_revision )
                            if can_use_disk_file:
                                trans.app.config.tool_data_path = work_dir
                                tool, valid, message, sample_files = suc.handle_sample_files_and_load_tool_from_disk( trans,
                                                                                                                      repo_files_dir,
                                                                                                                      full_path_to_tool_config,
                                                                                                                      work_dir )
                                if message:
                                    status = 'error'
                            else:
                                tool, message, sample_files = suc.handle_sample_files_and_load_tool_from_tmp_config( trans,
                                                                                                                     repo,
                                                                                                                     changeset_revision,
                                                                                                                     tool_config_filename,
                                                                                                                     work_dir )
                                if message:
                                    status = 'error'
                            break
                    if guid:
                        tool_lineage = self.get_versions_of_tool( trans, repository, repository_metadata, guid )
        else:
            metadata = None
        is_malicious = common.changeset_is_malicious( trans, repository_id, repository.tip( trans.app ) )
        changeset_revision_select_field = build_changeset_revision_select_field( trans,
                                                                                 repository,
                                                                                 selected_value=changeset_revision,
                                                                                 add_id_to_name=False,
                                                                                 downloadable=False )
        trans.app.config.tool_data_path = original_tool_data_path
        reviewed_by_user = common.changeset_revision_reviewed_by_user( trans, trans.user, repository, changeset_revision )
        if reviewed_by_user:
            review = common.get_review_by_repository_id_changeset_revision_user_id( trans, id, changeset_revision, trans.security.encode_id( trans.user.id ) )
            review_id = trans.security.encode_id( review.id )
        else:
            review_id = None
        return trans.fill_template( "/webapps/community/repository/view_tool_metadata.mako",
                                    repository=repository,
                                    metadata=metadata,
                                    tool=tool,
                                    tool_metadata_dict=tool_metadata_dict,
                                    tool_lineage=tool_lineage,
                                    changeset_revision=changeset_revision,
                                    revision_label=revision_label,
                                    changeset_revision_select_field=changeset_revision_select_field,
                                    is_malicious=is_malicious,
                                    reviewed_by_user=reviewed_by_user,
                                    review_id=review_id,
                                    message=message,
                                    status=status )

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

def build_changeset_revision_select_field( trans, repository, selected_value=None, add_id_to_name=True,
                                           downloadable=False, reviewed=False, not_reviewed=False ):
    """Build a SelectField whose options are the changeset_rev strings of certain revisions of the received repository."""
    options = []
    changeset_tups = []
    refresh_on_change_values = []
    if downloadable:
        # Restrict the options to downloadable revisions.
        repository_metadata_revisions = repository.downloadable_revisions
    elif reviewed:
        # Restrict the options to revisions that have been reviewed.
        repository_metadata_revisions = []
        metadata_changeset_revision_hashes = []
        for metadata_revision in repository.metadata_revisions:
            metadata_changeset_revision_hashes.append( metadata_revision.changeset_revision )
        for review in repository.reviews:
            if review.changeset_revision in metadata_changeset_revision_hashes:
                repository_metadata_revisions.append( review.repository_metadata )
    elif not_reviewed:
        # Restrict the options to revisions that have not yet been reviewed.
        repository_metadata_revisions = []
        reviewed_metadata_changeset_revision_hashes = []
        for review in repository.reviews:
            reviewed_metadata_changeset_revision_hashes.append( review.changeset_revision )
        for metadata_revision in repository.metadata_revisions:
            if metadata_revision.changeset_revision not in reviewed_metadata_changeset_revision_hashes:
                repository_metadata_revisions.append( metadata_revision )
    else:
        # Restrict the options to all revisions that have associated metadata.
        repository_metadata_revisions = repository.metadata_revisions
    for repository_metadata in repository_metadata_revisions:
        rev, label, changeset_revision = common.get_rev_label_changeset_revision_from_repository_metadata( trans, repository_metadata, repository=repository )
        changeset_tups.append( ( rev, label, changeset_revision ) )
        refresh_on_change_values.append( changeset_revision )
    # Sort options by the revision label.  Even though the downloadable_revisions query sorts by update_time,
    # the changeset revisions may not be sorted correctly because setting metadata over time will reset update_time.
    for changeset_tup in sorted( changeset_tups ):
        # Display the latest revision first.
        options.insert( 0, ( changeset_tup[1], changeset_tup[2] ) )
    if add_id_to_name:
        name = 'changeset_revision_%d' % repository.id
    else:
        name = 'changeset_revision'
    select_field = SelectField( name=name,
                                refresh_on_change=True,
                                refresh_on_change_values=refresh_on_change_values )
    for option_tup in options:
        selected = selected_value and option_tup[1] == selected_value
        select_field.add_option( option_tup[0], option_tup[1], selected=selected )
    return select_field
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.