~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
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# vars() causes W0612
# pylint: disable-msg=E0611,W0212,W0612,C0322

"""Implementation classes for a Person."""

__metaclass__ = type
__all__ = [
    'AlreadyConvertedException',
    'get_recipients',
    'generate_nick',
    'IrcID',
    'IrcIDSet',
    'JabberID',
    'JabberIDSet',
    'JoinTeamEvent',
    'NicknameGenerationError',
    'Owner',
    'Person',
    'person_sort_key',
    'PersonLanguage',
    'PersonSet',
    'SSHKey',
    'SSHKeySet',
    'TeamInvitationEvent',
    'ValidPersonCache',
    'WikiName',
    'WikiNameSet',
    ]

from datetime import (
    datetime,
    timedelta,
    )
from operator import attrgetter
import random
import re
import subprocess
import weakref

from lazr.delegates import delegates
from lazr.restful.utils import get_current_browser_request
import pytz
from sqlobject import (
    BoolCol,
    ForeignKey,
    IntCol,
    SQLMultipleJoin,
    SQLObjectNotFound,
    StringCol,
    )
from sqlobject.sqlbuilder import (
    AND,
    OR,
    SQLConstant,
    )
from storm.base import Storm
from storm.expr import (
    Alias,
    And,
    Desc,
    Exists,
    In,
    Join,
    LeftJoin,
    Min,
    Not,
    Or,
    Select,
    SQL,
    Union,
    Upper,
    With,
    )
from storm.info import ClassAlias
from storm.locals import (
    Int,
    Reference,
    )
from storm.store import (
    EmptyResultSet,
    Store,
    )
from zope.component import (
    adapter,
    getUtility,
    )
from zope.component.interfaces import ComponentLookupError
from zope.event import notify
from zope.interface import (
    alsoProvides,
    classImplements,
    implementer,
    implements,
    )
from zope.lifecycleevent import ObjectCreatedEvent
from zope.publisher.interfaces import Unauthorized
from zope.security.checker import (
    canAccess,
    canWrite,
    )
from zope.security.proxy import (
    ProxyFactory,
    removeSecurityProxy,
    )

from lp import _
from lp.answers.model.questionsperson import QuestionsPersonMixin
from lp.app.interfaces.launchpad import (
    IHasIcon,
    IHasLogo,
    IHasMugshot,
    ILaunchpadCelebrities,
    )
from lp.app.validators.email import valid_email
from lp.app.validators.name import (
    sanitize_name,
    valid_name,
    )
from lp.blueprints.enums import (
    SpecificationDefinitionStatus,
    SpecificationFilter,
    SpecificationImplementationStatus,
    SpecificationSort,
    )
from lp.blueprints.model.specification import (
    HasSpecificationsMixin,
    Specification,
    )
from lp.bugs.interfaces.bugtarget import IBugTarget
from lp.bugs.interfaces.bugtask import (
    BugTaskSearchParams,
    IBugTaskSet,
    )
from lp.bugs.model.bugtarget import HasBugsBase
from lp.bugs.model.bugtask import get_related_bugtasks_search_params
from lp.bugs.model.structuralsubscription import StructuralSubscription
from lp.code.interfaces.branchcollection import IBranchCollection
from lp.code.model.hasbranches import (
    HasBranchesMixin,
    HasMergeProposalsMixin,
    HasRequestedReviewsMixin,
    )
from lp.registry.errors import (
    InvalidName,
    JoinNotAllowed,
    NameAlreadyTaken,
    PPACreationError,
    TeamSubscriptionPolicyError,
    )
from lp.registry.interfaces.codeofconduct import ISignedCodeOfConductSet
from lp.registry.interfaces.distribution import IDistribution
from lp.registry.interfaces.gpg import IGPGKeySet
from lp.registry.interfaces.irc import (
    IIrcID,
    IIrcIDSet,
    )
from lp.registry.interfaces.jabber import (
    IJabberID,
    IJabberIDSet,
    )
from lp.registry.interfaces.mailinglist import (
    IMailingListSet,
    MailingListStatus,
    PostedMessageStatus,
    PURGE_STATES,
    )
from lp.registry.interfaces.mailinglistsubscription import (
    MailingListAutoSubscribePolicy,
    )
from lp.registry.interfaces.person import (
    CLOSED_TEAM_POLICY,
    ImmutableVisibilityError,
    IPerson,
    IPersonSet,
    IPersonSettings,
    ITeam,
    OPEN_TEAM_POLICY,
    PersonalStanding,
    PersonCreationRationale,
    PersonVisibility,
    TeamMembershipRenewalPolicy,
    TeamSubscriptionPolicy,
    validate_public_person,
    validate_subscription_policy,
    )
from lp.registry.interfaces.personnotification import IPersonNotificationSet
from lp.registry.interfaces.persontransferjob import IPersonMergeJobSource
from lp.registry.interfaces.pillar import IPillarNameSet
from lp.registry.interfaces.product import IProduct
from lp.registry.interfaces.projectgroup import IProjectGroup
from lp.registry.interfaces.ssh import (
    ISSHKey,
    ISSHKeySet,
    SSHKeyAdditionError,
    SSHKeyCompromisedError,
    SSHKeyType,
    )
from lp.registry.interfaces.teammembership import (
    IJoinTeamEvent,
    ITeamInvitationEvent,
    ITeamMembershipSet,
    TeamMembershipStatus,
    )
from lp.registry.interfaces.wikiname import (
    IWikiName,
    IWikiNameSet,
    )
from lp.registry.model.codeofconduct import SignedCodeOfConduct
from lp.registry.model.karma import (
    Karma,
    KarmaAction,
    KarmaAssignedEvent,
    KarmaCache,
    KarmaCategory,
    KarmaTotalCache,
    )
from lp.registry.model.personlocation import PersonLocation
from lp.registry.model.pillar import PillarName
from lp.registry.model.sourcepackagename import SourcePackageName
from lp.registry.model.teammembership import (
    TeamMembership,
    TeamMembershipSet,
    TeamParticipation,
    )
from lp.services.config import config
from lp.services.database import postgresql
from lp.services.database.constants import UTC_NOW
from lp.services.database.datetimecol import UtcDateTimeCol
from lp.services.database.decoratedresultset import DecoratedResultSet
from lp.services.database.enumcol import EnumCol
from lp.services.database.lpstorm import (
    IMasterObject,
    IMasterStore,
    IStore,
    )
from lp.services.database.sqlbase import (
    cursor,
    quote,
    quote_like,
    SQLBase,
    sqlvalues,
    )
from lp.services.helpers import (
    ensure_unicode,
    shortlist,
    )
from lp.services.identity.interfaces.account import (
    AccountCreationRationale,
    AccountStatus,
    AccountSuspendedError,
    IAccount,
    IAccountSet,
    INACTIVE_ACCOUNT_STATUSES,
    )
from lp.services.identity.interfaces.emailaddress import (
    EmailAddressStatus,
    IEmailAddress,
    IEmailAddressSet,
    InvalidEmailAddress,
    )
from lp.services.identity.model.account import (
    Account,
    AccountPassword,
    )
from lp.services.identity.model.emailaddress import (
    EmailAddress,
    HasOwnerMixin,
    )
from lp.services.librarian.model import LibraryFileAlias
from lp.services.mail.helpers import (
    get_contact_email_addresses,
    get_email_template,
    )
from lp.services.oauth.model import (
    OAuthAccessToken,
    OAuthRequestToken,
    )
from lp.services.openid.model.openididentifier import OpenIdIdentifier
from lp.services.propertycache import (
    cachedproperty,
    get_property_cache,
    )
from lp.services.salesforce.interfaces import (
    ISalesforceVoucherProxy,
    REDEEMABLE_VOUCHER_STATUSES,
    VOUCHER_STATUSES,
    )
from lp.services.statistics.interfaces.statistic import ILaunchpadStatisticSet
from lp.services.verification.interfaces.authtoken import LoginTokenType
from lp.services.verification.interfaces.logintoken import ILoginTokenSet
from lp.services.verification.model.logintoken import LoginToken
from lp.services.webapp.dbpolicy import MasterDatabasePolicy
from lp.services.webapp.interfaces import ILaunchBag
from lp.services.worlddata.model.language import Language
from lp.soyuz.enums import (
    ArchivePurpose,
    ArchiveStatus,
    )
from lp.soyuz.interfaces.archive import IArchiveSet
from lp.soyuz.interfaces.archivepermission import IArchivePermissionSet
from lp.soyuz.interfaces.archivesubscriber import IArchiveSubscriberSet
from lp.soyuz.model.archive import Archive
from lp.soyuz.model.publishing import SourcePackagePublishingHistory
from lp.soyuz.model.sourcepackagerelease import SourcePackageRelease
from lp.translations.model.hastranslationimports import (
    HasTranslationImportsMixin,
    )


class AlreadyConvertedException(Exception):
    """Raised when an attempt to claim a team that has been claimed."""


class JoinTeamEvent:
    """See `IJoinTeamEvent`."""

    implements(IJoinTeamEvent)

    def __init__(self, person, team):
        self.person = person
        self.team = team


class TeamInvitationEvent:
    """See `IJoinTeamEvent`."""

    implements(ITeamInvitationEvent)

    def __init__(self, member, team):
        self.member = member
        self.team = team


class ValidPersonCache(SQLBase):
    """Flags if a Person is active and usable in Launchpad.

    This is readonly, as this is a view in the database.

    Note that it performs poorly at least some of the time, and if
    EmailAddress and Person are already being queried, its probably better to
    query Account directly. See bug
    https://bugs.launchpad.net/launchpad/+bug/615237 for some
    corroborating information.
    """


def validate_person_visibility(person, attr, value):
    """Validate changes in visibility.

    * Prevent teams with inconsistent connections from being made private.
    * Prevent private teams from any transition.
    """

    # Prohibit any visibility changes for private teams.  This rule is
    # recognized to be Draconian and may be relaxed in the future.
    if person.visibility == PersonVisibility.PRIVATE:
        raise ImmutableVisibilityError(
            'A private team cannot change visibility.')

    # If transitioning to a non-public visibility, check for existing
    # relationships that could leak data.
    if value != PersonVisibility.PUBLIC:
        warning = person.visibilityConsistencyWarning(value)
        if warning is not None:
            raise ImmutableVisibilityError(warning)

    return value


_person_sort_re = re.compile("(?:[^\w\s]|[\d_])", re.U)


def person_sort_key(person):
    """Identical to `person_sort_key` in the database."""
    # Strip noise out of displayname. We do not have to bother with
    # name, as we know it is just plain ascii.
    displayname = _person_sort_re.sub(u'', person.displayname.lower())
    return "%s, %s" % (displayname.strip(), person.name)


class PersonSettings(Storm):
    "The relatively rarely used settings for person (not a team)."

    implements(IPersonSettings)

    __storm_table__ = 'PersonSettings'

    personID = Int("person", default=None, primary=True)
    person = Reference(personID, "Person.id")

    selfgenerated_bugnotifications = BoolCol(notNull=True, default=False)


def readonly_settings(message, interface):
    """Make an object that disallows writes to values on the interface.

    When you write, the message is raised in a NotImplementedError.
    """
    # We will make a class that has properties for each field on the
    # interface (we expect every name on the interface to correspond to a
    # zope.schema field).  Each property will have a getter that will
    # return the interface default for that name; and it will have a
    # setter that will raise a hopefully helpful error message
    # explaining why writing is not allowed.
    # This is the setter we are going to use for every property.
    def unwritable(self, value):
        raise NotImplementedError(message)
    # This will become the dict of the class.
    data = {}
    # The interface.names() method returns the names on the interface.  If
    # "all" is True, then you will also get the names on base
    # interfaces.  That is unlikely to be needed here, but would be the
    # expected behavior if it were.
    for name in interface.names(all=True):
        # This next line is a work-around for a classic problem of
        # closures in a loop. Closures are bound (indirectly) to frame
        # locals, which are a mutable collection. Therefore, if we
        # naively make closures for each different value within a loop,
        # each closure will be bound to the value as it is at the *end
        # of the loop*. That's usually not what we want. To prevent
        # this, we make a helper function (which has its own locals)
        # that returns the actual closure we want.
        closure_maker = lambda result: lambda self: result
        # Now we make a property with the name-specific getter and the generic
        # setter, and put it in the dictionary of the class we are making.
        data[name] = property(
            closure_maker(interface[name].default), unwritable)
    # Now we have all the attributes we want.  We will make the class...
    cls = type('Unwritable' + interface.__name__, (), data)
    # ...specify that the class implements the interface that we are working
    # with...
    classImplements(cls, interface)
    # ...and return an instance.  We should only need one, since it is
    # read-only.
    return cls()

_readonly_person_settings = readonly_settings(
    'Teams do not support changing this attribute.', IPersonSettings)


class Person(
    SQLBase, HasBugsBase, HasSpecificationsMixin, HasTranslationImportsMixin,
    HasBranchesMixin, HasMergeProposalsMixin, HasRequestedReviewsMixin,
    QuestionsPersonMixin):
    """A Person."""

    implements(IPerson, IHasIcon, IHasLogo, IHasMugshot)

    def __init__(self, *args, **kwargs):
        super(Person, self).__init__(*args, **kwargs)
        # Initialize our PersonSettings object/record.
        if not self.is_team:
            # This is a Person, not a team.  Teams may want a TeamSettings
            # in the future.
            settings = PersonSettings()
            settings.person = self

    @cachedproperty
    def _person_settings(self):
        if self.is_team:
            # Teams need to provide these attributes for reading in order for
            # things like snapshots to work, but they are not actually
            # pertinent to teams, so they are not actually implemented for
            # writes.
            return _readonly_person_settings
        else:
            # This is a person.
            return IStore(PersonSettings).find(
                PersonSettings,
                PersonSettings.person == self).one()

    delegates(IPersonSettings, context='_person_settings')

    sortingColumns = SQLConstant(
        "person_sort_key(Person.displayname, Person.name)")
    # Redefine the default ordering into Storm syntax.
    _storm_sortingColumns = ('Person.displayname', 'Person.name')
    # When doing any sort of set operations (union, intersect, except_) with
    # SQLObject we can't use sortingColumns because the table name Person is
    # not available in that context, so we use this one.
    _sortingColumnsForSetOperations = SQLConstant(
        "person_sort_key(displayname, name)")
    _defaultOrder = sortingColumns
    _visibility_warning_marker = object()
    _visibility_warning_cache = _visibility_warning_marker

    account = ForeignKey(dbName='account', foreignKey='Account', default=None)

    def _validate_name(self, attr, value):
        """Check that rename is allowed."""
        # Renaming a team is prohibited for any team that has a non-purged
        # mailing list.  This is because renaming a mailing list is not
        # trivial in Mailman 2.1 (see Mailman FAQ item 4.70).  We prohibit
        # such renames in the team edit details view, but just to be safe, we
        # also assert that such an attempt is not being made here.  To do
        # this, we must override the SQLObject method for setting the 'name'
        # database column.  Watch out for when SQLObject is creating this row,
        # because in that case self.name isn't yet available.
        if self.name is None:
            mailing_list = None
        else:
            mailing_list = getUtility(IMailingListSet).get(self.name)
        can_rename = (self._SO_creating or
                      not self.is_team or
                      mailing_list is None or
                      mailing_list.status == MailingListStatus.PURGED)
        assert can_rename, 'Cannot rename teams with mailing lists'
        # Everything's okay, so let SQLObject do the normal thing.
        return value

    name = StringCol(dbName='name', alternateID=True, notNull=True,
                     storm_validator=_validate_name)

    def __repr__(self):
        displayname = self.displayname.encode('ASCII', 'backslashreplace')
        return '<Person at 0x%x %s (%s)>' % (id(self), self.name, displayname)

    displayname = StringCol(dbName='displayname', notNull=True)

    teamdescription = StringCol(dbName='teamdescription', default=None)
    homepage_content = StringCol(default=None)
    icon = ForeignKey(
        dbName='icon', foreignKey='LibraryFileAlias', default=None)
    logo = ForeignKey(
        dbName='logo', foreignKey='LibraryFileAlias', default=None)
    mugshot = ForeignKey(
        dbName='mugshot', foreignKey='LibraryFileAlias', default=None)

    def _get_password(self):
        # We have to remove the security proxy because the password is
        # needed before we are authenticated. I'm not overly worried because
        # this method is scheduled for demolition -- StuartBishop 20080514
        password = IStore(AccountPassword).find(
            AccountPassword, accountID=self.accountID).one()
        if password is None:
            return None
        else:
            return password.password

    def _set_password(self, value):
        account = IMasterStore(Account).get(Account, self.accountID)
        assert account is not None, 'No account for this Person.'
        account.password = value

    password = property(_get_password, _set_password)

    def _get_account_status(self):
        account = IStore(Account).get(Account, self.accountID)
        if account is not None:
            return account.status
        else:
            return AccountStatus.NOACCOUNT

    def _set_account_status(self, value):
        assert self.accountID is not None, 'No account for this Person'
        account = IMasterStore(Account).get(Account, self.accountID)
        account.status = value

    # Deprecated - this value has moved to the Account table.
    # We provide this shim for backwards compatibility.
    account_status = property(_get_account_status, _set_account_status)

    def _get_account_status_comment(self):
        account = IStore(Account).get(Account, self.accountID)
        if account is not None:
            return account.status_comment

    def _set_account_status_comment(self, value):
        assert self.accountID is not None, 'No account for this Person'
        account = IMasterStore(Account).get(Account, self.accountID)
        account.status_comment = value

    # Deprecated - this value has moved to the Account table.
    # We provide this shim for backwards compatibility.
    account_status_comment = property(
            _get_account_status_comment, _set_account_status_comment)

    teamowner = ForeignKey(dbName='teamowner', foreignKey='Person',
                           default=None,
                           storm_validator=validate_public_person)

    sshkeys = SQLMultipleJoin('SSHKey', joinColumn='person')

    renewal_policy = EnumCol(
        enum=TeamMembershipRenewalPolicy,
        default=TeamMembershipRenewalPolicy.NONE)
    subscriptionpolicy = EnumCol(
        dbName='subscriptionpolicy',
        enum=TeamSubscriptionPolicy,
        default=TeamSubscriptionPolicy.MODERATED,
        storm_validator=validate_subscription_policy)
    defaultrenewalperiod = IntCol(dbName='defaultrenewalperiod', default=None)
    defaultmembershipperiod = IntCol(dbName='defaultmembershipperiod',
                                     default=None)
    mailing_list_auto_subscribe_policy = EnumCol(
        enum=MailingListAutoSubscribePolicy,
        default=MailingListAutoSubscribePolicy.ON_REGISTRATION)

    merged = ForeignKey(dbName='merged', foreignKey='Person', default=None)

    datecreated = UtcDateTimeCol(notNull=True, default=UTC_NOW)
    creation_rationale = EnumCol(enum=PersonCreationRationale, default=None)
    creation_comment = StringCol(default=None)
    registrant = ForeignKey(
        dbName='registrant', foreignKey='Person', default=None,
        storm_validator=validate_public_person)
    hide_email_addresses = BoolCol(notNull=True, default=False)
    verbose_bugnotifications = BoolCol(notNull=True, default=True)

    signedcocs = SQLMultipleJoin('SignedCodeOfConduct', joinColumn='owner')
    _ircnicknames = SQLMultipleJoin('IrcID', joinColumn='person')
    jabberids = SQLMultipleJoin('JabberID', joinColumn='person')

    entitlements = SQLMultipleJoin('Entitlement', joinColumn='person')
    visibility = EnumCol(
        enum=PersonVisibility,
        default=PersonVisibility.PUBLIC,
        storm_validator=validate_person_visibility)

    personal_standing = EnumCol(
        enum=PersonalStanding, default=PersonalStanding.UNKNOWN,
        notNull=True)

    personal_standing_reason = StringCol(default=None)

    @cachedproperty
    def ircnicknames(self):
        return list(self._ircnicknames)

    @cachedproperty
    def languages(self):
        """See `IPerson`."""
        results = Store.of(self).find(
            Language, And(Language.id == PersonLanguage.languageID,
                          PersonLanguage.personID == self.id))
        results.order_by(Language.englishname)
        return list(results)

    def getLanguagesCache(self):
        """Return this person's cached languages.

        :raises AttributeError: If the cache doesn't exist.
        """
        return get_property_cache(self).languages

    def setLanguagesCache(self, languages):
        """Set this person's cached languages.

        Order them by name if necessary.
        """
        get_property_cache(self).languages = sorted(
            languages, key=attrgetter('englishname'))

    def deleteLanguagesCache(self):
        """Delete this person's cached languages, if it exists."""
        del get_property_cache(self).languages

    def addLanguage(self, language):
        """See `IPerson`."""
        person_language = Store.of(self).find(
            PersonLanguage, And(PersonLanguage.languageID == language.id,
                                PersonLanguage.personID == self.id)).one()
        if person_language is not None:
            # Nothing to do.
            return
        PersonLanguage(person=self, language=language)
        self.deleteLanguagesCache()

    def removeLanguage(self, language):
        """See `IPerson`."""
        person_language = Store.of(self).find(
            PersonLanguage, And(PersonLanguage.languageID == language.id,
                                PersonLanguage.personID == self.id)).one()
        if person_language is None:
            # Nothing to do.
            return
        PersonLanguage.delete(person_language.id)
        self.deleteLanguagesCache()

    def _init(self, *args, **kw):
        """Mark the person as a team when created or fetched from database."""
        SQLBase._init(self, *args, **kw)
        if self.teamownerID is not None:
            alsoProvides(self, ITeam)

    def convertToTeam(self, team_owner):
        """See `IPerson`."""
        if self.is_team:
            raise AlreadyConvertedException(
                "%s has already been converted to a team." % self.name)
        assert self.account_status == AccountStatus.NOACCOUNT, (
            "Only Person entries whose account_status is NOACCOUNT can be "
            "converted into teams.")
        # Teams don't have Account records
        if self.account is not None:
            account_id = self.account.id
            self.account = None
            Account.delete(account_id)
        self.creation_rationale = None
        self.teamowner = team_owner
        alsoProvides(self, ITeam)
        # Add the owner as a team admin manually because we know what we're
        # doing and we don't want any email notifications to be sent.
        TeamMembershipSet().new(
            team_owner, self, TeamMembershipStatus.ADMIN, team_owner)

    @property
    def oauth_access_tokens(self):
        """See `IPerson`."""
        return Store.of(self).find(
            OAuthAccessToken,
            OAuthAccessToken.person == self,
            Or(OAuthAccessToken.date_expires == None,
               OAuthAccessToken.date_expires > UTC_NOW))

    @property
    def oauth_request_tokens(self):
        """See `IPerson`."""
        return Store.of(self).find(
            OAuthRequestToken,
            OAuthRequestToken.person == self,
            Or(OAuthRequestToken.date_expires == None,
               OAuthRequestToken.date_expires > UTC_NOW))

    @cachedproperty
    def location(self):
        """See `IObjectWithLocation`."""
        return PersonLocation.selectOneBy(person=self)

    @property
    def time_zone(self):
        """See `IHasLocation`."""
        if self.location is None:
            return None
        # Wrap the location with a security proxy to make sure the user has
        # enough rights to see it.
        return ProxyFactory(self.location).time_zone

    @property
    def latitude(self):
        """See `IHasLocation`."""
        if self.location is None:
            return None
        # Wrap the location with a security proxy to make sure the user has
        # enough rights to see it.
        return ProxyFactory(self.location).latitude

    @property
    def longitude(self):
        """See `IHasLocation`."""
        if self.location is None:
            return None
        # Wrap the location with a security proxy to make sure the user has
        # enough rights to see it.
        return ProxyFactory(self.location).longitude

    def setLocationVisibility(self, visible):
        """See `ISetLocation`."""
        assert not self.is_team, 'Cannot edit team location.'
        if self.location is None:
            get_property_cache(self).location = PersonLocation(
                person=self, visible=visible)
        else:
            self.location.visible = visible

    def setLocation(self, latitude, longitude, time_zone, user):
        """See `ISetLocation`."""
        assert not self.is_team, 'Cannot edit team location.'
        assert ((latitude is None and longitude is None) or
                (latitude is not None and longitude is not None)), (
            "Cannot set a latitude without longitude (and vice-versa).")

        if self.location is not None:
            self.location.time_zone = time_zone
            self.location.latitude = latitude
            self.location.longitude = longitude
            self.location.last_modified_by = user
            self.location.date_last_modified = UTC_NOW
        else:
            get_property_cache(self).location = PersonLocation(
                person=self, time_zone=time_zone, latitude=latitude,
                longitude=longitude, last_modified_by=user)

    # specification-related joins
    @property
    def assigned_specs(self):
        return shortlist(Specification.selectBy(
            assignee=self, orderBy=['-datecreated']))

    @property
    def assigned_specs_in_progress(self):
        replacements = sqlvalues(assignee=self)
        replacements['started_clause'] = Specification.started_clause
        replacements['completed_clause'] = Specification.completeness_clause
        query = """
            (assignee = %(assignee)s)
            AND (%(started_clause)s)
            AND NOT (%(completed_clause)s)
            """ % replacements
        return Specification.select(query, orderBy=['-date_started'], limit=5)

    @property
    def unique_displayname(self):
        """See `IPerson`."""
        return "%s (%s)" % (self.displayname, self.name)

    @property
    def has_any_specifications(self):
        """See `IHasSpecifications`."""
        return self.all_specifications.count()

    @property
    def all_specifications(self):
        return self.specifications(filter=[SpecificationFilter.ALL])

    @property
    def valid_specifications(self):
        return self.specifications(filter=[SpecificationFilter.VALID])

    def specifications(self, sort=None, quantity=None, filter=None,
                       prejoin_people=True):
        """See `IHasSpecifications`."""

        # Make a new list of the filter, so that we do not mutate what we
        # were passed as a filter
        if not filter:
            # if no filter was passed (None or []) then we must decide the
            # default filtering, and for a person we want related incomplete
            # specs
            filter = [SpecificationFilter.INCOMPLETE]

        # now look at the filter and fill in the unsaid bits

        # defaults for completeness: if nothing is said about completeness
        # then we want to show INCOMPLETE
        completeness = False
        for option in [
            SpecificationFilter.COMPLETE,
            SpecificationFilter.INCOMPLETE]:
            if option in filter:
                completeness = True
        if completeness is False:
            filter.append(SpecificationFilter.INCOMPLETE)

        # defaults for acceptance: in this case we have nothing to do
        # because specs are not accepted/declined against a person

        # defaults for informationalness: we don't have to do anything
        # because the default if nothing is said is ANY

        # if no roles are given then we want everything
        linked = False
        roles = set([
            SpecificationFilter.CREATOR,
            SpecificationFilter.ASSIGNEE,
            SpecificationFilter.DRAFTER,
            SpecificationFilter.APPROVER,
            SpecificationFilter.FEEDBACK,
            SpecificationFilter.SUBSCRIBER])
        for role in roles:
            if role in filter:
                linked = True
        if not linked:
            for role in roles:
                filter.append(role)

        # sort by priority descending, by default
        if sort is None or sort == SpecificationSort.PRIORITY:
            order = ['-priority', 'Specification.definition_status',
                     'Specification.name']
        elif sort == SpecificationSort.DATE:
            order = ['-Specification.datecreated', 'Specification.id']

        # figure out what set of specifications we are interested in. for
        # products, we need to be able to filter on the basis of:
        #
        #  - role (owner, drafter, approver, subscriber, assignee etc)
        #  - completeness.
        #  - informational.
        #

        # in this case the "base" is quite complicated because it is
        # determined by the roles so lets do that first

        base = '(1=0'  # we want to start with a FALSE and OR them
        if SpecificationFilter.CREATOR in filter:
            base += ' OR Specification.owner = %(my_id)d'
        if SpecificationFilter.ASSIGNEE in filter:
            base += ' OR Specification.assignee = %(my_id)d'
        if SpecificationFilter.DRAFTER in filter:
            base += ' OR Specification.drafter = %(my_id)d'
        if SpecificationFilter.APPROVER in filter:
            base += ' OR Specification.approver = %(my_id)d'
        if SpecificationFilter.SUBSCRIBER in filter:
            base += """ OR Specification.id in
                (SELECT specification FROM SpecificationSubscription
                 WHERE person = %(my_id)d)"""
        if SpecificationFilter.FEEDBACK in filter:
            base += """ OR Specification.id in
                (SELECT specification FROM SpecificationFeedback
                 WHERE reviewer = %(my_id)d)"""
        base += ') '

        # filter out specs on inactive products
        base += """AND (Specification.product IS NULL OR
                        Specification.product NOT IN
                         (SELECT Product.id FROM Product
                          WHERE Product.active IS FALSE))
                """

        base = base % {'my_id': self.id}

        query = base
        # look for informational specs
        if SpecificationFilter.INFORMATIONAL in filter:
            query += (' AND Specification.implementation_status = %s' %
                quote(SpecificationImplementationStatus.INFORMATIONAL))

        # filter based on completion. see the implementation of
        # Specification.is_complete() for more details
        completeness = Specification.completeness_clause

        if SpecificationFilter.COMPLETE in filter:
            query += ' AND ( %s ) ' % completeness
        elif SpecificationFilter.INCOMPLETE in filter:
            query += ' AND NOT ( %s ) ' % completeness

        # Filter for validity. If we want valid specs only then we should
        # exclude all OBSOLETE or SUPERSEDED specs
        if SpecificationFilter.VALID in filter:
            query += (
                ' AND Specification.definition_status NOT IN ( %s, ''%s ) ' %
                sqlvalues(SpecificationDefinitionStatus.OBSOLETE,
                          SpecificationDefinitionStatus.SUPERSEDED))

        # ALL is the trump card
        if SpecificationFilter.ALL in filter:
            query = base

        # Filter for specification text
        for constraint in filter:
            if isinstance(constraint, basestring):
                # a string in the filter is a text search filter
                query += ' AND Specification.fti @@ ftq(%s) ' % quote(
                    constraint)

        results = Specification.select(query, orderBy=order,
            limit=quantity)
        if prejoin_people:
            results = results.prejoin(['assignee', 'approver', 'drafter'])
        return results

    # XXX: Tom Berger 2008-04-14 bug=191799:
    # The implementation of these functions
    # is no longer appropriate, since it now relies on subscriptions,
    # rather than package bug supervisors.
    def getBugSubscriberPackages(self):
        """See `IPerson`."""
        # Avoid circular imports.
        from lp.registry.model.distributionsourcepackage import (
            DistributionSourcePackage,
            )
        from lp.registry.model.distribution import Distribution
        origin = (
            StructuralSubscription,
            Join(
                Distribution,
                StructuralSubscription.distributionID == Distribution.id),
            Join(
                SourcePackageName,
                StructuralSubscription.sourcepackagenameID ==
                    SourcePackageName.id)
            )
        result = Store.of(self).using(*origin).find(
            (Distribution, SourcePackageName),
            StructuralSubscription.subscriberID == self.id)
        result.order_by(SourcePackageName.name)

        def decorator(row):
            return DistributionSourcePackage(*row)

        return DecoratedResultSet(result, decorator)

    def findPathToTeam(self, team):
        """See `IPerson`."""
        # This is our guarantee that _getDirectMemberIParticipateIn() will
        # never return None
        assert self.hasParticipationEntryFor(team), (
            "%s doesn't seem to be a member/participant in %s"
            % (self.name, team.name))
        assert team.is_team, "You can't pass a person to this method."
        path = [team]
        team = self._getDirectMemberIParticipateIn(team)
        while team != self:
            path.insert(0, team)
            team = self._getDirectMemberIParticipateIn(team)
        return path

    def _getDirectMemberIParticipateIn(self, team):
        """Return a direct member of the given team that this person
        participates in.

        If there are more than one direct member of the given team that this
        person participates in, the one with the oldest creation date is
        returned.
        """
        query = AND(
            TeamMembership.q.teamID == team.id,
            TeamMembership.q.personID == Person.q.id,
            OR(TeamMembership.q.status == TeamMembershipStatus.ADMIN,
               TeamMembership.q.status == TeamMembershipStatus.APPROVED),
            TeamParticipation.q.teamID == Person.q.id,
            TeamParticipation.q.personID == self.id)
        clauseTables = ['TeamMembership', 'TeamParticipation']
        member = Person.selectFirst(
            query, clauseTables=clauseTables, orderBy='datecreated')
        assert member is not None, (
            "%(person)s is an indirect member of %(team)s but %(person)s "
            "is not a participant in any direct member of %(team)s"
            % dict(person=self.name, team=team.name))
        return member

    @property
    def is_team(self):
        """See `IPerson`."""
        return self.teamownerID is not None

    @property
    def mailing_list(self):
        """See `IPerson`."""
        return getUtility(IMailingListSet).get(self.name)

    def _customizeSearchParams(self, search_params):
        """No-op, to satisfy a requirement of HasBugsBase."""
        pass

    def searchTasks(self, search_params, *args, **kwargs):
        """See `IHasBugs`."""
        prejoins = kwargs.pop('prejoins', [])
        if search_params is None and len(args) == 0:
            # this method is called via webapi directly
            # calling this method on a Person object directly via the
            # webservice API means searching for user related tasks
            user = kwargs.pop('user')
            search_params = get_related_bugtasks_search_params(
                user, self, **kwargs)
            return getUtility(IBugTaskSet).search(
                *search_params, prejoins=prejoins)
        if len(kwargs) > 0:
            # if keyword arguments are supplied, use the deault
            # implementation in HasBugsBase.
            return HasBugsBase.searchTasks(
                self, search_params, prejoins=prejoins, **kwargs)
        else:
            # Otherwise pass all positional arguments to the
            # implementation in BugTaskSet.
            return getUtility(IBugTaskSet).search(
                search_params, *args, prejoins=prejoins)

    def getProjectsAndCategoriesContributedTo(self, limit=5):
        """See `IPerson`."""
        contributions = []
        # Pillars names have no concept of active. Extra pillars names are
        # requested because deactivated pillars will be filtered out.
        extra_limit = limit + 5
        results = self._getProjectsWithTheMostKarma(limit=extra_limit)
        for pillar_name, karma in results:
            pillar = getUtility(IPillarNameSet).getByName(
                pillar_name, ignore_inactive=True)
            if pillar is not None:
                contributions.append(
                    {'project': pillar,
                     'categories': self._getContributedCategories(pillar)})
            if len(contributions) == limit:
                break
        return contributions

    def _getProjectsWithTheMostKarma(self, limit=10):
        """Return the names and karma points of of this person on the
        product/distribution with that name.

        The results are ordered descending by the karma points and limited to
        the given limit.
        """
        # We want this person's total karma on a given context (that is,
        # across all different categories) here; that's why we use a
        # "KarmaCache.category IS NULL" clause here.
        query = """
            SELECT PillarName.name, KarmaCache.karmavalue
            FROM KarmaCache
            JOIN PillarName ON
                COALESCE(KarmaCache.distribution, -1) =
                COALESCE(PillarName.distribution, -1)
                AND
                COALESCE(KarmaCache.product, -1) =
                COALESCE(PillarName.product, -1)
            WHERE person = %(person)s
                AND KarmaCache.category IS NULL
                AND KarmaCache.project IS NULL
            ORDER BY karmavalue DESC, name
            LIMIT %(limit)s;
            """ % sqlvalues(person=self, limit=limit)
        cur = cursor()
        cur.execute(query)
        return cur.fetchall()

    def getOwnedOrDrivenPillars(self):
        """See `IPerson`."""
        find_spec = (PillarName, SQL('kind'), SQL('displayname'))
        origin = SQL("""
            PillarName
            JOIN (
                SELECT name, 3 as kind, displayname
                FROM product
                WHERE
                    active = True AND
                    (driver = %(person)s
                    OR owner = %(person)s)
                UNION
                SELECT name, 2 as kind, displayname
                FROM project
                WHERE
                    active = True AND
                    (driver = %(person)s
                    OR owner = %(person)s)
                UNION
                SELECT name, 1 as kind, displayname
                FROM distribution
                WHERE
                    driver = %(person)s
                    OR owner = %(person)s
                ) _pillar
                ON PillarName.name = _pillar.name
            """ % sqlvalues(person=self))
        results = IStore(self).using(origin).find(find_spec)
        results = results.order_by('kind', 'displayname')

        def get_pillar_name(result):
            pillar_name, kind, displayname = result
            return pillar_name

        return DecoratedResultSet(results, get_pillar_name)

    def getOwnedProjects(self, match_name=None):
        """See `IPerson`."""
        # Import here to work around a circular import problem.
        from lp.registry.model.product import Product

        clauses = ["""
            SELECT DISTINCT Product.id
            FROM Product, TeamParticipation
            WHERE TeamParticipation.person = %(person)s
            AND owner = TeamParticipation.team
            AND Product.active IS TRUE
            """ % sqlvalues(person=self)]

        # We only want to use the extra query if match_name is not None and it
        # is not the empty string ('' or u'').
        if match_name:
            like_query = "'%%' || %s || '%%'" % quote_like(match_name)
            quoted_query = quote(match_name)
            clauses.append(
                """(Product.name LIKE %s OR
                    Product.displayname LIKE %s OR
                    fti @@ ftq(%s))""" % (like_query,
                                          like_query,
                                          quoted_query))
        query = " AND ".join(clauses)
        results = Product.select("""id IN (%s)""" % query,
                                 orderBy=['displayname'])
        return results

    def isAnyPillarOwner(self):
        """See IPerson."""

        with_sql = [
            With("teams", SQL("""
                 SELECT team FROM TeamParticipation
                 WHERE TeamParticipation.person = %d
                """ % self.id)),
            With("owned_entities", SQL("""
                 SELECT Product.id
                 FROM Product
                 WHERE Product.owner IN (SELECT team FROM teams)
                 UNION ALL
                 SELECT Project.id
                 FROM Project
                 WHERE Project.owner IN (SELECT team FROM teams)
                 UNION ALL
                 SELECT Distribution.id
                 FROM Distribution
                 WHERE Distribution.owner IN (SELECT team FROM teams)
                """))
           ]
        store = IStore(self)
        rs = store.with_(with_sql).using("owned_entities").find(
            SQL("count(*) > 0"),
        )
        return rs.one()

    def isAnySecurityContact(self):
        """See IPerson."""
        with_sql = [
            With("teams", SQL("""
                 SELECT team FROM TeamParticipation
                 WHERE TeamParticipation.person = %d
                """ % self.id)),
            With("owned_entities", SQL("""
                 SELECT Product.id
                 FROM Product
                 WHERE Product.security_contact IN (SELECT team FROM teams)
                 UNION ALL
                 SELECT Distribution.id
                 FROM Distribution
                 WHERE Distribution.security_contact
                    IN (SELECT team FROM teams)
                """))
           ]
        store = IStore(self)
        rs = store.with_(with_sql).using("owned_entities").find(
            SQL("count(*) > 0"),
        )
        return rs.one()

    def getAllCommercialSubscriptionVouchers(self, voucher_proxy=None):
        """See `IPerson`."""
        if voucher_proxy is None:
            voucher_proxy = getUtility(ISalesforceVoucherProxy)
        commercial_vouchers = voucher_proxy.getAllVouchers(self)
        vouchers = {}
        for status in VOUCHER_STATUSES:
            vouchers[status] = []
        for voucher in commercial_vouchers:
            assert voucher.status in VOUCHER_STATUSES, (
                "Voucher %s has unrecognized status %s" %
                (voucher.voucher_id, voucher.status))
            vouchers[voucher.status].append(voucher)
        return vouchers

    def getRedeemableCommercialSubscriptionVouchers(self, voucher_proxy=None):
        """See `IPerson`."""
        if voucher_proxy is None:
            voucher_proxy = getUtility(ISalesforceVoucherProxy)
        vouchers = voucher_proxy.getUnredeemedVouchers(self)
        for voucher in vouchers:
            assert voucher.status in REDEEMABLE_VOUCHER_STATUSES, (
                "Voucher %s has invalid status %s" %
                (voucher.voucher_id, voucher.status))
        return vouchers

    def iterTopProjectsContributedTo(self, limit=10):
        getByName = getUtility(IPillarNameSet).getByName
        for name, ignored in self._getProjectsWithTheMostKarma(limit=limit):
            yield getByName(name)

    def _getContributedCategories(self, pillar):
        """Return the KarmaCategories to which this person has karma on the
        given pillar.

        The given pillar must be either an IProduct or an IDistribution.
        """
        if IProduct.providedBy(pillar):
            where_clause = "product = %s" % sqlvalues(pillar)
        elif IDistribution.providedBy(pillar):
            where_clause = "distribution = %s" % sqlvalues(pillar)
        else:
            raise AssertionError(
                "Pillar must be a product or distro, got %s" % pillar)
        replacements = sqlvalues(person=self)
        replacements['where_clause'] = where_clause
        query = """
            SELECT DISTINCT KarmaCategory.id
            FROM KarmaCategory
            JOIN KarmaCache ON KarmaCache.category = KarmaCategory.id
            WHERE %(where_clause)s
                AND category IS NOT NULL
                AND person = %(person)s
            """ % replacements
        cur = cursor()
        cur.execute(query)
        ids = ",".join(str(id) for [id] in cur.fetchall())
        return KarmaCategory.select("id IN (%s)" % ids)

    @property
    def karma_category_caches(self):
        """See `IPerson`."""
        store = Store.of(self)
        conditions = And(
            KarmaCache.category == KarmaCategory.id,
            KarmaCache.person == self.id,
            KarmaCache.product == None,
            KarmaCache.project == None,
            KarmaCache.distribution == None,
            KarmaCache.sourcepackagename == None)
        result = store.find((KarmaCache, KarmaCategory), conditions)
        result = result.order_by(KarmaCategory.title)
        return [karma_cache for (karma_cache, category) in result]

    @cachedproperty
    def karma(self):
        """See `IPerson`."""
        # May also be loaded from _members
        cache = KarmaTotalCache.selectOneBy(person=self)
        if cache is None:
            # Newly created accounts may not be in the cache yet, meaning the
            # karma updater script hasn't run since the account was created.
            return 0
        else:
            return cache.karma_total

    @property
    def is_valid_person_or_team(self):
        """See `IPerson`."""
        # Teams are always valid
        if self.is_team:
            return True

        return self.is_valid_person

    @cachedproperty
    def is_valid_person(self):
        """See `IPerson`."""
        # This is prepopulated by various queries in and out of person.py.
        if self.is_team:
            return False
        try:
            ValidPersonCache.get(self.id)
            return True
        except SQLObjectNotFound:
            return False

    @property
    def is_probationary(self):
        """See `IPerson`.

        Users without karma have not demostrated their intentions may not
        have the same privileges as users who have made contributions.
        """
        return not self.is_team and self.karma == 0

    def assignKarma(self, action_name, product=None, distribution=None,
                    sourcepackagename=None, datecreated=None):
        """See `IPerson`."""
        # Teams don't get Karma. Inactive accounts don't get Karma.
        # The system user and janitor, does not get karma.
        # No warning, as we don't want to place the burden on callsites
        # to check this.
        if (not self.is_valid_person
            or self.id == getUtility(ILaunchpadCelebrities).janitor.id):
            return None

        if product is not None:
            assert distribution is None and sourcepackagename is None
        elif distribution is not None:
            assert product is None
        else:
            raise AssertionError(
                'You must provide either a product or a distribution.')

        try:
            action = KarmaAction.byName(action_name)
        except SQLObjectNotFound:
            raise AssertionError(
                "No KarmaAction found with name '%s'." % action_name)

        if datecreated is None:
            datecreated = UTC_NOW
        karma = Karma(
            person=self, action=action, product=product,
            distribution=distribution, sourcepackagename=sourcepackagename,
            datecreated=datecreated)
        notify(KarmaAssignedEvent(self, karma))
        return karma

    def latestKarma(self, quantity=25):
        """See `IPerson`."""
        return Karma.selectBy(person=self,
            orderBy='-datecreated')[:quantity]

    # This is to cache TeamParticipation information as that's used tons of
    # times in each request.
    _inTeam_cache = None

    def inTeam(self, team):
        """See `IPerson`."""
        if team is None:
            return False

        # Translate the team name to an ITeam if we were passed a team.
        if isinstance(team, (str, unicode)):
            team = PersonSet().getByName(team)
            if team is None:
                # No team, no membership.
                return False

        if self.id == team.id:
            # A team is always a member of itself.
            return True

        if not team.is_team:
            # It is possible that this team is really a user since teams
            # are users are often interchangable.
            return False

        if self._inTeam_cache is None:
            # Initialize cache
            self._inTeam_cache = {}
        else:
            # Retun from cache or fall through.
            try:
                return self._inTeam_cache[team.id]
            except KeyError:
                pass

        tp = TeamParticipation.selectOneBy(team=team, person=self)
        in_team = tp is not None
        self._inTeam_cache[team.id] = in_team
        return in_team

    def hasParticipationEntryFor(self, team):
        """See `IPerson`."""
        return bool(TeamParticipation.selectOneBy(person=self, team=team))

    def leave(self, team):
        """See `IPerson`."""
        assert not ITeam.providedBy(self)
        self.retractTeamMembership(team, self)

    def join(self, team, requester=None, may_subscribe_to_list=True):
        """See `IPerson`."""
        if self in team.activemembers:
            return

        if requester is None:
            assert not self.is_team, (
                "You need to specify a reviewer when a team joins another.")
            requester = self

        proposed = TeamMembershipStatus.PROPOSED
        approved = TeamMembershipStatus.APPROVED

        if team.subscriptionpolicy == TeamSubscriptionPolicy.RESTRICTED:
            raise JoinNotAllowed("This is a restricted team")
        elif (team.subscriptionpolicy == TeamSubscriptionPolicy.MODERATED
            or team.subscriptionpolicy == TeamSubscriptionPolicy.DELEGATED):
            status = proposed
        elif team.subscriptionpolicy == TeamSubscriptionPolicy.OPEN:
            status = approved
        else:
            raise AssertionError(
                "Unknown subscription policy: %s" % team.subscriptionpolicy)

        # XXX Edwin Grubbs 2007-12-14 bug=117980
        # removeSecurityProxy won't be necessary after addMember()
        # is configured to call a method on the new member, so the
        # security configuration will verify that the logged in user
        # has the right permission to add the specified person to the team.
        naked_team = removeSecurityProxy(team)
        naked_team.addMember(
            self, reviewer=requester, status=status,
            force_team_add=True,
            may_subscribe_to_list=may_subscribe_to_list)

    def clearInTeamCache(self):
        """See `IPerson`."""
        self._inTeam_cache = {}

    #
    # ITeam methods
    #
    @property
    def super_teams(self):
        """See `IPerson`."""
        return Store.of(self).using(
            Join(
                Person,
                TeamParticipation,
                Person.id == TeamParticipation.teamID
            )).find(
                Person,
                TeamParticipation.personID == self.id,
                TeamParticipation.teamID != self.id)

    @property
    def sub_teams(self):
        """See `IPerson`."""
        query = """
            Person.id = TeamParticipation.person AND
            TeamParticipation.team = %s AND
            TeamParticipation.person != %s AND
            Person.teamowner IS NOT NULL
            """ % sqlvalues(self.id, self.id)
        return Person.select(query, clauseTables=['TeamParticipation'])

    def getTeamAdminsEmailAddresses(self):
        """See `IPerson`."""
        if not self.is_team:
            raise ValueError("This method must only be used for teams.")
        to_addrs = set()
        for admin in self.adminmembers:
            to_addrs.update(get_contact_email_addresses(admin))
        return sorted(to_addrs)

    def addMember(self, person, reviewer, comment=None, force_team_add=False,
                  status=TeamMembershipStatus.APPROVED,
                  may_subscribe_to_list=True):
        """See `IPerson`."""
        if not self.is_team:
            raise ValueError("You cannot add members to a person.")
        if status not in [TeamMembershipStatus.APPROVED,
                          TeamMembershipStatus.PROPOSED,
                          TeamMembershipStatus.ADMIN]:
            raise ValueError("You can't add a member with this status: %s."
                             % status.name)

        event = JoinTeamEvent
        tm = TeamMembership.selectOneBy(person=person, team=self)
        if tm is not None:
            if tm.status == TeamMembershipStatus.ADMIN or (
                tm.status == TeamMembershipStatus.APPROVED and status ==
                TeamMembershipStatus.PROPOSED):
                status = tm.status
        if person.is_team:
            assert not self.hasParticipationEntryFor(person), (
                "Team '%s' is a member of '%s'. As a consequence, '%s' can't "
                "be added as a member of '%s'"
                % (self.name, person.name, person.name, self.name))
            # By default, teams can only be invited as members, meaning that
            # one of the team's admins will have to accept the invitation
            # before the team is made a member. If force_team_add is True,
            # or the user is also an admin of the proposed member, then
            # we'll add a team as if it was a person.
            is_reviewer_admin_of_new_member = (
                person in reviewer.getAdministratedTeams())
            if not force_team_add and not is_reviewer_admin_of_new_member:
                if tm is None or tm.status not in (
                    TeamMembershipStatus.PROPOSED,
                    TeamMembershipStatus.APPROVED,
                    TeamMembershipStatus.ADMIN,
                    ):
                    status = TeamMembershipStatus.INVITED
                    event = TeamInvitationEvent
                else:
                    if tm.status == TeamMembershipStatus.PROPOSED:
                        status = TeamMembershipStatus.APPROVED
                    else:
                        status = tm.status

        status_changed = True
        expires = self.defaultexpirationdate
        if tm is None:
            tm = TeamMembershipSet().new(
                person, self, status, reviewer, dateexpires=expires,
                comment=comment)
            # Accessing the id attribute ensures that the team
            # creation has been flushed to the database.
            tm.id
            notify(event(person, self))
        else:
            # We can't use tm.setExpirationDate() here because the reviewer
            # here will be the member themselves when they join an OPEN team.
            tm.dateexpires = expires
            status_changed = tm.setStatus(status, reviewer, comment)

        if not person.is_team and may_subscribe_to_list:
            person.autoSubscribeToMailingList(self.mailing_list,
                                              requester=reviewer)
        return (status_changed, tm.status)

    # The three methods below are not in the IPerson interface because we want
    # to protect them with a launchpad.Edit permission. We could do that by
    # defining explicit permissions for all IPerson methods/attributes in
    # the zcml but that's far from optimal given the size of IPerson.
    def acceptInvitationToBeMemberOf(self, team, comment):
        """Accept an invitation to become a member of the given team.

        There must be a TeamMembership for this person and the given team with
        the INVITED status. The status of this TeamMembership will be changed
        to APPROVED.
        """
        tm = TeamMembership.selectOneBy(person=self, team=team)
        assert tm is not None
        assert tm.status == TeamMembershipStatus.INVITED
        tm.setStatus(
            TeamMembershipStatus.APPROVED, getUtility(ILaunchBag).user,
            comment=comment)

    def declineInvitationToBeMemberOf(self, team, comment):
        """Decline an invitation to become a member of the given team.

        There must be a TeamMembership for this person and the given team with
        the INVITED status. The status of this TeamMembership will be changed
        to INVITATION_DECLINED.
        """
        tm = TeamMembership.selectOneBy(person=self, team=team)
        assert tm is not None
        assert tm.status == TeamMembershipStatus.INVITED
        tm.setStatus(
            TeamMembershipStatus.INVITATION_DECLINED,
            getUtility(ILaunchBag).user, comment=comment)

    def retractTeamMembership(self, team, user, comment=None):
        """See `IPerson`"""
        # Include PROPOSED and INVITED so that teams can retract mistakes
        # without involving members of the other team.
        active_and_transitioning = {
            TeamMembershipStatus.ADMIN: TeamMembershipStatus.DEACTIVATED,
            TeamMembershipStatus.APPROVED: TeamMembershipStatus.DEACTIVATED,
            TeamMembershipStatus.PROPOSED: TeamMembershipStatus.DECLINED,
            TeamMembershipStatus.INVITED:
                TeamMembershipStatus.INVITATION_DECLINED,
            }
        constraints = And(
            TeamMembership.personID == self.id,
            TeamMembership.teamID == team.id,
            TeamMembership.status.is_in(active_and_transitioning.keys()))
        tm = Store.of(self).find(TeamMembership, constraints).one()
        if tm is not None:
            # Flush the cache used by the inTeam method.
            self._inTeam_cache = {}
            new_status = active_and_transitioning[tm.status]
            tm.setStatus(new_status, user, comment=comment)

    def renewTeamMembership(self, team):
        """Renew the TeamMembership for this person on the given team.

        The given team's renewal policy must be ONDEMAND and the membership
        must be active (APPROVED or ADMIN) and set to expire in less than
        DAYS_BEFORE_EXPIRATION_WARNING_IS_SENT days.
        """
        tm = TeamMembership.selectOneBy(person=self, team=team)
        assert tm.canBeRenewedByMember(), (
            "This membership can't be renewed by the member himself.")

        assert (team.defaultrenewalperiod is not None
                and team.defaultrenewalperiod > 0), (
            'Teams with a renewal policy of ONDEMAND must specify '
            'a default renewal period greater than 0.')
        # Keep the same status, change the expiration date and send a
        # notification explaining the membership has been renewed.
        tm.dateexpires += timedelta(days=team.defaultrenewalperiod)
        tm.sendSelfRenewalNotification()

    def setMembershipData(self, person, status, reviewer, expires=None,
                          comment=None):
        """See `IPerson`."""
        tm = TeamMembership.selectOneBy(person=person, team=self)
        assert tm is not None
        tm.setExpirationDate(expires, reviewer)
        tm.setStatus(status, reviewer, comment=comment)

    @cachedproperty
    def administrated_teams(self):
        return list(self.getAdministratedTeams())

    def getAdministratedTeams(self):
        """See `IPerson`."""
        owner_of_teams = Person.select('''
            Person.teamowner = TeamParticipation.team
            AND TeamParticipation.person = %s
            AND Person.merged IS NULL
            ''' % sqlvalues(self),
            clauseTables=['TeamParticipation'])
        admin_of_teams = Person.select('''
            Person.id = TeamMembership.team
            AND TeamMembership.status = %(admin)s
            AND TeamMembership.person = TeamParticipation.team
            AND TeamParticipation.person = %(person)s
            AND Person.merged IS NULL
            ''' % sqlvalues(person=self, admin=TeamMembershipStatus.ADMIN),
            clauseTables=['TeamParticipation', 'TeamMembership'])
        return admin_of_teams.union(
            owner_of_teams, orderBy=self._sortingColumnsForSetOperations)

    def getDirectAdministrators(self):
        """See `IPerson`."""
        if not self.is_team:
            raise ValueError("This method must only be used for teams.")
        owner = Person.select("id = %s" % sqlvalues(self.teamowner))
        return self.adminmembers.union(
            owner, orderBy=self._sortingColumnsForSetOperations)

    def getMembersByStatus(self, status, orderBy=None):
        """See `IPerson`."""
        query = ("TeamMembership.team = %s AND TeamMembership.status = %s "
                 "AND TeamMembership.person = Person.id" %
                 sqlvalues(self.id, status))
        if orderBy is None:
            orderBy = Person.sortingColumns
        return Person.select(
            query, clauseTables=['TeamMembership'], orderBy=orderBy)

    def _getEmailsByStatus(self, status):
        return Store.of(self).find(
            EmailAddress,
            EmailAddress.personID == self.id,
            EmailAddress.status == status)

    def checkOpenSubscriptionPolicyAllowed(self, policy='open'):
        """See `ITeam`"""
        if not self.is_team:
            raise ValueError("This method must only be used for teams.")

        # Does this team own or is the security contact for any pillars?
        if self.isAnyPillarOwner():
            raise TeamSubscriptionPolicyError(
                "The team subscription policy cannot be %s because it "
                "maintains one ore more products, project groups, or "
                "distributions." % policy)
        if self.isAnySecurityContact():
            raise TeamSubscriptionPolicyError(
                "The team subscription policy cannot be %s because it "
                "is the security contact for one ore more products, "
                "project groups, or distributions." % policy)

        # Does this team have any PPAs
        for ppa in self.ppas:
            if ppa.status != ArchiveStatus.DELETED:
                raise TeamSubscriptionPolicyError(
                    "The team subscription policy cannot be %s because it "
                    "has one or more active PPAs." % policy)

        # Does this team have any super teams that are closed?
        for team in self.super_teams:
            if team.subscriptionpolicy in CLOSED_TEAM_POLICY:
                raise TeamSubscriptionPolicyError(
                    "The team subscription policy cannot be %s because one "
                    "or more if its super teams are not open." % policy)

        # Does this team subscribe or is assigned to any private bugs.
        # Circular imports.
        from lp.bugs.model.bug import Bug
        from lp.bugs.model.bugsubscription import BugSubscription
        from lp.bugs.model.bugtask import BugTask
        # The team cannot be open if it is subscribed to or assigned to
        # private bugs.
        private_bugs_involved = IStore(Bug).execute(Union(
            Select(
                Bug.id,
                tables=(
                    Bug,
                    Join(BugSubscription, BugSubscription.bug_id == Bug.id)),
                where=And(
                    Bug.private == True,
                    BugSubscription.person_id == self.id)),
            Select(
                Bug.id,
                tables=(
                    Bug,
                    Join(BugTask, BugTask.bugID == Bug.id)),
                where=And(Bug.private == True, BugTask.assignee == self.id)),
            limit=1))
        if private_bugs_involved.rowcount:
            raise TeamSubscriptionPolicyError(
                "The team subscription policy cannot be %s because it is "
                "subscribed to or assigned to one or more private "
                "bugs." % policy)

    def checkClosedSubscriptionPolicyAllowed(self, policy='closed'):
        """See `ITeam`"""
        if not self.is_team:
            raise ValueError("This method must only be used for teams.")

        # The team must be open if any of it's members are open.
        for member in self.activemembers:
            if member.subscriptionpolicy in OPEN_TEAM_POLICY:
                raise TeamSubscriptionPolicyError(
                    "The team subscription policy cannot be %s because one "
                    "or more if its member teams are Open." % policy)

    @property
    def wiki_names(self):
        """See `IPerson`."""
        result = Store.of(self).find(WikiName, WikiName.person == self.id)
        return result.order_by(WikiName.wiki, WikiName.wikiname)

    @property
    def title(self):
        """See `IPerson`."""
        return self.displayname

    @property
    def allmembers(self):
        """See `IPerson`."""
        return self._members(direct=False)

    @property
    def all_members_prepopulated(self):
        """See `IPerson`."""
        return self._members(direct=False, need_karma=True,
            need_ubuntu_coc=True, need_location=True, need_archive=True,
            need_preferred_email=True, need_validity=True)

    @staticmethod
    def _validity_queries(person_table=None):
        """Return storm expressions and a decorator function for validity.

        Preloading validity implies preloading preferred email addresses.

        :param person_table: The person table to join to. Only supply if
            ClassAliases are in use.
        :return: A dict with four keys joins, tables, conditions, decorators

        * joins are additional joins to use. e.g. [LeftJoin,LeftJoin]
        * tables are tables to use e.g. [EmailAddress, Account]
        * decorators are callbacks to call for each row. Each decorator takes
        (Person, column) where column is the column in the result set for that
        decorators type.
        """
        if person_table is None:
            person_table = Person
            email_table = EmailAddress
            account_table = Account
        else:
            email_table = ClassAlias(EmailAddress)
            account_table = ClassAlias(Account)
        origins = []
        columns = []
        decorators = []
        # Teams don't have email, so a left join
        origins.append(
            LeftJoin(email_table, And(
                email_table.personID == person_table.id,
                email_table.status == EmailAddressStatus.PREFERRED)))
        columns.append(email_table)
        origins.append(
            LeftJoin(account_table, And(
                person_table.accountID == account_table.id,
                account_table.status == AccountStatus.ACTIVE)))
        columns.append(account_table)

        def handleemail(person, column):
            #-- preferred email caching
            if not person:
                return
            email = column
            get_property_cache(person).preferredemail = email

        decorators.append(handleemail)

        def handleaccount(person, column):
            #-- validity caching
            if not person:
                return
            # valid if:
            valid = (
                # -- valid account found
                column is not None
                # -- preferred email found
                and person.preferredemail is not None)
            get_property_cache(person).is_valid_person = valid
        decorators.append(handleaccount)
        return dict(
            joins=origins,
            tables=columns,
            decorators=decorators)

    def _members(self, direct, need_karma=False, need_ubuntu_coc=False,
        need_location=False, need_archive=False, need_preferred_email=False,
        need_validity=False):
        """Lookup all members of the team with optional precaching.

        :param direct: If True only direct members are returned.
        :param need_karma: The karma attribute will be cached.
        :param need_ubuntu_coc: The is_ubuntu_coc_signer attribute will be
            cached.
        :param need_location: The location attribute will be cached.
        :param need_archive: The archive attribute will be cached.
        :param need_preferred_email: The preferred email attribute will be
            cached.
        :param need_validity: The is_valid attribute will be cached.
        """
        # TODO: consolidate this with getMembersWithPreferredEmails.
        #       The difference between the two is that
        #       getMembersWithPreferredEmails includes self, which is arguably
        #       wrong, but perhaps deliberate.
        origin = [Person]
        if not direct:
            origin.append(Join(
                TeamParticipation, TeamParticipation.person == Person.id))
            conditions = And(
                # Members of this team,
                TeamParticipation.team == self.id,
                # But not the team itself.
                TeamParticipation.person != self.id)
        else:
            origin.append(Join(
                TeamMembership, TeamMembership.personID == Person.id))
            conditions = And(
                # Membership in this team,
                TeamMembership.team == self.id,
                # And approved or admin status
                TeamMembership.status.is_in([
                    TeamMembershipStatus.APPROVED,
                    TeamMembershipStatus.ADMIN]))
        # Use a PersonSet object that is not security proxied to allow
        # manipulation of the object.
        person_set = PersonSet()
        return person_set._getPrecachedPersons(
            origin, conditions, store=Store.of(self),
            need_karma=need_karma,
            need_ubuntu_coc=need_ubuntu_coc,
            need_location=need_location,
            need_archive=need_archive,
            need_preferred_email=need_preferred_email,
            need_validity=need_validity)

    def _getMembersWithPreferredEmails(self):
        """Helper method for public getMembersWithPreferredEmails.

        We can't return the preferred email address directly to the
        browser code, since it would circumvent the security restrictions
        on accessing person.preferredemail.
        """
        store = Store.of(self)
        origin = [
            Person,
            Join(TeamParticipation, TeamParticipation.person == Person.id),
            Join(EmailAddress, EmailAddress.person == Person.id),
            ]
        conditions = And(
            TeamParticipation.team == self.id,
            EmailAddress.status == EmailAddressStatus.PREFERRED)
        return store.using(*origin).find((Person, EmailAddress), conditions)

    def getMembersWithPreferredEmails(self):
        """See `IPerson`."""
        result = self._getMembersWithPreferredEmails()
        person_list = []
        for person, email in result:
            get_property_cache(person).preferredemail = email
            person_list.append(person)
        return person_list

    def getMembersWithPreferredEmailsCount(self):
        """See `IPerson`."""
        result = self._getMembersWithPreferredEmails()
        return result.count()

    @property
    def all_member_count(self):
        """See `IPerson`."""
        return self.allmembers.count()

    @property
    def invited_members(self):
        """See `IPerson`."""
        return self.getMembersByStatus(TeamMembershipStatus.INVITED)

    @property
    def invited_member_count(self):
        """See `IPerson`."""
        return self.invited_members.count()

    @property
    def deactivatedmembers(self):
        """See `IPerson`."""
        return self.getMembersByStatus(TeamMembershipStatus.DEACTIVATED)

    @property
    def deactivated_member_count(self):
        """See `IPerson`."""
        return self.deactivatedmembers.count()

    @property
    def expiredmembers(self):
        """See `IPerson`."""
        return self.getMembersByStatus(TeamMembershipStatus.EXPIRED)

    @property
    def expired_member_count(self):
        """See `IPerson`."""
        return self.expiredmembers.count()

    @property
    def proposedmembers(self):
        """See `IPerson`."""
        return self.getMembersByStatus(TeamMembershipStatus.PROPOSED)

    @property
    def proposed_member_count(self):
        """See `IPerson`."""
        return self.proposedmembers.count()

    @property
    def adminmembers(self):
        """See `IPerson`."""
        return self.getMembersByStatus(TeamMembershipStatus.ADMIN)

    @property
    def approvedmembers(self):
        """See `IPerson`."""
        return self.getMembersByStatus(TeamMembershipStatus.APPROVED)

    @property
    def activemembers(self):
        """See `IPerson`."""
        return self.approvedmembers.union(
            self.adminmembers, orderBy=self._sortingColumnsForSetOperations)

    @property
    def api_activemembers(self):
        """See `IPerson`."""
        return self._members(direct=True, need_karma=True,
            need_ubuntu_coc=True, need_location=True, need_archive=True,
            need_preferred_email=True, need_validity=True)

    @property
    def active_member_count(self):
        """See `IPerson`."""
        return self.activemembers.count()

    @property
    def inactivemembers(self):
        """See `IPerson`."""
        return self.expiredmembers.union(
            self.deactivatedmembers,
            orderBy=self._sortingColumnsForSetOperations)

    @property
    def inactive_member_count(self):
        """See `IPerson`."""
        return self.inactivemembers.count()

    @property
    def pendingmembers(self):
        """See `IPerson`."""
        return self.proposedmembers.union(
            self.invited_members,
            orderBy=self._sortingColumnsForSetOperations)

    @property
    def team_memberships(self):
        """See `IPerson`."""
        Team = ClassAlias(Person, "Team")
        store = Store.of(self)
        # Join on team to sort by team names. Upper is used in the sort so
        # sorting works as is user expected, e.g. (A b C) not (A C b).
        return store.find(TeamMembership,
            And(TeamMembership.personID == self.id,
                TeamMembership.teamID == Team.id,
                TeamMembership.status.is_in([
                    TeamMembershipStatus.APPROVED,
                    TeamMembershipStatus.ADMIN,
                    ]))).order_by(
                        Upper(Team.displayname),
                        Upper(Team.name))

    def anyone_can_join(self):
        open_types = (
            TeamSubscriptionPolicy.OPEN,
            TeamSubscriptionPolicy.DELEGATED
            )
        return (self.subscriptionpolicy in open_types)

    def _getMappedParticipantsLocations(self, limit=None):
        """See `IPersonViewRestricted`."""
        return PersonLocation.select("""
            PersonLocation.person = TeamParticipation.person AND
            TeamParticipation.team = %s AND
            -- We only need to check for a latitude here because there's a DB
            -- constraint which ensures they are both set or unset.
            PersonLocation.latitude IS NOT NULL AND
            PersonLocation.visible IS TRUE AND
            Person.id = PersonLocation.person AND
            Person.teamowner IS NULL
            """ % sqlvalues(self.id),
            clauseTables=['TeamParticipation', 'Person'],
            prejoins=['person', ], limit=limit)

    def getMappedParticipants(self, limit=None):
        """See `IPersonViewRestricted`."""
        # Pre-cache this location against its person.  Since we'll always
        # iterate over all persons returned by this property (to build the map
        # of team members), it becomes more important to cache their locations
        # than to return a lazy SelectResults (or similar) object that only
        # fetches the rows when they're needed.
        locations = self._getMappedParticipantsLocations(limit=limit)
        for location in locations:
            get_property_cache(location.person).location = location
        participants = set(location.person for location in locations)
        # Cache the ValidPersonCache query for all mapped participants.
        if len(participants) > 0:
            sql = "id IN (%s)" % ",".join(sqlvalues(*participants))
            list(ValidPersonCache.select(sql))
        getUtility(IPersonSet).cacheBrandingForPeople(participants)
        return list(participants)

    @property
    def mapped_participants_count(self):
        """See `IPersonViewRestricted`."""
        return self._getMappedParticipantsLocations().count()

    def getMappedParticipantsBounds(self, limit=None):
        """See `IPersonViewRestricted`."""
        max_lat = -90.0
        min_lat = 90.0
        max_lng = -180.0
        min_lng = 180.0
        locations = self._getMappedParticipantsLocations(limit)
        if self.mapped_participants_count == 0:
            raise AssertionError(
                'This method cannot be called when '
                'mapped_participants_count == 0.')
        latitudes = sorted(location.latitude for location in locations)
        if latitudes[-1] > max_lat:
            max_lat = latitudes[-1]
        if latitudes[0] < min_lat:
            min_lat = latitudes[0]
        longitudes = sorted(location.longitude for location in locations)
        if longitudes[-1] > max_lng:
            max_lng = longitudes[-1]
        if longitudes[0] < min_lng:
            min_lng = longitudes[0]
        center_lat = (max_lat + min_lat) / 2.0
        center_lng = (max_lng + min_lng) / 2.0
        return dict(
            min_lat=min_lat, min_lng=min_lng, max_lat=max_lat,
            max_lng=max_lng, center_lat=center_lat, center_lng=center_lng)

    @property
    def unmapped_participants(self):
        """See `IPersonViewRestricted`."""
        return Person.select("""
            Person.id = TeamParticipation.person AND
            TeamParticipation.team = %s AND
            TeamParticipation.person NOT IN (
                SELECT PersonLocation.person
                FROM PersonLocation INNER JOIN TeamParticipation ON
                     PersonLocation.person = TeamParticipation.person
                WHERE TeamParticipation.team = %s AND
                      PersonLocation.latitude IS NOT NULL) AND
            Person.teamowner IS NULL
            """ % sqlvalues(self.id, self.id),
            clauseTables=['TeamParticipation'])

    @property
    def unmapped_participants_count(self):
        """See `IPersonViewRestricted`."""
        return self.unmapped_participants.count()

    @property
    def open_membership_invitations(self):
        """See `IPerson`."""
        return TeamMembership.select("""
            TeamMembership.person = %s AND status = %s
            AND Person.id = TeamMembership.team
            """ % sqlvalues(self.id, TeamMembershipStatus.INVITED),
            clauseTables=['Person'],
            orderBy=Person.sortingColumns)

    # XXX: salgado, 2009-04-16: This should be called just deactivate(),
    # because it not only deactivates this person's account but also the
    # person.
    def deactivateAccount(self, comment):
        """See `IPersonSpecialRestricted`."""
        if not self.is_valid_person:
            raise AssertionError(
                "You can only deactivate an account of a valid person.")

        for membership in self.team_memberships:
            self.leave(membership.team)

        # Deactivate CoC signatures, invalidate email addresses, unassign bug
        # tasks and specs and reassign pillars and teams.
        for coc in self.signedcocs:
            coc.active = False
        for email in self.validatedemails:
            email = IMasterObject(email)
            email.status = EmailAddressStatus.NEW
        params = BugTaskSearchParams(self, assignee=self)
        for bug_task in self.searchTasks(params):
            # If the bugtask has a conjoined master we don't try to
            # update it, since we will update it correctly when we
            # update its conjoined master (see bug 193983).
            if bug_task.conjoined_master is not None:
                continue

            # XXX flacoste 2007-11-26 bug=164635 The comparison using id in
            # the assert below works around a nasty intermittent failure.
            assert bug_task.assignee.id == self.id, (
               "Bugtask %s assignee isn't the one expected: %s != %s" % (
                    bug_task.id, bug_task.assignee.name, self.name))
            bug_task.transitionToAssignee(None)
        for spec in self.assigned_specs:
            spec.assignee = None
        registry_experts = getUtility(ILaunchpadCelebrities).registry_experts
        for team in Person.selectBy(teamowner=self):
            team.teamowner = registry_experts
        for pillar_name in self.getOwnedOrDrivenPillars():
            pillar = pillar_name.pillar
            # XXX flacoste 2007-11-26 bug=164635 The comparison using id below
            # works around a nasty intermittent failure.
            changed = False
            if pillar.owner.id == self.id:
                pillar.owner = registry_experts
                changed = True
            if pillar.driver is not None and pillar.driver.id == self.id:
                pillar.driver = registry_experts
                changed = True

            if not changed:
                # Since we removed the person from all teams, something is
                # seriously broken here.
                raise AssertionError(
                    "%s was expected to be owner or driver of %s" %
                    (self.name, pillar.name))

        # Nuke all subscriptions of this person.
        removals = [
            ('BranchSubscription', 'person'),
            ('BugSubscription', 'person'),
            ('QuestionSubscription', 'person'),
            ('SpecificationSubscription', 'person'),
            ('AnswerContact', 'person')]
        cur = cursor()
        for table, person_id_column in removals:
            cur.execute("DELETE FROM %s WHERE %s=%d"
                        % (table, person_id_column, self.id))

        # Update the account's status, preferred email and name.
        self.account_status = AccountStatus.DEACTIVATED
        self.account_status_comment = comment
        IMasterObject(self.preferredemail).status = EmailAddressStatus.NEW
        del get_property_cache(self).preferredemail
        base_new_name = self.name + '-deactivatedaccount'
        self.name = self._ensureNewName(base_new_name)

    def _ensureNewName(self, base_new_name):
        """Return a unique name."""
        new_name = base_new_name
        count = 1
        while Person.selectOneBy(name=new_name) is not None:
            new_name = base_new_name + str(count)
            count += 1
        return new_name

    @property
    def private(self):
        """See `IPerson`."""
        if not self.is_team:
            return False
        elif self.visibility == PersonVisibility.PUBLIC:
            return False
        else:
            return True

    @property
    def is_merge_pending(self):
        """See `IPublicPerson`."""
        return not getUtility(
            IPersonMergeJobSource).find(from_person=self).is_empty()

    def visibilityConsistencyWarning(self, new_value):
        """Warning used when changing the team's visibility.

        A private-membership team cannot be connected to other
        objects, since it may be possible to infer the membership.
        """
        if self._visibility_warning_cache != self._visibility_warning_marker:
            return self._visibility_warning_cache

        cur = cursor()
        references = list(postgresql.listReferences(cur, 'person', 'id'))
        # These tables will be skipped since they do not risk leaking
        # team membership information, except StructuralSubscription
        # which will be checked further down to provide a clearer warning.
        # Note all of the table names and columns must be all lowercase.
        skip = set([
            ('emailaddress', 'person'),
            ('gpgkey', 'owner'),
            ('ircid', 'person'),
            ('jabberid', 'person'),
            ('karma', 'person'),
            ('karmacache', 'person'),
            ('karmatotalcache', 'person'),
            ('logintoken', 'requester'),
            ('personlanguage', 'person'),
            ('personlocation', 'person'),
            ('personsettings', 'person'),
            ('persontransferjob', 'minor_person'),
            ('persontransferjob', 'major_person'),
            ('signedcodeofconduct', 'owner'),
            ('sshkey', 'person'),
            ('structuralsubscription', 'subscriber'),
            ('teammembership', 'team'),
            ('teamparticipation', 'person'),
            ('teamparticipation', 'team'),
            # Skip mailing lists because if the mailing list is purged, it's
            # not a problem.  Do this check separately below.
            ('mailinglist', 'team'),
            ])

        # The following relationships are allowable for Private teams and
        # thus should be skipped.
        if new_value == PersonVisibility.PRIVATE:
            skip.update([('bugsubscription', 'person'),
                         ('bugtask', 'assignee'),
                         ('branch', 'owner'),
                         ('branchsubscription', 'person'),
                         ('branchvisibilitypolicy', 'team'),
                         ('archive', 'owner'),
                         ('archivesubscriber', 'subscriber'),
                         ])

        warnings = set()
        for src_tab, src_col, ref_tab, ref_col, updact, delact in references:
            if (src_tab, src_col) in skip:
                continue
            cur.execute('SELECT 1 FROM %s WHERE %s=%d LIMIT 1'
                        % (src_tab, src_col, self.id))
            if cur.rowcount > 0:
                if src_tab[0] in 'aeiou':
                    article = 'an'
                else:
                    article = 'a'
                warnings.add('%s %s' % (article, src_tab))

        # Private teams may have structural subscription, so the following
        # test is not applied to them.
        if new_value != PersonVisibility.PRIVATE:
            # Add warnings for subscriptions in StructuralSubscription table
            # describing which kind of object is being subscribed to.
            cur.execute("""
                SELECT
                    count(product) AS product_count,
                    count(productseries) AS productseries_count,
                    count(project) AS project_count,
                    count(milestone) AS milestone_count,
                    count(distribution) AS distribution_count,
                    count(distroseries) AS distroseries_count,
                    count(sourcepackagename) AS sourcepackagename_count
                FROM StructuralSubscription
                WHERE subscriber=%d LIMIT 1
                """ % self.id)

            row = cur.fetchone()
            for count, warning in zip(row, [
                    'a project subscriber',
                    'a project series subscriber',
                    'a project subscriber',
                    'a milestone subscriber',
                    'a distribution subscriber',
                    'a distroseries subscriber',
                    'a source package subscriber']):
                if count > 0:
                    warnings.add(warning)

        # Non-purged mailing list check for transitioning to or from PUBLIC.
        if PersonVisibility.PUBLIC in [self.visibility, new_value]:
            mailing_list = getUtility(IMailingListSet).get(self.name)
            if (mailing_list is not None and
                mailing_list.status != MailingListStatus.PURGED):
                warnings.add('a mailing list')

        # Compose warning string.
        warnings = sorted(warnings)

        if len(warnings) == 0:
            self._visibility_warning_cache = None
        else:
            if len(warnings) == 1:
                message = warnings[0]
            else:
                message = '%s and %s' % (
                    ', '.join(warnings[:-1]),
                    warnings[-1])
            self._visibility_warning_cache = (
                'This team cannot be converted to %s since it is '
                'referenced by %s.' % (new_value, message))
        return self._visibility_warning_cache

    @property
    def member_memberships(self):
        """See `IPerson`."""
        return self._getMembershipsByStatuses(
            [TeamMembershipStatus.ADMIN, TeamMembershipStatus.APPROVED])

    def getInactiveMemberships(self):
        """See `IPerson`."""
        return self._getMembershipsByStatuses(
            [TeamMembershipStatus.EXPIRED, TeamMembershipStatus.DEACTIVATED])

    def getInvitedMemberships(self):
        """See `IPerson`."""
        return self._getMembershipsByStatuses([TeamMembershipStatus.INVITED])

    def getProposedMemberships(self):
        """See `IPerson`."""
        return self._getMembershipsByStatuses([TeamMembershipStatus.PROPOSED])

    def _getMembershipsByStatuses(self, statuses):
        """All `ITeamMembership`s in any given status for this team's members.

        :param statuses: A list of `TeamMembershipStatus` items.

        If called on an person rather than a team, this will obviously return
        no memberships at all.
        """
        statuses = ",".join(quote(status) for status in statuses)
        # We don't want to escape 'statuses' so we can't easily use
        # sqlvalues() on the query below.
        query = """
            TeamMembership.status IN (%s)
            AND Person.id = TeamMembership.person
            AND TeamMembership.team = %d
            """ % (statuses, self.id)
        return TeamMembership.select(
            query, clauseTables=['Person'],
            prejoinClauseTables=['Person'],
            orderBy=Person.sortingColumns)

    def getLatestApprovedMembershipsForPerson(self, limit=5):
        """See `IPerson`."""
        result = self.team_memberships
        result = result.order_by(
            Desc(TeamMembership.datejoined),
            Desc(TeamMembership.id))
        return result[:limit]

    def getPathsToTeams(self):
        """See `Iperson`."""
        # Get all of the teams this person participates in.
        teams = list(self.teams_participated_in)

        # For cases where self is a team, we don't need self as a team
        # participated in.
        teams = [team for team in teams if team is not self]

        # Get all of the memberships for any of the teams this person is
        # a participant of. This must be ordered by date and id because
        # because the graph of the results will create needs to contain
        # the oldest path information to be consistent with results from
        # IPerson.findPathToTeam.
        store = Store.of(self)
        all_direct_memberships = store.find(TeamMembership,
            And(
                TeamMembership.personID.is_in(
                    [team.id for team in teams] + [self.id]),
                TeamMembership.teamID != self.id,
                TeamMembership.status.is_in([
                    TeamMembershipStatus.APPROVED,
                    TeamMembershipStatus.ADMIN,
                    ]))).order_by(
                        Desc(TeamMembership.datejoined),
                        Desc(TeamMembership.id))
        # Cast the results to list now, because they will be iterated over
        # several times.
        all_direct_memberships = list(all_direct_memberships)

        # Pull out the memberships directly used by this person.
        user_memberships = [
            membership for membership in
            all_direct_memberships
            if membership.person == self]

        all_direct_memberships = [
            (membership.team, membership.person) for membership in
            all_direct_memberships]

        # Create a graph from the edges provided by the other data sets.
        graph = dict(all_direct_memberships)

        # Build the teams paths from that graph.
        paths = {}
        for team in teams:
            path = [team]
            step = team
            while path[-1] != self:
                step = graph[step]
                path.append(step)
            paths[team] = path
        return (paths, user_memberships)

    @property
    def teams_participated_in(self):
        """See `IPerson`."""
        return Person.select("""
            Person.id = TeamParticipation.team
            AND TeamParticipation.person = %s
            AND Person.teamowner IS NOT NULL
            """ % sqlvalues(self.id),
            clauseTables=['TeamParticipation'],
            orderBy=Person.sortingColumns)

    @property
    def teams_indirectly_participated_in(self):
        """See `IPerson`."""
        Team = ClassAlias(Person, "Team")
        store = Store.of(self)
        origin = [
            Team,
            Join(TeamParticipation, Team.id == TeamParticipation.teamID),
            LeftJoin(TeamMembership,
                And(TeamMembership.person == self.id,
                    TeamMembership.teamID == TeamParticipation.teamID,
                    TeamMembership.status.is_in([
                        TeamMembershipStatus.APPROVED,
                        TeamMembershipStatus.ADMIN])))]
        find_objects = (Team)
        return store.using(*origin).find(find_objects,
            And(
                TeamParticipation.person == self.id,
                TeamParticipation.person != TeamParticipation.teamID,
                TeamMembership.id == None))

    @property
    def teams_with_icons(self):
        """See `IPerson`."""
        return Person.select("""
            Person.id = TeamParticipation.team
            AND TeamParticipation.person = %s
            AND Person.teamowner IS NOT NULL
            AND Person.icon IS NOT NULL
            AND TeamParticipation.team != %s
            """ % sqlvalues(self.id, self.id),
            clauseTables=['TeamParticipation'],
            orderBy=Person.sortingColumns)

    @property
    def defaultexpirationdate(self):
        """See `IPerson`."""
        days = self.defaultmembershipperiod
        if days:
            return datetime.now(pytz.timezone('UTC')) + timedelta(days)
        else:
            return None

    @property
    def defaultrenewedexpirationdate(self):
        """See `IPerson`."""
        days = self.defaultrenewalperiod
        if days:
            return datetime.now(pytz.timezone('UTC')) + timedelta(days)
        else:
            return None

    def reactivate(self, comment, password, preferred_email):
        """See `IPersonSpecialRestricted`."""
        account = IMasterObject(self.account)
        account.reactivate(comment, password, preferred_email)
        if '-deactivatedaccount' in self.name:
            # The name was changed by deactivateAccount(). Restore the
            # name, but we must ensure it does not conflict with a current
            # user.
            name_parts = self.name.split('-deactivatedaccount')
            base_new_name = name_parts[0]
            self.name = self._ensureNewName(base_new_name)

    def validateAndEnsurePreferredEmail(self, email):
        """See `IPerson`."""
        email = IMasterObject(email)
        assert not self.is_team, "This method must not be used for teams."
        if not IEmailAddress.providedBy(email):
            raise TypeError(
                "Any person's email address must provide the IEmailAddress "
                "interface. %s doesn't." % email)
        # XXX Steve Alexander 2005-07-05:
        # This is here because of an SQLobject comparison oddity.
        assert email.personID == self.id, 'Wrong person! %r, %r' % (
            email.personID, self.id)

        # We need the preferred email address. This method is called
        # recursively, however, and the email address may have just been
        # created. So we have to explicitly pull it from the master store
        # until we rewrite this 'icky mess.
        preferred_email = IMasterStore(EmailAddress).find(
            EmailAddress,
            EmailAddress.personID == self.id,
            EmailAddress.status == EmailAddressStatus.PREFERRED).one()

        # This email is already validated and is this person's preferred
        # email, so we have nothing to do.
        if preferred_email == email:
            return

        if preferred_email is None:
            # This branch will be executed only in the first time a person
            # uses Launchpad. Either when creating a new account or when
            # resetting the password of an automatically created one.
            self.setPreferredEmail(email)
        else:
            email.status = EmailAddressStatus.VALIDATED

    def setContactAddress(self, email):
        """See `IPerson`."""
        if not self.is_team:
            raise ValueError("This method must only be used for teams.")

        if email is None:
            self._unsetPreferredEmail()
        else:
            self._setPreferredEmail(email)
        # A team can have up to two addresses, the preferred one and one used
        # by the team mailing list.
        if (self.mailing_list is not None
            and self.mailing_list.status != MailingListStatus.PURGED):
            mailing_list_email = getUtility(IEmailAddressSet).getByEmail(
                self.mailing_list.address)
            if mailing_list_email is not None:
                mailing_list_email = IMasterObject(mailing_list_email)
        else:
            mailing_list_email = None
        all_addresses = IMasterStore(self).find(
            EmailAddress, EmailAddress.personID == self.id)
        for address in all_addresses:
            # Delete all email addresses that are not the preferred email
            # address, or the team's email address. If this method was called
            # with None, and there is no mailing list, then this condidition
            # is (None, None), causing all email addresses to be deleted.
            if address not in (email, mailing_list_email):
                address.destroySelf()

    def _unsetPreferredEmail(self):
        """Change the preferred email address to VALIDATED."""
        email_address = IMasterStore(EmailAddress).find(
            EmailAddress, personID=self.id,
            status=EmailAddressStatus.PREFERRED).one()
        if email_address is not None:
            email_address.status = EmailAddressStatus.VALIDATED
            email_address.syncUpdate()
        del get_property_cache(self).preferredemail

    def setPreferredEmail(self, email):
        """See `IPerson`."""
        assert not self.is_team, "This method must not be used for teams."
        if email is None:
            self._unsetPreferredEmail()
            return
        self._setPreferredEmail(email)

    def _setPreferredEmail(self, email):
        """Set this person's preferred email to the given email address.

        If the person already has an email address, then its status is
        changed to VALIDATED and the given one is made its preferred one.

        The given email address must implement IEmailAddress and be owned by
        this person.
        """
        if not IEmailAddress.providedBy(email):
            raise TypeError(
                "Any person's email address must provide the IEmailAddress "
                "interface. %s doesn't." % email)
        assert email.personID == self.id

        existing_preferred_email = IMasterStore(EmailAddress).find(
            EmailAddress, personID=self.id,
            status=EmailAddressStatus.PREFERRED).one()

        if existing_preferred_email is not None:
            existing_preferred_email.status = EmailAddressStatus.VALIDATED

        email = removeSecurityProxy(email)
        IMasterObject(email).status = EmailAddressStatus.PREFERRED
        IMasterObject(email).syncUpdate()

        # Now we update our cache of the preferredemail.
        get_property_cache(self).preferredemail = email

    @cachedproperty
    def preferredemail(self):
        """See `IPerson`."""
        emails = self._getEmailsByStatus(EmailAddressStatus.PREFERRED)
        # There can be only one preferred email for a given person at a
        # given time, and this constraint must be ensured in the DB, but
        # it's not a problem if we ensure this constraint here as well.
        emails = shortlist(emails)
        length = len(emails)
        assert length <= 1
        if length:
            return emails[0]
        else:
            return None

    @property
    def safe_email_or_blank(self):
        """See `IPerson`."""
        if (self.preferredemail is not None
            and not self.hide_email_addresses):
            return self.preferredemail.email
        else:
            return ''

    @property
    def validatedemails(self):
        """See `IPerson`."""
        return self._getEmailsByStatus(EmailAddressStatus.VALIDATED)

    @property
    def unvalidatedemails(self):
        """See `IPerson`."""
        query = """
            requester = %s
            AND (tokentype=%s OR tokentype=%s)
            AND date_consumed IS NULL
            """ % sqlvalues(self.id, LoginTokenType.VALIDATEEMAIL,
                            LoginTokenType.VALIDATETEAMEMAIL)
        return sorted(set(token.email for token in LoginToken.select(query)))

    @property
    def guessedemails(self):
        """See `IPerson`."""
        return self._getEmailsByStatus(EmailAddressStatus.NEW)

    @property
    def pending_gpg_keys(self):
        """See `IPerson`."""
        logintokenset = getUtility(ILoginTokenSet)
        return sorted(set(token.fingerprint for token in
                      logintokenset.getPendingGPGKeys(requesterid=self.id)))

    @property
    def inactive_gpg_keys(self):
        """See `IPerson`."""
        gpgkeyset = getUtility(IGPGKeySet)
        return gpgkeyset.getGPGKeys(ownerid=self.id, active=False)

    @property
    def gpg_keys(self):
        """See `IPerson`."""
        gpgkeyset = getUtility(IGPGKeySet)
        return gpgkeyset.getGPGKeys(ownerid=self.id)

    def getLatestMaintainedPackages(self):
        """See `IPerson`."""
        return self._latestSeriesQuery()

    def getLatestSynchronisedPublishings(self):
        """See `IPerson`."""
        query = """
            SourcePackagePublishingHistory.id IN (
                SELECT DISTINCT ON (spph.distroseries,
                                    spr.sourcepackagename)
                    spph.id
                FROM
                    SourcePackagePublishingHistory as spph, archive,
                    SourcePackagePublishingHistory as ancestor_spph,
                    SourcePackageRelease as spr
                WHERE
                    spph.sourcepackagerelease = spr.id AND
                    spph.creator = %(creator)s AND
                    spph.ancestor = ancestor_spph.id AND
                    spph.archive = archive.id AND
                    ancestor_spph.archive != spph.archive AND
                    archive.purpose = %(archive_purpose)s
                ORDER BY spph.distroseries,
                    spr.sourcepackagename,
                    spph.datecreated DESC,
                    spph.id DESC
            )
            """ % dict(
                   creator=quote(self.id),
                   archive_purpose=quote(ArchivePurpose.PRIMARY),
                   )

        return SourcePackagePublishingHistory.select(
            query,
            orderBy=['-SourcePackagePublishingHistory.datecreated',
                     '-SourcePackagePublishingHistory.id'],
            prejoins=['sourcepackagerelease', 'archive'])

    def getLatestUploadedButNotMaintainedPackages(self):
        """See `IPerson`."""
        return self._latestSeriesQuery(uploader_only=True)

    def getLatestUploadedPPAPackages(self):
        """See `IPerson`."""
        return self._latestSeriesQuery(
            uploader_only=True, ppa_only=True)

    def _latestSeriesQuery(self, uploader_only=False, ppa_only=False):
        """Return the sourcepackagereleases (SPRs) related to this person.

        :param uploader_only: controls if we are interested in SPRs where
            the person in question is only the uploader (creator) and not the
            maintainer (debian-syncs) if the `ppa_only` parameter is also
            False, or, if the flag is False, it returns all SPR maintained
            by this person.

        :param ppa_only: controls if we are interested only in source
            package releases targeted to any PPAs or, if False, sources
            targeted to primary archives.

        Active 'ppa_only' flag is usually associated with active
        'uploader_only' because there shouldn't be any sense of maintainership
        for packages uploaded to PPAs by someone else than the user himself.
        """
        clauses = ['sourcepackagerelease.upload_archive = archive.id']

        if uploader_only:
            clauses.append(
                'sourcepackagerelease.creator = %s' % quote(self.id))

        if ppa_only:
            # Source maintainer is irrelevant for PPA uploads.
            pass
        elif uploader_only:
            clauses.append(
                'sourcepackagerelease.maintainer != %s' % quote(self.id))
        else:
            clauses.append(
                'sourcepackagerelease.maintainer = %s' % quote(self.id))

        if ppa_only:
            clauses.append(
                'archive.purpose = %s' % quote(ArchivePurpose.PPA))
        else:
            clauses.append(
                'archive.purpose != %s' % quote(ArchivePurpose.PPA))

        query_clauses = " AND ".join(clauses)
        query = """
            SourcePackageRelease.id IN (
                SELECT DISTINCT ON (upload_distroseries,
                                    sourcepackagerelease.sourcepackagename,
                                    upload_archive)
                    sourcepackagerelease.id
                FROM sourcepackagerelease, archive,
                    sourcepackagepublishinghistory as spph
                WHERE
                    spph.sourcepackagerelease = sourcepackagerelease.id AND
                    spph.archive = archive.id AND
                    %(more_query_clauses)s
                ORDER BY upload_distroseries,
                    sourcepackagerelease.sourcepackagename,
                    upload_archive, dateuploaded DESC
              )
              """ % dict(more_query_clauses=query_clauses)

        rset = SourcePackageRelease.select(
            query,
            orderBy=['-SourcePackageRelease.dateuploaded',
                     'SourcePackageRelease.id'],
            prejoins=['sourcepackagename', 'maintainer', 'upload_archive'])

        return rset

    def createRecipe(self, name, description, recipe_text, distroseries,
                     registrant, daily_build_archive=None, build_daily=False):
        """See `IPerson`."""
        from lp.code.model.sourcepackagerecipe import SourcePackageRecipe
        recipe = SourcePackageRecipe.new(
            registrant, self, name, recipe_text, description, distroseries,
            daily_build_archive, build_daily)
        Store.of(recipe).flush()
        return recipe

    def getRecipe(self, name):
        from lp.code.model.sourcepackagerecipe import SourcePackageRecipe
        return Store.of(self).find(
            SourcePackageRecipe, SourcePackageRecipe.owner == self,
            SourcePackageRecipe.name == name).one()

    def getMergeQueue(self, name):
        from lp.code.model.branchmergequeue import BranchMergeQueue
        return Store.of(self).find(
            BranchMergeQueue,
            BranchMergeQueue.owner == self,
            BranchMergeQueue.name == unicode(name)).one()

    def isUploader(self, distribution):
        """See `IPerson`."""
        permissions = getUtility(IArchivePermissionSet).componentsForUploader(
            distribution.main_archive, self)
        return permissions.count() > 0

    @cachedproperty
    def is_ubuntu_coc_signer(self):
        """See `IPerson`."""
        # Also assigned to by self._members.
        store = Store.of(self)
        query = And(SignedCodeOfConduct.ownerID == self.id,
            Person._is_ubuntu_coc_signer_condition())
        return not store.find(SignedCodeOfConduct, query).is_empty()

    @staticmethod
    def _is_ubuntu_coc_signer_condition():
        """Generate a Storm Expr for determing the coc signing status."""
        sigset = getUtility(ISignedCodeOfConductSet)
        lastdate = sigset.getLastAcceptedDate()
        return And(SignedCodeOfConduct.active == True,
            SignedCodeOfConduct.datecreated >= lastdate)

    @property
    def activesignatures(self):
        """See `IPerson`."""
        sCoC_util = getUtility(ISignedCodeOfConductSet)
        return sCoC_util.searchByUser(self.id)

    @property
    def inactivesignatures(self):
        """See `IPerson`."""
        sCoC_util = getUtility(ISignedCodeOfConductSet)
        return sCoC_util.searchByUser(self.id, active=False)

    @cachedproperty
    def archive(self):
        """See `IPerson`."""
        return getUtility(IArchiveSet).getPPAOwnedByPerson(self)

    def getArchiveSubscriptionURLs(self, requester):
        """See `IPerson`."""
        agent = getUtility(ILaunchpadCelebrities).software_center_agent
        # If the requester isn't asking about themselves, and they aren't the
        # software center agent, deny them
        if requester.id != agent.id:
            if self.id != requester.id:
                raise Unauthorized
        subscriptions = getUtility(
            IArchiveSubscriberSet).getBySubscriberWithActiveToken(
                subscriber=self)
        return [token.archive_url for (subscription, token) in subscriptions
                if token is not None]

    def getArchiveSubscriptionURL(self, requester, archive):
        """See `IPerson`."""
        agent = getUtility(ILaunchpadCelebrities).software_center_agent
        # If the requester isn't asking about themselves, and they aren't the
        # software center agent, deny them
        if requester.id != agent.id:
            if self.id != requester.id:
                raise Unauthorized
        token = archive.getAuthToken(self)
        if token is None:
            token = archive.newAuthToken(self)
        return token.archive_url

    @property
    def ppas(self):
        """See `IPerson`."""
        return Archive.selectBy(
            owner=self, purpose=ArchivePurpose.PPA, orderBy='name')

    def getPPAByName(self, name):
        """See `IPerson`."""
        return getUtility(IArchiveSet).getPPAOwnedByPerson(self, name)

    def createPPA(self, name=None, displayname=None, description=None,
                  private=False):
        """See `IPerson`."""
        errors = Archive.validatePPA(self, name, private)
        if errors:
            raise PPACreationError(errors)
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        return getUtility(IArchiveSet).new(
            owner=self, purpose=ArchivePurpose.PPA,
            distribution=ubuntu, name=name, displayname=displayname,
            description=description, private=private)

    def isBugContributor(self, user=None):
        """See `IPerson`."""
        search_params = BugTaskSearchParams(user=user, assignee=self)
        bugtask_count = self.searchTasks(search_params).count()
        return bugtask_count > 0

    def isBugContributorInTarget(self, user=None, target=None):
        """See `IPerson`."""
        assert (IBugTarget.providedBy(target) or
                IProjectGroup.providedBy(target)), (
            "%s isn't a valid bug target." % target)
        search_params = BugTaskSearchParams(user=user, assignee=self)
        bugtask_count = target.searchTasks(search_params).count()
        return bugtask_count > 0

    @property
    def structural_subscriptions(self):
        """See `IPerson`."""
        return IStore(self).find(
            StructuralSubscription,
            StructuralSubscription.subscriberID == self.id).order_by(
                Desc(StructuralSubscription.date_created))

    def autoSubscribeToMailingList(self, mailinglist, requester=None):
        """See `IPerson`."""
        if mailinglist is None or not mailinglist.is_usable:
            return False

        if mailinglist.getSubscription(self):
            # We are already subscribed to the list.
            return False

        if self.preferredemail is None:
            return False

        if requester is None:
            # Assume the current user requested this action themselves.
            requester = self

        policy = self.mailing_list_auto_subscribe_policy

        if policy == MailingListAutoSubscribePolicy.ALWAYS:
            mailinglist.subscribe(self)
            return True
        elif (requester is self and
              policy == MailingListAutoSubscribePolicy.ON_REGISTRATION):
            # Assume that we requested to be joined.
            mailinglist.subscribe(self)
            return True
        else:
            # We don't want to subscribe to the list.
            return False

    @property
    def hardware_submissions(self):
        """See `IPerson`."""
        from lp.hardwaredb.model.hwdb import HWSubmissionSet
        return HWSubmissionSet().search(owner=self)

    @property
    def recipes(self):
        """See `IHasRecipes`."""
        from lp.code.model.sourcepackagerecipe import SourcePackageRecipe
        store = Store.of(self)
        return store.find(
            SourcePackageRecipe,
            SourcePackageRecipe.owner == self)

    def canAccess(self, obj, attribute):
        """See `IPerson.`"""
        return canAccess(obj, attribute)

    def canWrite(self, obj, attribute):
        """See `IPerson.`"""
        return canWrite(obj, attribute)

    def checkRename(self):
        """See `IPerson.`"""
        reasons = []
        atom = 'person'
        has_ppa = getUtility(IArchiveSet).getPPAOwnedByPerson(
            self, has_packages=True,
            statuses=[ArchiveStatus.ACTIVE,
                      ArchiveStatus.DELETING]) is not None
        has_mailing_list = None
        if ITeam.providedBy(self):
            atom = 'team'
            mailing_list = getUtility(IMailingListSet).get(self.name)
            has_mailing_list = (
                mailing_list is not None and
                mailing_list.status != MailingListStatus.PURGED)
        if has_ppa:
            reasons.append('an active PPA with packages published')
        if has_mailing_list:
            reasons.append('a mailing list')
        if reasons:
            return _('This %s has %s and may not be renamed.' % (
                atom, ' and '.join(reasons)))
        else:
            return None

    def canCreatePPA(self):
        """See `IPerson.`"""
        return self.subscriptionpolicy in CLOSED_TEAM_POLICY


class PersonSet:
    """The set of persons."""
    implements(IPersonSet)

    def __init__(self):
        self.title = 'People registered with Launchpad'

    def isNameBlacklisted(self, name, user=None):
        """See `IPersonSet`."""
        if user is None:
            user_id = 0
        else:
            user_id = user.id
        cur = cursor()
        cur.execute(
            "SELECT is_blacklisted_name(%(name)s, %(user_id)s)" % sqlvalues(
            name=name.encode('UTF-8'), user_id=user_id))
        return bool(cur.fetchone()[0])

    def getTopContributors(self, limit=50):
        """See `IPersonSet`."""
        # The odd ordering here is to ensure we hit the PostgreSQL
        # indexes. It will not make any real difference outside of tests.
        query = """
            id IN (
                SELECT person FROM KarmaTotalCache
                ORDER BY karma_total DESC, person DESC
                LIMIT %s
                )
            """ % limit
        top_people = shortlist(Person.select(query))
        return sorted(
            top_people,
            key=lambda obj: (obj.karma, obj.displayname, obj.id),
            reverse=True)

    def getOrCreateByOpenIDIdentifier(
        self, openid_identifier, email_address, full_name,
        creation_rationale, comment):
        """See `IPersonSet`."""
        assert email_address is not None and full_name is not None, (
                "Both email address and full name are required to "
                "create an account.")
        db_updated = False

        assert isinstance(openid_identifier, unicode)

        # Load the EmailAddress, Account and OpenIdIdentifier records
        # from the master (if they exist). We use the master to avoid
        # possible replication lag issues but this might actually be
        # unnecessary.
        with MasterDatabasePolicy():
            store = IMasterStore(EmailAddress)
            join = store.using(
                EmailAddress,
                LeftJoin(Account, EmailAddress.accountID == Account.id))
            email, account = (
                join.find(
                    (EmailAddress, Account),
                    EmailAddress.email.lower() ==
                        ensure_unicode(email_address).lower()).one()
                or (None, None))
            identifier = store.find(
                OpenIdIdentifier, identifier=openid_identifier).one()

            if email is None and identifier is None:
                # Neither the Email Address not the OpenId Identifier
                # exist in the database. Create the email address,
                # account, and associated info. OpenIdIdentifier is
                # created later.
                account_set = getUtility(IAccountSet)
                account, email = account_set.createAccountAndEmail(
                    email_address, creation_rationale, full_name,
                    password=None)
                db_updated = True

            elif email is None:
                # The Email Address does not exist in the database,
                # but the OpenId Identifier does. Create the Email
                # Address and link it to the account.
                assert account is None, 'Retrieved an account but not email?'
                account = identifier.account
                emailaddress_set = getUtility(IEmailAddressSet)
                email = emailaddress_set.new(
                    email_address, account=account)
                db_updated = True

            elif account is None:
                # Email address exists, but there is no Account linked
                # to it. Create the Account and link it to the
                # EmailAddress.
                account_set = getUtility(IAccountSet)
                account = account_set.new(
                    AccountCreationRationale.OWNER_CREATED_LAUNCHPAD,
                    full_name)
                email.account = account
                db_updated = True

            if identifier is None:
                # This is the first time we have seen that
                # OpenIdIdentifier. Link it.
                identifier = OpenIdIdentifier()
                identifier.account = account
                identifier.identifier = openid_identifier
                store.add(identifier)
                db_updated = True

            elif identifier.account != account:
                # The ISD OpenId server may have linked this OpenId
                # identifier to a new email address, or the user may
                # have transfered their email address to a different
                # Launchpad Account. If that happened, repair the
                # link - we trust the ISD OpenId server.
                identifier.account = account
                db_updated = True

            # We now have an account, email address, and openid identifier.

            if account.status == AccountStatus.SUSPENDED:
                raise AccountSuspendedError(
                    "The account matching the identifier is suspended.")

            elif account.status in [AccountStatus.DEACTIVATED,
                                    AccountStatus.NOACCOUNT]:
                password = ''  # Needed just to please reactivate() below.
                removeSecurityProxy(account).reactivate(
                    comment, password, removeSecurityProxy(email))
                db_updated = True
            else:
                # Account is active, so nothing to do.
                pass

            if IPerson(account, None) is None:
                removeSecurityProxy(account).createPerson(
                    creation_rationale, comment=comment)
                db_updated = True

            person = IPerson(account)
            if email.personID != person.id:
                removeSecurityProxy(email).person = person
                db_updated = True

            return person, db_updated

    def newTeam(self, teamowner, name, displayname, teamdescription=None,
                subscriptionpolicy=TeamSubscriptionPolicy.MODERATED,
                defaultmembershipperiod=None, defaultrenewalperiod=None):
        """See `IPersonSet`."""
        assert teamowner
        if self.getByName(name, ignore_merged=False) is not None:
            raise NameAlreadyTaken(
                "The name '%s' is already taken." % name)
        team = Person(teamowner=teamowner, name=name, displayname=displayname,
                teamdescription=teamdescription,
                defaultmembershipperiod=defaultmembershipperiod,
                defaultrenewalperiod=defaultrenewalperiod,
                subscriptionpolicy=subscriptionpolicy)
        notify(ObjectCreatedEvent(team))
        # Here we add the owner as a team admin manually because we know what
        # we're doing (so we don't need to do any sanity checks) and we don't
        # want any email notifications to be sent.
        TeamMembershipSet().new(
            teamowner, team, TeamMembershipStatus.ADMIN, teamowner)
        return team

    def createPersonAndEmail(
            self, email, rationale, comment=None, name=None,
            displayname=None, password=None, passwordEncrypted=False,
            hide_email_addresses=False, registrant=None):
        """See `IPersonSet`."""

        # This check is also done in EmailAddressSet.new() and also
        # generate_nick(). We repeat it here as some call sites want
        # InvalidEmailAddress rather than NicknameGenerationError that
        # generate_nick() will raise.
        if not valid_email(email):
            raise InvalidEmailAddress(
                "%s is not a valid email address." % email)

        if name is None:
            name = generate_nick(email)

        if not displayname:
            displayname = name.capitalize()

        # Convert the PersonCreationRationale to an AccountCreationRationale
        account_rationale = getattr(AccountCreationRationale, rationale.name)

        account = getUtility(IAccountSet).new(
                account_rationale, displayname, password=password,
                password_is_encrypted=passwordEncrypted)

        person = self._newPerson(
            name, displayname, hide_email_addresses, rationale=rationale,
            comment=comment, registrant=registrant, account=account)

        email = getUtility(IEmailAddressSet).new(
                email, person, account=account)

        assert email.accountID is not None, (
            'Failed to link EmailAddress to Account')
        return person, email

    def createPersonWithoutEmail(
        self, name, rationale, comment=None, displayname=None,
        registrant=None):
        """Create and return a new Person without using an email address.

        See `IPersonSet`.
        """
        return self._newPerson(
            name, displayname, hide_email_addresses=True, rationale=rationale,
            comment=comment, registrant=registrant)

    def _newPerson(self, name, displayname, hide_email_addresses,
                   rationale, comment=None, registrant=None, account=None):
        """Create and return a new Person with the given attributes."""
        if not valid_name(name):
            raise InvalidName(
                "%s is not a valid name for a person." % name)
        else:
            # The name should be okay, move on...
            pass
        if self.getByName(name, ignore_merged=False) is not None:
            raise NameAlreadyTaken(
                "The name '%s' is already taken." % name)

        if not displayname:
            displayname = name.capitalize()

        if account is None:
            account_id = None
        else:
            account_id = account.id
        person = Person(
            name=name, displayname=displayname, accountID=account_id,
            creation_rationale=rationale, creation_comment=comment,
            hide_email_addresses=hide_email_addresses, registrant=registrant)
        return person

    def ensurePerson(self, email, displayname, rationale, comment=None,
                     registrant=None):
        """See `IPersonSet`."""
        # Start by checking if there is an `EmailAddress` for the given
        # text address.  There are many cases where an email address can be
        # created without an associated `Person`. For instance, we created
        # an account linked to the address through an external system such
        # SSO or ShipIt.
        email_address = getUtility(IEmailAddressSet).getByEmail(email)

        # There is no `EmailAddress` for this text address, so we need to
        # create both the `Person` and `EmailAddress` here and we are done.
        if email_address is None:
            person, email_address = self.createPersonAndEmail(
                email, rationale, comment=comment, displayname=displayname,
                registrant=registrant, hide_email_addresses=True)
            return person

        # There is an `EmailAddress` for this text address, but no
        # associated `Person`.
        if email_address.person is None:
            assert email_address.accountID is not None, (
                '%s is not associated to a person or account'
                % email_address.email)
            account = IMasterStore(Account).get(
                Account, email_address.accountID)
            account_person = self.getByAccount(account)
            if account_person is None:
                # There is no associated `Person` to the email `Account`.
                # This is probably because the account was created externally
                # to Launchpad. Create just the `Person`, associate it with
                # the `EmailAddress` and return it.
                name = generate_nick(email)
                account_person = self._newPerson(
                    name, displayname, hide_email_addresses=True,
                    rationale=rationale, comment=comment,
                    registrant=registrant, account=email_address.account)
            # There is (now) a `Person` linked to the `Account`, link the
            # `EmailAddress` to this `Person` and return it.
            master_email = IMasterStore(EmailAddress).get(
                EmailAddress, email_address.id)
            master_email.personID = account_person.id
            # Populate the previously empty 'preferredemail' cached
            # property, so the Person record is up-to-date.
            if master_email.status == EmailAddressStatus.PREFERRED:
                cache = get_property_cache(account_person)
                cache.preferredemail = master_email
            return account_person

        # Easy, return the `Person` associated with the existing
        # `EmailAddress`.
        return IMasterStore(Person).get(Person, email_address.personID)

    def getByName(self, name, ignore_merged=True):
        """See `IPersonSet`."""
        query = (Person.q.name == name)
        if ignore_merged:
            query = AND(query, Person.q.mergedID == None)
        return Person.selectOne(query)

    def getByAccount(self, account):
        """See `IPersonSet`."""
        return Person.selectOne(Person.q.accountID == account.id)

    def updateStatistics(self, ztm):
        """See `IPersonSet`."""
        stats = getUtility(ILaunchpadStatisticSet)
        people_count = Person.select(
            AND(Person.q.teamownerID == None,
                Person.q.mergedID == None)).count()
        stats.update('people_count', people_count)
        ztm.commit()
        teams_count = Person.select(
            AND(Person.q.teamownerID != None,
                Person.q.mergedID == None)).count()
        stats.update('teams_count', teams_count)
        ztm.commit()

    def peopleCount(self):
        """See `IPersonSet`."""
        return getUtility(ILaunchpadStatisticSet).value('people_count')

    def teamsCount(self):
        """See `IPersonSet`."""
        return getUtility(ILaunchpadStatisticSet).value('teams_count')

    def _teamPrivacyQuery(self):
        """Generate the query needed for privacy filtering.

        If the visibility is not PUBLIC ensure the logged in user is a member
        of the team.
        """
        logged_in_user = getUtility(ILaunchBag).user
        if logged_in_user is not None:
            private_query = SQL("""
                TeamParticipation.person = ?
                AND Person.teamowner IS NOT NULL
                AND Person.visibility != ?
                """, (logged_in_user.id, PersonVisibility.PUBLIC.value))
        else:
            private_query = None

        base_query = SQL("Person.visibility = ?",
                         (PersonVisibility.PUBLIC.value, ),
                         tables=['Person'])

        if private_query is None:
            query = base_query
        else:
            query = Or(base_query, private_query)

        return query

    def _teamEmailQuery(self, text):
        """Product the query for team email addresses."""
        privacy_query = self._teamPrivacyQuery()
        # XXX: BradCrittenden 2009-06-08 bug=244768:  Use Not(Bar.foo == None)
        # instead of Bar.foo != None.
        team_email_query = And(
            privacy_query,
            TeamParticipation.team == Person.id,
            Not(Person.teamowner == None),
            Person.merged == None,
            EmailAddress.person == Person.id,
            EmailAddress.email.lower().startswith(ensure_unicode(text)))
        return team_email_query

    def _teamNameQuery(self, text):
        """Produce the query for team names."""
        privacy_query = self._teamPrivacyQuery()
        # XXX: BradCrittenden 2009-06-08 bug=244768:  Use Not(Bar.foo == None)
        # instead of Bar.foo != None.
        team_name_query = And(
            privacy_query,
            TeamParticipation.team == Person.id,
            Not(Person.teamowner == None),
            Person.merged == None,
            SQL("Person.fti @@ ftq(?)", (text, )))
        return team_name_query

    def find(self, text=""):
        """See `IPersonSet`."""
        if not text:
            # Return an empty result set.
            return EmptyResultSet()

        orderBy = Person._sortingColumnsForSetOperations
        text = ensure_unicode(text).lower()
        # Teams may not have email addresses, so we need to either use a LEFT
        # OUTER JOIN or do a UNION between four queries. Using a UNION makes
        # it a lot faster than with a LEFT OUTER JOIN.
        person_email_query = And(
            Person.teamowner == None,
            Person.merged == None,
            EmailAddress.person == Person.id,
            Person.account == Account.id,
            Not(Account.status.is_in(INACTIVE_ACCOUNT_STATUSES)),
            EmailAddress.email.lower().startswith(text))

        store = IStore(Person)

        # The call to order_by() is necessary to avoid having the default
        # ordering applied.  Since no value is passed the effect is to remove
        # the generation of an 'ORDER BY' clause on the intermediate results.
        # Otherwise the default ordering is taken from the ordering
        # declaration on the class.  The final result set will have the
        # appropriate ordering set.
        results = store.find(
            Person, person_email_query).order_by()

        person_name_query = And(
            Person.teamowner == None,
            Person.merged == None,
            Person.account == Account.id,
            Not(Account.status.is_in(INACTIVE_ACCOUNT_STATUSES)),
            SQL("Person.fti @@ ftq(?)", (text, ))
            )

        results = results.union(store.find(
            Person, person_name_query)).order_by()
        team_email_query = self._teamEmailQuery(text)
        results = results.union(
            store.find(Person, team_email_query)).order_by()
        team_name_query = self._teamNameQuery(text)
        results = results.union(
            store.find(Person, team_name_query)).order_by()

        return results.order_by(orderBy)

    def findPerson(
            self, text="", exclude_inactive_accounts=True,
            must_have_email=False, created_after=None, created_before=None):
        """See `IPersonSet`."""
        orderBy = Person._sortingColumnsForSetOperations
        text = ensure_unicode(text).lower()
        store = IStore(Person)
        base_query = And(
            Person.teamowner == None,
            Person.merged == None)

        clause_tables = []

        if exclude_inactive_accounts:
            clause_tables.append('Account')
            base_query = And(
                base_query,
                Person.account == Account.id,
                Not(Account.status.is_in(INACTIVE_ACCOUNT_STATUSES)))
        email_clause_tables = clause_tables + ['EmailAddress']
        if must_have_email:
            clause_tables = email_clause_tables
            base_query = And(
                base_query,
                EmailAddress.person == Person.id)
        if created_after is not None:
            base_query = And(
                base_query,
                Person.datecreated > created_after)
        if created_before is not None:
            base_query = And(
                base_query,
                Person.datecreated < created_before)

        # Short circuit for returning all users in order
        if not text:
            results = store.find(Person, base_query)
            return results.order_by(Person._storm_sortingColumns)

        # We use a UNION here because this makes things *a lot* faster
        # than if we did a single SELECT with the two following clauses
        # ORed.
        email_query = And(
            base_query,
            EmailAddress.person == Person.id,
            EmailAddress.email.lower().startswith(ensure_unicode(text)))

        name_query = And(
            base_query,
            SQL("Person.fti @@ ftq(?)", (text, )))
        email_results = store.find(Person, email_query).order_by()
        name_results = store.find(Person, name_query).order_by()
        combined_results = email_results.union(name_results)
        return combined_results.order_by(orderBy)

    def findTeam(self, text=""):
        """See `IPersonSet`."""
        orderBy = Person._sortingColumnsForSetOperations
        text = ensure_unicode(text).lower()
        # Teams may not have email addresses, so we need to either use a LEFT
        # OUTER JOIN or do a UNION between two queries. Using a UNION makes
        # it a lot faster than with a LEFT OUTER JOIN.
        email_query = self._teamEmailQuery(text)
        store = IStore(Person)
        email_results = store.find(Person, email_query).order_by()
        name_query = self._teamNameQuery(text)
        name_results = store.find(Person, name_query).order_by()
        combined_results = email_results.union(name_results)
        return combined_results.order_by(orderBy)

    def get(self, personid):
        """See `IPersonSet`."""
        try:
            return Person.get(personid)
        except SQLObjectNotFound:
            return None

    def getByEmail(self, email):
        """See `IPersonSet`."""
        address = self.getByEmails([email]).one()
        if address:
            return address[1]

    def getByEmails(self, emails, include_hidden=True):
        """See `IPersonSet`."""
        if not emails:
            return EmptyResultSet()
        addresses = [
            ensure_unicode(address.lower().strip())
            for address in emails]
        extra_query = True
        if not include_hidden:
            extra_query = Person.hide_email_addresses == False
        return IStore(Person).using(
            Person,
            Join(EmailAddress, EmailAddress.personID == Person.id)
        ).find(
            (EmailAddress, Person),
            EmailAddress.email.lower().is_in(addresses), extra_query)

    def latest_teams(self, limit=5):
        """See `IPersonSet`."""
        orderby = (Desc(Person.datecreated), Desc(Person.id))
        result = IStore(Person).find(
            Person,
            And(
                self._teamPrivacyQuery(),
                TeamParticipation.team == Person.id,
                Person.teamowner != None,
                Person.merged == None))
        return result.order_by(orderby).config(distinct=True)[:limit]

    def _merge_person_decoration(self, to_person, from_person, skip,
        decorator_table, person_pointer_column, additional_person_columns):
        """Merge a table that "decorates" Person.

        Because "person decoration" is becoming more frequent, we create a
        helper function that can be used for tables that decorate person.

        :to_person:       the IPerson that is "real"
        :from_person:     the IPerson that is being merged away
        :skip:            a list of table/column pairs that have been
                          handled
        :decorator_table: the name of the table that decorated Person
        :person_pointer_column:
                          the column on decorator_table that UNIQUE'ly
                          references Person.id
        :additional_person_columns:
                          additional columns in the decorator_table that
                          also reference Person.id but are not UNIQUE

        A Person decorator is a table that uniquely references Person,
        so that the information in the table "extends" the Person table.
        Because the reference to Person is unique, there can only be one
        row in the decorator table for any given Person. This function
        checks if there is an existing decorator for the to_person, and
        if so, it just leaves any from_person decorator in place as
        "noise". Otherwise, it updates any from_person decorator to
        point to the "to_person". There can also be other columns in the
        decorator which point to Person, these are assumed to be
        non-unique and will be updated to point to the to_person
        regardless.
        """
        store = Store.of(to_person)
        # First, update the main UNIQUE pointer row which links the
        # decorator table to Person. We do not update rows if there are
        # already rows in the table that refer to the to_person
        store.execute(
         """UPDATE %(decorator)s
            SET %(person_pointer)s=%(to_id)d
            WHERE %(person_pointer)s=%(from_id)d
              AND ( SELECT count(*) FROM %(decorator)s
                    WHERE %(person_pointer)s=%(to_id)d ) = 0
            """ % {
                'decorator': decorator_table,
                'person_pointer': person_pointer_column,
                'from_id': from_person.id,
                'to_id': to_person.id})

        # Now, update any additional columns in the table which point to
        # Person. Since these are assumed to be NOT UNIQUE, we don't
        # have to worry about multiple rows pointing at the to_person.
        for additional_column in additional_person_columns:
            store.execute(
             """UPDATE %(decorator)s
                SET %(column)s=%(to_id)d
                WHERE %(column)s=%(from_id)d
                """ % {
                    'decorator': decorator_table,
                    'from_id': from_person.id,
                    'to_id': to_person.id,
                    'column': additional_column})
        skip.append(
            (decorator_table.lower(), person_pointer_column.lower()))

    def _mergeAccessPolicyGrant(self, cur, from_id, to_id):
        # Update only the AccessPolicyGrants that will not conflict.
        cur.execute('''
            UPDATE AccessPolicyGrant
            SET grantee=%(to_id)d
            WHERE grantee = %(from_id)d AND (
                policy NOT IN
                    (
                    SELECT policy
                    FROM AccessPolicyGrant
                    WHERE grantee = %(to_id)d
                    )
                OR artifact NOT IN
                    (
                    SELECT artifact
                    FROM AccessPolicyGrant
                    WHERE grantee = %(to_id)d
                    )
                )
            ''' % vars())
        # and delete those left over.
        cur.execute('''
            DELETE FROM AccessPolicyGrant WHERE grantee = %(from_id)d
            ''' % vars())

    def _mergeBranches(self, from_person, to_person):
        # This shouldn't use removeSecurityProxy.
        branches = getUtility(IBranchCollection).ownedBy(from_person)
        for branch in branches.getBranches():
            removeSecurityProxy(branch).setOwner(to_person, to_person)

    def _mergeBranchMergeQueues(self, cur, from_id, to_id):
        cur.execute('''
            UPDATE BranchMergeQueue SET owner = %(to_id)s WHERE owner =
            %(from_id)s''', dict(to_id=to_id, from_id=from_id))

    def _mergeSourcePackageRecipes(self, from_person, to_person):
        # This shouldn't use removeSecurityProxy.
        recipes = from_person.recipes
        existing_names = [r.name for r in to_person.recipes]
        for recipe in recipes:
            new_name = recipe.name
            count = 1
            while new_name in existing_names:
                new_name = '%s-%s' % (recipe.name, count)
                count += 1
            naked_recipe = removeSecurityProxy(recipe)
            naked_recipe.owner = to_person
            naked_recipe.name = new_name

    def _mergeMailingListSubscriptions(self, cur, from_id, to_id):
        # Update MailingListSubscription. Note that since all the from_id
        # email addresses are set to NEW, all the subscriptions must be
        # removed because the user must confirm them.
        cur.execute('''
            DELETE FROM MailingListSubscription WHERE person=%(from_id)d
            ''' % vars())

    def _mergeBranchSubscription(self, cur, from_id, to_id):
        # Update only the BranchSubscription that will not conflict.
        cur.execute('''
            UPDATE BranchSubscription
            SET person=%(to_id)d
            WHERE person=%(from_id)d AND branch NOT IN
                (
                SELECT branch
                FROM BranchSubscription
                WHERE person = %(to_id)d
                )
            ''' % vars())
        # and delete those left over.
        cur.execute('''
            DELETE FROM BranchSubscription WHERE person=%(from_id)d
            ''' % vars())

    def _mergeBugAffectsPerson(self, cur, from_id, to_id):
        # Update only the BugAffectsPerson that will not conflict
        cur.execute('''
            UPDATE BugAffectsPerson
            SET person=%(to_id)d
            WHERE person=%(from_id)d AND bug NOT IN
                (
                SELECT bug
                FROM BugAffectsPerson
                WHERE person = %(to_id)d
                )
            ''' % vars())
        # and delete those left over.
        cur.execute('''
            DELETE FROM BugAffectsPerson WHERE person=%(from_id)d
            ''' % vars())

    def _mergeAnswerContact(self, cur, from_id, to_id):
        # Update only the AnswerContacts that will not conflict.
        cur.execute('''
            UPDATE AnswerContact
            SET person=%(to_id)d
            WHERE person=%(from_id)d
                AND distribution IS NULL
                AND product NOT IN (
                    SELECT product
                    FROM AnswerContact
                    WHERE person = %(to_id)d
                    )
            ''' % vars())
        cur.execute('''
            UPDATE AnswerContact
            SET person=%(to_id)d
            WHERE person=%(from_id)d
                AND distribution IS NOT NULL
                AND (distribution, sourcepackagename) NOT IN (
                    SELECT distribution,sourcepackagename
                    FROM AnswerContact
                    WHERE person = %(to_id)d
                    )
            ''' % vars())
        # and delete those left over.
        cur.execute('''
            DELETE FROM AnswerContact WHERE person=%(from_id)d
            ''' % vars())

    def _mergeQuestionSubscription(self, cur, from_id, to_id):
        # Update only the QuestionSubscriptions that will not conflict.
        cur.execute('''
            UPDATE QuestionSubscription
            SET person=%(to_id)d
            WHERE person=%(from_id)d AND question NOT IN
                (
                SELECT question
                FROM QuestionSubscription
                WHERE person = %(to_id)d
                )
            ''' % vars())
        # and delete those left over.
        cur.execute('''
            DELETE FROM QuestionSubscription WHERE person=%(from_id)d
            ''' % vars())

    def _mergeBugNotificationRecipient(self, cur, from_id, to_id):
        # Update BugNotificationRecipient entries that will not conflict.
        cur.execute('''
            UPDATE BugNotificationRecipient
            SET person=%(to_id)d
            WHERE person=%(from_id)d AND bug_notification NOT IN (
                SELECT bug_notification FROM BugNotificationRecipient
                WHERE person=%(to_id)d
                )
            ''' % vars())
        # and delete those left over.
        cur.execute('''
            DELETE FROM BugNotificationRecipient
            WHERE person=%(from_id)d
            ''' % vars())

    def _mergeStructuralSubscriptions(self, cur, from_id, to_id):
        # Update StructuralSubscription entries that will not conflict.
        # We separate this out from the parent query primarily to help
        # keep within our line length constraints, though it might make
        # things more readable otherwise as well.
        exists_query = '''
            SELECT StructuralSubscription.id
            FROM StructuralSubscription
            WHERE StructuralSubscription.subscriber=%(to_id)d AND (
                StructuralSubscription.product=SSub.product
                OR
                StructuralSubscription.project=SSub.project
                OR
                StructuralSubscription.distroseries=SSub.distroseries
                OR
                StructuralSubscription.milestone=SSub.milestone
                OR
                StructuralSubscription.productseries=SSub.productseries
                OR
                (StructuralSubscription.distribution=SSub.distribution
                 AND StructuralSubscription.sourcepackagename IS NULL
                 AND SSub.sourcepackagename IS NULL)
                OR
                (StructuralSubscription.sourcepackagename=
                    SSub.sourcepackagename
                 AND StructuralSubscription.sourcepackagename=
                    SSub.sourcepackagename)
                )
            '''
        cur.execute(('''
            UPDATE StructuralSubscription
            SET subscriber=%(to_id)d
            WHERE subscriber=%(from_id)d AND id NOT IN (
                SELECT SSub.id
                FROM StructuralSubscription AS SSub
                WHERE
                    SSub.subscriber=%(from_id)d
                    AND EXISTS (''' + exists_query + ''')
            )
            ''') % vars())
        # Delete the rest.  We have to explicitly delete the bug subscription
        # filters first because there is not a cascade delete set up in the
        # db.
        cur.execute('''
            DELETE FROM BugSubscriptionFilter
            WHERE structuralsubscription IN (
                SELECT id
                FROM StructuralSubscription
                WHERE subscriber=%(from_id)d)
            ''' % vars())
        cur.execute('''
            DELETE FROM StructuralSubscription WHERE subscriber=%(from_id)d
            ''' % vars())

    def _mergeSpecificationFeedback(self, cur, from_id, to_id):
        # Update the SpecificationFeedback entries that will not conflict
        # and trash the rest.

        # First we handle the reviewer.
        cur.execute('''
            UPDATE SpecificationFeedback
            SET reviewer=%(to_id)d
            WHERE reviewer=%(from_id)d AND specification NOT IN
                (
                SELECT specification
                FROM SpecificationFeedback
                WHERE reviewer = %(to_id)d
                )
            ''' % vars())
        cur.execute('''
            DELETE FROM SpecificationFeedback WHERE reviewer=%(from_id)d
            ''' % vars())

        # And now we handle the requester.
        cur.execute('''
            UPDATE SpecificationFeedback
            SET requester=%(to_id)d
            WHERE requester=%(from_id)d AND specification NOT IN
                (
                SELECT specification
                FROM SpecificationFeedback
                WHERE requester = %(to_id)d
                )
            ''' % vars())
        cur.execute('''
            DELETE FROM SpecificationFeedback WHERE requester=%(from_id)d
            ''' % vars())

    def _mergeSpecificationSubscription(self, cur, from_id, to_id):
        # Update the SpecificationSubscription entries that will not conflict
        # and trash the rest
        cur.execute('''
            UPDATE SpecificationSubscription
            SET person=%(to_id)d
            WHERE person=%(from_id)d AND specification NOT IN
                (
                SELECT specification
                FROM SpecificationSubscription
                WHERE person = %(to_id)d
                )
            ''' % vars())
        cur.execute('''
            DELETE FROM SpecificationSubscription WHERE person=%(from_id)d
            ''' % vars())

    def _mergeSprintAttendance(self, cur, from_id, to_id):
        # Update only the SprintAttendances that will not conflict
        cur.execute('''
            UPDATE SprintAttendance
            SET attendee=%(to_id)d
            WHERE attendee=%(from_id)d AND sprint NOT IN
                (
                SELECT sprint
                FROM SprintAttendance
                WHERE attendee = %(to_id)d
                )
            ''' % vars())
        # and delete those left over
        cur.execute('''
            DELETE FROM SprintAttendance WHERE attendee=%(from_id)d
            ''' % vars())

    def _mergePOExportRequest(self, cur, from_id, to_id):
        # Update only the POExportRequests that will not conflict
        # and trash the rest
        cur.execute('''
            UPDATE POExportRequest
            SET person=%(to_id)d
            WHERE person=%(from_id)d AND id NOT IN (
                SELECT a.id FROM POExportRequest AS a, POExportRequest AS b
                WHERE a.person = %(from_id)d AND b.person = %(to_id)d
                AND a.potemplate = b.potemplate
                AND a.pofile = b.pofile
                )
            ''' % vars())
        cur.execute('''
            DELETE FROM POExportRequest WHERE person=%(from_id)d
            ''' % vars())

    def _mergeTranslationMessage(self, cur, from_id, to_id):
        # Update the TranslationMessage. They should not conflict since each
        # of them are independent
        cur.execute('''
            UPDATE TranslationMessage
            SET submitter=%(to_id)d
            WHERE submitter=%(from_id)d
            ''' % vars())
        cur.execute('''
            UPDATE TranslationMessage
            SET reviewer=%(to_id)d
            WHERE reviewer=%(from_id)d
            ''' % vars())

    def _mergeTranslationImportQueueEntry(self, cur, from_id, to_id):
        # Update only the TranslationImportQueueEntry that will not conflict
        # and trash the rest
        cur.execute('''
            UPDATE TranslationImportQueueEntry
            SET importer=%(to_id)d
            WHERE importer=%(from_id)d AND id NOT IN (
                SELECT a.id
                FROM TranslationImportQueueEntry AS a,
                     TranslationImportQueueEntry AS b
                WHERE a.importer = %(from_id)d AND b.importer = %(to_id)d
                AND a.distroseries = b.distroseries
                AND a.sourcepackagename = b.sourcepackagename
                AND a.productseries = b.productseries
                AND a.path = b.path
                )
            ''' % vars())
        cur.execute('''
            DELETE FROM TranslationImportQueueEntry WHERE importer=%(from_id)d
            ''' % vars())

    def _mergeCodeReviewVote(self, cur, from_id, to_id):
        # Update only the CodeReviewVote that will not conflict,
        # and leave conflicts as noise
        cur.execute('''
            UPDATE CodeReviewVote
            SET reviewer=%(to_id)d
            WHERE reviewer=%(from_id)d AND id NOT IN (
                SELECT a.id FROM CodeReviewVote AS a, CodeReviewVote AS b
                WHERE a.reviewer = %(from_id)d AND b.reviewer = %(to_id)d
                AND a.branch_merge_proposal = b.branch_merge_proposal
                )
            ''' % vars())

    def _mergeTeamMembership(self, cur, from_id, to_id):
        # Transfer active team memberships
        approved = TeamMembershipStatus.APPROVED
        admin = TeamMembershipStatus.ADMIN
        cur.execute(
            'SELECT team, status FROM TeamMembership WHERE person = %s '
            'AND status IN (%s,%s)'
            % sqlvalues(from_id, approved, admin))
        for team_id, status in cur.fetchall():
            cur.execute('SELECT status FROM TeamMembership WHERE person = %s '
                        'AND team = %s'
                        % sqlvalues(to_id, team_id))
            result = cur.fetchone()
            if result is not None:
                current_status = result[0]
                # Now we can safely delete from_person's membership record,
                # because we know to_person has a membership entry for this
                # team, so may only need to change its status.
                cur.execute(
                    'DELETE FROM TeamMembership WHERE person = %s '
                    'AND team = %s' % sqlvalues(from_id, team_id))

                if current_status == admin.value:
                    # to_person is already an administrator of this team, no
                    # need to do anything else.
                    continue
                # to_person is either an approved or an inactive member,
                # while from_person is either admin or approved. That means we
                # can safely set from_person's membership status on
                # to_person's membership.
                assert status in (approved.value, admin.value)
                cur.execute(
                    'UPDATE TeamMembership SET status = %s WHERE person = %s '
                    'AND team = %s' % sqlvalues(status, to_id, team_id))
            else:
                # to_person is not a member of this team. just change
                # from_person with to_person in the membership record.
                cur.execute(
                    'UPDATE TeamMembership SET person = %s WHERE person = %s '
                    'AND team = %s'
                    % sqlvalues(to_id, from_id, team_id))

        cur.execute('SELECT team FROM TeamParticipation WHERE person = %s '
                    'AND person != team' % sqlvalues(from_id))
        for team_id in cur.fetchall():
            cur.execute(
                'SELECT team FROM TeamParticipation WHERE person = %s '
                'AND team = %s' % sqlvalues(to_id, team_id))
            if not cur.fetchone():
                cur.execute(
                    'UPDATE TeamParticipation SET person = %s WHERE '
                    'person = %s AND team = %s'
                    % sqlvalues(to_id, from_id, team_id))
            else:
                cur.execute(
                    'DELETE FROM TeamParticipation WHERE person = %s AND '
                    'team = %s' % sqlvalues(from_id, team_id))

    def _mergeKarmaCache(self, cur, from_id, to_id, from_karma):
        # Merge the karma total cache so the user does not think the karma
        # was lost.
        params = dict(from_id=from_id, to_id=to_id)
        if from_karma > 0:
            cur.execute('''
                SELECT karma_total FROM KarmaTotalCache
                WHERE person = %(to_id)d
                ''' % params)
            result = cur.fetchone()
            if result is not None:
                # Add the karma to the remaining user.
                params['karma_total'] = from_karma + result[0]
                cur.execute('''
                    UPDATE KarmaTotalCache SET karma_total = %(karma_total)d
                    WHERE person = %(to_id)d
                    ''' % params)
            else:
                # Make the existing karma belong to the remaining user.
                cur.execute('''
                    UPDATE KarmaTotalCache SET person = %(to_id)d
                    WHERE person = %(from_id)d
                    ''' % params)
        # Delete the old caches; the daily job will build them later.
        cur.execute('''
            DELETE FROM KarmaTotalCache WHERE person = %(from_id)d
            ''' % params)
        cur.execute('''
            DELETE FROM KarmaCache WHERE person = %(from_id)d
            ''' % params)

    def _mergeDateCreated(self, cur, from_id, to_id):
        cur.execute('''
            UPDATE Person
            SET datecreated = (
                SELECT MIN(datecreated) FROM Person
                WHERE id in (%(to_id)d, %(from_id)d) LIMIT 1)
            WHERE id = %(to_id)d
            ''' % vars())

    def _purgeUnmergableTeamArtifacts(self, from_team, to_team, reviewer):
        """Purge team artifacts that cannot be merged, but can be removed."""
        # A team cannot have more than one mailing list.
        mailing_list = getUtility(IMailingListSet).get(from_team.name)
        if mailing_list is not None:
            if mailing_list.status in PURGE_STATES:
                from_team.mailing_list.purge()
            elif mailing_list.status != MailingListStatus.PURGED:
                raise AssertionError(
                    "Teams with active mailing lists cannot be merged.")
        # Team email addresses are not transferable.
        from_team.setContactAddress(None)
        # Memberships in the team are not transferable because there
        # is a high probablity there will be a CyclicTeamMembershipError.
        comment = (
            'Deactivating all members as this team is being merged into %s.'
            % to_team.name)
        membershipset = getUtility(ITeamMembershipSet)
        membershipset.deactivateActiveMemberships(
            from_team, comment, reviewer)
        # Memberships in other teams are not transferable because there
        # is a high probablity there will be a CyclicTeamMembershipError.
        all_super_teams = set(from_team.teams_participated_in)
        indirect_super_teams = set(
            from_team.teams_indirectly_participated_in)
        super_teams = all_super_teams - indirect_super_teams
        naked_from_team = removeSecurityProxy(from_team)
        for team in super_teams:
            naked_from_team.retractTeamMembership(team, reviewer)
        IStore(from_team).flush()

    def mergeAsync(self, from_person, to_person, reviewer=None, delete=False):
        """See `IPersonSet`."""
        return getUtility(IPersonMergeJobSource).create(
            from_person=from_person, to_person=to_person, reviewer=reviewer,
            delete=delete)

    def delete(self, from_person, reviewer):
        """See `IPersonSet`."""
        # Deletes are implemented by merging into registry experts. Force
        # the target to prevent any accidental misuse by calling code.
        to_person = getUtility(ILaunchpadCelebrities).registry_experts
        return self._merge(from_person, to_person, reviewer, True)

    def merge(self, from_person, to_person, reviewer=None):
        """See `IPersonSet`."""
        return self._merge(from_person, to_person, reviewer)

    def _merge(self, from_person, to_person, reviewer, delete=False):
        """Helper for merge and delete methods."""
        # since we are doing direct SQL manipulation, make sure all
        # changes have been flushed to the database
        store = Store.of(from_person)
        store.flush()
        if (from_person.is_team and not to_person.is_team
            or not from_person.is_team and to_person.is_team):
            raise AssertionError("Users cannot be merged with teams.")
        if from_person.is_team and reviewer is None:
            raise AssertionError("Team merged require a reviewer.")
        if getUtility(IArchiveSet).getPPAOwnedByPerson(
            from_person, statuses=[ArchiveStatus.ACTIVE,
                                   ArchiveStatus.DELETING]) is not None:
            raise AssertionError(
                'from_person has a ppa in ACTIVE or DELETING status')
        if from_person.is_team:
            self._purgeUnmergableTeamArtifacts(
                from_person, to_person, reviewer)
        if getUtility(IEmailAddressSet).getByPerson(from_person).count() > 0:
            raise AssertionError('from_person still has email addresses.')

        # Get a database cursor.
        cur = cursor()

        # These table.columns will be skipped by the 'catch all'
        # update performed later
        skip = [
            ('teammembership', 'person'),
            ('teammembership', 'team'),
            ('teamparticipation', 'person'),
            ('teamparticipation', 'team'),
            ('personlanguage', 'person'),
            ('person', 'merged'),
            ('personsettings', 'person'),
            ('emailaddress', 'person'),
            # Polls are not carried over when merging teams.
            ('poll', 'team'),
            # We can safely ignore the mailinglist table as there's a sanity
            # check above which prevents teams with associated mailing lists
            # from being merged.
            ('mailinglist', 'team'),
            # I don't think we need to worry about the votecast and vote
            # tables, because a real human should never have two profiles
            # in Launchpad that are active members of a given team and voted
            # in a given poll. -- GuilhermeSalgado 2005-07-07
            # We also can't afford to change poll results after they are
            # closed -- StuartBishop 20060602
            ('votecast', 'person'),
            ('vote', 'person'),
            ('translationrelicensingagreement', 'person'),
            # These are ON DELETE CASCADE and maintained by triggers.
            ('bugsummary', 'viewed_by'),
            ('bugsummaryjournal', 'viewed_by'),
            ]

        references = list(postgresql.listReferences(cur, 'person', 'id'))

        # Sanity check. If we have an indirect reference, it must
        # be ON DELETE CASCADE. We only have one case of this at the moment,
        # but this code ensures we catch any new ones added incorrectly.
        for src_tab, src_col, ref_tab, ref_col, updact, delact in references:
            # If the ref_tab and ref_col is not Person.id, then we have
            # an indirect reference. Ensure the update action is 'CASCADE'
            if ref_tab != 'person' and ref_col != 'id':
                if updact != 'c':
                    raise RuntimeError(
                        '%s.%s reference to %s.%s must be ON UPDATE CASCADE'
                        % (src_tab, src_col, ref_tab, ref_col))

        # These rows are in a UNIQUE index, and we can only move them
        # to the new Person if there is not already an entry. eg. if
        # the destination and source persons are both subscribed to a bug,
        # we cannot change the source persons subscription. We just leave them
        # as noise for the time being.

        to_id = to_person.id
        from_id = from_person.id

        # Update PersonLocation, which is a Person-decorator table.
        self._merge_person_decoration(
            to_person, from_person, skip, 'PersonLocation', 'person',
            ['last_modified_by', ])

        # Update GPGKey. It won't conflict, but our sanity checks don't
        # know that.
        cur.execute(
            'UPDATE GPGKey SET owner=%(to_id)d WHERE owner=%(from_id)d'
            % vars())
        skip.append(('gpgkey', 'owner'))

        self._mergeAccessPolicyGrant(cur, from_id, to_id)
        skip.append(('accesspolicygrant', 'grantee'))

        # Update the Branches that will not conflict, and fudge the names of
        # ones that *do* conflict.
        self._mergeBranches(from_person, to_person)
        skip.append(('branch', 'owner'))

        self._mergeBranchMergeQueues(cur, from_id, to_id)
        skip.append(('branchmergequeue', 'owner'))

        self._mergeSourcePackageRecipes(from_person, to_person)
        skip.append(('sourcepackagerecipe', 'owner'))

        self._mergeMailingListSubscriptions(cur, from_id, to_id)
        skip.append(('mailinglistsubscription', 'person'))

        self._mergeBranchSubscription(cur, from_id, to_id)
        skip.append(('branchsubscription', 'person'))

        self._mergeBugAffectsPerson(cur, from_id, to_id)
        skip.append(('bugaffectsperson', 'person'))

        self._mergeAnswerContact(cur, from_id, to_id)
        skip.append(('answercontact', 'person'))

        self._mergeQuestionSubscription(cur, from_id, to_id)
        skip.append(('questionsubscription', 'person'))

        self._mergeBugNotificationRecipient(cur, from_id, to_id)
        skip.append(('bugnotificationrecipient', 'person'))

        # We ignore BugSubscriptionFilterMutes.
        skip.append(('bugsubscriptionfiltermute', 'person'))

        # We ignore BugMutes.
        skip.append(('bugmute', 'person'))

        self._mergeStructuralSubscriptions(cur, from_id, to_id)
        skip.append(('structuralsubscription', 'subscriber'))

        self._mergeSpecificationFeedback(cur, from_id, to_id)
        skip.append(('specificationfeedback', 'reviewer'))
        skip.append(('specificationfeedback', 'requester'))

        self._mergeSpecificationSubscription(cur, from_id, to_id)
        skip.append(('specificationsubscription', 'person'))

        self._mergeSprintAttendance(cur, from_id, to_id)
        skip.append(('sprintattendance', 'attendee'))

        self._mergePOExportRequest(cur, from_id, to_id)
        skip.append(('poexportrequest', 'person'))

        self._mergeTranslationMessage(cur, from_id, to_id)
        skip.append(('translationmessage', 'submitter'))
        skip.append(('translationmessage', 'reviewer'))

        # Handle the POFileTranslator cache by doing nothing. As it is
        # maintained by triggers, the data migration has already been done
        # for us when we updated the source tables.
        skip.append(('pofiletranslator', 'person'))

        self._mergeTranslationImportQueueEntry(cur, from_id, to_id)
        skip.append(('translationimportqueueentry', 'importer'))

        # XXX cprov 2007-02-22 bug=87098:
        # Since we only allow one PPA for each user,
        # we can't reassign the old user archive to the new user.
        # It need to be done manually, probably by reasinning all publications
        # to the old PPA to the new one, performing a careful_publishing on it
        # and removing the old one from disk.
        skip.append(('archive', 'owner'))

        self._mergeCodeReviewVote(cur, from_id, to_id)
        skip.append(('codereviewvote', 'reviewer'))

        self._mergeKarmaCache(cur, from_id, to_id, from_person.karma)
        skip.append(('karmacache', 'person'))
        skip.append(('karmatotalcache', 'person'))

        self._mergeDateCreated(cur, from_id, to_id)

        # Sanity check. If we have a reference that participates in a
        # UNIQUE index, it must have already been handled by this point.
        # We can tell this by looking at the skip list.
        for src_tab, src_col, ref_tab, ref_col, updact, delact in references:
            uniques = postgresql.listUniques(cur, src_tab, src_col)
            if len(uniques) > 0 and (src_tab, src_col) not in skip:
                raise NotImplementedError(
                        '%s.%s reference to %s.%s is in a UNIQUE index '
                        'but has not been handled' % (
                            src_tab, src_col, ref_tab, ref_col))

        # Handle all simple cases
        for src_tab, src_col, ref_tab, ref_col, updact, delact in references:
            if (src_tab, src_col) in skip:
                continue
            cur.execute('UPDATE %s SET %s=%d WHERE %s=%d' % (
                src_tab, src_col, to_person.id, src_col, from_person.id))

        self._mergeTeamMembership(cur, from_id, to_id)

        # Flag the person as merged
        cur.execute('''
            UPDATE Person SET merged=%(to_id)d WHERE id=%(from_id)d
            ''' % vars())

        # Append a -merged suffix to the person's name.
        name = base = "%s-merged" % from_person.name.encode('ascii')
        cur.execute("SELECT id FROM Person WHERE name = %s" % sqlvalues(name))
        i = 1
        while cur.fetchone():
            name = "%s%d" % (base, i)
            cur.execute("SELECT id FROM Person WHERE name = %s"
                        % sqlvalues(name))
            i += 1
        cur.execute("UPDATE Person SET name = %s WHERE id = %s"
                    % sqlvalues(name, from_person))

        # Since we've updated the database behind Storm's back,
        # flush its caches.
        store.invalidate()

        # Move OpenId Identifiers from the merged account to the new
        # account.
        if from_person.account is not None and to_person.account is not None:
            store.execute("""
                UPDATE OpenIdIdentifier SET account=%s WHERE account=%s
                """ % sqlvalues(to_person.accountID, from_person.accountID))

        if delete:
            # We don't notify anyone about deletes.
            return

        # Inform the user of the merge changes.
        if to_person.is_team:
            mail_text = get_email_template(
                'team-merged.txt', app='registry')
            subject = 'Launchpad teams merged'
        else:
            mail_text = get_email_template(
                'person-merged.txt', app='registry')
            subject = 'Launchpad accounts merged'
        mail_text = mail_text % {
            'dupename': from_person.name,
            'person': to_person.name,
            }
        getUtility(IPersonNotificationSet).addNotification(
            to_person, subject, mail_text)

    def getValidPersons(self, persons):
        """See `IPersonSet.`"""
        person_ids = [person.id for person in persons]
        if len(person_ids) == 0:
            return []

        # This has the side effect of sucking in the ValidPersonCache
        # items into the cache, allowing Person.is_valid_person calls to
        # not hit the DB.
        valid_person_ids = set(
                person_id.id for person_id in ValidPersonCache.select(
                    "id IN %s" % sqlvalues(person_ids)))
        return [
            person for person in persons if person.id in valid_person_ids]

    def getPeopleWithBranches(self, product=None):
        """See `IPersonSet`."""
        branch_clause = 'SELECT owner FROM Branch'
        if product is not None:
            branch_clause += ' WHERE product = %s' % quote(product)
        return Person.select('''
            Person.id in (%s)
            ''' % branch_clause)

    def updatePersonalStandings(self):
        """See `IPersonSet`."""
        cur = cursor()
        cur.execute("""
        UPDATE Person
        SET personal_standing = %s
        WHERE personal_standing = %s
        AND id IN (
            SELECT posted_by
            FROM MessageApproval
            WHERE status = %s
            GROUP BY posted_by
            HAVING COUNT(DISTINCT mailing_list) >= %s
            )
        """ % sqlvalues(PersonalStanding.GOOD,
                        PersonalStanding.UNKNOWN,
                        PostedMessageStatus.APPROVED,
                        config.standingupdater.approvals_needed))

    def cacheBrandingForPeople(self, people):
        """See `IPersonSet`."""
        aliases = []
        aliases.extend(person.iconID for person in people
                       if person.iconID is not None)
        aliases.extend(person.logoID for person in people
                       if person.logoID is not None)
        aliases.extend(person.mugshotID for person in people
                       if person.mugshotID is not None)
        if not aliases:
            return
        # Listify, since this is a pure cache.
        list(LibraryFileAlias.select("LibraryFileAlias.id IN %s"
             % sqlvalues(aliases), prejoins=["content"]))

    def getPrecachedPersonsFromIDs(
        self, person_ids, need_karma=False, need_ubuntu_coc=False,
        need_location=False, need_archive=False,
        need_preferred_email=False, need_validity=False, need_icon=False):
        """See `IPersonSet`."""
        origin = [Person]
        conditions = [
            Person.id.is_in(person_ids)]
        return self._getPrecachedPersons(
            origin, conditions,
            need_karma=need_karma, need_ubuntu_coc=need_ubuntu_coc,
            need_location=need_location, need_archive=need_archive,
            need_preferred_email=need_preferred_email,
            need_validity=need_validity, need_icon=need_icon)

    def _getPrecachedPersons(
        self, origin, conditions, store=None,
        need_karma=False, need_ubuntu_coc=False,
        need_location=False, need_archive=False, need_preferred_email=False,
        need_validity=False, need_icon=False):
        """Lookup all members of the team with optional precaching.

        :param store: Provide ability to specify the store.
        :param origin: List of storm tables and joins. This list will be
            appended to. The Person table is required.
        :param conditions: Storm conditions for tables in origin.
        :param need_karma: The karma attribute will be cached.
        :param need_ubuntu_coc: The is_ubuntu_coc_signer attribute will be
            cached.
        :param need_location: The location attribute will be cached.
        :param need_archive: The archive attribute will be cached.
        :param need_preferred_email: The preferred email attribute will be
            cached.
        :param need_validity: The is_valid attribute will be cached.
        :param need_icon: Cache the persons' icons so that their URLs can
            be generated without further reference to the database.
        """
        if store is None:
            store = IStore(Person)
        columns = [Person]
        decorators = []
        if need_karma:
            # New people have no karmatotalcache rows.
            origin.append(
                LeftJoin(KarmaTotalCache,
                    KarmaTotalCache.person == Person.id))
            columns.append(KarmaTotalCache)
        if need_ubuntu_coc:
            columns.append(Alias(Exists(Select(SignedCodeOfConduct,
                And(Person._is_ubuntu_coc_signer_condition(),
                    SignedCodeOfConduct.ownerID == Person.id))),
                name='is_ubuntu_coc_signer'))
        if need_location:
            # New people have no location rows
            origin.append(
                LeftJoin(PersonLocation,
                    PersonLocation.person == Person.id))
            columns.append(PersonLocation)
        if need_archive:
            # Not everyone has PPAs.
            # It would be nice to cleanly expose the soyuz rules for this to
            # avoid duplicating the relationships.
            archive_conditions = Or(
                Archive.id == None,
                And(
                    Archive.owner == Person.id,
                    Archive.id == Select(
                        tables=Archive,
                        columns=Min(Archive.id),
                        where=And(
                            Archive.purpose == ArchivePurpose.PPA,
                            Archive.owner == Person.id))))
            origin.append(
                LeftJoin(Archive, archive_conditions))
            columns.append(Archive)

        # Checking validity requires having a preferred email.
        if need_preferred_email and not need_validity:
            # Teams don't have email, so a left join
            origin.append(
                LeftJoin(EmailAddress, EmailAddress.person == Person.id))
            columns.append(EmailAddress)
            conditions = And(conditions,
                Or(EmailAddress.status == None,
                   EmailAddress.status == EmailAddressStatus.PREFERRED))
        if need_validity:
            valid_stuff = Person._validity_queries()
            origin.extend(valid_stuff["joins"])
            columns.extend(valid_stuff["tables"])
            decorators.extend(valid_stuff["decorators"])
        if need_icon:
            IconAlias = ClassAlias(LibraryFileAlias, "LibraryFileAlias")
            origin.append(LeftJoin(IconAlias, Person.icon == IconAlias.id))
            columns.append(IconAlias)
        if len(columns) == 1:
            column = columns[0]
            # Return a simple ResultSet
            return store.using(*origin).find(column, conditions)
        # Adapt the result into a cached Person.
        columns = tuple(columns)
        raw_result = store.using(*origin).find(columns, conditions)

        def prepopulate_person(row):
            result = row[0]
            cache = get_property_cache(result)
            index = 1
            #-- karma caching
            if need_karma:
                karma = row[index]
                index += 1
                if karma is None:
                    karma_total = 0
                else:
                    karma_total = karma.karma_total
                cache.karma = karma_total
            #-- ubuntu code of conduct signer status caching.
            if need_ubuntu_coc:
                signed = row[index]
                index += 1
                cache.is_ubuntu_coc_signer = signed
            #-- location caching
            if need_location:
                location = row[index]
                index += 1
                cache.location = location
            #-- archive caching
            if need_archive:
                archive = row[index]
                index += 1
                cache.archive = archive
            #-- preferred email caching
            if need_preferred_email and not need_validity:
                email = row[index]
                index += 1
                cache.preferredemail = email
            for decorator in decorators:
                column = row[index]
                index += 1
                decorator(result, column)
            return result
        return DecoratedResultSet(raw_result,
            result_decorator=prepopulate_person)


# Provide a storm alias from Person to Owner. This is useful in queries on
# objects that have more than just an owner associated with them.
Owner = ClassAlias(Person, 'Owner')


class PersonLanguage(SQLBase):
    _table = 'PersonLanguage'

    person = ForeignKey(foreignKey='Person', dbName='person', notNull=True)
    language = ForeignKey(foreignKey='Language', dbName='language',
                          notNull=True)


class SSHKey(SQLBase):
    implements(ISSHKey)
    _defaultOrder = ["person", "keytype", "keytext"]

    _table = 'SSHKey'

    person = ForeignKey(foreignKey='Person', dbName='person', notNull=True)
    keytype = EnumCol(dbName='keytype', notNull=True, enum=SSHKeyType)
    keytext = StringCol(dbName='keytext', notNull=True)
    comment = StringCol(dbName='comment', notNull=True)


class SSHKeySet:
    implements(ISSHKeySet)

    def new(self, person, sshkey):
        try:
            kind, keytext, comment = sshkey.split(' ', 2)
        except (ValueError, AttributeError):
            raise SSHKeyAdditionError

        if not (kind and keytext and comment):
            raise SSHKeyAdditionError

        process = subprocess.Popen(
            '/usr/bin/ssh-vulnkey -', shell=True, stdin=subprocess.PIPE,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (out, err) = process.communicate(sshkey.encode('utf-8'))
        if 'compromised' in out.lower():
            raise SSHKeyCompromisedError

        if kind == 'ssh-rsa':
            keytype = SSHKeyType.RSA
        elif kind == 'ssh-dss':
            keytype = SSHKeyType.DSA
        else:
            raise SSHKeyAdditionError

        return SSHKey(person=person, keytype=keytype, keytext=keytext,
                      comment=comment)

    def getByID(self, id, default=None):
        try:
            return SSHKey.get(id)
        except SQLObjectNotFound:
            return default

    def getByPeople(self, people):
        """See `ISSHKeySet`"""
        return SSHKey.select("""
            SSHKey.person IN %s
            """ % sqlvalues([person.id for person in people]))


class WikiName(SQLBase, HasOwnerMixin):
    implements(IWikiName)

    _table = 'WikiName'

    person = ForeignKey(dbName='person', foreignKey='Person', notNull=True)
    wiki = StringCol(dbName='wiki', notNull=True)
    wikiname = StringCol(dbName='wikiname', notNull=True)

    @property
    def url(self):
        return self.wiki + self.wikiname


class WikiNameSet:
    implements(IWikiNameSet)

    def getByWikiAndName(self, wiki, wikiname):
        """See `IWikiNameSet`."""
        return WikiName.selectOneBy(wiki=wiki, wikiname=wikiname)

    def get(self, id):
        """See `IWikiNameSet`."""
        try:
            return WikiName.get(id)
        except SQLObjectNotFound:
            return None

    def new(self, person, wiki, wikiname):
        """See `IWikiNameSet`."""
        return WikiName(person=person, wiki=wiki, wikiname=wikiname)


class JabberID(SQLBase, HasOwnerMixin):
    implements(IJabberID)

    _table = 'JabberID'
    _defaultOrder = ['jabberid']

    person = ForeignKey(dbName='person', foreignKey='Person', notNull=True)
    jabberid = StringCol(dbName='jabberid', notNull=True)


class JabberIDSet:
    implements(IJabberIDSet)

    def new(self, person, jabberid):
        """See `IJabberIDSet`"""
        return JabberID(person=person, jabberid=jabberid)

    def getByJabberID(self, jabberid):
        """See `IJabberIDSet`"""
        return JabberID.selectOneBy(jabberid=jabberid)

    def getByPerson(self, person):
        """See `IJabberIDSet`"""
        return JabberID.selectBy(person=person)


class IrcID(SQLBase, HasOwnerMixin):
    """See `IIrcID`"""
    implements(IIrcID)

    _table = 'IrcID'

    person = ForeignKey(dbName='person', foreignKey='Person', notNull=True)
    network = StringCol(dbName='network', notNull=True)
    nickname = StringCol(dbName='nickname', notNull=True)


class IrcIDSet:
    """See `IIrcIDSet`"""
    implements(IIrcIDSet)

    def get(self, id):
        """See `IIrcIDSet`"""
        try:
            return IrcID.get(id)
        except SQLObjectNotFound:
            return None

    def new(self, person, network, nickname):
        """See `IIrcIDSet`"""
        return IrcID(person=person, network=network, nickname=nickname)


class NicknameGenerationError(Exception):
    """I get raised when something went wrong generating a nickname."""


def _is_nick_registered(nick):
    """Answer the question: is this nick registered?"""
    return PersonSet().getByName(nick) is not None


def generate_nick(email_addr, is_registered=_is_nick_registered):
    """Generate a LaunchPad nick from the email address provided.

    See lp.app.validators.name for the definition of a
    valid nick.

    It is technically possible for this function to raise a
    NicknameGenerationError, but this will only occur if an operator
    has majorly screwed up the name blacklist.
    """
    email_addr = email_addr.strip().lower()

    if not valid_email(email_addr):
        raise NicknameGenerationError("%s is not a valid email address"
                                      % email_addr)

    user = re.match("^(\S+)@(?:\S+)$", email_addr).groups()[0]
    user = user.replace(".", "-").replace("_", "-")

    person_set = PersonSet()

    def _valid_nick(nick):
        if not valid_name(nick):
            return False
        elif is_registered(nick):
            return False
        elif person_set.isNameBlacklisted(nick):
            return False
        else:
            return True

    generated_nick = sanitize_name(user)
    if _valid_nick(generated_nick):
        return generated_nick

    # We seed the random number generator so we get consistent results,
    # making the algorithm repeatable and thus testable.
    random_state = random.getstate()
    random.seed(sum(ord(letter) for letter in generated_nick))
    try:
        attempts = 0
        prefix = ''
        suffix = ''
        mutated_nick = [letter for letter in generated_nick]
        chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
        while attempts < 1000:
            attempts += 1

            # Prefer a nickname with a suffix
            suffix += random.choice(chars)
            if _valid_nick(generated_nick + '-' + suffix):
                return generated_nick + '-' + suffix

            # Next a prefix
            prefix += random.choice(chars)
            if _valid_nick(prefix + '-' + generated_nick):
                return prefix + '-' + generated_nick

            # Or a mutated character
            index = random.randint(0, len(mutated_nick) - 1)
            mutated_nick[index] = random.choice(chars)
            if _valid_nick(''.join(mutated_nick)):
                return ''.join(mutated_nick)

            # Or a prefix + generated + suffix
            if _valid_nick(prefix + '-' + generated_nick + '-' + suffix):
                return prefix + '-' + generated_nick + '-' + suffix

            # Or a prefix + mutated + suffix
            if _valid_nick(
                    prefix + '-' + ''.join(mutated_nick) + '-' + suffix):
                return prefix + '-' + ''.join(mutated_nick) + '-' + suffix

        raise NicknameGenerationError(
            "No nickname could be generated. "
            "This should be impossible to trigger unless some twonk has "
            "registered a match everything regexp in the black list.")

    finally:
        random.setstate(random_state)


@adapter(IAccount)
@implementer(IPerson)
def person_from_account(account):
    """Adapt an `IAccount` into an `IPerson`.

    If there is a current browser request, we cache the looked up Person in
    the request's annotations so that we don't have to hit the DB once again
    when further adaptation is needed.  We know this cache may cross
    transaction boundaries, but this should not be a problem as the Person ->
    Account link can't be changed.

    This cache is necessary because our security adapters may need to adapt
    the Account representing the logged in user into an IPerson multiple
    times.
    """
    request = get_current_browser_request()
    person = None
    # First we try to get the person from the cache, but only if there is a
    # browser request.
    if request is not None:
        cache = request.annotations.setdefault(
            'launchpad.person_to_account_cache', weakref.WeakKeyDictionary())
        person = cache.get(account)

    # If it's not in the cache, then we get it from the database, and in that
    # case, if there is a browser request, we also store that person in the
    # cache.
    if person is None:
        person = IStore(Person).find(Person, account=account).one()
        if request is not None:
            cache[account] = person

    if person is None:
        raise ComponentLookupError()
    return person


@ProxyFactory
def get_recipients(person):
    """Return a set of people who receive email for this Person (person/team).

    If <person> has a preferred email, the set will contain only that
    person.  If <person> doesn't have a preferred email but is a team,
    the set will contain the preferred email address of each member of
    <person>, including indirect members, that has an active account and an
    preferred (active) address.

    Finally, if <person> doesn't have a preferred email and is not a team,
    the set will be empty.
    """
    if person.preferredemail:
        return [person]
    elif person.is_team:
        # Get transitive members of a team that does not itself have a
        # preferred email.
        return _get_recipients_for_team(person)
    else:
        return []


def _get_recipients_for_team(team):
    """Given a team without a preferred email, return recipients.

    Helper for get_recipients, divided out simply to make get_recipients
    easier to understand in broad brush."""
    store = IStore(Person)
    source = store.using(TeamMembership,
                         Join(Person,
                              TeamMembership.personID == Person.id),
                         LeftJoin(EmailAddress,
                                  And(
                                      EmailAddress.person == Person.id,
                                      EmailAddress.status ==
                                        EmailAddressStatus.PREFERRED)),
                         LeftJoin(Account,
                             Person.accountID == Account.id))
    pending_team_ids = [team.id]
    recipient_ids = set()
    seen = set()
    while pending_team_ids:
        # Find Persons that have a preferred email address and an active
        # account, or are a team, or both.
        intermediate_transitive_results = source.find(
            (TeamMembership.personID, EmailAddress.personID),
            In(TeamMembership.status,
               [TeamMembershipStatus.ADMIN.value,
                TeamMembershipStatus.APPROVED.value]),
            In(TeamMembership.teamID, pending_team_ids),
            Or(
                And(EmailAddress.personID != None,
                    Account.status == AccountStatus.ACTIVE),
                Person.teamownerID != None)).config(distinct=True)
        next_ids = []
        for (person_id,
             preferred_email_marker) in intermediate_transitive_results:
            if preferred_email_marker is None:
                # This is a team without a preferred email address.
                if person_id not in seen:
                    next_ids.append(person_id)
                    seen.add(person_id)
            else:
                recipient_ids.add(person_id)
        pending_team_ids = next_ids
    return getUtility(IPersonSet).getPrecachedPersonsFromIDs(
        recipient_ids,
        need_validity=True,
        need_preferred_email=True)