~launchpad-pqm/launchpad/devel

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
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Browser views for archive."""

__metaclass__ = type

__all__ = [
    'ArchiveAdminView',
    'ArchiveActivateView',
    'ArchiveBadges',
    'ArchiveBuildsView',
    'ArchiveDeleteView',
    'ArchiveEditDependenciesView',
    'ArchiveEditView',
    'ArchiveIndexActionsMenu',
    'ArchiveNavigation',
    'ArchiveNavigationMenu',
    'ArchivePackageCopyingView',
    'ArchivePackageDeletionView',
    'ArchivePackagesActionMenu',
    'ArchivePackagesView',
    'ArchiveView',
    'ArchiveViewBase',
    'make_archive_vocabulary',
    'PackageCopyingMixin',
    'traverse_named_ppa',
    ]


from cgi import escape
from datetime import (
    datetime,
    timedelta,
    )
from urlparse import urlparse

import pytz
from sqlobject import SQLObjectNotFound
from storm.expr import Desc
from zope.app.form.browser import TextAreaWidget
from zope.component import getUtility
from zope.formlib import form
from zope.interface import (
    implements,
    Interface,
    )
from zope.schema import (
    Bool,
    Choice,
    List,
    TextLine,
    )
from zope.schema.interfaces import IContextSourceBinder
from zope.schema.vocabulary import (
    SimpleTerm,
    SimpleVocabulary,
    )
from zope.security.interfaces import Unauthorized

from canonical.launchpad import _
from canonical.launchpad.browser.librarian import FileNavigationMixin
from canonical.launchpad.components.tokens import create_token
from canonical.launchpad.helpers import english_list
from canonical.launchpad.webapp import (
    canonical_url,
    enabled_with_permission,
    LaunchpadView,
    Link,
    Navigation,
    stepthrough,
    )
from canonical.launchpad.webapp.authorization import check_permission
from canonical.launchpad.webapp.badge import HasBadgeBase
from canonical.launchpad.webapp.batching import BatchNavigator
from canonical.launchpad.webapp.interfaces import (
    ICanonicalUrlData,
    IStructuredString,
    )
from canonical.launchpad.webapp.menu import (
    NavigationMenu,
    structured,
    )
from canonical.lazr.utils import smartquote
from lp.app.browser.launchpadform import (
    action,
    custom_widget,
    LaunchpadEditFormView,
    LaunchpadFormView,
    )
from lp.app.browser.lazrjs import (
    TextAreaEditorWidget,
    TextLineEditorWidget,
    )
from lp.app.errors import NotFoundError
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.app.widgets.itemswidgets import (
    LabeledMultiCheckBoxWidget,
    LaunchpadDropdownWidget,
    LaunchpadRadioWidget,
    PlainMultiCheckBoxWidget,
    )
from lp.app.widgets.textwidgets import StrippedTextWidget
from lp.buildmaster.enums import BuildStatus
from lp.registry.interfaces.person import (
    IPersonSet,
    PersonVisibility,
    )
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.interfaces.series import SeriesStatus
from lp.registry.interfaces.sourcepackagename import ISourcePackageNameSet
from lp.services.browser_helpers import get_user_agent_distroseries
from lp.services.database.bulk import load
from lp.services.features import getFeatureFlag
from lp.services.propertycache import cachedproperty
from lp.services.worlddata.interfaces.country import ICountrySet
from lp.soyuz.adapters.archivedependencies import (
    default_component_dependency_name,
    default_pocket_dependency,
    )
from lp.soyuz.adapters.archivesourcepublication import (
    ArchiveSourcePublications,
    )
from lp.soyuz.browser.build import (
    BuildNavigationMixin,
    BuildRecordsView,
    )
from lp.soyuz.browser.sourceslist import (
    SourcesListEntries,
    SourcesListEntriesView,
    )
from lp.soyuz.browser.widgets.archive import PPANameWidget
from lp.soyuz.enums import (
    ArchivePermissionType,
    ArchivePurpose,
    ArchiveStatus,
    PackageCopyPolicy,
    PackagePublishingStatus,
    )
from lp.soyuz.interfaces.archive import (
    CannotCopy,
    IArchive,
    IArchiveEditDependenciesForm,
    IArchiveSet,
    NoSuchPPA,
    )
from lp.soyuz.interfaces.archivepermission import IArchivePermissionSet
from lp.soyuz.interfaces.archivesubscriber import IArchiveSubscriberSet
from lp.soyuz.interfaces.binarypackagebuild import BuildSetStatus
from lp.soyuz.interfaces.binarypackagename import IBinaryPackageNameSet
from lp.soyuz.interfaces.component import IComponentSet
from lp.soyuz.interfaces.packagecopyjob import IPlainPackageCopyJobSource
from lp.soyuz.interfaces.packagecopyrequest import IPackageCopyRequestSet
from lp.soyuz.interfaces.packageset import IPackagesetSet
from lp.soyuz.interfaces.processor import IProcessorFamilySet
from lp.soyuz.interfaces.publishing import (
    active_publishing_status,
    inactive_publishing_status,
    IPublishingSet,
    )
from lp.soyuz.model.archive import Archive
from lp.soyuz.model.binarypackagename import BinaryPackageName
from lp.soyuz.model.publishing import (
    BinaryPackagePublishingHistory,
    SourcePackagePublishingHistory,
    )
from lp.soyuz.scripts.packagecopier import (
    check_copy_permissions,
    do_copy,
    )

# Feature flag: up to how many package sync requests (inclusive) are to be
# processed synchronously within the web request?
# Set to -1 to disable synchronous syncs.
FEATURE_FLAG_MAX_SYNCHRONOUS_SYNCS = (
    'soyuz.derived_series.max_synchronous_syncs')


class ArchiveBadges(HasBadgeBase):
    """Provides `IHasBadges` for `IArchive`."""

    def getPrivateBadgeTitle(self):
        """Return private badge info useful for a tooltip."""
        return "This archive is private."


def traverse_named_ppa(person_name, ppa_name):
    """For PPAs, traverse the right place.

    :param person_name: The person part of the URL
    :param ppa_name: The PPA name part of the URL
    """
    person = getUtility(IPersonSet).getByName(person_name)
    try:
        archive = person.getPPAByName(ppa_name)
    except NoSuchPPA:
        raise NotFoundError("%s/%s", (person_name, ppa_name))

    return archive


class DistributionArchiveURL:
    """Dynamic URL declaration for `IDistributionArchive`.

    When dealing with distribution archives we want to present them under
    IDistribution as /<distro>/+archive/<name>, for example:
    /ubuntu/+archive/partner
    """
    implements(ICanonicalUrlData)
    rootsite = None

    def __init__(self, context):
        self.context = context

    @property
    def inside(self):
        return self.context.distribution

    @property
    def path(self):
        return u"+archive/%s" % self.context.name


class PPAURL:
    """Dynamic URL declaration for named PPAs."""
    implements(ICanonicalUrlData)
    rootsite = None

    def __init__(self, context):
        self.context = context

    @property
    def inside(self):
        return self.context.owner

    @property
    def path(self):
        return u"+archive/%s" % self.context.name


class ArchiveNavigation(Navigation, FileNavigationMixin,
                        BuildNavigationMixin):
    """Navigation methods for IArchive."""

    usedfor = IArchive

    @stepthrough('+sourcepub')
    def traverse_sourcepub(self, name):
        return self._traverse_publication(name, source=True)

    @stepthrough('+binarypub')
    def traverse_binarypub(self, name):
        return self._traverse_publication(name, source=False)

    def _traverse_publication(self, name, source):
        try:
            pub_id = int(name)
        except ValueError:
            return None

        # The ID is not enough on its own to identify the publication,
        # we need to make sure it matches the context archive as well.
        return getUtility(IPublishingSet).getByIdAndArchive(
            pub_id, self.context, source)

    @stepthrough('+binaryhits')
    def traverse_binaryhits(self, name_str):
        """Traverse to an `IBinaryPackageReleaseDownloadCount`.

        A matching path is something like this:

          +binaryhits/foopkg/1.0/i386/2010-03-11/AU

        To reach one where the country is None, use:

          +binaryhits/foopkg/1.0/i386/2010-03-11/unknown
        """

        if len(self.request.stepstogo) < 4:
            return None

        version = self.request.stepstogo.consume()
        archtag = self.request.stepstogo.consume()
        date_str = self.request.stepstogo.consume()
        country_str = self.request.stepstogo.consume()

        try:
            name = getUtility(IBinaryPackageNameSet)[name_str]
        except NotFoundError:
            return None

        # This will return None if there are multiple BPRs with the same
        # name in the archive's history, but in that case downloads
        # won't be counted either.
        bpr = self.context.getBinaryPackageRelease(name, version, archtag)
        if bpr is None:
            return None

        try:
            date = datetime.strptime(date_str, '%Y-%m-%d').date()
        except ValueError:
            return None

        # 'unknown' should always be safe, since the key is the two letter
        # ISO code, and 'unknown' has more than two letters.
        if country_str == 'unknown':
            country = None
        else:
            try:
                country = getUtility(ICountrySet)[country_str]
            except NotFoundError:
                return None

        return self.context.getPackageDownloadCount(bpr, date, country)

    @stepthrough('+subscriptions')
    def traverse_subscription(self, person_name):
        try:
            person = getUtility(IPersonSet).getByName(person_name)
        except NotFoundError:
            return None

        subscriptions = getUtility(IArchiveSubscriberSet).getBySubscriber(
            person, archive=self.context)

        # If a person is subscribed with a direct subscription as well as
        # via a team, subscriptions will contain both, so need to grab
        # the direct subscription:
        for subscription in subscriptions:
            if subscription.subscriber == person:
                return subscription

        return None

    @stepthrough('+upload')
    def traverse_upload_permission(self, name):
        """Traverse the data part of the URL for upload permissions."""
        return self._traverse_permission(name, ArchivePermissionType.UPLOAD)

    @stepthrough('+queue-admin')
    def traverse_queue_admin_permission(self, name):
        """Traverse the data part of the URL for queue admin permissions."""
        return self._traverse_permission(
            name, ArchivePermissionType.QUEUE_ADMIN)

    def _traverse_permission(self, name, permission_type):
        """Traversal helper function.

        The data part ("name") is a compound value of the format:
        user.item
        where item is a component or a source package name,
        """

        def get_url_param(param_name):
            """Return the URL parameter with the given name or None."""
            param_seq = self.request.query_string_params.get(param_name)
            if param_seq is None or len(param_seq) == 0:
                return None
            else:
                # Return whatever value was specified last in the URL.
                return param_seq.pop()

        # Look up the principal first.
        user = getUtility(IPersonSet).getByName(name)
        if user is None:
            return None

        # Obtain the item type and name from the URL parameters.
        item_type = get_url_param('type')
        item = get_url_param('item')

        if item_type is None or item is None:
            return None

        if item_type == 'component':
            # See if "item" is a component name.
            try:
                the_item = getUtility(IComponentSet)[item]
            except NotFoundError:
                pass
        elif item_type == 'packagename':
            # See if "item" is a source package name.
            the_item = getUtility(ISourcePackageNameSet).queryByName(item)
        elif item_type == 'packageset':
            the_item = None
            # Was a 'series' URL param passed?
            series = get_url_param('series')
            if series is not None:
                # Get the requested distro series.
                try:
                    series = self.context.distribution[series]
                except NotFoundError:
                    series = None
            if series is not None:
                the_item = getUtility(IPackagesetSet).getByName(
                    item, distroseries=series)
        else:
            the_item = None

        if the_item is not None:
            result_set = getUtility(IArchivePermissionSet).checkAuthenticated(
                user, self.context, permission_type, the_item)
            try:
                return result_set[0]
            except IndexError:
                return None
        else:
            return None

    @stepthrough('+dependency')
    def traverse_dependency(self, id):
        """Traverse to an archive dependency by archive ID.

        We use IArchive.getArchiveDependency here, which is protected by
        launchpad.View, so you cannot get to a dependency of a private
        archive that you can't see.
        """
        try:
            id = int(id)
        except ValueError:
            # Not a number.
            return None

        try:
            archive = getUtility(IArchiveSet).get(id)
        except SQLObjectNotFound:
            return None

        return self.context.getArchiveDependency(archive)


class ArchiveMenuMixin:

    def ppa(self):
        text = 'View PPA'
        return Link(canonical_url(self.context), text, icon='info')

    @enabled_with_permission('launchpad.Commercial')
    def admin(self):
        text = 'Administer archive'
        return Link('+admin', text, icon='edit')

    @enabled_with_permission('launchpad.Append')
    def manage_subscribers(self):
        text = 'Manage access'
        link = Link('+subscriptions', text, icon='edit')

        # This link should only be available for private archives:
        view = self.context
        archive = view.context
        if not archive.private or not archive.is_active:
            link.enabled = False
        return link

    @enabled_with_permission('launchpad.Edit')
    def edit(self):
        text = 'Change details'
        view = self.context
        return Link(
            '+edit', text, icon='edit', enabled=view.context.is_active)

    @enabled_with_permission('launchpad.Edit')
    def delete_ppa(self):
        text = 'Delete PPA'
        view = self.context
        return Link(
            '+delete', text, icon='trash-icon',
            enabled=view.context.is_active)

    def builds(self):
        text = 'View all builds'
        return Link('+builds', text, icon='info')

    def builds_successful(self):
        text = 'View successful builds'
        return Link('+builds?build_state=built', text, icon='info')

    def builds_pending(self):
        text = 'View pending builds'
        return Link('+builds?build_state=pending', text, icon='info')

    def builds_building(self):
        text = 'View in-progress builds'
        return Link('+builds?build_state=building', text, icon='info')

    def packages(self):
        text = 'View package details'
        link = Link('+packages', text, icon='info')
        # Disable the link for P3As if they don't have upload rights,
        # except if the user is a commercial admin.
        if self.context.private:
            if not check_permission('launchpad.Append', self.context):
                admins = getUtility(ILaunchpadCelebrities).commercial_admin
                if not self.user.inTeam(admins):
                    link.enabled = False
        return link

    @enabled_with_permission('launchpad.Edit')
    def delete(self):
        """Display a delete menu option for non-copy archives."""
        text = 'Delete packages'
        link = Link('+delete-packages', text, icon='edit')

        # This link should not be available for copy archives or
        # archives without any sources.
        if self.context.is_copy or not self.context.has_sources:
            link.enabled = False
        view = self.context
        if not view.context.is_active:
            link.enabled = False
        return link

    @enabled_with_permission('launchpad.AnyPerson')
    def copy(self):
        """Display a copy menu option for non-copy archives."""
        text = 'Copy packages'
        link = Link('+copy-packages', text, icon='edit')

        # This link should not be available for copy archives.
        if self.context.is_copy:
            link.enabled = False
        return link

    @enabled_with_permission('launchpad.Edit')
    def edit_dependencies(self):
        text = 'Edit PPA dependencies'
        view = self.context
        return Link(
            '+edit-dependencies', text, icon='edit',
            enabled=view.context.is_active)


class ArchiveNavigationMenu(NavigationMenu, ArchiveMenuMixin):
    """Overview Menu for IArchive."""

    usedfor = IArchive
    facet = 'overview'
    links = ['admin', 'builds', 'builds_building',
             'builds_pending', 'builds_successful',
             'packages', 'ppa']


class IArchiveIndexActionsMenu(Interface):
    """A marker interface for the ppa index actions menu."""


class ArchiveIndexActionsMenu(NavigationMenu, ArchiveMenuMixin):
    """Archive index navigation menu."""
    usedfor = IArchiveIndexActionsMenu
    facet = 'overview'
    links = ['admin', 'edit', 'edit_dependencies',
             'manage_subscribers', 'packages', 'delete_ppa']


class IArchivePackagesActionMenu(Interface):
    """A marker interface for the packages action menu."""


class ArchivePackagesActionMenu(NavigationMenu, ArchiveMenuMixin):
    """An action menu for archive package-related actions."""
    usedfor = IArchivePackagesActionMenu
    facet = 'overview'
    links = ['copy', 'delete']


class ArchiveViewBase(LaunchpadView):
    """Common features for Archive view classes."""

    def initialize(self):
        # If the archive has publishing disabled, present a warning.  If
        # the current user has lp.Edit then add a link to +edit to fix
        # this.
        if not self.context.publish and self.context.is_active:
            can_edit = check_permission('launchpad.Edit', self.context)
            notification = "Publishing has been disabled for this archive."
            if can_edit:
                edit_url = canonical_url(self.context) + '/+edit'
                notification += (
                    " <a href=%s>(re-enable publishing)</a>" % edit_url)
            if self.context.private:
                notification += (
                    " Since this archive is private, no builds are "
                    "being dispatched.")
            self.request.response.addNotification(structured(notification))
        super(ArchiveViewBase, self).initialize()

    @cachedproperty
    def private(self):
        return self.context.private

    @cachedproperty
    def has_sources(self):
        """Whether or not this PPA has any sources for the view.

        This can be overridden by subclasses as necessary. It allows
        the view to determine whether to display "This PPA does not yet
        have any published sources" or "No sources matching 'blah'."
        """
        return not self.context.getPublishedSources().is_empty()

    @cachedproperty
    def repository_usage(self):
        """Return a dictionary with usage details of this repository."""

        def package_plural(control):
            if control == 1:
                return 'package'
            return 'packages'

        # Calculate the label for the package counters respecting
        # singular/plural forms.
        number_of_sources = self.context.number_of_sources
        source_label = '%s source %s' % (
            number_of_sources, package_plural(number_of_sources))

        number_of_binaries = self.context.number_of_binaries
        binary_label = '%s binary %s' % (
            number_of_binaries, package_plural(number_of_binaries))

        # Quota is stored in MiB, convert it to bytes.
        quota = self.context.authorized_size * (2 ** 20)
        used = self.context.estimated_size

        # Calculate the usage factor and limit it to 100%.
        used_factor = (float(used) / quota)
        if used_factor > 1:
            used_factor = 1

        # Calculate the appropriate CSS class to be used with the usage
        # factor. Highlight it (in red) if usage is over 90% of the quota.
        if used_factor > 0.90:
            used_css_class = 'red'
        else:
            used_css_class = 'green'

        # Usage percentage with 2 degrees of precision (more than enough
        # for humans).
        used_percentage = "%0.2f" % (used_factor * 100)

        return dict(
            source_label=source_label,
            sources_size=self.context.sources_size,
            binary_label=binary_label,
            binaries_size=self.context.binaries_size,
            used=used,
            used_percentage=used_percentage,
            used_css_class=used_css_class,
            quota=quota)

    @property
    def archive_url(self):
        """Return an archive_url where available, or None."""
        if self.has_sources and not self.context.is_copy:
            return self.context.archive_url
        else:
            return None

    @property
    def archive_label(self):
        """Return either 'PPA' or 'Archive' as the label for archives.

        It is desired to use the name 'PPA' for branding reasons where
        appropriate, even though the template logic is the same (and hence
        not worth splitting off into a separate template or macro)
        """
        if self.context.is_ppa:
            return 'PPA'
        else:
            return 'archive'

    @cachedproperty
    def build_counters(self):
        """Return a dict representation of the build counters."""
        return self.context.getBuildCounters()

    @cachedproperty
    def dependencies(self):
        return list(self.context.dependencies)

    @property
    def show_dependencies(self):
        """Whether or not to present the archive-dependencies section.

        The dependencies section is presented if there are any dependency set
        or if the user has permission to change it.
        """
        can_edit = check_permission('launchpad.Edit', self.context)
        return can_edit or len(self.dependencies) > 0

    @property
    def has_disabled_dependencies(self):
        """Whether this archive has disabled archive dependencies or not.

        Although, it will be True only if the requester has permission
        to edit the context archive (i.e. if the user can do something
        about it).
        """
        disabled_dependencies = [
            archive_dependency
            for archive_dependency in self.dependencies
            if not archive_dependency.dependency.enabled]
        can_edit = check_permission('launchpad.Edit', self.context)
        return can_edit and len(disabled_dependencies) > 0

    @cachedproperty
    def package_copy_requests(self):
        """Return any package copy requests associated with this archive."""
        copy_requests = getUtility(
            IPackageCopyRequestSet).getByTargetArchive(self.context)
        return list(copy_requests)

    @property
    def disabled_warning_message(self):
        """Return an appropriate message if the archive is disabled."""
        if self.context.enabled:
            return None

        if self.context.status in (
            ArchiveStatus.DELETED, ArchiveStatus.DELETING):
            return "This %s has been deleted." % self.archive_label
        else:
            return "This %s has been disabled." % self.archive_label


class ArchiveSeriesVocabularyFactory:
    """A factory for generating vocabularies of an archive's series."""

    implements(IContextSourceBinder)

    def __call__(self, context):
        """Return a vocabulary created dynamically from the context archive.

        :param context: The context used to generate the vocabulary. This
            is passed automatically by the zope machinery. Therefore
            this factory can only be used in a class where the context is
            an IArchive.
        """
        series_terms = []
        for distroseries in context.series_with_sources:
            series_terms.append(
                SimpleTerm(distroseries, token=distroseries.name,
                           title=distroseries.displayname))
        return SimpleVocabulary(series_terms)


class SeriesFilterWidget(LaunchpadDropdownWidget):
    """Redefining default display value as 'Any series'."""
    _messageNoValue = _("any", "Any series")


class StatusFilterWidget(LaunchpadDropdownWidget):
    """Redefining default display value as 'Any status'."""
    _messageNoValue = _("any", "Any status")


class IPPAPackageFilter(Interface):
    """The interface used as the schema for the package filtering form."""
    name_filter = TextLine(
        title=_("Package name contains"), required=False)

    series_filter = Choice(
        source=ArchiveSeriesVocabularyFactory(), required=False)

    status_filter = Choice(vocabulary=SimpleVocabulary((
        SimpleTerm(active_publishing_status, 'published', 'Published'),
        SimpleTerm(inactive_publishing_status, 'superseded', 'Superseded'),
        )), required=False)


class ArchiveSourcePackageListViewBase(ArchiveViewBase, LaunchpadFormView):
    """A Form view for filtering and batching source packages."""

    schema = IPPAPackageFilter
    custom_widget('series_filter', SeriesFilterWidget)
    custom_widget('status_filter', StatusFilterWidget)

    # By default this view will not display the sources with selectable
    # checkboxes, but subclasses can override as needed.
    selectable_sources = False

    @cachedproperty
    def series_with_sources(self):
        """Cache the context's series with sources."""
        return self.context.series_with_sources

    @property
    def specified_name_filter(self):
        """Return the specified name filter if one was specified """
        requested_name_filter = self.request.query_string_params.get(
            'field.name_filter')

        if requested_name_filter and requested_name_filter[0]:
            return requested_name_filter[0]
        else:
            return None

    def getSelectedFilterValue(self, filter_name):
        """Return the selected filter or the default, given a filter name.

        This is needed because zope's form library does not consider
        query string params (GET params) during a post request.
        """
        field_name = 'field.' + filter_name
        requested_filter = self.request.query_string_params.get(field_name)

        # If an empty filter was specified, then it's explicitly
        # been set to empty - so we use None.
        if requested_filter == ['']:
            return None

        # If the requested filter is none, then we use the default.
        default_filter_attr = 'default_' + filter_name
        if requested_filter is None:
            return getattr(self, default_filter_attr)

        # If the request included a filter, try to use it - if it's
        # invalid we use the default instead.
        vocab = self.widgets[filter_name].vocabulary
        if requested_filter[0] in vocab.by_token:
            return vocab.getTermByToken(requested_filter[0]).value
        else:
            return getattr(self, default_filter_attr)

    @property
    def plain_status_filter_widget(self):
        """Render a <select> control with no <div>s around it."""
        return self.widgets['status_filter'].renderValue(
            self.getSelectedFilterValue('status_filter'))

    @property
    def plain_series_filter_widget(self):
        """Render a <select> control with no <div>s around it."""
        return self.widgets['series_filter'].renderValue(
            self.getSelectedFilterValue('series_filter'))

    @property
    def filtered_sources(self):
        """Return the source results for display after filtering."""
        return self.context.getPublishedSources(
            name=self.specified_name_filter,
            status=self.getSelectedFilterValue('status_filter'),
            distroseries=self.getSelectedFilterValue('series_filter'),
            eager_load=True)

    @property
    def default_status_filter(self):
        """Return the default status_filter value.

        Subclasses of ArchiveViewBase can override this when required.
        """
        return self.widgets['status_filter'].vocabulary.getTermByToken(
            'published').value

    @property
    def default_series_filter(self):
        """Return the default series_filter value.

        Subclasses of ArchiveViewBase can override this when required.
        """
        return None

    @cachedproperty
    def batchnav(self):
        """Return a batch navigator of the filtered sources."""
        return BatchNavigator(self.filtered_sources, self.request)

    @cachedproperty
    def batched_sources(self):
        """Return the current batch of archive source publications."""
        results = list(self.batchnav.currentBatch())
        return ArchiveSourcePublications(results)

    @cachedproperty
    def has_sources_for_display(self):
        """Whether or not the PPA has any source packages for display.

        This is after any filtering or overriding of the sources() method.
        """
        # Trying to use bool(self.filtered_sources) here resulted in bug
        # 702425 :(
        return self.filtered_sources.count() > 0


class ArchiveView(ArchiveSourcePackageListViewBase):
    """Default Archive view class.

    Implements useful actions and collects useful sets for the page template.
    """

    implements(IArchiveIndexActionsMenu)

    def initialize(self):
        """Redirect if our context is a main archive."""
        if self.context.is_main:
            self.request.response.redirect(
                canonical_url(self.context.distribution))
            return
        super(ArchiveView, self).initialize()

    @property
    def displayname_edit_widget(self):
        display_name = IArchive['displayname']
        title = "Edit the displayname"
        return TextLineEditorWidget(self.context, display_name, title, 'h1')

    @property
    def sources_list_entries(self):
        """Setup and return the source list entries widget."""
        entries = SourcesListEntries(
            self.context.distribution, self.archive_url,
            self.context.series_with_sources)
        return SourcesListEntriesView(entries, self.request)

    @property
    def default_series_filter(self):
        """Return the distroseries identified by the user-agent."""
        version_number = get_user_agent_distroseries(
            self.request.getHeader('HTTP_USER_AGENT'))

        # Check if this version is one of the available
        # distroseries for this archive:
        vocabulary = self.widgets['series_filter'].vocabulary
        for term in vocabulary:
            if (term.value is not None and
                term.value.version == version_number):
                return term.value

        # Otherwise we default to 'any'
        return None

    @property
    def archive_description_html(self):
        """The archive's description as HTML."""
        linkify_text = True
        if self.context.is_ppa:
            linkify_text = not self.context.owner.is_probationary
        archive = self.context
        description = IArchive['description']
        title = self.archive_label + " description"
        # Don't hide empty archive descriptions.  Even though the interface
        # says they are required, the model doesn't.
        return TextAreaEditorWidget(
            archive, description, title, hide_empty=False,
            linkify_text=linkify_text)

    @cachedproperty
    def latest_updates(self):
        """Return the last five published sources for this archive."""
        sources = self.context.getPublishedSources(
            status=PackagePublishingStatus.PUBLISHED)
        sources.order_by(Desc(SourcePackagePublishingHistory.datepublished))
        result_tuples = sources[:5]

        # We want to return a list of dicts for easy template rendering.
        latest_updates_list = []

        # The status.title is not presentable and the description for
        # each status is too long for use here, so define a dict of
        # concise status descriptions that will fit in a small area.
        status_names = {
            'FULLYBUILT': 'Successfully built',
            'FULLYBUILT_PENDING': 'Successfully built',
            'NEEDSBUILD': 'Waiting to build',
            'FAILEDTOBUILD': 'Failed to build:',
            'BUILDING': 'Currently building',
            'UPLOADING': 'Currently uploading',
            }

        now = datetime.now(tz=pytz.UTC)
        source_ids = [result_tuple.id for result_tuple in result_tuples]
        summaries = getUtility(
            IPublishingSet).getBuildStatusSummariesForSourceIdsAndArchive(
                source_ids, self.context)
        for source_id, status_summary in summaries.items():
            date_published = status_summary['date_published']
            source_package_name = status_summary['source_package_name']
            current_status = status_summary['status']
            duration = now - date_published

            # We'd like to include the builds in the latest updates
            # iff the build failed.
            builds = []
            if current_status == BuildSetStatus.FAILEDTOBUILD:
                builds = status_summary['builds']

            latest_updates_list.append({
                'date_published': date_published,
                'title': source_package_name,
                'status': status_names[current_status.title],
                'status_class': current_status.title,
                'duration': duration,
                'builds': builds,
                })

        latest_updates_list.sort(
            key=lambda x: x['date_published'], reverse=True)
        return latest_updates_list

    def num_updates_over_last_days(self, num_days=30):
        """Return the number of updates over the past days."""
        now = datetime.now(tz=pytz.UTC)
        created_since = now - timedelta(num_days)
        return self.context.getPublishedSources(
            created_since_date=created_since).count()

    @property
    def num_pkgs_building(self):
        """Return the number of building/waiting to build packages."""
        pkgs_building_count, pkgs_waiting_count = (
            self.context.num_pkgs_building)
        # The total is just used for conditionals in the template.
        return {
            'building': pkgs_building_count,
            'waiting': pkgs_waiting_count,
            'total': pkgs_building_count + pkgs_waiting_count,
            }


class ArchivePackagesView(ArchiveSourcePackageListViewBase):
    """Detailed packages view for an archive."""
    implements(IArchivePackagesActionMenu)

    def initialize(self):
        super(ArchivePackagesView, self).initialize()
        if self.context.private:
            # To see the +packages page, you must be an uploader, or a
            # commercial admin.
            if not check_permission('launchpad.Append', self.context):
                admins = getUtility(ILaunchpadCelebrities).commercial_admin
                if not self.user.inTeam(admins):
                    raise Unauthorized

    @property
    def page_title(self):
        return smartquote('Packages in "%s"' % self.context.displayname)

    @property
    def label(self):
        return self.page_title

    @property
    def series_list_string(self):
        """Return an English string of the distroseries."""
        return english_list(
            series.displayname for series in self.series_with_sources)

    @property
    def is_copy(self):
        """Return whether the context of this view is a copy archive."""
        # This property enables menu items to be shared between
        # context and view menues.
        return self.context.is_copy


class ArchiveSourceSelectionFormView(ArchiveSourcePackageListViewBase):
    """Base class to implement a source selection widget for PPAs."""

    custom_widget('selected_sources', LabeledMultiCheckBoxWidget)

    selectable_sources = True

    def setNextURL(self):
        """Set self.next_url based on current context.

        This should be called during actions of subclasses.
        """
        query_string = self.request.get('QUERY_STRING', '')
        if query_string:
            self.next_url = "%s?%s" % (self.request.URL, query_string)
        else:
            self.next_url = self.request.URL

    def setUpWidgets(self, context=None):
        """Setup our custom widget which depends on the filter widget values.
        """
        # To create the selected sources field, we need to define a
        # vocabulary based on the currently selected sources (using self
        # batched_sources) but this itself requires the current values of
        # the filtering widgets. So we setup the widgets, then add the
        # extra field and create its widget too.
        super(ArchiveSourceSelectionFormView, self).setUpWidgets()

        self.form_fields += self.createSelectedSourcesField()

        self.widgets += form.setUpWidgets(
            self.form_fields.select('selected_sources'),
            self.prefix, self.context, self.request,
            data=self.initial_values, ignore_request=False)

    def focusedElementScript(self):
        """Override `LaunchpadFormView`.

        Ensure focus is only set if there are sources actually presented.
        """
        if not self.has_sources_for_display:
            return ''
        return LaunchpadFormView.focusedElementScript(self)

    def createSelectedSourcesField(self):
        """Creates the 'selected_sources' field.

        'selected_sources' is a list of elements of a vocabulary based on
        the source publications that will be presented. This way zope
        infrastructure will do the validation for us.
        """
        terms = []

        for pub in self.batched_sources:
            terms.append(SimpleTerm(pub, str(pub.id), pub.displayname))
        return form.Fields(
            List(__name__='selected_sources',
                 title=_('Available sources'),
                 value_type=Choice(vocabulary=SimpleVocabulary(terms)),
                 required=False,
                 default=[],
                 description=_('Select one or more sources to be submitted '
                               'to an action.')))

    @property
    def action_url(self):
        """The forms should post to themselves, including GET params."""
        return "%s?%s" % (self.request.getURL(), self.request['QUERY_STRING'])


class IArchivePackageDeletionForm(IPPAPackageFilter):
    """Schema used to delete packages within an archive."""

    deletion_comment = TextLine(
        title=_("Deletion comment"), required=False,
        description=_("The reason why the package is being deleted."))


class ArchivePackageDeletionView(ArchiveSourceSelectionFormView):
    """Archive package deletion view class.

    This view presents a package selection slot in a POST form implementing
    a deletion action that can be performed upon a set of selected packages.
    """

    schema = IArchivePackageDeletionForm
    custom_widget('deletion_comment', StrippedTextWidget, displayWidth=50)
    label = 'Delete packages'

    @property
    def label(self):
        return 'Delete packages from %s' % self.context.displayname

    @property
    def default_status_filter(self):
        """Present records in any status by default."""
        return None

    @cachedproperty
    def filtered_sources(self):
        """Return the filtered results of publishing records for deletion.

        This overrides ArchiveViewBase.filtered_sources to use a
        different method on the context specific to deletion records.
        """
        return self.context.getSourcesForDeletion(
            name=self.specified_name_filter,
            status=self.getSelectedFilterValue('status_filter'),
            distroseries=self.getSelectedFilterValue('series_filter'))

    @cachedproperty
    def has_sources(self):
        """Whether or not this PPA has any sources before filtering.

        Overrides the ArchiveViewBase.has_sources
        to ensure that it only returns true if there are sources
        that can be deleted in this archive.
        """
        return bool(self.context.getSourcesForDeletion())

    def validate_delete(self, action, data):
        """Validate deletion parameters.

        Ensure we have, at least, one source selected and deletion_comment
        is given.
        """
        form.getWidgetsData(self.widgets, 'field', data)

        if len(data.get('selected_sources', [])) == 0:
            self.setFieldError('selected_sources', 'No sources selected.')

    @action(_("Request Deletion"), name="delete", validator="validate_delete")
    def delete_action(self, action, data):
        """Perform the deletion of the selected packages.

        The deletion will be performed upon the 'selected_sources' contents
        storing the given 'deletion_comment'.
        """
        if len(self.errors) != 0:
            return

        comment = data.get('deletion_comment')
        selected_sources = data.get('selected_sources')

        # Perform deletion of the source and its binaries.
        publishing_set = getUtility(IPublishingSet)
        publishing_set.requestDeletion(selected_sources, self.user, comment)

        # Present a page notification describing the action.
        messages = []
        messages.append(
            '<p>Source and binaries deleted by %s request:'
            % self.user.displayname)
        for source in selected_sources:
            messages.append('<br/>%s' % source.displayname)
        messages.append('</p>')
        # Replace the 'comment' content added by the user via structured(),
        # so it will be quoted appropriately.
        messages.append("<p>Deletion comment: %(comment)s</p>")

        notification = "\n".join(messages)
        self.request.response.addNotification(
            structured(notification, comment=comment))

        self.setNextURL()


class DestinationArchiveDropdownWidget(LaunchpadDropdownWidget):
    """Redefining default display value as 'This PPA'."""
    _messageNoValue = _("vocabulary-copy-to-context-ppa", "This PPA")


class DestinationSeriesDropdownWidget(LaunchpadDropdownWidget):
    """Redefining default display value as 'The same series'."""
    _messageNoValue = _("vocabulary-copy-to-same-series", "The same series")


def preload_binary_package_names(copies):
    """Preload `BinaryPackageName`s to speed up display-name construction."""
    bpn_ids = [
        copy.binarypackagerelease.binarypackagenameID for copy in copies
        if isinstance(copy, BinaryPackagePublishingHistory)]
    load(BinaryPackageName, bpn_ids)


def compose_synchronous_copy_feedback(copies, dest_archive, dest_url=None,
                                      dest_display_name=None):
    """Compose human-readable feedback after a synchronous copy."""
    if dest_url is None:
        dest_url = escape(
            canonical_url(dest_archive) + '/+packages', quote=True)

    if dest_display_name is None:
        dest_display_name = escape(dest_archive.displayname)

    if len(copies) == 0:
        return structured(
            '<p>All packages already copied to <a href="%s">%s</a>.</p>'
            % (dest_url, dest_display_name))
    else:
        messages = []
        messages.append(
            '<p>Packages copied to <a href="%s">%s</a>:</p>'
            % (dest_url, dest_display_name))
        messages.append('<ul>')
        messages.append("\n".join([
            '<li>%s</li>' % escape(copy) for copy in copies]))
        messages.append('</ul>')
        return structured("\n".join(messages))


def copy_synchronously(source_pubs, dest_archive, dest_series, dest_pocket,
                       include_binaries, dest_url=None,
                       dest_display_name=None, person=None,
                       check_permissions=True):
    """Copy packages right now.

    :return: A `structured` with human-readable feedback about the
        operation.
    :raises CannotCopy: If `check_permissions` is True and the copy is
        not permitted.
    """
    copies = do_copy(
        source_pubs, dest_archive, dest_series, dest_pocket, include_binaries,
        allow_delayed_copies=True, person=person,
        check_permissions=check_permissions)

    preload_binary_package_names(copies)

    return compose_synchronous_copy_feedback(
        [copy.displayname for copy in copies], dest_archive, dest_url,
        dest_display_name)


def copy_asynchronously(source_pubs, dest_archive, dest_series, dest_pocket,
                        include_binaries, dest_url=None,
                        dest_display_name=None, person=None,
                        check_permissions=True):
    """Schedule jobs to copy packages later.

    :return: A `structured` with human-readable feedback about the
        operation.
    :raises CannotCopy: If `check_permissions` is True and the copy is
        not permitted.
    """
    if check_permissions:
        spns = [
            spph.sourcepackagerelease.sourcepackagename
            for spph in source_pubs]
        check_copy_permissions(
            person, dest_archive, dest_series, dest_pocket, spns)

    job_source = getUtility(IPlainPackageCopyJobSource)
    for spph in source_pubs:
        job_source.create(
            spph.source_package_name, spph.archive, dest_archive, dest_series,
            dest_pocket, include_binaries=include_binaries,
            package_version=spph.sourcepackagerelease.version,
            copy_policy=PackageCopyPolicy.INSECURE)

    return structured("""
        <p>Requested sync of %s packages.</p>
        <p>Please allow some time for these to be processed.</p>
        """, len(source_pubs))


def render_cannotcopy_as_html(cannotcopy_exception):
    """Render `CannotCopy` exception as HTML for display in the page."""
    error_lines = str(cannotcopy_exception).splitlines()

    if len(error_lines) == 1:
        intro = "The following source cannot be copied:"
    else:
        intro = "The following sources cannot be copied:"

    # Produce structured HTML.  Include <li>%s</li> placeholders for
    # each error line, but have "structured" interpolate the actual
    # package names.  It will escape them as needed.
    html_text = """
        <p>%s</p>
        <ul>
        %s
        </ul>
        """ % (intro, "<li>%s</li>" * len(error_lines))
    return structured(html_text, *error_lines)


class PackageCopyingMixin:
    """A mixin class that adds helpers for package copying."""

    def canCopySynchronously(self, source_pubs):
        """Can we afford to copy `source_pubs` synchronously?"""
        # Fixed estimate: up to 100 packages can be copied in acceptable
        # time.  Anything more than that and we go async.
        limit = getFeatureFlag(FEATURE_FLAG_MAX_SYNCHRONOUS_SYNCS)
        try:
            limit = int(limit)
        except:
            limit = 100

        return len(source_pubs) <= limit

    def do_copy(self, sources_field_name, source_pubs, dest_archive,
                dest_series, dest_pocket, include_binaries,
                dest_url=None, dest_display_name=None, person=None,
                check_permissions=True, force_async=False):
        """Copy packages and add appropriate feedback to the browser page.

        This may either copy synchronously, if there are few enough
        requests to process right now; or asynchronously in which case
        it will schedule jobs that will be processed by a script.

        :param sources_field_name: The name of the form field to set errors
            on when the copy fails
        :param source_pubs: A list of SourcePackagePublishingHistory to copy
        :param dest_archive: The destination IArchive
        :param dest_series: The destination IDistroSeries
        :param dest_pocket: The destination PackagePublishingPocket
        :param include_binaries: Boolean, whether to copy binaries with the
            sources
        :param dest_url: The URL of the destination to display in the
            notification box.  Defaults to the target archive and will be
            automatically escaped for inclusion in the output.
        :param dest_display_name: The text to use for the dest_url link.
            Defaults to the target archive's display name and will be
            automatically escaped for inclusion in the output.
        :param person: The person requesting the copy.
        :param: check_permissions: boolean indicating whether or not the
            requester's permissions to copy should be checked.
        :param force_async: Force the copy to create package copy jobs and
            perform the copy asynchronously.

        :return: True if the copying worked, False otherwise.
        """
        try:
            if (force_async == False and
                    self.canCopySynchronously(source_pubs)):
                notification = copy_synchronously(
                    source_pubs, dest_archive, dest_series, dest_pocket,
                    include_binaries, dest_url=dest_url,
                    dest_display_name=dest_display_name, person=person,
                    check_permissions=check_permissions)
            else:
                notification = copy_asynchronously(
                    source_pubs, dest_archive, dest_series, dest_pocket,
                    include_binaries, dest_url=dest_url,
                    dest_display_name=dest_display_name, person=person,
                    check_permissions=check_permissions)
        except CannotCopy, error:
            self.setFieldError(
                sources_field_name, render_cannotcopy_as_html(error))
            return False

        self.request.response.addNotification(notification)
        return True


def make_archive_vocabulary(archives):
    terms = []
    for archive in archives:
        token = '%s/%s' % (archive.owner.name, archive.name)
        label = '%s (%s)' % (archive.displayname, token)
        terms.append(SimpleTerm(archive, token, label))
    return SimpleVocabulary(terms)


class ArchivePackageCopyingView(ArchiveSourceSelectionFormView,
                                PackageCopyingMixin):
    """Archive package copying view class.

    This view presents a package selection slot in a POST form implementing
    a copying action that can be performed upon a set of selected packages.
    """
    schema = IPPAPackageFilter
    custom_widget('destination_archive', DestinationArchiveDropdownWidget)
    custom_widget('destination_series', DestinationSeriesDropdownWidget)
    custom_widget('include_binaries', LaunchpadRadioWidget)
    label = 'Copy packages'

    @property
    def label(self):
        return 'Copy packages from %s' % self.context.displayname

    default_pocket = PackagePublishingPocket.RELEASE

    @property
    def default_status_filter(self):
        """Present published records by default."""
        return self.widgets['status_filter'].vocabulary.getTermByToken(
            'published').value

    def setUpFields(self):
        """Override `ArchiveSourceSelectionFormView`.

        See `createDestinationFields` method.
        """
        ArchiveSourceSelectionFormView.setUpFields(self)
        self.form_fields = (
            self.createDestinationArchiveField() +
            self.createDestinationSeriesField() +
            self.createIncludeBinariesField() +
            self.form_fields)

    @cachedproperty
    def ppas_for_user(self):
        """Return all PPAs for which the user accessing the page can copy."""
        return list(
            ppa
            for ppa in getUtility(IArchiveSet).getPPAsForUser(self.user)
            if check_permission('launchpad.Append', ppa))

    @cachedproperty
    def can_copy(self):
        """Whether or not the current user can copy packages to any PPA."""
        return len(self.ppas_for_user) > 0

    @cachedproperty
    def can_copy_to_context_ppa(self):
        """Whether or not the current user can copy to the context PPA.

        It's always False for non-PPA archives, copies to non-PPA archives
        are explicitly denied in the UI.
        """
        # XXX cprov 2009-07-17 bug=385503: copies cannot be properly traced
        # that's why we explicitly don't allow them do be done via the UI
        # in main archives, only PPAs.
        return (self.context.is_ppa and
                self.context.checkArchivePermission(self.user))

    def createDestinationArchiveField(self):
        """Create the 'destination_archive' field."""
        # Do not include the context PPA in the dropdown widget.
        ppas = [ppa for ppa in self.ppas_for_user if self.context != ppa]
        return form.Fields(
            Choice(__name__='destination_archive',
                   title=_('Destination PPA'),
                   vocabulary=make_archive_vocabulary(ppas),
                   description=_("Select the destination PPA."),
                   missing_value=self.context,
                   required=not self.can_copy_to_context_ppa))

    def createDestinationSeriesField(self):
        """Create the 'destination_series' field."""
        terms = []
        # XXX cprov 20080408: this code uses the context PPA series instead
        # of targeted or all series available in Launchpad. It might become
        # a problem when we support PPAs for other distribution. If we do
        # it will be probably simpler to use the DistroSeries vocabulary
        # and validate the selected value before copying.
        for series in self.context.distribution.series:
            if series.status == SeriesStatus.OBSOLETE:
                continue
            terms.append(
                SimpleTerm(series, str(series.name), series.displayname))
        return form.Fields(
            Choice(__name__='destination_series',
                   title=_('Destination series'),
                   vocabulary=SimpleVocabulary(terms),
                   description=_("Select the destination series."),
                   required=False))

    def createIncludeBinariesField(self):
        """Create the 'include_binaries' field.

        'include_binaries' widget is a choice, rendered as radio-buttons,
        with two options that provides a Boolean as its value:

         ||      Option     || Value ||
         || REBUILD_SOURCES || False ||
         || COPY_BINARIES   || True  ||

        When omitted in the form, this widget defaults for REBUILD_SOURCES
        option when rendered.
        """
        rebuild_sources = SimpleTerm(
                False, 'REBUILD_SOURCES', _('Rebuild the copied sources'))
        copy_binaries = SimpleTerm(
            True, 'COPY_BINARIES', _('Copy existing binaries'))
        terms = [rebuild_sources, copy_binaries]

        return form.Fields(
            Choice(__name__='include_binaries',
                   title=_('Copy options'),
                   vocabulary=SimpleVocabulary(terms),
                   description=_("How the selected sources should be copied "
                                 "to the destination archive."),
                   missing_value=rebuild_sources,
                   default=False,
                   required=True))

    @action(_("Update"), name="update")
    def update_action(self, action, data):
        """Simply re-issue the form with the new values."""
        pass

    @action(_("Copy Packages"), name="copy")
    def copy_action(self, action, data):
        """Perform the copy of the selected packages.

        Ensure that at least one source is selected. Executes `do_copy`
        for all the selected sources.

        If `do_copy` raises `CannotCopy` the error content is set as
        the 'selected_sources' field error.

        if `do_copy` succeeds, an informational messages is set containing
        the copied packages.
        """
        selected_sources = data.get('selected_sources')
        destination_archive = data.get('destination_archive')
        destination_series = data.get('destination_series')
        include_binaries = data.get('include_binaries')
        destination_pocket = self.default_pocket

        if len(selected_sources) == 0:
            self.setFieldError('selected_sources', 'No sources selected.')
            return

        # PackageCopyingMixin.do_copy() does the work of copying and
        # setting up on-page notifications.
        if self.do_copy(
            'selected_sources', selected_sources, destination_archive,
            destination_series, destination_pocket, include_binaries,
            person=self.user):
            # The copy worked so we can redirect back to the page to
            # show the result.
            self.setNextURL()


def get_escapedtext(message):
    """Return escapedtext if message is an `IStructuredString`."""
    if IStructuredString.providedBy(message):
        return message.escapedtext
    else:
        return message


class ArchiveEditDependenciesView(ArchiveViewBase, LaunchpadFormView):
    """Archive dependencies view class."""

    schema = IArchiveEditDependenciesForm

    custom_widget('selected_dependencies', PlainMultiCheckBoxWidget,
                  cssClass='line-through-when-checked ppa-dependencies')
    custom_widget('primary_dependencies', LaunchpadRadioWidget,
                  cssClass='highlight-selected')
    custom_widget('primary_components', LaunchpadRadioWidget,
                  cssClass='highlight-selected')

    label = "Edit PPA dependencies"
    page_title = label

    def initialize(self):
        self.cancel_url = canonical_url(self.context)
        self._messages = []
        LaunchpadFormView.initialize(self)

    def setUpFields(self):
        """Override `LaunchpadFormView`.

        In addition to setting schema fields, also initialize the
        'selected_dependencies' field.

        See `createSelectedSourcesField` method.
        """
        LaunchpadFormView.setUpFields(self)

        self.form_fields = (
            self.createSelectedDependenciesField() +
            self.createPrimaryDependenciesField() +
            self.createPrimaryComponentsField() +
            self.form_fields)

    def focusedElementScript(self):
        """Override `LaunchpadFormView`.

        Move focus to the 'dependency_candidate' input field when there is
        no recorded dependency to present. Otherwise it will default to
        the first recorded dependency checkbox.
        """
        if not self.has_dependencies:
            self.initial_focus_widget = "dependency_candidate"
        return LaunchpadFormView.focusedElementScript(self)

    def createSelectedDependenciesField(self):
        """Creates the 'selected_dependencies' field.

        'selected_dependencies' is a list of elements of a vocabulary
        containing all the current recorded dependencies for the context
        PPA.
        """
        terms = []
        for archive_dependency in self.context.dependencies:
            dependency = archive_dependency.dependency
            if not dependency.is_ppa:
                continue
            if check_permission('launchpad.View', dependency):
                dependency_label = structured(
                    '<a href="%s">%s</a>',
                    canonical_url(dependency), archive_dependency.title)
            else:
                dependency_label = archive_dependency.title
            dependency_token = '%s/%s' % (
                dependency.owner.name, dependency.name)
            term = SimpleTerm(
                dependency, dependency_token, dependency_label)
            terms.append(term)
        return form.Fields(
            List(__name__='selected_dependencies',
                 title=_('Extra dependencies'),
                 value_type=Choice(vocabulary=SimpleVocabulary(terms)),
                 required=False,
                 default=[],
                 description=_(
                    'Select one or more dependencies to be removed.')))

    def createPrimaryDependenciesField(self):
        """Create the 'primary_dependencies' field.

        'primary_dependency' widget is a choice, rendered as radio-buttons,
        with 5 options that provides `PackagePublishingPocket` as result:

         || Option    || Value     ||
         || Release   || RELEASE   ||
         || Security  || SECURITY  ||
         || Default   || UPDATES   ||
         || Proposed  || PROPOSED  ||
         || Backports || BACKPORTS ||

        When omitted in the form, this widget defaults for 'Default'
        option when rendered.
        """
        release = SimpleTerm(
            PackagePublishingPocket.RELEASE, 'RELEASE',
            _('Basic (only released packages).'))
        security = SimpleTerm(
            PackagePublishingPocket.SECURITY, 'SECURITY',
            _('Security (basic dependencies and important security '
              'updates).'))
        updates = SimpleTerm(
            PackagePublishingPocket.UPDATES, 'UPDATES',
            _('Default (security dependencies and recommended updates).'))
        proposed = SimpleTerm(
            PackagePublishingPocket.PROPOSED, 'PROPOSED',
            _('Proposed (default dependencies and proposed updates).'))
        backports = SimpleTerm(
            PackagePublishingPocket.BACKPORTS, 'BACKPORTS',
            _('Backports (default dependencies and unsupported updates).'))

        terms = [release, security, updates, proposed, backports]

        primary_dependency = self.context.getArchiveDependency(
            self.context.distribution.main_archive)
        if primary_dependency is None:
            default_value = default_pocket_dependency
        else:
            default_value = primary_dependency.pocket

        primary_dependency_vocabulary = SimpleVocabulary(terms)
        current_term = primary_dependency_vocabulary.getTerm(
            default_value)

        return form.Fields(
            Choice(__name__='primary_dependencies',
                   title=_(
                    "%s dependencies"
                    % self.context.distribution.displayname),
                   vocabulary=primary_dependency_vocabulary,
                   description=_(
                    "Select which packages of the %s primary archive "
                    "should be used as build-dependencies when building "
                    "sources in this PPA."
                    % self.context.distribution.displayname),
                   missing_value=current_term,
                   default=default_value,
                   required=True))

    def createPrimaryComponentsField(self):
        """Create the 'primary_components' field.

        'primary_components' widget is a choice, rendered as radio-buttons,
        with two options that provides an IComponent as its value:

         ||      Option    ||   Value    ||
         || ALL_COMPONENTS || multiverse ||
         || FOLLOW_PRIMARY ||    None    ||

        When omitted in the form, this widget defaults to 'All ubuntu
        components' option when rendered.
        """
        multiverse = getUtility(IComponentSet)['multiverse']

        all_components = SimpleTerm(
            multiverse, 'ALL_COMPONENTS',
            _('Use all %s components available.' %
              self.context.distribution.displayname))
        follow_primary = SimpleTerm(
            None, 'FOLLOW_PRIMARY',
            _('Use the same components used for each source in the %s '
              'primary archive.' % self.context.distribution.displayname))

        primary_dependency = self.context.getArchiveDependency(
            self.context.distribution.main_archive)
        if primary_dependency is None:
            default_value = getUtility(IComponentSet)[
                default_component_dependency_name]
        else:
            default_value = primary_dependency.component

        terms = [all_components, follow_primary]
        primary_components_vocabulary = SimpleVocabulary(terms)
        current_term = primary_components_vocabulary.getTerm(default_value)

        return form.Fields(
            Choice(__name__='primary_components',
                   title=_('%s components' %
                           self.context.distribution.displayname),
                   vocabulary=primary_components_vocabulary,
                   description=_("Which %s components of the archive pool "
                                 "should be used when fetching build "
                                 "dependencies." %
                                 self.context.distribution.displayname),
                   missing_value=current_term,
                   default=default_value,
                   required=True))

    @cachedproperty
    def has_dependencies(self):
        """Whether or not the PPA has recorded dependencies."""
        return bool(self.context.dependencies)

    @property
    def messages(self):
        return '\n'.join(map(get_escapedtext, self._messages))

    def _remove_dependencies(self, data):
        """Perform the removal of the selected dependencies."""
        selected_dependencies = data.get('selected_dependencies', [])

        if len(selected_dependencies) == 0:
            return

        # Perform deletion of the source and its binaries.
        for dependency in selected_dependencies:
            self.context.removeArchiveDependency(dependency)

        # Present a page notification describing the action.
        self._messages.append('<p>Dependencies removed:')
        for dependency in selected_dependencies:
            self._messages.append(
                structured('<br/>%s', dependency.displayname))
        self._messages.append('</p>')

    def _add_ppa_dependencies(self, data):
        """Record the selected dependency."""
        dependency_candidate = data.get('dependency_candidate')
        if dependency_candidate is None:
            return

        self.context.addArchiveDependency(
            dependency_candidate, PackagePublishingPocket.RELEASE,
            getUtility(IComponentSet)['main'])

        self._messages.append(structured(
            '<p>Dependency added: %s</p>', dependency_candidate.displayname))

    def _add_primary_dependencies(self, data):
        """Record the selected dependency."""
        # Received values.
        dependency_pocket = data.get('primary_dependencies')
        dependency_component = data.get('primary_components')

        # Check if the given values correspond to the default scenario
        # for the context archive.
        default_component_dependency = getUtility(IComponentSet)[
            default_component_dependency_name]
        is_default_dependency = (
            dependency_pocket == default_pocket_dependency and
            dependency_component == default_component_dependency)

        primary_dependency = self.context.getArchiveDependency(
            self.context.distribution.main_archive)

        # No action is required if there is no primary_dependency
        # override set and the given values match it.
        if primary_dependency is None and is_default_dependency:
            return

        # Similarly, no action is required if the given values match
        # the existing primary_dependency override.
        if (primary_dependency is not None and
            primary_dependency.pocket == dependency_pocket and
            primary_dependency.component == dependency_component):
            return

        # Remove any primary dependencies overrides.
        if primary_dependency is not None:
            self.context.removeArchiveDependency(
                self.context.distribution.main_archive)

        if is_default_dependency:
            self._messages.append(
                '<p>Default primary dependencies restored.</p>')
            return

        # Install the required primary archive dependency override.
        primary_dependency = self.context.addArchiveDependency(
            self.context.distribution.main_archive, dependency_pocket,
            dependency_component)
        self._messages.append(structured(
            '<p>Primary dependency added: %s</p>', primary_dependency.title))

    def validate(self, data):
        """Validate dependency configuration changes.

        Skip checks if no dependency candidate was sent in the form.

        Validate if the requested PPA dependency is sane (different than
        the context PPA and not yet registered).

        Also check if the dependency candidate is private, if so, it can
        only be set if the user has 'launchpad.View' permission on it and
        the context PPA is also private (this way P3A credentials will be
        sanitized from buildlogs).
        """
        dependency_candidate = data.get('dependency_candidate')

        if dependency_candidate is None:
            return

        if dependency_candidate == self.context:
            self.setFieldError('dependency_candidate',
                               "An archive should not depend on itself.")
            return

        if self.context.getArchiveDependency(dependency_candidate):
            self.setFieldError('dependency_candidate',
                               "This dependency is already registered.")
            return

        if not check_permission('launchpad.View', dependency_candidate):
            self.setFieldError(
                'dependency_candidate',
                "You don't have permission to use this dependency.")
            return

        if dependency_candidate.private and not self.context.private:
            self.setFieldError(
                'dependency_candidate',
                "Public PPAs cannot depend on private ones.")

    @action(_("Save"), name="save")
    def save_action(self, action, data):
        """Save dependency configuration changes.

        See `_remove_dependencies`, `_add_ppa_dependencies` and
        `_add_primary_dependencies`.

        Redirect to the same page once the form is processed, to avoid widget
        refreshing. And render a page notification with the summary of the
        changes made.
        """
        # Redirect after POST.
        self.next_url = self.request.URL

        # Process the form.
        self._add_primary_dependencies(data)
        self._add_ppa_dependencies(data)
        self._remove_dependencies(data)

        # Issue a notification if anything was changed.
        if len(self.messages) > 0:
            self.request.response.addNotification(
                structured(self.messages))


class ArchiveActivateView(LaunchpadFormView):
    """PPA activation view class."""

    schema = IArchive
    field_names = ('name', 'displayname', 'description')
    custom_widget('description', TextAreaWidget, height=3)
    custom_widget('name', PPANameWidget, label="URL")
    label = 'Activate a Personal Package Archive'
    page_title = 'Activate PPA'

    @property
    def ubuntu(self):
        return getUtility(ILaunchpadCelebrities).ubuntu

    @property
    def initial_values(self):
        """Set up default values for form fields."""
        # Suggest a default value of "ppa" for the name for the
        # first PPA activation.
        if self.context.archive is None:
            return {'name': 'ppa'}
        return {}

    def setUpFields(self):
        """Override `LaunchpadFormView`.

        Reorder the fields in a way the make more sense to users and also
        present a checkbox for acknowledging the PPA-ToS if the user is
        creating his first PPA.
        """
        LaunchpadFormView.setUpFields(self)

        if self.context.archive is None:
            accepted = Bool(
                __name__='accepted',
                title=_("I have read and accepted the PPA Terms of Use."),
                required=True, default=False)
            self.form_fields += form.Fields(accepted)

    def validate(self, data):
        """Ensure user has checked the 'accepted' checkbox."""
        if len(self.errors) > 0:
            return

        default_ppa = self.context.archive

        proposed_name = data.get('name')
        if proposed_name is None and default_ppa is not None:
            self.addError(
                'The default PPA is already activated. Please specify a '
                'name for the new PPA and resubmit the form.')

        errors = Archive.validatePPA(self.context, proposed_name)
        if errors is not None:
            self.addError(errors)

        if default_ppa is None and not data.get('accepted'):
            self.setFieldError(
                'accepted',
                "PPA Terms of Service must be accepted to activate a PPA.")

    @action(_("Activate"), name="activate")
    def save_action(self, action, data):
        """Activate a PPA and moves to its page."""

        # 'name' field is omitted from the form data for default PPAs and
        # it's dealt with by IArchive.new(), which will use the default
        # PPA name.
        name = data.get('name', None)

        # XXX cprov 2009-03-27 bug=188564: We currently only create PPAs
        # for Ubuntu distribution. PPA creation should be revisited when we
        # start supporting other distribution (debian, mainly).
        ppa = getUtility(IArchiveSet).new(
            owner=self.context, purpose=ArchivePurpose.PPA,
            distribution=self.ubuntu, name=name,
            displayname=data['displayname'], description=data['description'])

        self.next_url = canonical_url(ppa)

    @property
    def is_private_team(self):
        """Is the person a private team?

        :return: True only if visibility is PRIVATE.
        :rtype: bool
        """
        return self.context.visibility == PersonVisibility.PRIVATE


class ArchiveBuildsView(ArchiveViewBase, BuildRecordsView):
    """Build Records View for IArchive."""

    # The archive builds view presents all package builds (binary
    # or source package recipe builds).
    binary_only = False

    @property
    def default_build_state(self):
        """See `IBuildRecordsView`.

        Present NEEDSBUILD build records by default for PPAs.
        """
        return BuildStatus.NEEDSBUILD


class BaseArchiveEditView(LaunchpadEditFormView, ArchiveViewBase):

    schema = IArchive
    field_names = []

    @action(_("Save"), name="save", validator="validate_save")
    def save_action(self, action, data):
        # Archive is enabled and user wants it disabled.
        if self.context.enabled == True and data['enabled'] == False:
            self.context.disable()
        # Archive is disabled and user wants it enabled.
        if self.context.enabled == False and data['enabled'] == True:
            self.context.enable()
        # IArchive.enabled is a read-only property that cannot be set
        # directly.
        del(data['enabled'])
        self.updateContextFromData(data)
        self.next_url = canonical_url(self.context)

    @property
    def cancel_url(self):
        return canonical_url(self.context)

    def validate_save(self, action, data):
        """Default save validation does nothing."""
        pass


class ArchiveEditView(BaseArchiveEditView):

    field_names = ['displayname', 'description', 'enabled', 'publish']
    custom_widget(
        'description', TextAreaWidget, height=10, width=30)
    page_title = 'Change details'

    @property
    def label(self):
        return 'Edit %s' % self.context.displayname


class ArchiveAdminView(BaseArchiveEditView):

    field_names = ['enabled', 'private', 'commercial', 'require_virtualized',
                   'build_debug_symbols', 'buildd_secret', 'authorized_size',
                   'relative_build_score', 'external_dependencies']
    custom_widget('external_dependencies', TextAreaWidget, height=3)
    custom_widget('enabled_restricted_families', LabeledMultiCheckBoxWidget)
    page_title = 'Administer'

    @property
    def label(self):
        return 'Administer %s' % self.context.displayname

    def updateContextFromData(self, data):
        """Update context from form data.

        If the user did not specify a buildd secret but marked the
        archive as private, generate a secret for them.
        """
        if data['private'] and data['buildd_secret'] is None:
            # buildd secrets are only used by builders, autogenerate one.
            self.context.buildd_secret = create_token(16)
            del(data['buildd_secret'])
        super(ArchiveAdminView, self).updateContextFromData(data)

    def validate_save(self, action, data):
        """Validate the save action on ArchiveAdminView.

        buildd_secret can only, and must, be set for private archives.
        If the archive is private and the buildd secret is not set it will be
        generated.
        """
        form.getWidgetsData(self.widgets, 'field', data)

        if data.get('private') != self.context.private:
            # The privacy is being switched.
            if not self.context.getPublishedSources().is_empty():
                self.setFieldError(
                    'private',
                    'This archive already has published sources. It is '
                    'not possible to switch the privacy.')

        if self.owner_is_private_team and not data['private']:
            self.setFieldError(
                'private',
                'Private teams may not have public archives.')
        elif data.get('buildd_secret') is not None and not data['private']:
            self.setFieldError(
                'buildd_secret',
                'Do not specify for non-private archives')

        # Check the external_dependencies field.
        ext_deps = data.get('external_dependencies')
        if ext_deps is not None:
            errors = self.validate_external_dependencies(ext_deps)
            if len(errors) != 0:
                error_text = "\n".join(errors)
                self.setFieldError('external_dependencies', error_text)

        if data.get('commercial') is True and not data['private']:
            self.setFieldError(
                'commercial',
                'Can only set commericial for private archives.')

    def validate_external_dependencies(self, ext_deps):
        """Validate the external_dependencies field.

        :param ext_deps: The dependencies form field to check.
        """
        errors = []
        # The field can consist of multiple entries separated by
        # newlines, so process each in turn.
        for dep in ext_deps.splitlines():
            try:
                deb, url, suite, components = dep.split(" ", 3)
            except ValueError:
                errors.append(
                    "'%s' is not a complete and valid sources.list entry"
                        % dep)
                continue

            if deb != "deb":
                errors.append("%s: Must start with 'deb'" % dep)
            url_components = urlparse(url)
            if not url_components[0] or not url_components[1]:
                errors.append("%s: Invalid URL" % dep)

        return errors

    @property
    def owner_is_private_team(self):
        """Is the owner a private team?

        :return: True only if visibility is PRIVATE.
        :rtype: bool
        """
        return self.context.owner.visibility == PersonVisibility.PRIVATE

    def setUpFields(self):
        """Override `LaunchpadEditFormView`.

        See `createEnabledRestrictedFamilies` method.
        """
        super(ArchiveAdminView, self).setUpFields()
        self.form_fields += self.createEnabledRestrictedFamilies()

    def createEnabledRestrictedFamilies(self):
        """Creates the 'enabled_restricted_families' field.

        """
        terms = []
        for family in getUtility(IProcessorFamilySet).getRestricted():
            terms.append(SimpleTerm(
                family, token=family.name, title=family.title))
        old_field = IArchive['enabled_restricted_families']
        return form.Fields(
            List(__name__=old_field.__name__,
                 title=old_field.title,
                 value_type=Choice(vocabulary=SimpleVocabulary(terms)),
                 required=False,
                 description=old_field.description),
                 render_context=self.render_context)


class ArchiveDeleteView(LaunchpadFormView):
    """View class for deleting `IArchive`s"""

    schema = Interface

    @property
    def page_title(self):
        return smartquote('Delete "%s"' % self.context.displayname)

    @property
    def label(self):
        return self.page_title

    @property
    def can_be_deleted(self):
        return self.context.status not in (
            ArchiveStatus.DELETING, ArchiveStatus.DELETED)

    @property
    def waiting_for_deletion(self):
        return self.context.status == ArchiveStatus.DELETING

    @property
    def next_url(self):
        # We redirect back to the PPA owner's profile page on a
        # successful action.
        return canonical_url(self.context.owner)

    @property
    def cancel_url(self):
        return canonical_url(self.context)

    @action(_("Permanently delete PPA"), name="delete_ppa")
    def action_delete_ppa(self, action, data):
        self.context.delete(self.user)
        self.request.response.addInfoNotification(
            "Deletion of '%s' has been requested and the repository will be "
            "removed shortly." % self.context.title)