Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: UTF-8 -*- 

# Copyright 2009-2016 Luc Saffre. 

# License: BSD, see LICENSE for more details. 

 

"""Defines the :class:`Site` class. For an overview see 

:doc:`/dev/site` and :doc:`/dev/plugins` . 

 

.. This document is part of the Lino test suite. You can test only 

    this document using:: 

 

        $ python setup.py test -s tests.CoreTests.test_site 

        $ python lino/core/site.py 

 

    doctest init: 

    >>> from __future__ import unicode_literals 

    >>> import lino 

    >>> lino.startup('lino.projects.docs.settings') 

 

""" 

from __future__ import unicode_literals 

from future import standard_library 

standard_library.install_aliases() 

from builtins import map 

import six 

# str = six.text_type 

from builtins import str 

# from past.builtins import basestring 

# from builtins import object 

 

import os 

from os.path import normpath, dirname, join, isdir, relpath, exists 

import inspect 

import datetime 

import warnings 

import collections 

import threading 

from importlib import import_module 

from urllib.parse import urlencode 

from lino import AFTER17 

if AFTER17: 

    from django.apps import AppConfig 

 

from unipath import Path 

from atelier.utils import AttrDict, date_offset ,tuple_py2 

from atelier import rstgen 

 

from django.utils.translation import ugettext_lazy as _ 

from django.utils.translation import get_language 

# from django.core.exceptions import ImproperlyConfigured 

 

from lino.core.plugin import Plugin 

 

from lino import assert_django_code, DJANGO_DEFAULT_LANGUAGE 

from lino.utils.xmlgen.html import E 

from lino.core.exceptions import ChangedAPI 

# from .roles import SiteUser 

 

startup_rlock = threading.RLock()  # Lock() or RLock()? 

 

LanguageInfo = collections.namedtuple( 

    'LanguageInfo', ('django_code', 'name', 'index', 'suffix')) 

""" 

A named tuple with four fields: 

 

- `django_code` -- how Django calls this language 

- `name` --        how Lino calls it 

- `index` --       the position in the :attr:`Site.languages` tuple 

- `suffix` --      the suffix to append to babel fields for this language 

 

""" 

 

 

def to_locale(language): 

    """Simplified copy of `django.utils.translation.to_locale`, but we 

    need it while the `settings` module is being loaded, i.e. we 

    cannot yet import django.utils.translation.  Also we don't need 

    the to_lower argument. 

 

    """ 

    p = language.find('-') 

    if p >= 0: 

        # Get correct locale for sr-latn 

        if len(language[p + 1:]) > 2: 

            return language[:p].lower() + '_' \ 

                + language[p + 1].upper() + language[p + 2:].lower() 

        return language[:p].lower() + '_' + language[p + 1:].upper() 

    else: 

        return language.lower() 

 

 

def class2str(cl): 

    return cl.__module__ + '.' + cl.__name__ 

 

gettext_noop = lambda s: s 

 

PLUGIN_CONFIGS = {} 

 

 

def configure_plugin(app_label, **kwargs): 

    """Set one ore several configuration settings of the given plugin 

    *before* the :setting:`SITE` has been instantiated. 

    See :doc:`/dev/plugins`. 

 

    """ 

    # if PLUGIN_CONFIGS is None: 

    #     raise ImproperlyConfigured( 

    #         "Tried to call configure_plugin after Site instantiation") 

    cfg = PLUGIN_CONFIGS.setdefault(app_label, {}) 

    cfg.update(kwargs) 

 

 

# from django.db.models.fields import NOT_PROVIDED 

class NOT_PROVIDED(object): 

    pass 

 

 

class Site(object): 

    """The base class for a Lino application.  This class is designed to 

    be overridden by both application developers and local site 

    administrators.  Your :setting:`SITE` setting is expected to 

    contain an instance of a subclass of this. 

 

    .. attribute:: modules 

 

        An :class:`AttrDict <atelier.utils.AttrDict>` which maps every 

        installed `app_label` to the corresponding :xfile:`models.py` 

        module object. 

 

        This is also available as the shortcut :attr:`rt.modules 

        <lino.api.rt.modules>`. 

 

        See :doc:`/dev/plugins` 

 

    .. attribute:: LANGUAGE_CHOICES 

     

        A tuple in the format expected by Django's `choices 

        <https://docs.djangoproject.com/en/1.7/ref/models/fields/#choices>`__ 

        attribute, used e.g. by :class:`LanguageField 

        <lino.utils.mldbc.fields.LanguageField>`. It's content is 

        automatically populated from :attr:`languages` and application 

        code should not change it's value. 

 

    """ 

 

    auto_fit_column_widths = True 

    """The default value for the 

    :attr:`auto_fit_column_widths<lino.core.tables.AbstractTable.auto_fit_column_widths>` 

    of tables in this application. 

 

    """ 

 

    confdirs = None 

    """This attribute is available only after site startup.  See 

    :mod:`lino.utils.config`. 

    """ 

 

    kernel = None 

    """ 

    This attribute is available only after :meth:`startup`. 

    See :mod:`lino.core.kernel`. 

 

    """ 

 

    ui = None 

    """ 

    Deprecated alias for :attr:`kernel`. 

 

    """ 

 

    readonly = False 

    """Setting this to `True` turns this site in a readonly site.  This 

    means that :setting:`DATABASES` must point to the 

    :setting:`DATABASES` of some other (non-readonly) site, and that 

    :manage:`initdb` will do nothing. 

 

    """ 

 

    the_demo_date = None 

    """A hard-coded constant date to be used as reference by :meth:`today` 

    and :meth:`demo_date`. For example many demo databases have this 

    set because certain tests rely on a constant reference date. 

 

    """ 

 

    title = None 

    """The title of this web site to appear in the browser window.  If 

    this is None, Lino will use :attr:`verbose_name` as default value. 

 

    """ 

 

    verbose_name = "yet another Lino application" 

    """The name of this application, to be displayed to end-users at 

    different places. 

 

    Note the difference between :attr:`title` and 

    :attr:`verbose_name`: 

 

    - :attr:`title` may be None, :attr:`verbose_name` not. 

 

    - :attr:`title` is used by the 

      :srcref:`index.html <lino/modlib/extjs/config/extjs/index.html>` for 

      :mod:`lino.modlib.extjs`. 

 

    - :attr:`title` and :attr:`verbose_name` are used by 

      :xfile:`admin_main.html` to generate the fragments "Welcome to the 

      **title** site" and "We are running **verbose_name** version 

      **x.y**"  (the latter only if :attr:`version` is set). 

 

    - :meth:`site_version` uses :attr:`verbose_name` (not :attr:`title`) 

 

    IOW, the :attr:`title` is rather for usage by local system 

    administrators, while the :attr:`verbose_name` is rather for usage 

    by application developers. 

 

    """ 

 

    version = None 

    "The version number." 

 

    url = None 

    """ 

    The URL of the website that describes this application. 

    Used e.g. in a :menuselection:`Site --> About` dialog box. 

    """ 

 

    server_url = None 

    """The "official" URL used by "normal" users when accessing this Lino 

    site. This is used by templates such as the email sent by 

    :class:`lino.modlib.notifier.Notification` 

 

    """ 

 

    obj2text_template = "*{0}*" 

    """The format template to use when rendering a ForeignKey as plain 

    text. 

 

    Note: reSTructuredText uses *italic* and **bold**.  Changing this 

    can cause lots of trivial failures in test suites.  It is also 

    used by :mod:`lino.modlib.notifier` when generating the mail body. 

 

    """ 

    make_missing_dirs = True 

    """ 

    Set this to `False` if you don't want this Site to automatically 

    create missing directories when needed (but to raise an exception 

    in these cases, asking you to create it yourself) 

 

    """ 

    userdocs_prefix = '' 

 

    project_name = None 

    """A nickname for this project. This is used to set :attr:`cache_dir` 

    and therefore should be unique for all Lino projects on a given 

    machine. 

 

    If this is None, Lino will find a default value by splitting 

    :attr:`project_dir` and taking the last part (or the second-last 

    if the last part is 'settings'. 

 

    """ 

 

    cache_dir = None 

    """The directory where Lino will create temporary data for this 

    project, including the :xfile:`media` directory and the 

    :xfile:`default.db` file. 

 

    This is either the same as :attr:`project_dir` or (if 

    :envvar:`LINO_CACHE_ROOT` is set), will be set to 

    :envvar:`LINO_CACHE_ROOT` + :attr:`project_name`. 

 

    """ 

 

    project_dir = None 

    """Full path to your local project directory.  Local subclasses should 

    not override this variable. 

     

    Lino sets this to the directory of the :xfile:`settings.py` file 

    (or however your :envvar:`DJANGO_SETTINGS_MODULE` is named). 

 

    If it countains a :xfile:`config` directory, this will be added to 

    the config search path. 

 

    """ 

 

    languages = None 

    """The language distribution used on this site.  It has its own 

    chapter :doc:`/dev/languages` in the Developers Guide. 

 

    """ 

 

    not_found_msg = '(not installed)' 

 

    django_settings = None 

    """This is a reference to the `globals()` dictionary of your 

    :xfile:`settings.py` file (the one you provided when instantiating 

    the Site object). 

 

    """ 

 

    startup_time = None 

    """ 

    The time when this Site has been instantiated, 

    in other words the startup time of this Django process. 

    Don't modify this. 

 

    """ 

 

    plugins = None 

    """An :class:`AttrDict` object with one entry for each installed app, 

    mapping the `app_label` of every plugin to the corresponding 

    :class:`lino.core.plugin.Plugin` instance. 

 

    This attribute is automatically filled by Lino and available as 

    :attr:`dd.plugins <lino.api.dd>` already before Django starts to 

    import :xfile:`models.py` modules. 

 

    """ 

 

    modules = AttrDict() 

 

    top_level_menus = [ 

        ("master", _("Master")), 

        ("main", None), 

        ("reports", _("Reports")), 

        ("config", _("Configure")), 

        ("explorer", _("Explorer")), 

        ("site", _("Site")), 

    ] 

    "The list of top-level menu items. See :meth:`setup_menu`." 

 

    is_local_project_dir = False 

    """Contains `True` if this is a "local" project.  For local projects, 

    Lino checks for local fixtures and config directories and adds 

    them to the default settings. 

 

    This is automatically set when a :class:`Site` is instantiated. 

 

    """ 

 

    ignore_model_errors = False 

    """Not yet sure whether this is needed. Maybe when generating 

    documentation. 

 

    """ 

 

    loading_from_dump = False 

    """Whether the process is currently loading data from a Python dump. 

 

    When loading from a python dump, application code should not 

    generate certain automatic data because that data is also part of 

    the dump. 

 

    This is normally `False`, but a Python dump created with 

    :manage:`dump2py` explicitly calls :meth:`install_migrations` 

    which sets this to `True`. 

 

    Application code should not change this setting except for certain 

    special test cases. 

 

    """ 

 

    # see docs/settings.rst 

    migration_class = None 

    """ 

    If you maintain a data migrator module for your application,  

    specify its name here. 

 

    See :ref:`datamig` and/or :func:`lino.utils.dpy.install_migrations`. 

 

    """ 

 

    hidden_languages = None 

    """A string with a space-separated list of django codes of languages 

    that should be hidden. 

 

    :ref:`welfare` uses this because the demo database has 4 

    languages, but `nl` is currently hidden bu default. 

 

    """ 

 

    BABEL_LANGS = tuple() 

 

    partners_app_label = 'contacts' 

    """ 

    Temporary setting, see :ref:`polymorphism`. 

    """ 

 

    # three constants used by lino_xl.lib.workflows: 

    max_state_value_length = 20 

    max_action_name_length = 50 

    max_actor_name_length = 100 

 

    trusted_templates = False 

    """ 

    Set this to True if you are sure that the users of your site won't try to 

    misuse Jinja's capabilities. 

 

    """ 

 

    allow_duplicate_cities = False 

    """In a default configuration (when :attr:`allow_duplicate_cities` is 

    False), Lino declares a UNIQUE clause for :class:`Places 

    <lino.modlib.countries.models.Places>` to make sure that your 

    database never contains duplicate cities.  This behaviour mighr 

    disturb e.g. when importing legacy data that did not have this 

    restriction.  Set it to True to remove the UNIQUE clause. 

     

    Changing this setting might affect your database structure and 

    thus require a :doc:`/topics/datamig` if your application uses 

    :mod:`lino.modlib.countries`. 

 

    """ 

 

    uid = 'myuid' 

    """A universal identifier for this Site.  This is needed when 

    synchronizing with CalDAV server.  Locally created calendar 

    components in remote calendars will get a UID based on this 

    parameter, using ``"%s@%s" % (self.pk, settings.SITE.kernel)``. 

     

    The default value is ``'myuid'``, and you should certainly 

    override this on a production server that uses remote calendars. 

 

    """ 

 

    project_model = None 

    """ 

    Optionally set this to the <applabel.ModelName> of a model used as 

    "central project" in your application.  Which concretely means that 

    certain other models like notes.Note, outbox.Mail, ... have an 

    additional ForeignKey to this model. 

 

    """ 

 

    user_model = None 

    """If :mod:`lino.modlib.users` is installed, this holds a reference to 

    the model class which represents a user of the system. Default 

    value is `None`, meaning that this application has no user 

    management.  See also :meth:`set_user_model` 

 

    """ 

 

    # use_auth = True 

    # """Whether this site uses authentication.  If this is set to `False`, 

    # all requests are anonymous (as if :attr:`user_model` was `None`). 

    # This is ignored when :attr:`user_model` is `None`. 

    # """ 

 

    auth_middleware = None 

    """ 

    Override used Authorisation middlewares with supplied tuple of 

    middleware class names. 

 

    If None, use logic described in :doc:`/topics/auth` 

   

 

    """ 

 

    user_profiles_module = None 

    """The full Python path of the **user profiles module** to be used on 

    this site. 

 

    This must be set if you want to enable permission control based on 

    user roles defined in :attr:`Permittable.required_roles 

    <lino.core.permissions.Permittable.required_roles>` and 

    :attr:`UserProfile.role 

    <lino.modlib.users.choicelists.UserProfile.role>`. 

 

    Default value is `None`, meaning that role-based permission 

    control is inactive: every user can see everything. 

 

    If set, Lino will import this module during site startup. It is 

    expected to define application-specific user roles (if necessary) 

    and to fill the :class:`UserProfiles 

    <lino.modlib.users.choicelists.UserProfiles>` choicelist. 

 

    For example:: 

 

        class Site(Site): 

            user_profiles_module = 'lino.modlib.users.roles' 

 

    Examples of such user profiles modules are 

    :mod:`lino.modlib.users.roles` and 

    :mod:`lino.projects.presto.roles`. 

 

    """ 

 

    legacy_data_path = None 

    """ 

    Used by custom fixtures that import data from some legacy 

    database. 

 

    """ 

 

    propvalue_max_length = 200 

    """ 

    Used by :mod:`lino_xl.lib.properties`. 

    """ 

 

    show_internal_field_names = True 

    """Whether the internal field names should be visible.  ExtUI 

    implements this by prepending them to the tooltip, which means 

    that :attr:`use_quicktips` must also be `True`.  Default is 

    `True`. 

 

    """ 

 

    never_build_site_cache = False 

    """Set this to `True` if you want that Lino never (re)builds the site 

    cache, even when asked.  This can be useful on a development 

    server when you are debugging directly on the generated 

    :xfile:`lino*.js`.  Or for certain unit test cases. 

 

    """ 

 

    build_js_cache_on_startup = False 

    """Whether the Javascript cache files should be built on startup for 

    all user profiles and languages. 

     

    On a production server this should be `True` for best performance, 

    but often this is not necessary, so default value is `False`, 

    which means that each file is built upon need (when a first 

    request comes in). 

     

    You can also set it to `None`, which means that Lino decides 

    automatically during startup: it becomes `False` if either 

    :func:`lino.core.utils.is_devserver` returns True or 

    setting:`DEBUG` is set. 

 

    """ 

 

    keep_erroneous_cache_files = False 

    """When some exception occurs during 

    :meth:`lino.core.kernel.Kernel.make_cache_file`, Lino usually 

    removes the partly generated file to make sure that it will try to 

    generate it again (and report the same error message) for every 

    subsequent next request. 

 

    Set this to `True` if you need to see the partly generated cache 

    file.  **Don't forget to remove this** when you have inspected the 

    file and fixed the reason of the exception, because if this is 

    `True` and some next exception occurs (which will happen sooner or 

    later), then all subsequent requests will usually end up to the 

    user with a blank screen and (if they notice it), a message 

    :message:`TypeError: Lino.main_menu is undefined` in their 

    Javascript console. 

 

    """ 

 

    use_java = True 

    """ 

    A site-wide option to disable everything that needs Java.  Note that 

    it is up to the apps which include Java applications to respect this 

    setting. Usage example is :mod:`lino_xl.lib.beid`. 

 

    """ 

 

    use_experimental_features = False 

    """Whether to include "experimental features". 

    """ 

    site_config_defaults = {} 

    """ 

    Default values to be used when creating the :attr:`site_config`. 

     

    Usage example:: 

     

      site_config_defaults = dict(default_build_method='appypdf') 

       

 

    """ 

 

    # default_build_method = "appypdf" 

    # default_build_method = "appyodt" 

    # default_build_method = "wkhtmltopdf" 

    default_build_method = None 

    """The default build method to use when rendering printable documents. 

 

    This is the last default value, used only when 

    :attr:`default_build_method 

    <lino.modlib.system.models.SiteConfig.default_build_method>` in 

    :class:`SiteConfig <lino.modlib.system.models.SiteConfig>` is 

    empty. 

 

    """ 

 

    is_demo_site = True 

    """When this is `True`, then this site runs in "demo" mode.  "Demo 

    mode" means: 

     

    - the welcome text for anonymous users says "This demo site has X 

      users, they all have "1234" as password", followed by a list of 

      available usernames. 

     

    Default value is `True`.  On a production site you will of course 

    set this to `False`. 

     

    See also :attr:`demo_fixtures`. 

 

    """ 

 

    demo_email = 'demo@example.com' 

 

    demo_fixtures = ['std', 'demo', 'demo2'] 

    """ 

    The list of fixtures to be loaded by the :manage:`initdb_demo` 

    command. 

 

    """ 

 

    use_spinner = False  # doesn't work. leave this to False 

 

    #~ django_admin_prefix = '/django' 

    django_admin_prefix = None 

    """ 

    The prefix to use for Django admin URLs. 

    Leave this unchanged as long as :srcref:`docs/tickets/70` is not solved. 

    """ 

 

    time_format_extjs = 'H:i' 

    """ 

    Format (in ExtJS syntax) to use for displaying dates to the user. 

    If you change this setting, you also need to override :meth:`parse_time`. 

 

    """ 

 

    date_format_extjs = 'd.m.Y' 

    """Format (in ExtJS syntax) to use for displaying dates to the user. 

    If you change this setting, you also need to override :meth:`parse_date`. 

 

    """ 

 

    alt_date_formats_extjs = 'd/m/Y|Y-m-d' 

    """Alternative date entry formats accepted by ExtJS Date widgets. 

 

    """ 

 

    #~ default_number_format_extjs = '0,000.00/i' 

    default_number_format_extjs = '0,00/i' 

 

    uppercase_last_name = False 

    """ 

    Whether last name of persons should (by default) be printed with 

    uppercase letters.  See :mod:`lino.test_apps.human` 

 

    """ 

 

    tinymce_base_url = "http://www.tinymce.com/js/tinymce/jscripts/tiny_mce/" 

    "Replaced by :attr:`lino.modlib.tinymce.Plugin.media_base_url`." 

 

    jasmine_root = None 

    """Path to the Jasmine root directory.  Only used on a development 

    server if the `media` directory has no symbolic link to the 

    Jasmine root directory and only if :attr:`use_jasmine` is True. 

 

    """ 

 

    # tinymce_root = None 

    # "Replaced by :attr:`lino.modlib.tinymce.Plugin.media_root`." 

 

    default_user = None 

    """Username of the user to be used for all incoming requests.  Setting 

    this to a nonempty value will disable authentication on this site. 

    The special value `'anonymous'` will cause anonymous requests 

    (whose `user` attribute is the :class:`AnonymousUser 

    <lino.modlib.users.utils.AnonymousUser>` instance). 

 

    See also :meth:`get_auth_method`. 

 

    This setting should be `None` when :attr:`user_model` is `None`. 

 

    """ 

 

    anonymous_user_profile = '000' 

    """The user profile to be assigned to the anonymous user 

(:class:`AnonymousUser <lino.modlib.users.utils.AnonymousUser>`). 

 

    """ 

 

    remote_user_header = None 

    """The name of the header (set by the web server) that Lino should 

    consult for finding the user of a request.  The default value 

    `None` means that http authentication is not used.  Apache's 

    default value is ``"REMOTE_USER"``. 

 

    """ 

    ldap_auth_server = None 

    """ 

    This should be a string with the domain name and DNS (separated by a 

    space) of the LDAP server to be used for authentication.  Example:: 

 

      ldap_auth_server = 'DOMAIN_NAME SERVER_DNS' 

 

    """ 

 

    use_gridfilters = True 

 

    use_eid_applet = False 

    """Whether to include functionality to read Belgian id cards using 

    the official `eid-applet <http://code.google.com/p/eid-applet>`_. 

    This option is experimental and doesn't yet work.  See 

    `/blog/2012/1105`. 

 

    """ 

 

    use_esteid = False 

    """ 

    Whether to include functionality to read Estonian id cards. 

    This option is experimental and doesn't yet work. 

    """ 

 

    use_filterRow = not use_gridfilters 

    """ 

    See `/blog/2011/0630`. 

    This option was experimental and doesn't yet work (and maybe never will). 

    """ 

 

    use_awesome_uploader = False 

    """ 

    Whether to use AwesomeUploader. 

    This option was experimental and doesn't yet work (and maybe never will). 

    """ 

 

    use_tinymce = True 

    """Replaced by :mod:`lino.modlib.tinymce`. 

    """ 

 

    use_jasmine = False 

    """Whether to use the `Jasmine <https://github.com/pivotal/jasmine>`_ 

    testing library. 

 

    """ 

 

    use_quicktips = True 

    """Whether to make use of `Ext.QuickTips 

    <http://docs.sencha.com/ext-js/3-4/#!/api/Ext.QuickTips>`_ for 

    displaying :ref:`help_texts` and internal field names (if 

    :attr:`show_internal_field_names`). 

 

    """ 

 

    use_css_tooltips = False 

    """ 

    Whether to make use of CSS tooltips 

    when displaying help texts defined in :class:`lino.models.HelpText`. 

    """ 

 

    use_vinylfox = False 

    """ 

    Whether to use VinylFox extensions for HtmlEditor.  

    This feature was experimental and doesn't yet work (and maybe never will). 

    See `/blog/2011/0523`. 

    """ 

 

    webdav_root = None 

    """ 

    The path on server to store webdav files. 

    Default is :attr:`cache_dir` + ´/media/webdav'. 

    """ 

 

    webdav_url = None 

    """The URL prefix for webdav files.  In a normal production 

    configuration you should leave this to `None`, Lino will set a 

    default value "/media/webdav/", supposing that your Apache is 

    configured as described in :doc:`/admin/webdav`. 

     

    This may be used to simulate a :term:`WebDAV` location on a 

    development server.  For example on a Windows machine, you may set 

    it to ``w:\``, and before invoking :manage:`runserver`, you issue in 

    a command prompt:: 

     

        subst w: <dev_project_path>\media\webdav 

 

    """ 

 

    sidebar_width = 0 

    """ 

    Used by :mod:`lino.modlib.plain`. 

    Width of the sidebar in 1/12 of total screen width. 

    Meaningful values are 0 (no sidebar), 2 or 3. 

 

    """ 

 

    config_id = 1 

    """The primary key of the one and only 

    :class:`SiteConfig<lino.modlib.system.models.SiteConfig>` instance 

    of this :class:`Site`. Default value is 1. 

 

    This is Lino's equivalent of Django's :setting:`SITE_ID` setting. 

    Lino applications don't need ``django.contrib.sites`` (`The "sites" 

    framework 

    <https://docs.djangoproject.com/en/dev/ref/contrib/sites/>`_) because 

    this functionality is integral part of :mod:`lino.modlib.system`. 

 

    """ 

 

    preview_limit = 15 

    """Default value for the 

    :attr:`preview_limit<lino.core.tables.AbstractTable.preview_limit>` 

    parameter of all tables who don't specify their own one.  Default 

    value is 15. 

 

    """ 

 

    default_ui = 'extjs' 

    """The `app_label` of the plugin which is to be used as default user 

    interface on this :class:`Site`. 

 

    Default value is ``'extjs'`` (which points to 

    :mod:`lino.modlib.extjs`). Other candidates are 

    :mod:`lino_xl.lib.pages` and 

    :mod:`lino.modlib.bootstrap3`. 

 

    Another possibility is to set it to `None`. In that case you will 

    probably also set :attr:`root_urlconf` to a custom URL dispatcher. 

    Usage example see :mod:`lino.projects.cms`. 

 

    """ 

 

    root_urlconf = 'lino.core.urls' 

    """The value to be attribute to :setting:`ROOT_URLCONF` when this 

    :class:`Site` instantiates. 

 

    The default value is :mod:`lino.core.urls`. 

 

    """ 

 

    textfield_format = 'plain' 

    """The default format for text fields. 

    Valid choices are currently 'plain' and 'html'. 

 

    Text fields are either Django's `models.TextField` or 

    :class:`lino.core.fields.RichTextField`. 

 

    You'll probably better leave the global option as 'plain', 

    and specify explicitly the fields you want as html by declaring 

    them:: 

 

      foo = fields.RichTextField(...,format='html') 

 

    We even recommend that you declare your *plain* text fields also 

    using `fields.RichTextField` and not `models.TextField`:: 

 

      foo = fields.RichTextField() 

 

    Because that gives subclasses of your application the possibility to 

    make that specific field html-formatted:: 

 

       resolve_field('Bar.foo').set_format('html') 

 

    """ 

 

    log_each_action_request = False 

    """Whether Lino should log every incoming request for non 

    :attr:`readonly <lino.core.actions.Action.readonly>` actions. 

 

    This is experimental. Theoretically it is useless to ask Lino for 

    logging every request since Apache does this. OTOH Lino can 

    produce more readable logs. 

 

    Note also that there is no warranty that actually *each* request 

    is being logged.  It corrently works only for requests that are 

    being processed by the kernel's :meth:`run_action 

    <lino.core.kernel.Kernel.run_action>` or 

    :meth:`run_callback 

    <lino.core.kernel.Kernel.run_callback>` methods. 

 

    """ 

 

    verbose_client_info_message = False 

    """ 

    Set this to True if actions should send debug messages to the client. 

    These will be shown in the client's Javascript console only. 

 

    """ 

 

    help_url = "http://www.lino-framework.org" 

 

    help_email = "users@lino-framework.org" 

    """ 

    An e-mail address where users can get help. This is included in 

    :xfile:`admin_main.html`. 

 

    """ 

 

    catch_layout_exceptions = True 

    """Lino usually catches any exception during startup (in 

    :func:`create_layout_element 

    <lino.core.layouts.create_layout_element>`) to report errors of 

    style "Unknown element "postings.PostingsByController 

    ('postings')" referred in layout <PageDetail on pages.Pages>." 

     

    Setting this to `False` is useful when there's some problem 

    *within* the framework. 

 

    """ 

 

    strict_choicelist_values = True 

    """Whether invalid values in a ChoiceList should raise an exception. 

 

    This should be `True` except for exceptional situations. 

    """ 

 

    csv_params = dict() 

    """Site-wide default parameters for CSV generation.  This must be a 

    dictionary that will be used as keyword parameters to Python 

    `csv.writer() 

    <http://docs.python.org/library/csv.html#csv.writer>`_ 

     

    Possible keys include: 

     

    - encoding :  

      the charset to use when responding to a CSV request. 

      See  

      http://docs.python.org/library/codecs.html#standard-encodings 

      for a list of available values. 

       

    - many more allowed keys are explained in 

      `Dialects and Formatting Parameters 

      <http://docs.python.org/library/csv.html#csv-fmt-params>`_. 

 

    """ 

 

    auto_configure_logger_names = 'atelier lino' 

    """ 

    A string with a space-separated list of logger names to be 

    automatically configured. See :mod:`lino.utils.log`. 

 

    """ 

 

    # appy_params = dict(ooPort=8100) 

    appy_params = dict( 

        ooPort=8100, pythonWithUnoPath='/usr/bin/python3', 

        raiseOnError=True) 

    """Used by :class:`lino.mixins.printable.AppyBuildMethod`. 

 

    Allowed keyword arguments for `appy.pod.renderer.Render` are:: 

 

      pythonWithUnoPath=None, 

      ooPort=2002 

      stylesMapping={} 

      forceOoCall=False, 

      finalizeFunction=None 

      overwriteExisting=False 

      raiseOnError=False 

      imageResolver=None 

 

    See `the source code 

    <http://bazaar.launchpad.net/~appy-dev/appy/trunk/view/head:/pod/renderer.py>`_ 

    for details. 

 

    See also :doc:`/admin/oood` 

 

    """ 

 

    #~ decimal_separator = '.' 

    decimal_separator = ',' 

    """Set this to either ``'.'`` or ``','`` to define wether to use 

    comma or dot as decimal point separator when entering a 

    `DecimalField`. 

 

    """ 

 

    # decimal_group_separator = ',' 

    # decimal_group_separator = ' ' 

    # decimal_group_separator = '.' 

    decimal_group_separator = u"\u00A0" 

 

    """ 

    Decimal group separator for :meth:`decfmt`. 

    """ 

 

    time_format_strftime = '%H:%M' 

    """ 

    Format (in strftime syntax) to use for displaying dates to the user. 

    If you change this setting, you also need to override :meth:`parse_time`. 

 

    """ 

 

    date_format_strftime = '%d.%m.%Y' 

    """ 

    Format (in strftime syntax) to use for displaying dates to the user. 

    If you change this setting, you also need to override :meth:`parse_date`. 

 

    """ 

 

    date_format_regex = "/^[0123]?\d\.[01]?\d\.-?\d+$/" 

    """ 

    Format (in Javascript regex syntax) to use for displaying dates to 

    the user.  If you change this setting, you also need to override 

    :meth:`parse_date`. 

 

    """ 

 

    datetime_format_strftime = '%Y-%m-%dT%H:%M:%S' 

    """ 

    Format (in strftime syntax) to use for formatting timestamps in 

    AJAX responses.  If you change this setting, you also need to 

    override :meth:`parse_datetime`. 

 

    """ 

 

    datetime_format_extjs = 'Y-m-d\TH:i:s' 

    """ 

    Format (in ExtJS syntax) to use for formatting timestamps in AJAX 

    calls.  If you change this setting, you also need to override 

    :meth:`parse_datetime`. 

 

    """ 

 

    # for internal use: 

    # _welcome_actors = [] 

    _welcome_handlers = [] 

    _site_config = None 

    _logger = None 

    override_modlib_models = None 

    """A dictionary automatically filled at startup. 

    You can inspect it, but you should not modify it. 

 

    It maps model class names to the plugin which overrides them. 

 

    This dictionary is needed mainly for :meth:`is_abstract_model`. 

 

    The challenge is that we want to know exactly where every model's 

    concrete class will be defined *before* actually starting to 

    import the :xfile:`models.py` modules.  That's why we need 

    :attr:`extends_models <lino.core.plugin.Plugin.extends_models>`. 

 

    This can be tricky, see e.g. 20160205. 

 

    """ 

 

    def __init__(self, settings_globals=None, local_apps=[], **kwargs): 

        """Every Lino application calls this once in it's 

        :file:`settings.py` file. 

        See :doc:`/usage`. 

 

        `settings_globals` is the `globals()` dictionary of your 

        :xfile:`settings.py`. 

 

        """ 

        # self.logger.info("20140226 Site.__init__() a %s", self) 

        #~ print "20130404 ok?" 

        if settings_globals is None: 

            settings_globals = {} 

        self.init_before_local(settings_globals, local_apps) 

        if 'no_local' in kwargs: 

            kwargs.pop('no_local') 

            # For the moment we just silently ignore it, but soon: 

            if False: 

                raise ChangedAPI("The no_local argument is no longer needed.") 

        # no_local = kwargs.pop('no_local', False) 

        # if not no_local: 

        #     self.run_lino_site_module() 

        self.run_lino_site_module() 

        self.override_settings(**kwargs) 

        self.load_plugins() 

        self.setup_plugins() 

        self.install_settings() 

 

        from lino.utils.config import ConfigDirCache 

        self.confdirs = ConfigDirCache(self) 

 

        for k in ('ignore_dates_before', 'ignore_dates_after'): 

            if hasattr(self, k): 

                msg = "{0} is no longer a site attribute" 

                msg += " but a plugin attribute on lino_xl.lib.cal." 

                msg = msg.format(k) 

                raise ChangedAPI(msg) 

 

    def init_before_local(self, settings_globals, local_apps): 

        """If your :attr:`project_dir` contains no :xfile:`models.py`, but 

        *does* contain a `fixtures` subdir, then Lino automatically adds this 

        as a local fixtures directory to Django's :setting:`FIXTURE_DIRS`. 

 

        But only once: if your application defines its own local 

        fixtures directory, then this directory "overrides" those of 

        parent applications. E.g. lino_noi.projects.care does not want 

        to load the application-specific fixtures of 

        lino_noi.projects.team. 

 

        """ 

        if isinstance(local_apps, six.string_types): 

            local_apps = [local_apps] 

        if not isinstance(settings_globals, dict): 

            raise Exception(""" 

            The first argument when instantiating a %s 

            must be your settings.py file's `globals()` 

            and not %r 

            """ % (self.__class__.__name__, settings_globals)) 

 

        if isinstance(local_apps, six.string_types): 

            local_apps = [local_apps] 

        self.local_apps = local_apps 

 

        self.django_settings = settings_globals 

        project_file = settings_globals.get('__file__', '.') 

 

        self.project_dir = Path(dirname(project_file)).absolute().resolve() 

 

        # inherit `project_name` from parent? 

        # if self.__dict__.get('project_name') is None: 

        if self.project_name is None: 

            parts = reversed(self.project_dir.split(os.sep)) 

            # print(20150129, list(parts)) 

            for part in parts: 

                if part != 'settings': 

                    self.project_name = part 

                    break 

 

        cache_root = os.environ.get('LINO_CACHE_ROOT', None) 

        if cache_root: 

            cr = Path(cache_root).absolute() 

            if not cr.exists(): 

                msg = "LINO_CACHE_ROOT ({0}) does not exist!".format(cr) 

                raise Exception(msg) 

            self.cache_dir = cr.child(self.project_name).resolve() 

            self.setup_cache_directory() 

        else: 

            self.cache_dir = Path(self.project_dir).absolute() 

 

        self._startup_done = False 

        self.startup_time = datetime.datetime.now() 

 

        if self.cache_dir is not None: 

            dbname = self.cache_dir.child('default.db') 

            self.django_settings.update(DATABASES={ 

                'default': { 

                    'ENGINE': 'django.db.backends.sqlite3', 

                    'NAME': dbname 

                } 

            }) 

 

        self.update_settings(SERIALIZATION_MODULES={ 

            "py": "lino.utils.dpy", 

        }) 

 

        if self.site_prefix != '/': 

            if not self.site_prefix.endswith('/'): 

                raise Exception("`site_prefix` must end with a '/'!") 

            if not self.site_prefix.startswith('/'): 

                raise Exception("`site_prefix` must start with a '/'!") 

            self.update_settings( 

                SESSION_COOKIE_PATH=self.site_prefix[:-1]) 

            # self.update_settings(SESSION_COOKIE_NAME='ssid') 

 

        ## Local project directory 

        modname = self.__module__ 

        i = modname.rfind('.') 

        if i != -1: 

            modname = modname[:i] 

        self.is_local_project_dir = not modname in self.local_apps 

 

        self.VIRTUAL_FIELDS = [] 

 

        self.update_settings( 

            LOGGING_CONFIG='lino.utils.log.configure', 

            LOGGING=dict( 

                filename=None, 

                level='INFO', 

                logger_names=self.auto_configure_logger_names, 

                disable_existing_loggers=True,  # Django >= 1.5 

            ), 

        ) 

 

    def run_lino_site_module(self): 

        """See :ref:`djangosite_local`. 

 

        """ 

        site_module = os.environ.get('LINO_SITE_MODULE', None) 

        if site_module: 

            mod = import_module(site_module) 

            func = getattr(mod, 'setup_site', None) 

            if func: 

                func(self) 

        # try: 

        #     from djangosite_local import setup_site 

        # except ImportError: 

        #     pass 

        # else: 

        #     setup_site(self) 

 

    def override_settings(self, **kwargs): 

        # Called internally during `__init__` method. 

        # Also called from :mod:`lino.utils.djangotest` 

 

        #~ logger.info("20130404 lino.site.Site.override_defaults") 

 

        for k, v in list(kwargs.items()): 

            if not hasattr(self, k): 

                raise Exception("%s has no attribute %s" % (self.__class__, k)) 

            setattr(self, k, v) 

 

        self.apply_languages() 

 

    def load_plugins(self): 

        """Load all plugins and build the :setting:`INSTALLED_APPS` setting 

        for Django. 

 

        This includes a call to :meth:`get_apps_modifiers` and 

        :meth:`get_installed_apps`. 

 

        """ 

        # Called internally during `__init__` method. 

 

        requested_apps = [] 

        apps_modifiers = self.get_apps_modifiers() 

 

        if hasattr(self, 'hidden_apps'): 

            raise Exception("Replace hidden_apps by get_apps_modifiers()") 

 

        def add(x): 

            if isinstance(x, six.string_types): 

                app_label = x.split('.')[-1] 

                x = apps_modifiers.pop(app_label, x) 

                if x: 

                    requested_apps.append(x) 

            else: 

                # if it's not a string, then it's an iterable of strings 

                for xi in x: 

                    add(xi) 

 

        for x in self.get_installed_apps(): 

            add(x) 

 

        if apps_modifiers: 

            raise Exception( 

                "Invalid app_label '{0}' in your get_apps_modifiers!".format( 

                    list(apps_modifiers.keys())[0])) 

 

        # actual_apps = [] 

        plugins = [] 

        self.plugins = AttrDict() 

 

        def install_plugin(app_name, needed_by=None): 

            # Django does not accept newstr, and we don't want to see 

            # ``u'applabel'`` in doctests. 

            app_name = six.binary_type(app_name) 

            app_mod = import_module(app_name) 

 

            # Can an `__init__.py` file explicitly set ``Plugin = 

            # None``? Is that feature being used? 

            app_class = getattr(app_mod, 'Plugin', None) 

            if app_class is None: 

                app_class = Plugin 

            # print "Loading plugin", app_name 

            k = app_name.rsplit('.')[-1] 

            if k in self.plugins: 

                txt = self.plugins[k] 

                raise Exception("Tried to install '%s' where '%s' " 

                                "is already installed." % ( 

                                    app_name, txt)) 

            p = app_class(self, k, app_name, app_mod, needed_by) 

            cfg = PLUGIN_CONFIGS.pop(k, None) 

            if cfg: 

                p.configure(**cfg) 

 

            for dep in p.needs_plugins: 

                k2 = dep.rsplit('.')[-1] 

                if k2 not in self.plugins: 

                    install_plugin(dep, needed_by=p) 

                    # plugins.append(dep) 

 

            # actual_apps.append(app_name) 

            plugins.append(p) 

            self.plugins.define(k, p) 

 

        # lino_startup is always the first plugin: 

        install_plugin(str('lino.modlib.lino_startup')) 

 

        for app_name in requested_apps: 

            install_plugin(app_name) 

 

        # The return value of get_auth_method() may depend on a 

        # plugin, so if needed we must add the django.contrib.sessions 

        # afterwards. 

        if self.get_auth_method() == 'session': 

            # actual_apps.insert(0, str('django.contrib.sessions')) 

            install_plugin(str('django.contrib.sessions')) 

 

        # install_plugin(str('lino.modlib.database_ready')) 

 

        # self.update_settings(INSTALLED_APPS=tuple(actual_apps)) 

        self.update_settings( 

            INSTALLED_APPS=tuple([p.app_name for p in plugins])) 

        self.installed_plugins = tuple(plugins) 

 

        if self.override_modlib_models is not None: 

            raise ChangedAPI("override_modlib_models no longer allowed") 

 

        self.override_modlib_models = dict() 

 

        def reg(p, pp, m): 

            name = pp.__module__ + '.' + m 

            self.override_modlib_models[name] = p 

 

        for p in self.installed_plugins: 

            if p.extends_models is not None: 

                for m in p.extends_models: 

                    if "." in m: 

                        raise Exception( 

                            "extends_models in %s still uses '.'" % 

                            p.app_name) 

                    # found = False 

                    root = None 

                    # for pp in p.__class__.__bases__: 

                    for pp in p.__class__.__mro__: 

                        if issubclass(pp, Plugin) and pp not in ( 

                                p.__class__, Plugin): 

                            root = pp 

                            reg(p, pp, m) 

                            # if pp.extends_models and m in pp.extends_models: 

                            #     reg(p, pp, m) 

                                # break 

                    if not root: 

                        msg = "{0} declares to extend_models {1}, but " \ 

                              "cannot find parent plugin".format(p, m) 

                        raise Exception(msg) 

                    # reg(p, root, m) 

 

        # raise Exception("20140825 %s", self.override_modlib_models) 

 

        # Tried to prevent accidental calls to configure_plugin() 

        # *after* Site initialization. 

 

        # global PLUGIN_CONFIGS 

        # PLUGIN_CONFIGS = None 

 

    def setup_plugins(self): 

        """This method is called exactly once during site startup, after 

        :meth:`load_plugins` and before models are being populated. 

 

        """ 

        pass 

 

    def install_settings(self): 

 

        assert not self.help_url.endswith('/') 

        # import django 

        # django.setup() 

        if self.cache_dir is not None: 

            if self.webdav_url is None: 

                self.webdav_url = self.site_prefix + 'media/webdav/' 

            if self.webdav_root is None: 

                self.webdav_root = join(self.cache_dir, 'media', 'webdav') 

            self.django_settings.update( 

                MEDIA_ROOT=join(self.cache_dir, 'media')) 

 

        self.update_settings(ROOT_URLCONF=self.root_urlconf) 

        self.update_settings(MEDIA_URL='/media/') 

 

        if not self.django_settings.get('STATIC_ROOT', False): 

            cache_root = os.environ.get('LINO_CACHE_ROOT', None) 

            if cache_root: 

                self.django_settings.update( 

                    STATIC_ROOT=Path(cache_root).child('collectstatic')) 

            else: 

                self.django_settings.update( 

                    STATIC_ROOT=self.cache_dir.child('static')) 

        if not self.django_settings.get('STATIC_URL', False): 

            self.update_settings(STATIC_URL='/static/') 

 

        loaders = [ 

            'lino.modlib.jinja.loader.Loader', 

            'django.template.loaders.filesystem.Loader', 

            'django.template.loaders.app_directories.Loader', 

            #~ 'django.template.loaders.eggs.Loader', 

        ] 

 

        tcp = [] 

        if self.user_model == 'auth.User':  # not tested 

            self.update_settings(LOGIN_URL='/accounts/login/') 

            self.update_settings(LOGIN_REDIRECT_URL="/") 

            tcp += ['django.contrib.auth.context_processors.auth'] 

 

        tcp += [ 

            'django.template.context_processors.debug', 

            'django.template.context_processors.i18n', 

            'django.template.context_processors.media', 

            'django.template.context_processors.static', 

            'django.template.context_processors.tz', 

            'django.contrib.messages.context_processors.messages', 

        ] 

        # self.update_settings(TEMPLATE_LOADERS=tuple(loaders)) 

        # self.update_settings(TEMPLATE_CONTEXT_PROCESSORS=tuple(tcp)) 

 

        TEMPLATES = [ 

            { 

                'BACKEND': 'django.template.backends.django.DjangoTemplates', 

                'DIRS': [], 

                'APP_DIRS': True, 

                'OPTIONS': { 

                    'context_processors': tcp, 

                    # 'loaders': loaders 

                }, 

            }, 

        ] 

        TEMPLATES.append( 

            { 

                'BACKEND': 'django.template.backends.jinja2.Jinja2', 

                'DIRS': [], 

                'OPTIONS': { 

                    'environment': 'lino.modlib.jinja.get_environment' 

                }, 

            }) 

 

        self.update_settings(TEMPLATES=TEMPLATES) 

 

        self.define_settings( 

            MIDDLEWARE_CLASSES=tuple(self.get_middleware_classes())) 

 

        def collect_settings_subdirs(lst, name, max_count=None): 

            def add(p): 

                p = p.replace(os.sep, "/") 

                if p not in lst: 

                    lst.append(p) 

 

            for p in self.get_settings_subdirs(name): 

                # if the parent of a settings subdir has a 

                # `models.py`, then it is a plugin and we must not add 

                # the subdir because Django does that. 

                if exists(join(p, '..', 'models.py')): 

                    self.logger.info( 

                        "Ignoring %s %s because it is a plugin", p, name) 

                else: 

                    add(p) 

                    if (max_count is not None) and len(lst) >= max_count: 

                        break 

 

            # local_dir = self.cache_dir.child(name) 

            # if local_dir.exists(): 

            #     print "20150427 adding local directory %s" % local_dir 

            #     add(local_dir) 

            # The STATICFILES_DIRS setting should not contain the 

            # STATIC_ROOT setting 

 

            if False: 

                # If a plugin has no "fixtures" ("config") directory 

                # of its own, inherit it from parents.  That would be 

                # nice and it even works, but with a stud: these 

                # fixtures will be loaded at the end. 

                for ip in self.installed_plugins: 

                    if not ip.get_subdir(name): 

                        pc = ip.extends_from() 

                        while pc and issubclass(pc, Plugin): 

                            p = pc.get_subdir(name) 

                            if p: 

                                add(p) 

                            pc = pc.extends_from() 

 

        fixture_dirs = list(self.django_settings.get('FIXTURE_DIRS', [])) 

        locale_paths = list(self.django_settings.get('LOCALE_PATHS', [])) 

        sfd = list(self.django_settings.get('STATICFILES_DIRS', [])) 

        # sfd.append(self.cache_dir.child('genjs')) 

        collect_settings_subdirs(fixture_dirs, 'fixtures', 1) 

        collect_settings_subdirs(locale_paths, 'locale') 

        collect_settings_subdirs(sfd, 'static') 

        self.update_settings(FIXTURE_DIRS=tuple(fixture_dirs)) 

        self.update_settings(LOCALE_PATHS=tuple(locale_paths)) 

        self.update_settings(STATICFILES_DIRS=tuple(sfd)) 

 

        # print(20150331, self.django_settings['FIXTURE_DIRS']) 

 

    def setup_cache_directory(self): 

        """When :envvar:`LINO_CACHE_ROOT` is set, Lino adds a stamp file 

        called :xfile:`lino_cache.txt` to every project's cache 

        directory in order to avoid duplicate use of same cache 

        directory. 

 

        .. xfile:: lino_cache.txt 

 

            A small text file with one line of text which contains the 

            path of the project which uses this cache directory. 

 

        """ 

 

        stamp = self.cache_dir.child('lino_cache.txt') 

        this = class2str(self.__class__) 

        if stamp.exists(): 

            other = stamp.read_file() 

            if other == this: 

                ok = True 

            else: 

                ok = False 

                for parent in self.__class__.__mro__: 

                    if other == class2str(parent): 

                        ok = True 

                        break 

            if not ok: 

                msg = ("Cannot use {cache_dir} for {this} " 

                       "because it is used for {other}.") 

                msg = msg.format( 

                    cache_dir=self.cache_dir, 

                    this=this, 

                    other=other) 

                if True: 

                    raise Exception(msg) 

                else: 

                    # print(msg) 

                    self.cache_dir = None 

        else: 

            self.makedirs_if_missing(self.cache_dir) 

            stamp.write_file(this) 

 

    def set_user_model(self, spec): 

        """This can be called during the :meth:`on_init 

        <lino.core.plugin.Plugin.on_init>` of plugins which provide 

        user management (the only plugin which does this is currently 

        :mod:`lino.modlib.users`). 

 

        """ 

        if self.user_model is not None: 

            msg = "Site.user_model was already set!" 

            # Theoretically this should raise an exception. But in a 

            # transitional phase after 20150116 we just ignore it. A 

            # warning would be nice, but we cannot use the logger here 

            # since it is not yet configured. 

            if False: 

                # self.logger.warning(msg) 

                raise Exception(msg) 

        self.user_model = spec 

 

    def get_auth_method(self): 

        """Returns the authentication method used on this site. This is one of 

        `None`, `'remote'` or `'session'`. 

 

        It depends on the values in 

        :attr:`user_model`, 

        :attr:`default_user` and 

        :attr:`remote_user_header`. 

 

        It influences the results of 

        :meth:`get_middleware_classes` and 

        :meth:`get_installed_apps`. 

 

        """ 

        if self.user_model is None: 

            return None 

        if self.default_user is not None: 

            return None 

        if self.remote_user_header is None: 

            return 'session' 

        return 'remote' 

 

    def get_apps_modifiers(self, **kw): 

        """Override or hide individual plugins of an existing application. 

 

        For example, if your site inherits from 

        :mod:`lino.projects.min2`:: 

 

            def get_apps_modifiers(self, **kw): 

                kw.update(sales=None) 

                kw.update(courses='my.modlib.courses') 

                return kw 

 

        The default implementation returns an empty dict. 

 

        This method adds an additional level of customization because 

        it lets you remove or replace individual plugins from 

        :setting:`INSTALLED_APPS` without rewriting your own 

        :meth:`get_installed_apps`. 

 

        This will be called during Site instantiation and is expected to 

        return a dict of `app_label` to `full_python_path` 

        mappings which you want to override in the list of plugins 

        returned by :meth:`get_installed_apps`. 

 

        Mapping an `app_label` to `None` will remove that plugin from 

        :setting:`INSTALLED_APPS`. 

 

        It is theoretically possible but not recommended to replace an 

        existing `app_label` by an app with a different 

        `app_label`. For example, the following might work but is not 

        recommended:: 

 

                kw.update(courses='my.modlib.mycourses') 

 

        """ 

 

        return kw 

 

    def is_hidden_app(self, app_label): 

        """ 

        Return True if the app is known, but has been disabled using 

        :meth:`get_apps_modifiers`. 

 

        """ 

        am = self.get_apps_modifiers() 

        if am.get(app_label, 1) is None: 

            return True 

 

    def update_settings(self, **kw): 

        """This may be called from within a :doc:`djangosite_local.setup_site 

        </admin/djangosite_local>` function. 

 

        """ 

        self.django_settings.update(**kw) 

 

    def define_settings(self, **kwargs): 

        """Same as :meth:`update_settings`, but raises an exception if a 

        setting already exists. 

         

        TODO: Currently this exception is deactivated.  Because it 

        doesn't work as expected.  For some reason (maybe because 

        settings is being imported twice on a devserver) it raises a 

        false exception when :meth:`override_defaults` tries to use it 

        on `MIDDLEWARE_CLASSES`... 

 

        """ 

        if False: 

            for name in list(kwargs.keys()): 

                if name in self.django_settings: 

                    raise Exception( 

                        "Tried to define existing Django setting %s" % name) 

        self.django_settings.update(kwargs) 

 

    _starting_up = False 

 

    def startup(self): 

        """Start up this Site. 

 

        You probably don't want to override this method since it is 

        designed to be called potentially several times in case your 

        code wants to make sure that it was called. 

 

        This is called exactly once when Django has has populated it's model 

        cache. 

 

        """ 

 

        # This code can run several times at once when running 

        # e.g. under mod_wsgi: another thread has started and not yet 

        # finished `startup()`. 

 

        with startup_rlock: 

 

            # if self.cache_dir is not None: 

            #     raise Exception("No cache_dir is defined. " 

            #                     "Check your LINO_CACHE_ROOT and project_name.") 

 

            if self._starting_up: 

                # This is needed because Django "somehow" imports the 

                # settings module twice. The phenomen is not fully 

                # explained, but without this test we had the startup 

                # code being run twice, which caused various error 

                # messages (e.g. Duplicate label in workflow setup) 

                return 

 

            if self._startup_done: 

                # self.logger.info("20140227 Lino startup already done") 

                return 

 

            self._starting_up = True 

 

            # print "20151010 Site.startup()" 

 

            # if AFTER17: 

            #     print "20151010 Site.startup() gonna call django.setup" 

            #     import django 

            #     django.setup() 

 

            from lino.core.signals import pre_startup, post_startup 

 

            pre_startup.send(self) 

            # lino_startup = False 

            # for index , plugin in enumerate(self.installed_plugins): 

            #     if 'lino_startup' in plugin.app_name: 

            #         lino_startup = plugin 

            #         del plugin 

 

            # if lino_startup: 

            #     badguy = list(self.installed_plugins) 

            #     badguy.append(lino_startup) 

            #     # badguy = tuple(badguy) 

            #     self.installed_plugins = tuple(badguy) 

 

            # from django.conf import settings 

            # import django 

            # 

            # if not settings.INSTALLED_APPS: 

            #     django.setup() 

 

            for p in self.installed_plugins: 

                # m = loading.load_app(p.app_name, False) 

                if AFTER17: 

                    # In Django17+ we cannot say can_postpone=False, 

                    # and we don't need to, because anyway we used it 

                    # just for our hack in `lino.models` 

                    # load_app(app_name) is deprecated 

                    # from django.apps import apps 

                    # m = apps.load_app(p.app_name) 

                    try: 

                        from django.apps import apps 

                        app_config = AppConfig.create(p.app_name) 

                        app_config.import_models( 

                            apps.all_models[app_config.label]) 

                        apps.app_configs[app_config.label] = app_config 

                        apps.clear_cache() 

                        m = app_config.models_module 

                    except ImportError: 

                        self.logger.debug("No module {0}.models", p.app_name) 

                        # print(rrrr) 

                else: 

                    from django.db.models import loading 

                    m = loading.load_app(p.app_name, False) 

 

                self.modules.define(six.text_type(p.app_label), m) 

 

            for p in self.installed_plugins: 

                p.on_site_startup(self) 

            self.do_site_startup() 

            # self.logger.info("20140227 Site.do_site_startup() done") 

            post_startup.send(self) 

 

            self._startup_done = True 

 

    @property 

    def logger(self): 

        if self._logger is None: 

            import logging 

            self._logger = logging.getLogger(__name__) 

        return self._logger 

 

    def get_settings_subdirs(self, subdir_name): 

        """Yield all (existing) directories named `subdir_name` of this Site's 

        project directory and it's inherited project directories. 

 

        """ 

        # if local settings.py doesn't subclass Site: 

        if self.project_dir != normpath(dirname( 

                inspect.getfile(self.__class__))): 

            pth = join(self.project_dir, subdir_name) 

            if isdir(pth): 

                yield pth 

 

        for cl in self.__class__.__mro__: 

            #~ logger.info("20130109 inspecting class %s",cl) 

            if cl is not object and not inspect.isbuiltin(cl): 

                pth = join(dirname(inspect.getfile(cl)), subdir_name) 

                if isdir(pth): 

                    yield pth 

 

    def makedirs_if_missing(self, dirname): 

        """Make missing directories if they don't exist and if 

        :attr:`make_missing_dirs` is `True`. 

 

        """ 

        if dirname and not isdir(dirname): 

            if self.make_missing_dirs: 

                os.makedirs(dirname) 

            else: 

                raise Exception("Please create yourself directory %s" % 

                                dirname) 

 

    def is_abstract_model(self, module_name, model_name): 

        """Return True if the named model is declared as being extended by 

        :attr:`lino.core.plugin.Plugin.extends_models`. 

 

        Typical usage:: 

 

            class MyModel(dd.Model): 

                 class Meta: 

                     abstract = dd.is_abstract_model(__name__, 'MyModel') 

 

        See :doc:`/dev/plugin_inheritance`. 

 

        """ 

        name = '.'.join(module_name.split('.')[:-1]) 

        name += '.' + model_name 

        rv = name in self.override_modlib_models 

        # if model_name == 'Enrolment': 

        #     self.logger.info("20160205 is_abstract_model %s -> %s (%s)", 

        #                      name, rv, self.override_modlib_models.keys()) 

        return rv 

 

    def is_installed_model_spec(self, model_spec): 

        """Deprecated. This feature was a bit too automagic and caused bugs 

        to pass silently.  See e.g. :blogref:`20131025`. 

 

        """ 

        if False:  # mod_wsgi interprets them as error 

            warnings.warn("is_installed_model_spec is deprecated.", 

                          category=DeprecationWarning) 

 

        if model_spec == 'self': 

            return True 

        app_label, model_name = model_spec.split(".") 

        return self.is_installed(app_label) 

 

    def is_installed(self, app_label): 

        """ 

        Return `True` if :setting:`INSTALLED_APPS` contains an item 

        which ends with the specified `app_label`. 

 

        """ 

        return app_label in self.plugins 

 

    def setup_model_spec(self, obj, name): 

        """If the value of the named attribute of `obj` is a string, replace 

        it by the model specified by that string. 

 

        Example usage:: 

 

            # library code: 

            class ThingBase(object): 

                the_model = None 

 

                def __init__(self): 

                    settings.SITE.setup_model_spec(self, 'the_model') 

     

            # user code: 

            class MyThing(ThingBase): 

                the_model = "contacts.Partner" 

 

 

        """ 

        spec = getattr(obj, name) 

        if spec and isinstance(spec, six.string_types): 

            if not self.is_installed_model_spec(spec): 

                setattr(obj, name, None) 

                return 

            from lino.core.utils import resolve_model 

            msg = "Unresolved model '%s' in {0}.".format(name) 

            setattr(obj, name, resolve_model(spec, strict=msg)) 

 

    def on_each_app(self, methname, *args): 

        """Call the named method on the :xfile:`models.py` module of each 

        installed app. 

 

        Note that this mechanism is deprecated. It is still used (on 

        names like ``setup_workflows`` and ``setup_site``) for 

        historical reasons but will disappear one day. 

 

        """ 

        if AFTER17: 

            from django.apps import apps 

            apps = [a.models_module for a in apps.get_app_configs()] 

        else: 

            from django.db.models import loading 

            apps = loading.get_apps() 

        for mod in apps: 

            meth = getattr(mod, methname, None) 

            if meth is not None: 

                if False:  # 20150925 once we will do it for good... 

                    raise ChangedAPI("{0} still has a function {1}".format( 

                        mod, methname)) 

                meth(self, *args) 

 

    def for_each_app(self, func, *args, **kw): 

        """ 

        Call the given function on each installed app. 

        Successor of :meth:`on_each_app`.  This also loops over 

 

        - apps that don't have a models module 

        - inherited apps 

 

        """ 

 

        from importlib import import_module 

        done = set() 

        for p in self.installed_plugins: 

            for b in p.__class__.__mro__: 

                if b not in (object, Plugin): 

                    if b.__module__ not in done: 

                        done.add(b.__module__) 

                        parent = import_module(b.__module__) 

                        func(b.__module__, parent, *args, **kw) 

            if p.app_name not in done: 

                func(p.app_name, p.app_module, *args, **kw) 

 

    def demo_date(self, *args, **kwargs): 

        """ 

        Deprecated. Should be replaced by :meth:`today`. 

        Compute a date using :func:`atelier.utils.date_offset` based on 

        the process startup time (or :attr:`the_demo_date` if this is 

        set). 

 

        Used in Python fixtures and unit tests. 

 

 

        """ 

        base = self.the_demo_date or self.startup_time.date() 

        return date_offset(base, *args, **kwargs) 

 

    def today(self, *args, **kwargs): 

        """This is almost equivalent to calling :func:`datetime.date.today`. 

 

        The difference is when :attr:`the_demo_date` is set, in which 

        case :meth:`today` will return that date. 

 

        Another difference is that any arguments are forwarded to 

        :func:`atelier.utils.date_offset`. 

 

        This feature is being used in many test cases where e.g. the 

        age of people would otherwise change. 

 

        """ 

        if self.site_config is not None: 

            base = self.site_config.simulate_today \ 

                or self.the_demo_date or datetime.date.today() 

        else: 

            base = self.the_demo_date or datetime.date.today() 

        return date_offset(base, *args, **kwargs) 

 

    def welcome_text(self): 

        """Returns the text to display in a console window when this Site 

        starts. 

 

        """ 

        return "This is %s using %s." % ( 

            self.site_version(), self.using_text()) 

 

    def using_text(self): 

        """ 

        Text to display in a console window when Lino starts. 

        """ 

        return ', '.join([u"%s %s" % (n, v) 

                          for n, v, u in self.get_used_libs()]) 

 

    def site_version(self): 

        """Used in footnote or header of certain printed documents. 

 

        """ 

        if self.version: 

            return self.verbose_name + ' ' + self.version 

        return self.verbose_name 

 

    def configure_plugin(self, app_label, **kw): 

        raise Exception("Replace SITE.configure_plugin by ad.configure_plugin") 

 

    def install_migrations(self, *args): 

        """ 

        See :func:`lino.utils.dpy.install_migrations`. 

        """ 

        from lino.utils.dpy import install_migrations 

        install_migrations(self, *args) 

 

    def parse_date(self, s): 

        """Convert a string formatted using :attr:`date_format_strftime` or 

        :attr:`date_format_extjs` into a `(y,m,d)` tuple (not a 

        `datetime.date` instance).  See `/blog/2010/1130`. 

 

        """ 

        ymd = tuple(reversed(list(map(int, s.split('.'))))) 

        assert len(ymd) == 3 

        return ymd 

        #~ return datetime.date(*ymd) 

 

    def parse_time(self, s): 

        """Convert a string formatted using :attr:`time_format_strftime` or 

        :attr:`time_format_extjs` into a `datetime.time` instance. 

 

        """ 

        hms = list(map(int, s.split(':'))) 

        return datetime.time(*hms) 

 

    def parse_datetime(self, s): 

        """Convert a string formatted using :attr:`datetime_format_strftime` 

        or :attr:`datetime_format_extjs` into a `datetime.datetime` 

        instance. 

 

        """ 

        #~ print "20110701 parse_datetime(%r)" % s 

        #~ s2 = s.split() 

        s2 = s.split('T') 

        if len(s2) != 2: 

            raise Exception("Invalid datetime string %r" % s) 

        ymd = list(map(int, s2[0].split('-'))) 

        hms = list(map(int, s2[1].split(':'))) 

        return datetime.datetime(*(ymd + hms)) 

        #~ d = datetime.date(*self.parse_date(s[0])) 

        #~ return datetime.combine(d,t) 

 

    def strftime(self, t): 

        if t is None: 

            return '' 

        return t.strftime(self.time_format_strftime) 

 

    def resolve_virtual_fields(self): 

        for vf in self.VIRTUAL_FIELDS: 

            vf.lino_resolve_type() 

        self.VIRTUAL_FIELDS = [] 

 

    def register_virtual_field(self, vf): 

        self.VIRTUAL_FIELDS.append(vf) 

 

    def do_site_startup(self): 

        """This method is called exactly once during site startup, 

        just between the pre_startup and the post_startup signals. 

        A hook for subclasses. 

 

        TODO: rename this to `on_startup`? 

 

        If you override it, don't forget to call the super method 

        which calls :meth:`on_site_startup 

        <lino.core.plugin.Plugin.on_site_startup>` for each installed 

        plugin. 

 

        """ 

        # self.logger.info("20140227 lino_site.Site.do_site_startup() a") 

 

        self.user_interfaces = tuple([ 

            p for p in self.installed_plugins if p.ui_label is not None]) 

 

        # self.logger.info("20150428 user_interfaces %s", self.user_interfaces) 

 

        from lino.core.kernel import Kernel 

        self.kernel = Kernel(self) 

        # self.ui = self.kernel  # internal backwards compat 

 

        # self.logger.info("20140227 lino_site.Site.do_site_startup() b") 

 

    def find_config_file(self, *args, **kwargs): 

        return self.confdirs.find_config_file(*args, **kwargs) 

 

    def find_template_config_files(self, *args, **kwargs): 

        return self.confdirs.find_template_config_files(*args, **kwargs) 

 

    def setup_user_profiles(self): 

        """Deprecated. Use :attr:`user_profiles_module` instead. 

 

        """ 

        pass 

 

    def setup_choicelists(self): 

        """This is a hook for code to be run *after* all plugins have been 

        instantiated and *before* the models are being discovered. 

 

        This is useful for redefining your application's ChoiceLists. 

 

        Note that you may not specify values longer than `max_length` when 

        redefining your choicelists.  This limitation is because these 

        redefinitions happen at a moment where database fields have 

        already been instantiated, so it is too late to change their 

        max_length.  Note that this limitation is only for the *values*, 

        not for the names or texts of choices. 

 

        """ 

        self.setup_user_profiles() 

 

    def setup_workflows(self): 

        self.on_each_app('setup_workflows') 

 

    def setup_actions(self): 

        """Hook for subclasses to add or modify actions. 

 

        Usage example:: 

 

            def setup_actions(self): 

                super(Site, self).setup_actions() 

                from lino.core.merge import MergeAction 

                partners = self.modules.contacts 

                for m in (partners.Person, partners.Organisation): 

                    m.define_action(merge_row=MergeAction(m)) 

 

        """ 

        pass 

 

    def setup_layouts(self): 

        '''Hook for subclasses to add or modify layouts. 

         

        Usage example:: 

 

            def setup_layouts(self): 

                super(Site, self).setup_layouts() 

 

                self.modules.system.SiteConfigs.set_detail_layout(""" 

                site_company next_partner_id:10 

                default_build_method 

                clients_account   sales_account     sales_vat_account 

                suppliers_account purchases_account purchases_vat_account 

                """) 

 

                self.modules.accounts.Accounts.set_detail_layout(""" 

                ref:10 name id:5 

                seqno group type clearable 

                ledger.MovementsByAccount 

                """) 

 

        ''' 

        pass 

 

    def add_user_field(self, name, fld): 

        if self.user_model: 

            from lino.api import dd 

            dd.inject_field(self.user_model, name, fld) 

 

    def get_used_libs(self, html=None): 

        """ 

        Yield a list of (name, version, url) tuples describing the 

        third-party software used on this Site. 

 

        This function is used by :meth:`using_text` which is used by 

        :meth:`welcome_text`. 

 

        """ 

 

        import lino 

        yield ("Lino", lino.SETUP_INFO['version'], lino.SETUP_INFO['url']) 

 

        try: 

            import mod_wsgi 

            version = "{0}.{1}".format(*mod_wsgi.version) 

            yield ("mod_wsgi", version, "http://www.modwsgi.org/") 

        except ImportError: 

            pass 

 

        import django 

        yield ("Django", django.get_version(), "http://www.djangoproject.com") 

 

        import sys 

        version = "%d.%d.%d" % sys.version_info[:3] 

        yield ("Python", version, "http://www.python.org/") 

 

        import babel 

        yield ("Babel", babel.__version__, "http://babel.edgewall.org/") 

 

        #~ import tidylib 

        #~ version = getattr(tidylib,'__version__','') 

        #~ yield ("tidylib",version,"http://countergram.com/open-source/pytidylib") 

 

        #~ import pyPdf 

        #~ version = getattr(pyPdf,'__version__','') 

        #~ yield ("pyPdf",version,"http://countergram.com/open-source/pytidylib") 

 

        import jinja2 

        version = getattr(jinja2, '__version__', '') 

        yield ("Jinja", version, "http://jinja.pocoo.org/") 

 

        import sphinx 

        version = getattr(sphinx, '__version__', '') 

        yield ("Sphinx", version, "http://sphinx-doc.org/") 

 

        import dateutil 

        version = getattr(dateutil, '__version__', '') 

        yield ("python-dateutil", version, "http://labix.org/python-dateutil") 

 

        #~ try: 

            #~ import Cheetah 

            #~ version = Cheetah.Version 

            #~ yield ("Cheetah",version ,"http://cheetahtemplate.org/") 

        #~ except ImportError: 

            #~ pass 

 

        try: 

            from odf import opendocument 

            version = opendocument.__version__ 

        except ImportError: 

            version = self.not_found_msg 

        yield ("OdfPy", version, "http://pypi.python.org/pypi/odfpy") 

 

        try: 

            import docutils 

            version = docutils.__version__ 

        except ImportError: 

            version = self.not_found_msg 

        yield ("docutils", version, "http://docutils.sourceforge.net/") 

 

        try: 

            import suds 

            version = suds.__version__ 

        except ImportError: 

            version = self.not_found_msg 

        yield ("suds", version, "https://fedorahosted.org/suds/") 

 

        import yaml 

        version = getattr(yaml, '__version__', '') 

        yield ("PyYaml", version, "http://pyyaml.org/") 

 

        if False: 

            try: 

                import pyratemp 

                version = getattr(pyratemp, '__version__', '') 

            except ImportError: 

                version = self.not_found_msg 

            yield ("pyratemp", version, 

                   "http://www.simple-is-better.org/template/pyratemp.html") 

 

        if False: 

            try: 

                import ho.pisa as pisa 

                version = getattr(pisa, '__version__', '') 

                yield ("xhtml2pdf", version, "http://www.xhtml2pdf.com") 

            except ImportError: 

                pass 

 

            try: 

                import reportlab 

                version = reportlab.Version 

            except ImportError: 

                version = self.not_found_msg 

            yield ("ReportLab", version, 

                   "http://www.reportlab.org/rl_toolkit.html") 

 

        try: 

            #~ import appy 

            from appy import version 

            version = version.verbose 

        except ImportError: 

            version = self.not_found_msg 

        yield ("Appy", version, "http://appyframework.org/pod.html") 

 

        for p in self.installed_plugins: 

            for u in p.get_used_libs(html): 

                yield u 

 

    def apply_languages(self): 

        """This function is called when a Site object gets instantiated, 

        i.e. while Django is still loading the settings. It analyzes 

        the :attr:`languages` attribute and converts it to a tuple of 

        :data:`LanguageInfo` objects. 

 

        """ 

 

        if isinstance(self.languages, tuple) \ 

           and isinstance(self.languages[0], LanguageInfo): 

            # e.g. override_defaults() has been called explicitly, without 

            # specifying a languages keyword. 

            return 

 

        self.language_dict = dict()  # maps simple_code -> LanguageInfo 

 

        self.LANGUAGE_CHOICES = [] 

        self.LANGUAGE_DICT = dict()  # used in lino.modlib.users 

        must_set_language_code = False 

 

        #~ self.AVAILABLE_LANGUAGES = (to_locale(self.DEFAULT_LANGUAGE),) 

        if self.languages is None: 

            self.languages = [DJANGO_DEFAULT_LANGUAGE] 

            #~ self.update_settings(USE_L10N = False) 

 

            #~ info = LanguageInfo(DJANGO_DEFAULT_LANGUAGE,to_locale(DJANGO_DEFAULT_LANGUAGE),0,'') 

            #~ self.DEFAULT_LANGUAGE = info 

            #~ self.languages = (info,) 

            #~ self.language_dict[info.name] = info 

        else: 

            if isinstance(self.languages, six.string_types): 

                self.languages = self.languages.split() 

            #~ lc = [x for x in self.django_settings.get('LANGUAGES' if x[0] in languages] 

            #~ lc = language_choices(*self.languages) 

            #~ self.update_settings(LANGUAGES = lc) 

            #~ self.update_settings(LANGUAGE_CODE = lc[0][0]) 

            #~ self.update_settings(LANGUAGE_CODE = self.languages[0]) 

            self.update_settings(USE_L10N=True) 

            must_set_language_code = True 

 

        languages = [] 

        for i, django_code in enumerate(self.languages): 

            assert_django_code(django_code) 

            name = (to_locale(django_code)) 

            if name in self.language_dict: 

                raise Exception("Duplicate name %s for language code %r" 

                                % (name, django_code)) 

            if i == 0: 

                suffix = '' 

            else: 

                suffix = '_' + name 

            info = LanguageInfo(str(django_code), str(name), i, str(suffix)) 

            self.language_dict[name] = info 

            languages.append(info) 

 

        new_languages = languages 

        for info in tuple(new_languages): 

            if '-' in info.django_code: 

                base, loc = info.django_code.split('-') 

                if not base in self.language_dict: 

                    self.language_dict[base] = info 

 

                    # replace the complicated info by a simplified one 

                    #~ newinfo = LanguageInfo(info.django_code,base,info.index,info.suffix) 

                    #~ new_languages[info.index] = newinfo 

                    #~ del self.language_dict[info.name] 

                    #~ self.language_dict[newinfo.name] = newinfo 

 

        #~ for base,lst in simple_codes.items(): 

            #~ if len(lst) == 1 and and not base in self.language_dict: 

                #~ self.language_dict[base] = lst[0] 

 

        self.languages = tuple(new_languages) 

        self.DEFAULT_LANGUAGE = self.languages[0] 

 

        self.BABEL_LANGS = tuple(self.languages[1:]) 

 

        if must_set_language_code: 

            self.update_settings(LANGUAGE_CODE=self.languages[0].django_code) 

            # Note: LANGUAGE_CODE is what *Django* believes to be the 

            # default language.  This should be some variant of 

            # English ('en' or 'en-us') if you use 

            # `django.contrib.humanize` 

            # https://code.djangoproject.com/ticket/20059 

 

        self.setup_languages() 

 

    def setup_languages(self): 

        """ 

        Reduce Django's :setting:`LANGUAGES` to my `languages`. 

        Note that lng.name are not yet translated, we take these 

        from `django.conf.global_settings`. 

        """ 

 

        from django.conf.global_settings import LANGUAGES 

 

        def langtext(code): 

            for k, v in LANGUAGES: 

                if k == code: 

                    return v 

            # returns None if not found 

 

        def _add_language(code, lazy_text): 

            self.LANGUAGE_DICT[code] = lazy_text 

            self.LANGUAGE_CHOICES.append((code, lazy_text)) 

 

        if self.languages is None: 

 

            _add_language(DJANGO_DEFAULT_LANGUAGE, _("English")) 

 

        else: 

 

            for lang in self.languages: 

                code = lang.django_code 

                text = langtext(code) 

                if text is None: 

                    # Django doesn't know these 

                    if code == 'de-be': 

                        text = gettext_noop("German (Belgium)") 

                    elif code == 'de-ch': 

                        text = gettext_noop("German (Swiss)") 

                    elif code == 'de-at': 

                        text = gettext_noop("German (Austria)") 

                    elif code == 'en-us': 

                        text = gettext_noop("American English") 

                    else: 

                        raise Exception( 

                            "Unknown language code %r (must be one of %s)" % ( 

                                lang.django_code, 

                                [x[0] for x in LANGUAGES])) 

 

                text = _(text) 

                _add_language(lang.django_code, text) 

 

            """ 

            Cannot activate the site's default language 

            because some test cases in django.contrib.humanize 

            rely on en-us as default language 

            """ 

            #~ set_language(self.get_default_language()) 

 

            """ 

            reduce LANGUAGES to my babel languages: 

            """ 

            self.update_settings( 

                LANGUAGES=[x for x in LANGUAGES 

                           if x[0] in self.LANGUAGE_DICT]) 

 

    def get_language_info(self, code): 

        """Use this in Python fixtures or tests to test whether a Site 

        instance supports a given language.  `code` must be a 

        Django-style language code. 

         

        On a site with only one locale of a language (and optionally 

        some other languages), you can use only the language code to 

        get a tuple of :data:`LanguageInfo` objects. 

         

        >>> from lino.core.site import TestSite as Site 

        >>> Site(languages="en-us fr de-be de").get_language_info('en') 

        LanguageInfo(django_code='en-us', name='en_US', index=0, suffix='') 

         

        On a site with two locales of a same language (e.g. 'en-us' 

        and 'en-gb'), the simple code 'en' yields that first variant: 

         

        >>> site = Site(languages="en-us en-gb") 

        >>> print site.get_language_info('en') 

        LanguageInfo(django_code='en-us', name='en_US', index=0, suffix='') 

 

        """ 

        return self.language_dict.get(code, None) 

 

    def resolve_languages(self, languages): 

        """ 

        This is used by `UserProfile`. 

         

        Examples: 

         

        >>> from lino.core.site import TestSite as Site 

        >>> lst = Site(languages="en fr de nl et pt").resolve_languages('en fr') 

        >>> [i.name for i in lst] 

        ['en', 'fr'] 

         

        You may not specify languages which don't exist on this site: 

         

        >>> Site(languages="en fr de").resolve_languages('en nl') 

        Traceback (most recent call last): 

        ... 

        Exception: Unknown language code 'nl' (must be one of ['en', 'fr', 'de']) 

         

         

        """ 

        rv = [] 

        if isinstance(languages, six.string_types): 

            languages = languages.split() 

        for k in languages: 

            if isinstance(k, six.string_types): 

                li = self.get_language_info(k) 

                if li is None: 

                    raise Exception("Unknown language code %r (must be one of %s)" % ( 

                        str(k), [li.name for li in self.languages])) 

                rv.append(li) 

            else: 

                assert k in self.languages 

                rv.append(k) 

        return tuple(rv) 

 

    def language_choices(self, language, choices): 

        l = choices.get(language, None) 

        if l is None: 

            l = choices.get(self.DEFAULT_LANGUAGE) 

        return l 

 

    def get_default_language(self): 

        """ 

        The django code of the default language to use in every 

        :class:`dd.LanguageField`. 

         

        """ 

        return self.DEFAULT_LANGUAGE.django_code 

 

    def str2kw(self, name, txt,  **kw): 

        """ 

        Return a dictionary which maps the internal field names for 

        babelfield `name` to their respective translation of the given 

        lazy translatable string `text`. 

 

        >>> from django.utils.translation import ugettext_lazy as _ 

        >>> from lino.core.site import TestSite as Site 

        >>> from atelier.utils import dict_py2 

        >>> site = Site(languages='de fr es') 

        >>> dict_py2(site.str2kw('name', _("January"))) 

        {'name_fr': 'janvier', 'name': 'Januar', 'name_es': 'Enero'} 

        >>> site = Site(languages='fr de es') 

        >>> dict_py2(site.str2kw('name', _("January"))) 

        {'name_de': 'Januar', 'name': 'janvier', 'name_es': 'Enero'} 

 

        """ 

        from django.utils import translation 

        for simple, info in list(self.language_dict.items()): 

            with translation.override(simple): 

                kw[name + info.suffix] = str(txt) 

        return kw 

 

    def babelkw(self, name, **kw): 

        """ 

        Return a dict with appropriate resolved field names for a 

        BabelField `name` and a set of hard-coded values. 

 

        You have some hard-coded multilingual content in a fixture: 

        >>> from lino.core.site import TestSite as Site 

        >>> from atelier.utils import dict_py2 

        >>> kw = dict(de="Hallo", en="Hello", fr="Salut") 

 

        The field names where this info gets stored depends on the 

        Site's `languages` distribution. 

         

        >>> dict_py2(Site(languages="de-be en").babelkw('name',**kw)) 

        {'name_en': 'Hello', 'name': 'Hallo'} 

         

        >>> dict_py2(Site(languages="en de-be").babelkw('name',**kw)) 

        {'name_de_BE': 'Hallo', 'name': 'Hello'} 

         

        >>> dict_py2(Site(languages="en-gb de").babelkw('name',**kw)) 

        {'name_de': 'Hallo', 'name': 'Hello'} 

         

        >>> dict_py2(Site(languages="en").babelkw('name',**kw)) 

        {'name': 'Hello'} 

         

        >>> dict_py2(Site(languages="de-be en").babelkw('name',de="Hallo",en="Hello")) 

        {'name_en': 'Hello', 'name': 'Hallo'} 

 

        In the following example `babelkw` attributes the  

        keyword `de` to the *first* language variant: 

         

        >>> dict_py2(Site(languages="de-ch de-be").babelkw('name',**kw)) 

        {'name': 'Hallo'} 

         

         

        """ 

        d = dict() 

        for simple, info in list(self.language_dict.items()): 

            v = kw.get(simple, None) 

            if v is not None: 

                d[name + info.suffix] = v 

        return d 

 

    def args2kw(self, name, *args): 

        """ 

        Takes the basename of a BabelField and the values for each language. 

        Returns a `dict` mapping the actual fieldnames to their values. 

        """ 

        assert len(args) == len(self.languages) 

        kw = {name: args[0]} 

        for i, lang in enumerate(self.BABEL_LANGS): 

            kw[name + '_' + lang] = args[i + 1] 

        return kw 

 

    def field2kw(self, obj, name, **known_values): 

        """Return a `dict` with all values of the BabelField `name` in the 

given object `obj`. The dict will have one key for each 

:attr:`languages`. 

 

        Examples: 

 

        >>> from lino.core.site import TestSite as Site 

        >>> from atelier.utils import AttrDict 

        >>> from atelier.utils import dict_py2 

        >>> def testit(site_languages): 

        ...     site = Site(languages=site_languages) 

        ...     obj = AttrDict(site.babelkw( 

        ...         'name', de="Hallo", en="Hello", fr="Salut")) 

        ...     return site,obj 

 

 

        >>> site, obj = testit('de en') 

        >>> dict_py2(site.field2kw(obj, 'name')) 

        {'de': 'Hallo', 'en': 'Hello'} 

 

        >>> site, obj = testit('fr et') 

        >>> dict_py2(site.field2kw(obj, 'name')) 

        {'fr': 'Salut'} 

 

        """ 

        #~ d = { self.DEFAULT_LANGUAGE.name : getattr(obj,name) } 

        for lng in self.languages: 

            v = getattr(obj, name + lng.suffix, None) 

            if v: 

                known_values[lng.name] = v 

        return known_values 

 

    def field2args(self, obj, name): 

        """ 

        Return a list of the babel values of this field in the order of 

        this Site's :attr:`Site.languages` attribute. 

         

        """ 

        return [getattr(obj, name + li.suffix) for li in self.languages] 

        #~ l = [ getattr(obj,name) ] 

        #~ for lang in self.BABEL_LANGS: 

            #~ l.append(getattr(obj,name+'_'+lang)) 

        #~ return l 

 

    def babelitem(self, *args, **values): 

        """ 

        Given a dictionary with babel values, return the 

        value corresponding to the current language. 

 

        This is available in templates as a function `tr`. 

 

        >>> kw = dict(de="Hallo", en="Hello", fr="Salut") 

 

        >>> from lino.core.site import TestSite as Site 

        >>> from django.utils import translation 

 

        A Site with default language "de": 

 

        >>> site = Site(languages="de en") 

        >>> tr = site.babelitem 

        >>> with translation.override('de'): 

        ...    print(tr(**kw)) 

        Hallo 

 

        >>> with translation.override('en'): 

        ...    print(tr(**kw)) 

        Hello 

 

        If the current language is not found in the specified `values`, 

        then it returns the site's default language: 

 

        >>> with translation.override('jp'): 

        ...    print(tr(en="Hello", de="Hallo", fr="Salut")) 

        Hallo 

 

        Testing detail: default language should be "de" in our example, but 

        we are playing here with more than one Site instance while Django 

        knows only one "default language" which is the one specified in 

        `lino.projects.docs.settings`. 

 

        Another way is to specify an explicit default value using a 

        positional argument. In that case the language's default language 

        doesn'n matter: 

 

        >>> with translation.override('jp'): 

        ...    print(tr("Tere", de="Hallo", fr="Salut")) 

        Tere 

 

        >>> with translation.override('de'): 

        ...     print(tr("Tere", de="Hallo", fr="Salut")) 

        Hallo 

 

        You may not specify more than one default value: 

 

        >>> tr("Hello", "Hallo") 

        Traceback (most recent call last): 

        ... 

        ValueError: ('Hello', 'Hallo') is more than 1 default value. 

 

 

        """ 

        if len(args) == 0: 

            info = self.language_dict.get( 

                get_language(), self.DEFAULT_LANGUAGE) 

            default_value = None 

            if info == self.DEFAULT_LANGUAGE: 

                return values.get(info.name) 

            x = values.get(info.name, None) 

            if x is None: 

                return values.get(self.DEFAULT_LANGUAGE.name) 

            return x 

        elif len(args) == 1: 

            info = self.language_dict.get(get_language(), None) 

            if info is None: 

                return args[0] 

            default_value = args[0] 

            return values.get(info.name, default_value) 

        args = tuple_py2(args) 

        print(type(args)) 

        raise ValueError("%(values)s is more than 1 default value." % 

                         dict(values=args)) 

 

    # babel_get(v) = babelitem(**v) 

 

    def babeldict_getitem(self, d, k): 

        v = d.get(k, None) 

        if v is not None: 

            assert type(v) is dict 

            return self.babelitem(**v) 

 

    def babelattr(self, obj, attrname, default=NOT_PROVIDED, language=None): 

        """ 

        Return the value of the specified babel field `attrname` of `obj` 

        in the current language. 

 

        This is to be used in multilingual document templates.  For 

        example in a document template of a Contract you may use the 

        following expression:: 

 

          babelattr(self.type, 'name') 

 

        This will return the correct value for the current language. 

 

        Examples: 

 

        >>> from __future__ import unicode_literals 

        >>> from django.utils import translation 

        >>> from lino.core.site import TestSite as Site 

        >>> from atelier.utils import AttrDict 

        >>> def testit(site_languages): 

        ...     site = Site(languages=site_languages) 

        ...     obj = AttrDict(site.babelkw( 

        ...         'name', de="Hallo", en="Hello", fr="Salut")) 

        ...     return site, obj 

 

 

        >>> site,obj = testit('de en') 

        >>> with translation.override('de'): 

        ...     print(site.babelattr(obj,'name')) 

        Hallo 

 

        >>> with translation.override('en'): 

        ...     print(site.babelattr(obj,'name')) 

        Hello 

 

        If the object has no translation for a given language, return 

        the site's default language.  Two possible cases: 

 

        The language exists on the site, but the object has no 

        translation for it: 

 

        >>> site,obj = testit('en es') 

        >>> with translation.override('es'): 

        ...     print(site.babelattr(obj, 'name')) 

        Hello 

 

        Or a language has been activated which doesn't exist on the site: 

 

        >>> with translation.override('fr'): 

        ...     print(site.babelattr(obj, 'name')) 

        Hello 

 

 

        """ 

        if language is None: 

            language = get_language() 

        info = self.language_dict.get(language, self.DEFAULT_LANGUAGE) 

        if info.index != 0: 

            v = getattr(obj, attrname + info.suffix, None) 

            if v: 

                return v 

        if default is NOT_PROVIDED: 

            return getattr(obj, attrname) 

        else: 

            return getattr(obj, attrname, default) 

        #~ if lang is not None and lang != self.DEFAULT_LANGUAGE: 

            #~ v = getattr(obj,attrname+"_"+lang,None) 

            #~ if v: 

                #~ return v 

        #~ return getattr(obj,attrname,*args) 

 

    def diagnostic_report_rst(self, *args): 

        """Returns a string with a diagnostic report about this 

site. :manage:`diag` is a command-line shortcut to this. 

 

        """ 

        s = '' 

        s += rstgen.header(1, "Plugins") 

        for n, kp in enumerate(self.plugins.items()): 

            s += "%d. " % (n + 1) 

            s += "%s : %s\n" % kp 

        # s += "config_dirs: %s\n" % repr(self.confdirs.config_dirs) 

        s += "\n" 

        s += rstgen.header(1, "Config directories") 

        for n, cd in enumerate(self.confdirs.config_dirs): 

            s += "%d. " % (n + 1) 

            ln = relpath(cd.name) 

            if cd.writeable: 

                ln += " [writeable]" 

            s += ln + '\n' 

        # for arg in args: 

        #     p = self.plugins[arg] 

        return s 

 

    # def get_db_overview_rst(self): 

    #     from lino.utils.diag import analyzer 

    #     analyzer.show_db_overview() 

 

    def override_defaults(self, **kwargs): 

        self.override_settings(**kwargs) 

        self.install_settings() 

 

    def is_imported_partner(self, obj): 

        """ 

        Return whether the specified 

        :class:`Partner <ml.contacts.Partner>` instance 

        `obj` is to be considered as imported from some legacy database. 

        """ 

        #~ return obj.id is not None and (obj.id < 200000 or obj.id > 299999) 

        return False 

        #~ return obj.id is not None and (obj.id > 10 and obj.id < 21) 

 

    def site_header(self): 

        """Used in footnote or header of certain printed documents. 

 

        The convention is to call it as follows from an appy.pod template 

        (use the `html` function, not `xhtml`) 

        :: 

 

          do text 

          from html(settings.SITE.site_header()) 

 

        Note that this is expected to return a unicode string possibly 

        containing valid HTML (not XHTML) tags for formatting. 

 

        """ 

        if self.is_installed('contacts'): 

            if self.site_config.site_company: 

                return self.site_config.site_company.get_address('<br/>') 

                #~ s = unicode(self.site_config.site_company) + " / "  + s 

        #~ return '' 

 

    def setup_main_menu(self): 

        """ 

        To be implemented by applications. 

        """ 

        pass 

 

    @property 

    def site_config(self): 

        """ 

        This property holds a cached version of the one and only 

        :class:`ml.system.SiteConfig` row that holds site-wide 

        database-stored and web-editable Site configuration parameters. 

 

        If no instance exists (which happens in a virgin database), we 

        create it using default values from :attr:`site_config_defaults`. 

 

        This is always `None` when :mod:`lino.modlib.system` is not installed. 

 

        """ 

        if 'system' not in self.modules: 

            return None 

 

        if self._site_config is None: 

            #~ raise Exception(20130301) 

            #~ print '20130320 create _site_config' 

            #~ from lino.core.utils import resolve_model 

            #~ from lino.core.utils import obj2str 

            #~ from lino.utils import dblogger as logger 

            #~ SiteConfig = resolve_model('system.SiteConfig') 

            SiteConfig = self.modules.system.SiteConfig 

            #~ from .models import SiteConfig 

            #~ from django.db.utils import DatabaseError 

            try: 

                #~ self._site_config = SiteConfig.real_objects.get(pk=1) 

                self._site_config = SiteConfig.real_objects.get( 

                    pk=self.config_id) 

                #~ print "20130301 Loaded SiteConfig record", obj2str(self._site_config,True) 

            #~ except (SiteConfig.DoesNotExist,DatabaseError): 

            except SiteConfig.DoesNotExist: 

            #~ except Exception,e: 

                kw = dict(pk=self.config_id) 

                #~ kw.update(settings.SITE.site_config_defaults) 

                kw.update(self.site_config_defaults) 

                self._site_config = SiteConfig(**kw) 

                #~ print "20130301 Created SiteConfig record", obj2str(self._site_config,True) 

                # 20120725 

                # polls_tutorial menu selection `Config --> Site Parameters` 

                # said "SiteConfig 1 does not exist" 

                # cannot save the instance here because the db table possibly doesn't yet exit. 

                #~ self._site_config.save() 

        return self._site_config 

    #~ site_config = property(get_site_config) 

 

    #~ def shutdown(self): 

        #~ self.clear_site_config() 

        #~ return super(Site,self).shutdown() 

 

    def clear_site_config(self): 

        """ 

        Clear the cached SiteConfig instance. 

 

        This is needed e.g. when the test runner has created a new 

        test database. 

        """ 

        self._site_config = None 

        #~ print "20130320 clear_site_config" 

 

    def get_quicklinks(self, ar): 

        from lino.core import menus 

        m = menus.Toolbar(ar.get_user().profile, 'quicklinks') 

        self.setup_quicklinks(ar, m) 

        return m 

 

    def get_site_menu(self, ui, profile): 

        """ 

        Return this site's main menu for the given UserProfile. 

        Must be a :class:`lino.core.menus.Toolbar` instance. 

        Applications usually should not need to override this. 

        """ 

        from lino.core import menus 

        main = menus.Toolbar(profile, 'main') 

        self.setup_menu(profile, main) 

        main.compress() 

        return main 

 

    def setup_quicklinks(self, ar, m): 

        """ 

        Override this 

        in application-specific (or even local) :xfile:`settings.py` files 

        to define a series of *quick links* to appear below the main menu bar. 

        Example see :meth:`lino.projects.pcsw.settings.Site.setup_quicklinks`. 

        """ 

        self.on_each_app('setup_quicklinks', ar, m) 

 

    def setup_menu(self, profile, main): 

        """Set up the application's menu structure. 

 

        The default implementation uses a system of predefined 

        top-level items that are filled by the different installed 

        plugins. 

 

        - `setup_master_menu` 

        - `setup_main_menu` 

        - `setup_reports_menu` 

        - `setup_config_menu` 

        - `setup_explorer_menu` 

        - `setup_site_menu` 

 

 

        """ 

        if AFTER17: 

            from django.apps import apps 

            apps = [a.models_module for a in apps.get_app_configs()] 

        else: 

            from django.db.models import loading 

            apps = loading.get_apps() 

 

        for k, label in self.top_level_menus: 

            methname = "setup_{0}_menu".format(k) 

 

            for mod in apps: 

                if hasattr(mod, methname): 

                    msg = "{0} still has a function {1}(). \ 

Please convert to Plugin method".format(mod, methname) 

                    raise Exception(msg) 

 

            if label is None: 

                menu = main 

            else: 

                menu = main.add_menu(k, label) 

            for p in self.installed_plugins: 

                meth = getattr(p, methname, None) 

                if meth is not None: 

                    meth(self, profile, menu) 

 

    def get_middleware_classes(self): 

        """Yields the strings to be stored in 

        the :setting:`MIDDLEWARE_CLASSES` setting. 

 

        In case you don't want to use this method for defining 

        :setting:`MIDDLEWARE_CLASSES`, you can simply set 

        :setting:`MIDDLEWARE_CLASSES` in your :xfile:`settings.py` 

        after the :class:`Site` has been instantiated. 

 

        `Django and standard HTTP authentication 

        <http://stackoverflow.com/questions/152248/can-i-use-http-basic-authentication-with-django>`_ 

 

        """ 

 

        yield 'django.middleware.common.CommonMiddleware' 

        #~ yield 'django.contrib.sessions.middleware.SessionMiddleware' 

        if self.languages and len(self.languages) > 1: 

            yield 'django.middleware.locale.LocaleMiddleware' 

        #~ yield 'django.contrib.auth.middleware.AuthenticationMiddleware' 

        #~ if self.user_model: 

        #~ if self.user_model is None: 

            #~ yield 'lino.core.auth.NoUserMiddleware' 

 

        if self.auth_middleware: 

            yield self.auth_middleware 

        else: 

            if self.user_model is None: 

                yield 'lino.core.auth.NoUserMiddleware' 

            elif self.default_user: 

                if self.default_user == "anonymous": 

                    yield 'lino.core.auth.NoUserMiddleware' 

                else: 

                    yield 'lino.core.auth.DefaultUserMiddleware' 

            elif self.remote_user_header: 

                yield 'lino.core.auth.RemoteUserMiddleware' 

            else: 

                # not using remote http auth, so we need sessions 

                yield 'django.contrib.sessions.middleware.SessionMiddleware' 

                if self.ldap_auth_server: 

                    yield 'lino.core.auth.LDAPAuthMiddleware' 

                else: 

                    yield 'lino.core.auth.SessionUserMiddleware' 

 

                #~ raise Exception("""\ 

    #~ `user_model` is not None, but no `remote_user_header` in your settings.SITE.""") 

        #~ yield 'lino.utils.editing.EditingMiddleware' 

        if True: 

            yield 'lino.utils.ajax.AjaxExceptionResponse' 

 

        if False:  # not BYPASS_PERMS: 

            yield 'django.contrib.auth.middleware.RemoteUserMiddleware' 

            # TODO: find solution for this: 

            #~ AUTHENTICATION_BACKENDS = ( 

              #~ 'django.contrib.auth.backends.RemoteUserBackend', 

            #~ ) 

 

        if False: 

            #~ yield 'lino.utils.sqllog.ShortSQLLogToConsoleMiddleware' 

            yield 'lino.utils.sqllog.SQLLogToConsoleMiddleware' 

            #~ yield 'lino.utils.sqllog.SQLLogMiddleware' 

 

    # def get_main_action(self, profile): 

    #     """No longer used. 

    #     Return the action to show as top-level "index.html". 

    #     The default implementation returns `None`, which means 

    #     that Lino will call :meth:`get_main_html`. 

    #     """ 

    #     return None 

 

    def get_main_html(self, request): 

        """Return a chunk of html to be displayed in the main area of the 

        admin index.  This is being called only if 

        :meth:`get_main_action` returns `None`.  The default 

        implementation renders the :xfile:`admin_main.html` template. 

 

        """ 

        return self.plugins.jinja.render_from_request( 

            request, 'admin_main.html') 

 

    def get_welcome_messages(self, ar): 

        """ 

        Yields a list of "welcome messages" (see 

        :meth:`lino.core.actors.Actor.get_welcome_messages`) of all 

        actors.  This is being called from :xfile:`admin_main.html`. 

        """ 

 

        for h in self._welcome_handlers: 

            for msg in h(ar): 

                yield msg 

        # for a in self._welcome_actors: 

        #     for msg in a.get_welcome_messages(ar): 

        #         yield msg 

 

    def add_welcome_handler(self, func): 

        """Add the given callable as a "welcome handler".  Lino will call 

        every welcome handler for every incoming request, passing them 

        a :class:`BaseRequest <lino.core.requests.BaseRequest>` 

        instance representing this request as positional argument. 

        The callable is expected to yield a series of messages 

        (usually either 0 or 1). Each message must be either a string 

        or a :class:`E.span <lino.utils.xmlgen.html.E>` element. 

 

        """ 

        self._welcome_handlers.append(func) 

 

    def get_installed_apps(self): 

        """Yield the list of apps to be installed on this site.  Each item 

        must be either a string (unicode being converted to str) or a 

        *generator* which will be iterated recursively (again 

        expecting either strings or generators of strings). 

 

        Lino will call this method exactly once when the :class:`Site` 

        instantiates.  The resulting list of names will then possibly 

        altered by the :meth:`get_apps_modifiers` method before being 

        assigned to the :setting:`INSTALLED_APPS` setting. 

 

        """ 

 

        if self.django_admin_prefix: 

            yield 'django.contrib.admin' 

        yield 'django.contrib.staticfiles' 

        yield 'lino.modlib.about' 

 

        if self.default_ui == "extjs": 

            yield 'lino.modlib.extjs' 

            yield 'lino.modlib.bootstrap3' 

        elif self.default_ui == "bootstrap3": 

            yield 'lino.modlib.bootstrap3' 

 

        for a in self.local_apps: 

            yield a 

 

        # yield "lino.modlib.lino_startup" 

 

    site_prefix = '/' 

    """The string to prefix to every URL of the Lino web interface. 

 

    This must *start and end with a *slash*.  Default value is 

    ``'/'``. 

 

    This must be set if your project is not being served at the "root" 

    URL of your server. 

 

    If this is different from the default value, Lino also sets 

    :setting:`SESSION_COOKIE_PATH`. 

 

    When this Site is running under something else than a development 

    server, this setting must correspond to your web server's 

    configuration.  For example if you have:: 

     

        WSGIScriptAlias /foo /home/luc/mypy/lino_sites/foo/wsgi.py 

       

    Then your :xfile:`settings.py` should specify:: 

     

        site_prefix = '/foo/' 

     

    See also :ref:`mass_hosting`. 

 

    """ 

 

    def buildurl(self, *args, **kw): 

        url = self.site_prefix + ("/".join(args)) 

        if len(kw): 

            url += "?" + urlencode(kw) 

        return url 

 

    def build_media_url(self, *args, **kw): 

        from django.conf import settings 

        url = settings.MEDIA_URL + ("/".join(args)) 

        if len(kw): 

            url += "?" + urlencode(kw) 

        return url 

 

    def build_static_url(self, *args, **kw): 

        from django.conf import settings 

        url = settings.STATIC_URL + ("/".join(args)) 

        if len(kw): 

            url += "?" + urlencode(kw) 

        return url 

 

    def get_system_note_recipients(self, request, obj, silent): 

        """Return or yield a list of recipients 

        (i.e. strings like "John Doe  <john@example.com>" ) 

        to be notified by email about a system note issued 

        by action request `ar` about the object instance `obj`. 

 

        Default behaviour is to simply forward it to the `obj`'s 

        :meth:`get_system_note_recipients 

        <lino.core.model.Model.get_system_note_recipients>`, but here 

        is a hook to define local exceptions to the application 

        specific default rules. 

 

        """ 

        return obj.get_system_note_recipients(request, silent) 

 

    def emit_system_note(self, request, owner, subject, body, silent): 

        """Send a *system note* about the given model instance `owner` with 

        the given `subject` and `body` . 

 

        A **system note** is a text message attached to a given 

        database object (`owner`) which is stored and propagated 

        through a series of customizable and configurable channels. 

 

        The text part consists basically of a subject and a body, both 

        usually generated by the application and possibly edited by 

        the user (e.g. in the dialog window of a 

        :class:`NotifyingAction <lino.core.actions.NotifyingAction>`). 

 

        This method will build the list of email recipients by calling 

        the locally configurable :meth:`get_system_note_recipients` 

        method and send an email to each of these recipients. 

 

        """ 

        # self.logger.info("20121016 add_system_note() '%s'",subject) 

 

        if self.is_installed('notes'): 

            notes = self.modules.notes 

            notes.add_system_note(request, owner, subject, body) 

        sender = request.user.email or self.django_settings['SERVER_EMAIL'] 

        if not sender or '@example.com' in sender: 

            return 

        recipients = self.get_system_note_recipients(request, owner, silent) 

        self.send_email(subject, sender, body, recipients) 

 

    def send_email(self, subject, sender, body, recipients): 

        """Send an email message with the specified arguments (the same 

signature as `django.core.mail.EmailMessage`. 

 

        `recipients` is an iterator over a list of strings with email 

        addresses. Any address containing '@example.com' will be 

        removed. Does nothing if the resulting list of recipients is 

        empty. 

 

        """ 

        if '@example.com' in sender: 

            self.logger.info( 

                "Ignoring email '%s' because sender is %s", subject, sender) 

            return 

        recipients = [a for a in recipients if '@example.com' not in a] 

        if not len(recipients): 

            self.logger.info( 

                "Ignoring email '%s' because there is no recipient", subject) 

            return 

 

        from django.core.mail import EmailMessage 

        msg = EmailMessage(subject=subject, 

                           from_email=sender, body=body, to=recipients) 

        self.logger.info( 

            "Send email '%s' from %s to %s", subject, sender, recipients) 

        msg.send() 

 

    def welcome_html(self, ui=None): 

        """ 

        Return a HTML version of the "This is APPLICATION 

        version VERSION using ..." text. to be displayed in the 

        About dialog, in the plain html footer, and maybe at other 

        places. 

 

        """ 

        from django.utils.translation import ugettext as _ 

 

        p = [] 

        sep = '' 

        if self.verbose_name: 

            p.append(_("This is ")) 

            if self.url: 

                p.append( 

                    E.a(self.verbose_name, href=self.url, target='_blank')) 

            else: 

                p.append(E.b(self.verbose_name)) 

            if self.version: 

                p.append(' ') 

                p.append(self.version) 

            sep = _(' using ') 

 

        for name, version, url in self.get_used_libs(html=E): 

            p.append(sep) 

            p.append(E.a(name, href=url, target='_blank')) 

            p.append(' ') 

            p.append(version) 

            sep = ', ' 

        return E.span(*p) 

 

    def login(self, username=None, **kw): 

        """Open a session as the user with the given `username`. 

     

        For usage from a shell or a tested document.  Does not require 

        any password because when somebody has command-line access we 

        trust that she has already authenticated. 

     

        It returns a 

        :class:`BaseRequest <lino.core.requests.BaseRequest>` object. 

 

        """ 

        self.startup() 

        User = self.user_model 

        if User and username: 

            try: 

                kw.update(user=User.objects.get(username=username)) 

            except User.DoesNotExist: 

                raise Exception("'{0}' : no such user".format(username)) 

 

        # if not 'renderer' in kw: 

        #     kw.update(renderer=self.ui.text_renderer) 

 

        from lino.core import requests 

        import lino.core.urls  # hack: trigger ui instantiation 

        return requests.BaseRequest(**kw) 

 

    def get_letter_date_text(self, today=None): 

        """Returns a string like "Eupen, den 26. August 2013". 

 

        """ 

        sc = self.site_config.site_company 

        if today is None: 

            today = self.today() 

        from lino.utils.format_date import fdl 

        if sc and sc.city: 

            return _("%(place)s, %(date)s") % dict( 

                place=str(sc.city.name), date=fdl(today)) 

        return fdl(today) 

 

    def get_admin_main_items(self, ar): 

        """Expected to yield a sequence of "items" to be rendered on the home 

        page (:xfile:`admin_main.html`). 

 

        Every item is expected to be a :class:`dd.Table` or a 

        :class:`dd.VirtualTable`. These tables are rendered in that order, 

        with a limit of :attr:`dd.AbstractTable.preview_limit` rows. 

 

        """ 

        return [] 

 

    def decfmt(self, v, places=2, **kw): 

        r""" Format a Decimal value using :func:`lino.utils.moneyfmt`, but 

        applying the site settings 

        :attr:`lino.Lino.decimal_group_separator` and 

        :attr:`lino.Lino.decimal_separator`. 

 

        >>> from lino.core.site import TestSite as Site 

        >>> from decimal import Decimal 

        >>> self = Site() 

        >>> self.decimal_group_separator 

        u'\xa0' 

        >>> print(self.decimal_separator) 

        , 

 

        >>> x = Decimal(1234) 

        >>> self.decfmt(x) 

        u'1\xa0234,00' 

 

        >>> print(self.decfmt(x, sep=".")) 

        1.234,00 

 

        >>> self.decimal_group_separator = '.' 

        >>> print(self.decfmt(x)) 

        1.234,00 

         

        >>> self.decimal_group_separator = "oops" 

        >>> print(self.decfmt(x)) 

        1oops234,00 

         

 

        """ 

        kw.setdefault('sep', self.decimal_group_separator) 

        kw.setdefault('dp', self.decimal_separator) 

        from lino.utils import moneyfmt 

        return moneyfmt(v, places=places, **kw) 

 

    LOOKUP_OP = '__iexact' 

 

    def lookup_filter(self, fieldname, value, **kw): 

        """ 

        Return a `models.Q` to be used if you want to search for a given  

        string in any of the languages for the given babel field. 

        """ 

        from django.db.models import Q 

        kw[fieldname + self.LOOKUP_OP] = value 

        #~ kw[fieldname] = value 

        flt = Q(**kw) 

        del kw[fieldname + self.LOOKUP_OP] 

        for lng in self.BABEL_LANGS: 

            kw[fieldname + lng.suffix + self.LOOKUP_OP] = value 

            flt = flt | Q(**kw) 

            del kw[fieldname + lng.suffix + self.LOOKUP_OP] 

        return flt 

 

    def relpath(self, p): 

        """Used by :class:`lino.mixins.printable.EditTemplate` in order to 

        write a testable message... 

 

        """ 

        if p.startswith(self.project_dir): 

            p = "$(PRJ)" + p[len(self.project_dir):] 

        return p 

 

 

class TestSite(Site): 

 

    """Used to simplify doctest strings because it inserts default values 

    for the two first arguments that are mandatory but not used in our 

    examples:: 

     

    >> from lino.core.site import Site 

    >> Site(globals(), ...) 

     

    >> from lino.core.site import TestSite as Site 

    >> Site(...) 

 

    """ 

 

    def __init__(self, *args, **kwargs): 

        # kwargs.update(no_local=True) 

        g = dict(__file__=__file__) 

        g.update(SECRET_KEY="20227")  # see :djangoticket:`20227` 

        super(TestSite, self).__init__(g, *args, **kwargs) 

 

        # 20140913 Hack needed for doctests in :mod:`ad`. 

        from django.utils import translation 

        translation._default = None 

 

 

def _test(): 

    # we want to raise an Exception if there is a failure, but 

    # doctest's raise_on_error=True option is not useful because it 

    # does not report the traceback if some test fails. 

    import doctest 

    res = doctest.testmod() 

    if res.failed > 0: 

        raise Exception("{0} (see earlier output)".format(res)) 

 

 

if __name__ == "__main__": 

    _test()