1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
|
''' -- imports from installed packages -- '''
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.template.loader import render_to_string
from django.template.defaultfilters import slugify
from django.shortcuts import render_to_response # , render
from django.http import HttpResponse
from django.core.serializers.json import DjangoJSONEncoder
from django.core.cache import cache
from mongokit import paginator
import mongokit
''' -- imports from application folders/files -- '''
from gnowsys_ndf.settings import META_TYPE, GSTUDIO_NROER_GAPPS
from gnowsys_ndf.settings import GSTUDIO_DEFAULT_GAPPS_LIST, GSTUDIO_WORKING_GAPPS, BENCHMARK
from gnowsys_ndf.ndf.models import db, node_collection, triple_collection
from gnowsys_ndf.ndf.models import *
from gnowsys_ndf.ndf.org2any import org2html
from gnowsys_ndf.mobwrite.models import TextObj
from gnowsys_ndf.ndf.models import HistoryManager, Benchmark
from gnowsys_ndf.notification import models as notification
''' -- imports from python libraries -- '''
# import os -- Keep such imports here
import datetime
import time
from sys import getsizeof, exc_info
import subprocess
import re
import ast
import string
import json
import locale
import multiprocessing as mp
from datetime import datetime, timedelta, date
# import csv
# from collections import Counter
from collections import OrderedDict
col = db[Benchmark.collection_name]
history_manager = HistoryManager()
theme_GST = node_collection.one({'_type': 'GSystemType', 'name': 'Theme'})
theme_item_GST = node_collection.one({'_type': 'GSystemType', 'name': 'theme_item'})
topic_GST = node_collection.one({'_type': 'GSystemType', 'name': 'Topic'})
# C O M M O N M E T H O D S D E F I N E D F O R V I E W S
grp_st = node_collection.one({'$and': [{'_type': 'GSystemType'}, {'name': 'Group'}]})
ins_objectid = ObjectId()
def get_execution_time(f):
if BENCHMARK == 'ON':
def wrap(*args,**kwargs):
time1 = time.time()
total_parm_size = 0
for key, value in kwargs.iteritems():
total_parm_size = total_parm_size + getsizeof(value)
total_param = len(kwargs)
ret = f(*args,**kwargs)
t2 = time.clock()
time2 = time.time()
time_diff = time2 - time1
benchmark_node = col.Benchmark()
benchmark_node.time_taken = unicode(str(time_diff))
benchmark_node.name = unicode(f.func_name)
benchmark_node.parameters = unicode(total_param)
benchmark_node.size_of_parameters = unicode(total_parm_size)
benchmark_node.last_update = datetime.today()
#benchmark_node.functionOplength = unicode(getsizeof(ret))
try:
benchmark_node.calling_url = unicode(args[0].path)
except:
pass
benchmark_node.save()
return ret
if BENCHMARK == 'ON':
return wrap
if BENCHMARK == 'OFF':
return f
@get_execution_time
def get_group_name_id(group_name_or_id, get_obj=False):
'''
- This method takes possible group name/id as an argument and returns (group-name and id) or group object.
- If no second argument is passed, as method name suggests, returned result is "group_name" first and "group_id" second.
- When we need the entire group object, just pass second argument as (boolian) True. In the case group object will be returned.
Example 1: res_group_name, res_group_id = get_group_name_id(group_name_or_id)
- "res_group_name" will contain name of the group.
- "res_group_id" will contain _id/ObjectId of the group.
Example 2: res_group_obj = get_group_name_id(group_name_or_id, True)
- "res_group_obj" will contain entire object.
Optimization Tip: before calling this method, try to cast group_id to ObjectId as follows (or copy paste following snippet at start of function or wherever there is a need):
try:
group_id = ObjectId(group_id)
except:
group_name, group_id = get_group_name_id(group_id)
'''
# if cached result exists return it
if not get_obj:
slug = slugify(group_name_or_id)
# for unicode strings like hindi-text slugify doesn't works
cache_key = 'get_group_name_id_' + str(slug) if slug else str(abs(hash(group_name_or_id)))
cache_result = cache.get(cache_key)
if cache_result:
return cache_result
# ---------------------------------
# case-1: argument - "group_name_or_id" is ObjectId
if ObjectId.is_valid(group_name_or_id):
group_obj = node_collection.one({"_id": ObjectId(group_name_or_id)})
# checking if group_obj is valid
if group_obj:
# if (group_name_or_id == group_obj._id):
group_id = group_name_or_id
group_name = group_obj.name
if get_obj:
return group_obj
else:
# setting cache with both ObjectId and group_name
cache.set(cache_key, (group_name, group_id), 60*60)
cache_key = u'get_group_name_id_' + slugify(group_name)
cache.set(cache_key, (group_name, group_id), 60*60)
return group_name, group_id
# case-2: argument - "group_name_or_id" is group name
else:
group_obj = node_collection.one({"_type": {"$in": ["Group", "Author"] }, "name": unicode(group_name_or_id)})
# checking if group_obj is valid
if group_obj:
# if (group_name_or_id == group_obj.name):
group_name = group_name_or_id
group_id = group_obj._id
if get_obj:
return group_obj
else:
# setting cache with both ObjectId and group_name
cache.set(cache_key, (group_name, group_id), 60*60)
cache_key = u'get_group_name_id_' + slugify(group_name)
cache.set(cache_key, (group_name, group_id), 60*60)
return group_name, group_id
if get_obj:
return None
else:
return None, None
@get_execution_time
def check_delete(main):
try:
def check(*args, **kwargs):
relns=""
node_id=kwargs['node_id']
ins_objectid = ObjectId()
if ins_objectid.is_valid(node_id) :
node = node_collection.one({'_id': ObjectId(node_id)})
relns=node.get_possible_relations(node.member_of)
attrbts=node.get_possible_attributes(node.member_of)
return main(*args, **kwargs)
else:
print "Not a valid id"
return check
except Exception as e:
print "Error in check_delete "+str(e)
@get_execution_time
def get_all_resources_for_group(group_id):
if ins_objectid.is_valid(group_id):
obj_resources = node_collection.find({'$and': [{'$or': [{'_type': 'GSystem'}, {'_type': 'File'}]}, {'group_set': {'$all': [ObjectId(group_id)]}}, {'member_of': {'$nin': [grp_st._id]}}]})
return obj_resources
@get_execution_time
def get_gapps(default_gapp_listing=False, already_selected_gapps=[]):
"""Returns list of GApps.
Arguments:
default_gapp_listing -- (Optional argument)
- This is to decide which list should be considered for listing GAPPs;
that is, in menu-bar and GAPPs selection menu for a given group
- True: DEFAULT_GAPPS (menu-bar)
- At present used in listing GAPPS whenever a new group is created
- False: GSTUDIO_WORKING_GAPPS (selection-menu)
- At present used in listing GAPPS for setting-up GAPPS for a group
already_selected_gapps -- (Optional argument)
- List of GApps already set for a given group in form of
dictionary variable
- If specified, then these listed GApps are excluded from
the list of GApps returned by this function
Returns:
- List of GApps where each GApp is in form of node/dictionary
"""
gapps_list = []
global GSTUDIO_DEFAULT_GAPPS_LIST
gapps_list = GSTUDIO_DEFAULT_GAPPS_LIST
if not gapps_list or not default_gapp_listing:
# If GSTUDIO_DEFAULT_GAPPS_LIST not set (i.e. empty)
# Or we need to setup list for selection purpose of GAPPS
# for a group
gapps_list = GSTUDIO_WORKING_GAPPS
# If already_selected_gapps is non-empty,
# Then append their names in list of GApps to be excluded
if already_selected_gapps:
gapps_list_remove = gapps_list.remove
#Function used by Processes implemented below
def multi_(lst):
for each_gapp in lst:
gapp_name = each_gapp["name"]
if gapp_name in gapps_list:
gapps_list_remove(gapp_name)
#this empty list will have the Process objects as its elements
processes=[]
n1=len(already_selected_gapps)
lst1=already_selected_gapps
#returns no of cores in the cpu
x=mp.cpu_count()
#divides the list into those many parts
n2=n1/x
#Process object is created.The list after being partioned is also given as an argument.
for i in range(x):
processes.append(mp.Process(target=multi_,args=(lst1[i*n2:(i+1)*n2],)))
for i in range(x):
processes[i].start() #each Process started
for i in range(x):
processes[i].join() #each Process converges
# Find all GAPPs
meta_type = node_collection.one({
"_type": "MetaType", "name": META_TYPE[0]
})
gapps_cur = None
gapps_cur = node_collection.find({
"_type": "GSystemType", "member_of": meta_type._id,
"name": {"$in": gapps_list}
}).sort("created_at")
return list(gapps_cur)
@get_execution_time
def forum_notification_status(group_id,user_id):
"""Checks forum notification turn off for an author object
"""
try:
grp_obj = node_collection.one({'_id': ObjectId(group_id)})
auth_obj = node_collection.one({'_id': ObjectId(user_id)})
at_user_pref = node_collection.one({'$and': [{'_type': 'AttributeType'}, {'name': 'user_preference_off'}]})
list_at_pref=[]
if at_user_pref:
poss_attrs=auth_obj.get_possible_attributes(at_user_pref._id)
if poss_attrs:
if 'user_preference_off' in poss_attrs:
list_at_pref=poss_attrs['user_preference_off']['object_value']
if grp_obj in list_at_pref:
return False
else:
return True
return True
except Exception as e:
print "Exception in forum notification status check "+str(e)
@get_execution_time
def get_forum_repl_type(forrep_id):
forum_st = node_collection.one({'$and': [{'_type': 'GSystemType'}, {'name': GAPPS[5]}]})
obj = node_collection.one({'_id': ObjectId(forrep_id)})
if obj:
if forum_st._id in obj.member_of:
return "Forum"
else:
return "Reply"
else:
return "None"
@get_execution_time
def check_existing_group(group_name):
if type(group_name) == 'unicode':
colg = node_collection.find({'_type': u'Group', "name": group_name})
if colg.count()>0:
return True
if ins_objectid.is_valid(group_name): #if group_name holds group_id
colg = node_collection.find({'_type': u'Group', "_id": ObjectId(group_name)})
if colg.count()>0:
return True
else:
colg = node_collection.find({'_type': {'$in':['Group', 'Author']}, "_id": ObjectId(group_name)})
if colg.count()>0:
return True
else:
if ins_objectid.is_valid(group_name): #if group_name holds group_id
colg = node_collection.find({'_type': u'Group', "_id": ObjectId(group_name)})
if colg.count()>0:
return True
colg = node_collection.find({'_type': {'$in':['Group', 'Author']}, "_id": ObjectId(group_name)})
if colg.count()>0:
return True
else:
colg = node_collection.find({'_type': {'$in':['Group', 'Author']}, "_id": group_name._id})
if colg.count() >= 1:
return True
else:
return False
@get_execution_time
def filter_drawer_nodes(nid, group_id=None):
page_gst = node_collection.one({'_type': 'GSystemType', 'name': 'Page'})
file_gst = node_collection.one({'_type': 'GSystemType', 'name': 'File'})
Pandora_video_gst = node_collection.one({'_type': 'GSystemType', 'name': 'Pandora_video'})
quiz_gst = node_collection.one({'_type': 'GSystemType', 'name': 'Quiz'})
quizItem_gst = node_collection.one({'_type': "GSystemType", 'name': "QuizItem"})
query = None
if group_id:
query = {'_type': {'$in': ['GSystem', 'File']}, 'group_set': ObjectId(group_id),
'collection_set': {'$exists': True, '$not': {'$size': 0}, '$in':[ObjectId(nid)]},
'member_of': {'$in': [page_gst._id,file_gst._id,Pandora_video_gst._id,quiz_gst._id,quizItem_gst._id] }
}
else:
query = {'_type': {'$in': ['GSystem', 'File']},'collection_set': {'$exists': True, '$not': {'$size': 0}, '$in':[ObjectId(nid)]},
'member_of': {'$in': [page_gst._id,file_gst._id,Pandora_video_gst._id,quiz_gst._id,quizItem_gst._id] }
}
nodes = node_collection.find(query)
# Remove parent nodes in which current node exists
def filter_nodes(parents, group_id=None):
length = []
if parents:
length.extend(parents)
inner_parents = []
for each in parents:
if group_id:
query = {'_type': {'$in': ['GSystem', 'File']}, 'group_set': ObjectId(group_id),
'collection_set': {'$exists': True, '$not': {'$size': 0}, '$in':[ObjectId(each)]},
'member_of': {'$in': [page_gst._id,file_gst._id,Pandora_video_gst._id,quiz_gst._id,quizItem_gst._id] }
}
else:
query = {'_type': {'$in': ['GSystem', 'File']},
'collection_set': {'$exists': True, '$not': {'$size': 0}, '$in':[ObjectId(each)]},
'member_of': {'$in': [page_gst._id,file_gst._id,Pandora_video_gst._id,quiz_gst._id,quizItem_gst._id] }
}
nodes = node_collection.find(query)
if nodes.count() > 0:
for k in nodes:
inner_parents.append(k._id)
for each in inner_parents:
if each not in parents:
parents.append(each)
if set(length) != set(parents):
parents = filter_nodes(parents, group_id)
return parents
else:
return parents
parents_list = []
if nodes.count() > 0:
for each in nodes:
parents_list.append(each._id)
parents = filter_nodes(parents_list, group_id)
return parents
else:
return parents_list
@get_execution_time
def get_drawers(group_id, nid=None, nlist=[], page_no=1, checked=None, **kwargs):
"""Get both drawers-list.
"""
dict_drawer = {}
dict1 = {}
dict2 = [] # Changed from dictionary to list so that it's content are reflected in a sequential-order
filtering = []
drawer = None
if checked:
if nid:
filtering = filter_drawer_nodes(nid, group_id)
if checked == "Page":
gst_page_id = node_collection.one({'_type': "GSystemType", 'name': "Page"})._id
drawer = node_collection.find({'_type': u"GSystem", '_id': {'$nin': filtering},'member_of': {'$all':[gst_page_id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "File":
drawer = node_collection.find({'_type': u"File", '_id': {'$nin': filtering},'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "Image":
gst_image_id = node_collection.one({'_type': "GSystemType", 'name': "Image"})._id
drawer = node_collection.find({'_type': u"File", '_id': {'$nin': filtering},'member_of': {'$in':[gst_image_id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "Video":
gst_video_id = node_collection.one({'_type': "GSystemType", 'name': "Video"})._id
drawer = node_collection.find({'_type': u"File", '_id': {'$nin': filtering},'member_of': {'$in':[gst_video_id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "Quiz":
# For prior-node-list
drawer = node_collection.find({'_type': {'$in' : [u"GSystem", u"File"]}, '_id': {'$nin': filtering},'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "QuizObj" or checked == "assesses":
# For collection-list
gst_quiz_id = node_collection.one({'_type': "GSystemType", 'name': "Quiz"})._id
gst_quiz_item_id = node_collection.one({'_type': "GSystemType", 'name': "QuizItem"})._id
drawer = node_collection.find({'_type': u"GSystem", '_id': {'$nin': filtering},'member_of': {'$in':[gst_quiz_id, gst_quiz_item_id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "OnlyQuiz":
gst_quiz_id = node_collection.one({'_type': "GSystemType", 'name': "Quiz"})._id
drawer = node_collection.find({'_type': u"GSystem", '_id': {'$nin': filtering},'member_of': {'$all':[gst_quiz_id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "QuizItem":
gst_quiz_item_id = node_collection.one({'_type': "GSystemType", 'name': "QuizItem"})._id
drawer = node_collection.find({'_type': u"GSystem", '_id': {'$nin': filtering},'member_of': {'$all':[gst_quiz_item_id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "Group":
drawer = node_collection.find({'_type': u"Group", '_id': {'$nin': filtering} })
elif checked == "Users":
drawer = node_collection.find({'_type': u"Author", '_id': {'$nin': filtering} })
elif checked == "Forum":
gst_forum_id = node_collection.one({'_type': "GSystemType", 'name': "Forum"})._id
drawer = node_collection.find({'_type': u"GSystem", '_id': {'$nin': filtering},'member_of': {'$all':[gst_forum_id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "Module":
gst_module_id = node_collection.one({'_type': "GSystemType", 'name': "Module"})._id
drawer = node_collection.find({'_type': u"GSystem", '_id': {'$nin': filtering},'member_of': {'$all':[gst_module_id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "Pandora Video":
gst_pandora_video_id = node_collection.one({'_type': "GSystemType", 'name': "Pandora_video"})._id
drawer = node_collection.find({'_type': u"File", '_id': {'$nin': filtering},'member_of': {'$all':[gst_pandora_video_id]}, 'group_set': {'$all': [ObjectId(group_id)]}}).limit(50)
elif checked == "Theme":
drawer = node_collection.find({'_type': u"GSystem", '_id': {'$nin': filtering},'member_of': {'$in':[theme_GST._id, topic_GST._id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "theme_item":
drawer = node_collection.find({'_type': u"GSystem", '_id': {'$nin': filtering},'member_of': {'$in':[theme_item_GST._id, topic_GST._id]}, 'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "Topic":
drawer = node_collection.find({'_type': {'$in' : [u"GSystem", u"File"]}, '_id': {'$nin': filtering},'member_of':{'$nin':[theme_GST._id, theme_item_GST._id, topic_GST._id]},'group_set': {'$all': [ObjectId(group_id)]}})
elif checked == "RelationType" or checked == "CourseUnits":
# Special case used while dealing with RelationType widget
if kwargs.has_key("left_drawer_content"):
drawer = kwargs["left_drawer_content"]
else:
# For heterogeneous collection
if checked == "RelationType" or checked == "CourseUnits":
# Special case used while dealing with RelationType widget
drawer = checked
else:
filtering = filter_drawer_nodes(nid, group_id)
Page = node_collection.one({'_type': 'GSystemType', 'name': 'Page'})
File = node_collection.one({'_type': 'GSystemType', 'name': 'File'})
Quiz = node_collection.one({'_type': "GSystemType", 'name': "Quiz"})
drawer = node_collection.find({'_type': {'$in' : [u"GSystem", u"File"]},
'_id': {'$nin': filtering},'group_set': {'$all': [ObjectId(group_id)]},
'member_of':{'$in':[Page._id,File._id,Quiz._id]}
})
if checked != "RelationType" and checked != "CourseUnits":
paged_resources = paginator.Paginator(drawer, page_no, 10)
drawer.rewind()
if (nid is None) and (not nlist):
for each in drawer:
dict_drawer[each._id] = each
elif (nid is None) and (nlist):
for each in drawer:
if each._id not in nlist:
dict1[each._id] = each
#loop replaced by a list comprehension
dict2=[node_collection.one({'_id': oid}) for oid in nlist]
for oid in nlist:
obj = node_collection.one({'_id': oid})
dict2.append(obj)
dict_drawer['1'] = dict1
dict_drawer['2'] = dict2
else:
for each in drawer:
if each._id != nid:
if each._id not in nlist:
dict1[each._id] = each
#loop replaced by a list comprehension
dict2=[node_collection.one({'_id': oid}) for oid in nlist]
for oid in nlist:
obj = node_collection.one({'_id': oid})
dict2.append(obj)
dict_drawer['1'] = dict1
dict_drawer['2'] = dict2
if checked == "RelationType" or checked == "CourseUnits":
return dict_drawer
else:
return dict_drawer, paged_resources
# get type of resourc
@get_execution_time
def get_resource_type(request,node_id):
get_resource_type=node_collection.one({'_id':ObjectId(node_id)})
get_type=get_resource_type._type
return get_type
@get_execution_time
def get_translate_common_fields(request, get_type, node, group_id, node_type, node_id):
""" retrive & update the common fields required for translation of the node """
usrid = int(request.user.id)
content_org = request.POST.get('content_org')
tags = request.POST.get('tags')
name = request.POST.get('name')
tags = request.POST.get('tags')
access_policy = request.POST.get('access_policy')
usrid = int(request.user.id)
language= request.POST.get('lan')
if get_type == "File":
get_parent_node = node_collection.one({'_id': ObjectId(node_id)})
get_mime_type=get_parent_node.mime_type
get_fs_file_ids=get_parent_node.fs_file_ids
node.mime_type=get_mime_type
node.fs_file_ids=get_fs_file_ids
if not ('_id' in node):
node.created_by = usrid
if get_type == "File":
get_node_type = node_collection.one({"_type": "GSystemType", 'name': get_type})
node.member_of.append(get_node_type._id)
if 'image' in get_mime_type:
get_image_type = node_collection.one({"_type": "GSystemType", 'name': 'Image'})
node.member_of.append(get_image_type._id)
if 'video' in get_mime_type:
get_video_type = node_collection.one({"_type": "GSystemType", 'name': 'Video'})
node.member_of.append(get_video_type._id)
else:
node.member_of.append(node_type._id)
node.name = unicode(name)
node.language=unicode(language)
node.modified_by = usrid
if access_policy:
node.access_policy = access_policy
if usrid not in node.contributors:
node.contributors.append(usrid)
group_obj = node_collection.one({'_id': ObjectId(group_id)})
if group_obj._id not in node.group_set:
node.group_set.append(group_obj._id)
if tags:
node.tags = [unicode(t.strip()) for t in tags.split(",") if t != ""]
if tags:
node.tags = [unicode(t.strip()) for t in tags.split(",") if t != ""]
if content_org:
node.content_org = unicode(content_org)
node.name=unicode(name)
# Required to link temporary files with the current user who is modifying this document
usrname = request.user.username
filename = slugify(name) + "-" + usrname + "-" + ObjectId().__str__()
node.content = org2html(content_org, file_prefix=filename)
@get_execution_time
def get_node_common_fields(request, node, group_id, node_type, coll_set=None):
"""Updates the retrieved values of common fields from request
into the given node.
"""
group_obj = node_collection.one({'_id': ObjectId(group_id)})
# theme_item_GST = node_collection.one({'_type': 'GSystemType', 'name': 'theme_item'})
# topic_GST = node_collection.one({'_type': 'GSystemType', 'name': 'Topic'})
# collection = None
if coll_set:
if "Theme" in coll_set.member_of_names_list:
node_type = theme_GST
if "theme_item" in coll_set.member_of_names_list:
node_type = theme_item_GST
if "Topic" in coll_set.member_of_names_list:
node_type = topic_GST
name = request.POST.get('name_' + str(coll_set._id), "")
content_org = request.POST.get(str(coll_set._id), "")
tags = request.POST.get('tags' + str(coll_set._id), "")
else:
name = request.POST.get('name', '').strip()
content_org = request.POST.get('content_org')
tags = request.POST.get('tags','')
language = request.POST.get('lan')
sub_theme_name = request.POST.get("sub_theme_name", '')
add_topic_name = request.POST.get("add_topic_name", '')
is_changed = False
sub_theme_name = unicode(request.POST.get("sub_theme_name", ''))
add_topic_name = unicode(request.POST.get("add_topic_name", ''))
usrid = int(request.user.id)
usrname = unicode(request.user.username)
access_policy = request.POST.get("login-mode", '')
right_drawer_list = []
checked = request.POST.get("checked", '')
check_collection = request.POST.get("check_collection", '')
if check_collection:
if check_collection == "collection":
right_drawer_list = request.POST.get('collection_list', '')
elif check_collection == "prior_node":
right_drawer_list = request.POST.get('prior_node_list', '')
elif check_collection == "teaches":
right_drawer_list = request.POST.get('teaches_list', '')
elif check_collection == "assesses":
right_drawer_list = request.POST.get('assesses_list', '')
elif check_collection == "module":
right_drawer_list = request.POST.get('module_list', '')
map_geojson_data = request.POST.get('map-geojson-data')
user_last_visited_location = request.POST.get('last_visited_location')
altnames = request.POST.get('altnames', '')
# featured = request.POST.get('featured', '')
if map_geojson_data:
map_geojson_data = map_geojson_data + ","
map_geojson_data = list(ast.literal_eval(map_geojson_data))
else:
map_geojson_data = []
# For create only ---------------------------------------------------------
if not ("_id" in node):
node.created_by = usrid
if node_type._id not in node.member_of:
node.member_of.append(node_type._id)
if node_type.name == "Term":
node.member_of.append(topic_GST._id)
if group_obj._id not in node.group_set:
node.group_set.append(group_obj._id)
if access_policy == "PUBLIC":
node.access_policy = unicode(access_policy)
else:
node.access_policy = unicode(access_policy)
node.status = "PUBLISHED"
is_changed = True
# End of if
specific_url = set_all_urls(node.member_of)
node.url = specific_url
if name:
if node.name != name:
node.name = name
is_changed = True
if altnames or "altnames" in request.POST:
if node.altnames != altnames:
node.altnames = altnames
is_changed = True
# if featured or (not featured):
# if node.featured != featured:
# node.featured = featured
# is_changed = True
if sub_theme_name:
if node.name != sub_theme_name:
node.name = sub_theme_name
is_changed = True
if add_topic_name:
if node.name != add_topic_name:
node.name = add_topic_name
is_changed = True
# language
if language:
node.language = unicode(language)
else:
node.language = u"en"
# access_policy
if access_policy:
# Policy will be changed only by the creator of the resource
# via access_policy(public/private) option on the template which
# is visible only to the creator
if access_policy == "PUBLIC" and node.access_policy != access_policy:
node.access_policy = u"PUBLIC"
# print "\n Changed: access_policy (pu 2 pr)"
is_changed = True
elif access_policy == "PRIVATE" and node.access_policy != access_policy:
node.access_policy = u"PRIVATE"
# print "\n Changed: access_policy (pr 2 pu)"
is_changed = True
else:
node.access_policy = u"PUBLIC"
# For displaying nodes in home group as well as in creator group.
user_group_obj = node_collection.one({
'_type': ObjectId(group_id), 'name': usrname
})
if group_obj._id not in node.group_set:
node.group_set.append(group_obj._id)
else:
if user_group_obj:
if user_group_obj._id not in node.group_set:
node.group_set.append(user_group_obj._id)
# tags
# if tags:
tags_list = []
for tag in tags.split(","):
tag = unicode(tag.strip())
if tag:
tags_list.append(tag)
if set(node.tags) != set(tags_list):
node.tags = tags_list
is_changed = True
# Build collection, prior node, teaches and assesses lists
if check_collection:
changed = build_collection(node, check_collection, right_drawer_list, checked)
if changed:
is_changed = True
# org-content
if node.content_org != content_org:
node.content_org = content_org
# Required to link temporary files with the current user who is
# modifying this document
usrname = request.user.username
filename = slugify(name) + "-" + slugify(usrname) + "-" + ObjectId().__str__()
node.content = org2html(content_org, file_prefix=filename)
is_changed = True
# visited_location in author class
if node.location != map_geojson_data:
node.location = map_geojson_data # Storing location data
is_changed = True
if user_last_visited_location:
user_last_visited_location = list(ast.literal_eval(user_last_visited_location))
author = node_collection.one({'_type': "GSystemType", 'name': "Author"})
user_group_location = node_collection.one({
'_type': "Author", 'member_of': author._id, 'created_by': usrid,
'name': usrname
})
if user_group_location:
if node._type == "Author" and user_group_location._id == node._id:
if node['visited_location'] != user_last_visited_location:
node['visited_location'] = user_last_visited_location
is_changed = True
else:
user_group_location['visited_location'] = user_last_visited_location
user_group_location.save()
if is_changed:
node.status = unicode("DRAFT")
node.modified_by = usrid
if usrid not in node.contributors:
node.contributors.append(usrid)
return is_changed
# ============= END of def get_node_common_fields() ==============
@get_execution_time
def build_collection(node, check_collection, right_drawer_list, checked):
is_changed = False
if check_collection == "prior_node":
if right_drawer_list != '':
# prior_node_list = [ObjectId(each.strip()) for each in prior_node_list.split(",")]
right_drawer_list = [ObjectId(each.strip()) for each in right_drawer_list.split(",")]
if node.prior_node != right_drawer_list:
i = 0
node.prior_node=[]
node_prior_node_append_temp=node.prior_node.append #a temp. variable which stores the lookup for append method
while (i < len(right_drawer_list)):
node_id = ObjectId(right_drawer_list[i])
node_obj = node_collection.one({"_id": node_id})
if node_obj:
node_prior_node_append_temp(node_id)
i = i+1
# print "\n Changed: prior_node"
is_changed = True
else:
node.prior_node = []
is_changed = True
elif check_collection == "collection":
# collection
if right_drawer_list != '':
right_drawer_list = [ObjectId(each.strip()) for each in right_drawer_list.split(",")]
nlist = node.collection_set
# if set(node.collection_set) != set(right_drawer_list):
if node.collection_set != right_drawer_list:
i = 0
node.collection_set = []
# checking if each _id in collection_list is valid or not
nlist_append_temp=nlist.append #a temp. variable which stores the lookup for append method
node_collection_set_append_temp=node.collection_set.append #a temp. variable which stores the lookup for append method
while (i < len(right_drawer_list)):
node_id = ObjectId(right_drawer_list[i])
node_obj = node_collection.one({"_id": node_id})
if node_obj:
if node_id not in nlist:
nlist_append_temp(node_id)
else:
node_collection_set_append_temp(node_id)
# After adding it to collection_set also make the 'node' as prior node for added collection element
node_collection.collection.update({'_id': ObjectId(node_id), 'prior_node': {'$nin':[node._id]} },{'$push': {'prior_node': ObjectId(node._id)}})
i = i+1
for each in nlist:
if each not in node.collection_set:
node_collection_set_append_temp(each)
node.status = u"PUBLISHED"
node.save()
# After adding it to collection_set also make the 'node' as prior node for added collection element
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$nin':[node._id]} },{'$push': {'prior_node': ObjectId(node._id)}})
# For removing collection elements from heterogeneous collection drawer only
if not checked:
if nlist:
for each in nlist:
if each not in right_drawer_list:
node.collection_set.remove(each)
# Also for removing prior node element after removing collection element
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
else:
if nlist and checked:
if checked == "QuizObj":
quiz = node_collection.one({'_type': 'GSystemType', 'name': "Quiz" })
quizitem = node_collection.one({'_type': 'GSystemType', 'name': "QuizItem" })
for each in nlist:
obj = node_collection.one({'_id': ObjectId(each) })
if quiz._id in obj.member_of or quizitem._id in obj.member_of:
if obj._id not in right_drawer_list:
node.collection_set.remove(obj._id)
# Also for removing prior node element after removing collection element
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
elif checked == "Pandora Video":
check = node_collection.one({'_type': 'GSystemType', 'name': 'Pandora_video' })
for each in nlist:
obj = node_collection.one({'_id': ObjectId(each) })
if check._id == obj.member_of[0]:
if obj._id not in right_drawer_list:
node.collection_set.remove(obj._id)
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
else:
check = node_collection.one({'_type': 'GSystemType', 'name': unicode(checked) })
for each in nlist:
obj = node_collection.one({'_id': ObjectId(each) })
if len(obj.member_of) < 2:
if check._id == obj.member_of[0]:
if obj._id not in right_drawer_list:
node.collection_set.remove(obj._id)
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
else:
if check._id == obj.member_of[1]:
if obj._id not in right_drawer_list:
node.collection_set.remove(obj._id)
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
is_changed = True
else:
if node.collection_set and checked:
if checked == "QuizObj":
quiz = node_collection.one({'_type': 'GSystemType', 'name': "Quiz" })
quizitem = node_collection.one({'_type': 'GSystemType', 'name': "QuizItem" })
for each in node.collection_set:
obj = node_collection.one({'_id': ObjectId(each) })
if quiz._id in obj.member_of or quizitem._id in obj.member_of:
node.collection_set.remove(obj._id)
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
elif checked == "Pandora Video":
check = node_collection.one({'_type': 'GSystemType', 'name': 'Pandora_video' })
for each in node.collection_set:
obj = node_collection.one({'_id': ObjectId(each) })
if check._id == obj.member_of[0]:
node.collection_set.remove(obj._id)
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
else:
check = node_collection.one({'_type': 'GSystemType', 'name': unicode(checked) })
for each in node.collection_set:
obj = node_collection.one({'_id': ObjectId(each) })
if len(obj.member_of) < 2:
if check._id == obj.member_of[0]:
node.collection_set.remove(obj._id)
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
else:
if check._id == obj.member_of[1]:
node.collection_set.remove(obj._id)
node_collection.collection.update({'_id': ObjectId(each), 'prior_node': {'$in':[node._id]} },{'$pull': {'prior_node': ObjectId(node._id)}})
else:
node.collection_set = []
is_changed = True
elif check_collection == "teaches":
# Teaches
if right_drawer_list != '':
right_drawer_list = [ObjectId(each.strip()) for each in right_drawer_list.split(",")]
relationtype = node_collection.one({"_type": "RelationType", "name": "teaches"})
list_grelations = triple_collection.find({"_type": "GRelation", "subject": node._id, "relation_type.$id": relationtype._id})
for relation in list_grelations:
# nlist.append(ObjectId(relation.right_subject))
relation.delete()
if right_drawer_list:
list_grelations.rewind()
i = 0
while (i < len(right_drawer_list)):
node_id = ObjectId(right_drawer_list[i])
node_obj = node_collection.one({"_id": node_id})
if node_obj:
create_grelation(node._id,relationtype,node_id)
i = i+1
# print "\n Changed: teaches_list"
is_changed = True
else:
relationtype = node_collection.one({"_type": "RelationType", "name": "teaches"})
list_grelations = triple_collection.find({"_type": "GRelation", "subject": node._id, "relation_type.$id": relationtype._id})
for relation in list_grelations:
relation.delete()
is_changed = True
elif check_collection == "assesses":
# Assesses
if right_drawer_list != '':
right_drawer_list = [ObjectId(each.strip()) for each in right_drawer_list.split(",")]
relationtype = node_collection.one({"_type": "RelationType", "name": "assesses"})
list_grelations = triple_collection.find({"_type": "GRelation", "subject": node._id, "relation_type.$id": relationtype._id})
for relation in list_grelations:
relation.delete()
if right_drawer_list:
list_grelations.rewind()
i = 0
while (i < len(right_drawer_list)):
node_id = ObjectId(right_drawer_list[i])
node_obj = node_collection.one({"_id": node_id})
if node_obj:
create_grelation(node._id,relationtype,node_id)
i = i + 1
# print "\n Changed: teaches_list"
is_changed = True
else:
relationtype = node_collection.one({"_type": "RelationType", "name": "assesses"})
list_grelations = triple_collection.find({"_type": "GRelation", "subject": node._id, "relation_type.$id": relationtype._id})
for relation in list_grelations:
relation.delete()
is_changed = True
# elif check_collection == "module":
# Module
# if right_drawer_list != '':
# right_drawer_list = [ObjectId(each.strip()) for each in right_drawer_list.split(",")]
# if set(node.collection_set) != set(right_drawer_list):
# i = 0
# while (i < len(right_drawer_list)):
# node_id = ObjectId(right_drawer_list[i])
# node_obj = node_collection.one({"_id": node_id})
# if node_obj:
# if node_id not in node.collection_set:
# node.collection_set.append(node_id)
# i = i+1
# print "\n Changed: module_list"
# is_changed = True
# else:
# node.module_set = []
# is_changed = True
if is_changed == True:
return True
else:
return False
"""
@get_execution_time
def get_versioned_page(node):
rcs = RCS()
fp = history_manager.get_file_path(node)
cmd= 'rlog %s' % \
(fp)
rev_no =""
proc1=subprocess.Popen(cmd,shell=True,
stdout=subprocess.PIPE)
for line in iter(proc1.stdout.readline,b''):
if line.find('revision')!=-1 and line.find('selected') == -1:
rev_no=string.split(line,'revision')
rev_no=rev_no[1].strip( '\t\n\r')
rev_no=rev_no.split()[0]
if line.find('status')!=-1:
up_ind=line.find('status')
if line.find(('PUBLISHED'),up_ind) !=-1:
rev_no=rev_no.split()[0]
node=history_manager.get_version_document(node,rev_no)
proc1.kill()
return (node,rev_no)
if rev_no == '1.1':
node=history_manager.get_version_document(node,'1.1')
proc1.kill()
return(node,'1.1')
"""
@get_execution_time
def get_versioned_page(node):
rcs = RCS()
fp = history_manager.get_file_path(node)
cmd= 'rlog %s' % \
(fp)
rev_no =""
proc1=subprocess.Popen(cmd,shell=True,
stdout=subprocess.PIPE)
for line in iter(proc1.stdout.readline,b''):
if line.find('revision')!=-1 and line.find('selected') == -1:
rev_no=string.split(line,'revision')
rev_no=rev_no[1].strip( '\t\n\r')
rev_no=rev_no.split()[0]
if line.find('status')!=-1:
up_ind=line.find('status')
if line.find(('PUBLISHED'),up_ind) !=-1:
rev_no=rev_no.split()[0]
node=history_manager.get_version_document(node,rev_no)
proc1.kill()
return (node,rev_no)
if rev_no == '1.1':
node=history_manager.get_version_document(node,'1.1')
proc1.kill()
return(node,'1.1')
@get_execution_time
def get_user_page(request,node):
''' function gives the last docment submited by the currently logged in user either it
can be drafted or published
'''
rcs = RCS()
fp = history_manager.get_file_path(node)
cmd= 'rlog %s' % \
(fp)
rev_no =""
proc1=subprocess.Popen(cmd,shell=True,
stdout=subprocess.PIPE)
for line in iter(proc1.stdout.readline,b''):
if line.find('revision')!=-1 and line.find('selected') == -1:
rev_no=string.split(line,'revision')
rev_no=rev_no[1].strip( '\t\n\r')
rev_no=rev_no.strip(' ')
if line.find('updated')!=-1:
up_ind=line.find('updated')
if line.find(str(request.user),up_ind) !=-1:
rev_no=rev_no.strip(' ')
node=history_manager.get_version_document(node,rev_no)
proc1.kill()
return (node,rev_no)
if rev_no == '1.1':
node=history_manager.get_version_document(node,'1.1')
proc1.kill()
return(node,'1.1')
@get_execution_time
def get_page(request,node):
'''
function to filter between the page to be displyed to user
i.e which page to be shown to the user drafted or the published page
if a user have some drafted content then he would be shown his own drafted contents
and if he has published his contents then he would be shown the current published contents
'''
username =request.user
node1,ver1=get_versioned_page(node)
node2,ver2=get_user_page(request,node)
if ver2 != '1.1':
if node2 is not None:
if node2.status == 'PUBLISHED':
if float(ver2) > float(ver1):
return (node2,ver2)
elif float(ver2) < float(ver1):
return (node1,ver1)
elif float(ver2) == float(ver1):
return(node1,ver1)
elif node2.status == 'DRAFT':
#========== conditions for Group===============#
if node._type == "Group":
count=check_page_first_creation(request,node2)
if count == 1:
return (node1,ver1)
elif count == 2:
return (node2,ver2)
return (node2,ver2)
else:
return(node1,ver1)
else:
# if node._type == "GSystem" and node1.status == "DRAFT":
# if node1.created_by ==request.user.id:
# return (node2,ver2)
# else:
# return (node2,ver2)
return (node1,ver1)
@get_execution_time
def check_page_first_creation(request,node):
''' function to check wheather the editing is performed by the user very first time '''
rcs = RCS()
fp = history_manager.get_file_path(node)
cmd= 'rlog %s' % \
(fp)
rev_no =""
count=0
proc1=subprocess.Popen(cmd,shell=True,
stdout=subprocess.PIPE)
for line in iter(proc1.stdout.readline,b''):
if line.find('updated')!=-1 or line.find('created')!=-1:
if line.find(str(request.user))!=-1:
count =count+1
if count ==2:
proc1.kill()
return (count)
proc1.kill()
if count == 1:
return(count)
@get_execution_time
def tag_info(request, group_id, tagname=None):
'''
Function to get all the resources related to tag
'''
group_name, group_id = get_group_name_id(group_id)
group_id = ObjectId(group_id)
cur = None
total = None
total_length = None
yesterdays_result = []
week_ago_result = []
search_result = []
group_cur_list = [] # for AutheticatedUser
today = date.today()
yesterdays_search = {date.today() - timedelta(days=1)}
week_ago_search = {date.today() - timedelta(days=7)}
locale.setlocale(locale.LC_ALL, '')
userid = request.user.id
# collection = get_database()[Node.collection_name]
tag_str = request.GET.get("search", "")
tag_search = tag_str.lower()
# if coming from search, set tagname value to search value
if tag_search:
tagname = tag_search
if request.user.is_superuser: # Superuser can see private an public files
if tagname:
cur = node_collection.find({'tags': {'$regex': tagname, '$options': "i"},
'group_set':ObjectId(group_id)
})
#loop replaced by a list comprehension
search_result=[every for every in cur]
# Autheticate user can see all public files
elif request.user.is_authenticated():
# group_cur = node_collection.find({'_type': 'Group',
# '$or': [
# {'created_by': userid},
# {'group_admin': userid},
# {'author_set': userid},
# {'group_type': u'PUBLIC'},
# ]
# })
# for each in group_cur:
# group_cur_list.append(each._id)
if tagname: # and (group_id in group_cur_list):
cur = node_collection.find({'tags': {'$regex': tagname, '$options': "i"},
'group_set': ObjectId(group_id),
'$or': [
{'status': u'PUBLISHED'},
{'created_by': userid},
]
})
#loop replaced by a list comprehension
search_result=[every for every in cur]
else: # Unauthenticated user can see all public files.
group_node = node_collection.one({'_id': ObjectId(group_id)})
if group_node.group_type == u"PUBLIC":
if tagname:
cur = node_collection.find({'tags': {'$regex': tagname, '$options': "i"},
'group_set': group_id,
'status': u'PUBLISHED'
}
)
#loop replaced by a list comprehension
search_result=[every for every in cur]
if search_result:
total = len(search_result)
total = locale.format("%d", total, grouping=True)
if len(search_result) == 0:
total_length = len(search_result)
context_variable = {'group_id': group_id, 'groupid': group_id,
'search_result': search_result,
'tagname': tagname, 'total': total,
'total_length': total_length}
return render_to_response(
"ndf/tag_browser.html",
context_variable,
context_instance=RequestContext(request)
)
# code for merging two text Documents
import difflib
@get_execution_time
def diff_string(original, revised):
# build a list of sentences for each input string
original_text = _split_with_maintain(original)
new_text = _split_with_maintain(revised)
a=original_text + new_text
strings='\n'.join(a)
#f=(strings.replace("*", ">").replace("-","="))
#f=(f.replace("> 1 >",">").replace("= 1 =","="))
return strings
STANDARD_REGEX = '[.!?]'
def _split_with_maintain(value, treat_trailing_spaces_as_sentence = True, split_char_regex = STANDARD_REGEX):
result = []
check = value
# compile regex
rx = re.compile(split_char_regex)
# traverse the string
while len(check) > 0:
found = rx.search(str(check))
if found == None:
result.append(check)
break
idx = found.start()
result.append(str(check[:idx])) # append the string
result.append(str(check[idx:idx+1])) # append the puncutation so changing ? to . doesn't invalidate the whole sentence
check = check[idx + 1:]
# group the trailing spaces if requested
if treat_trailing_spaces_as_sentence:
space_idx = 0
while True:
if space_idx >= len(check):
break
if check[space_idx] != " ":
break
space_idx += 1
if space_idx != 0:
result.append(check[0:space_idx])
check = check[space_idx:]
return result
@get_execution_time
def update_mobwrite_content_org(node_system):
'''
on revert or merge of nodes,a content_org is synced to mobwrite object
input :
node
'''
system = node_system
filename = TextObj.safe_name(str(system._id))
textobj = TextObj.objects.filter(filename=filename)
content_org = system.content_org
if textobj:
textobj = TextObj.objects.get(filename=filename)
textobj.text = content_org
textobj.save()
else:
textobj = TextObj(filename=filename,text=content_org)
textobj.save()
return textobj
@get_execution_time
def cast_to_data_type(value, data_type):
'''
This method will cast first argument: "value" to second argument: "data_type" and returns catsed value.
'''
# print "\n\t\tin method: ", value, " == ", data_type
value = value.strip()
casted_value = value
if data_type == "unicode":
casted_value = unicode(value)
elif data_type == "basestring":
casted_value = str(value)
elif (data_type == "int") and str(value):
casted_value = int(value) if (str.isdigit(str(value))) else value
elif (data_type == "float") and str(value):
casted_value = float(value) if (str.isdigit(str(value))) else value
elif (data_type == "long") and str(value):
casted_value = long(value) if (str.isdigit(str(value))) else value
elif data_type == "bool" and str(value): # converting unicode to int and then to bool
if (str.isdigit(str(value))):
casted_value = bool(int(value))
elif unicode(value) in [u"True", u"False"]:
if (unicode(value) == u"True"):
casted_value = True
elif (unicode(value) == u"False"):
casted_value = False
elif (data_type == "list") and (not isinstance(value, list)):
value = value.replace("\n", "").split(",")
# check for complex list type like: [int] or [unicode]
if isinstance(data_type, list) and len(data_type) and isinstance(data_type[0], type):
casted_value = [data_type[0](i.strip()) for i in value if i]
else: # otherwise normal list
casted_value = [i.strip() for i in value if i]
elif data_type == "datetime.datetime":
# "value" should be in following example format
# In [10]: datetime.datetime.strptime( "11/12/2014", "%d/%m/%Y")
# Out[10]: datetime.datetime(2014, 12, 11, 0, 0)
casted_value = datetime.datetime.strptime(value, "%d/%m/%Y")
return casted_value
@get_execution_time
def get_node_metadata(request, node, **kwargs):
'''
Getting list of updated GSystems with kwargs arguments.
Pass is_changed=True as last/third argument while calling this/get_node_metadata method.
Example:
updated_ga_nodes = get_node_metadata(request, node_obj, GST_FILE_OBJ, is_changed=True)
'''
attribute_type_list = ["age_range", "audience", "timerequired",
"interactivitytype", "basedonurl", "educationaluse",
"textcomplexity", "readinglevel", "educationalsubject",
"educationallevel", "curricular", "educationalalignment",
"adaptation_of", "other_contributors", "creator", "source"
]
if "is_changed" in kwargs:
updated_ga_nodes = []
if('_id' in node):
for atname in attribute_type_list:
field_value = request.POST.get(atname, "")
at = node_collection.one({"_type": "AttributeType", "name": atname})
if at:
field_value = cast_to_data_type(field_value, at["data_type"])
if "is_changed" in kwargs:
temp_res = create_gattribute(node._id, at, field_value, is_changed=True)
if temp_res["is_changed"]: # if value is true
updated_ga_nodes.append(temp_res)
else:
create_gattribute(node._id, at, field_value)
if "is_changed" in kwargs:
return updated_ga_nodes
@get_execution_time
def create_grelation_list(subject_id, relation_type_name, right_subject_id_list):
# function to create grelations for new ones and delete old ones.
relationtype = node_collection.one({"_type": "RelationType", "name": unicode(relation_type_name)})
# list_current_grelations = triple_collection.find({"_type":"GRelation","subject":subject_id,"relation_type":relationtype})
# removes all existing relations given subject and relation type and then creates again.
triple_collection.collection.remove({"_type": "GRelation", "subject": subject_id, "relation_type.$id": relationtype._id})
for relation_id in right_subject_id_list:
create_grelation(ObjectId(subject_id), relationtype, ObjectId(relation_id))
# gr_node = triple_collection.collection.GRelation()
# gr_node.subject = ObjectId(subject_id)
# gr_node.relation_type = relationtype
# gr_node.right_subject = ObjectId(relation_id)
# gr_node.status = u"PUBLISHED"
# gr_node.save()
for relation_id in right_subject_id_list:
gr_node = collection.GRelation()
gr_node.subject = ObjectId(subject_id)
gr_node.relation_type = relationtype
gr_node.right_subject = ObjectId(relation_id)
gr_node.status = u"PUBLISHED"
gr_node.save()
@get_execution_time
def get_widget_built_up_data(at_rt_objectid_or_attr_name_list, node, type_of_set=[]):
"""
Returns data in list of dictionary format which is required for building html widget.
This data is used by html_widget template-tag.
"""
if not isinstance(at_rt_objectid_or_attr_name_list, list):
at_rt_objectid_or_attr_name_list = [at_rt_objectid_or_attr_name_list]
#a temp. variable which stores the lookup for append method
type_of_set_append_temp=type_of_set.append
if not type_of_set:
node["property_order"] = []
#a temp. variable which stores the lookup for append method
node_property_order_append_temp=node["property_order"].append
gst_nodes = node_collection.find({'_type': "GSystemType", '_id': {'$in': node["member_of"]}}, {'type_of': 1, 'property_order': 1})
for gst in gst_nodes:
for type_of in gst["type_of"]:
if type_of not in type_of_set:
type_of_set_append_temp(type_of)
for po in gst["property_order"]:
if po not in node["property_order"]:
node_property_order_append_temp(po)
BASE_FIELD_METADATA = {
'name': {'name': "name", '_type': "BaseField", 'altnames': "Name", 'required': True},
'content_org': {'name': "content_org", '_type': "BaseField", 'altnames': "Describe", 'required': False},
# 'featured': {'name': "featured", '_type': "BaseField", 'altnames': "Featured"},
'location': {'name': "location", '_type': "BaseField", 'altnames': "Location", 'required': False},
'status': {'name': "status", '_type': "BaseField", 'altnames': "Status", 'required': False},
'tags': {'name': "tags", '_type': "BaseField", 'altnames': "Tags", 'required': False}
}
widget_data_list = []
#a temp. variable which stores the lookup for append method
widget_data_list_append_temp=widget_data_list.append
for at_rt_objectid_or_attr_name in at_rt_objectid_or_attr_name_list:
if type(at_rt_objectid_or_attr_name) == ObjectId: #ObjectId.is_valid(at_rt_objectid_or_attr_name):
# For attribute-field(s) and/or relation-field(s)
field = node_collection.one({'_id': ObjectId(at_rt_objectid_or_attr_name)}, {'_type': 1, 'subject_type': 1, 'object_type': 1, 'name': 1, 'altnames': 1, 'inverse_name': 1})
altnames = u""
value = None
data_type = None
if field._type == RelationType or field._type == "RelationType":
# For RelationTypes
if set(node["member_of"]).issubset(field.subject_type):
# It means we are dealing with normal relation &
data_type = node.structure[field.name]
value = node[field.name]
if field.altnames:
if ";" in field.altnames:
altnames = field.altnames.split(";")[0]
else:
altnames = field.altnames
elif set(node["member_of"]).issubset(field.object_type):
# It means we are dealing with inverse relation
data_type = node.structure[field.inverse_name]
value = node[field.inverse_name]
if field.altnames:
if ";" in field.altnames:
altnames = field.altnames.split(";")[1]
else:
altnames = field.altnames
elif type_of_set:
# If current node's GST is not in subject_type
# Search for that GST's type_of field value in subject_type
for each in type_of_set:
if each in field.subject_type:
data_type = node.structure[field.name]
value = node[field.name]
if field.altnames:
if ";" in field.altnames:
altnames = field.altnames.split(";")[0]
else:
altnames = field.altnames
elif each in field.object_type:
data_type = node.structure[field.inverse_name]
value = node[field.inverse_name]
if field.altnames:
if ";" in field.altnames:
altnames = field.altnames.split(";")[0]
else:
altnames = field.altnames
else:
# For AttributeTypes
altnames = field.altnames
data_type = node.structure[field.name]
value = node[field.name]
widget_data_list_append_temp({ '_type': field._type, # It's only use on details-view template; overridden in ndf_tags html_widget()
'_id': field._id,
'data_type': data_type,
'name': field.name, 'altnames': altnames,
'value': value
})
else:
# For node's base-field(s)
# widget_data_list.append([node['member_of'], BASE_FIELD_METADATA[at_rt_objectid_or_attr_name], node[at_rt_objectid_or_attr_name]])
widget_data_list_append_temp({ '_type': BASE_FIELD_METADATA[at_rt_objectid_or_attr_name]['_type'],
'data_type': node.structure[at_rt_objectid_or_attr_name],
'name': at_rt_objectid_or_attr_name, 'altnames': BASE_FIELD_METADATA[at_rt_objectid_or_attr_name]['altnames'],
'value': node[at_rt_objectid_or_attr_name],
'required': BASE_FIELD_METADATA[at_rt_objectid_or_attr_name]['required']
})
return widget_data_list
@get_execution_time
def get_property_order_with_value(node):
new_property_order = []
demo = None
if '_id' in node:
demo = node_collection.one({'_id': node._id})
else:
demo = eval("node_collection.collection"+"."+node['_type'])()
demo["member_of"] = node["member_of"]
if demo["_type"] not in ["MetaType", "GSystemType", "AttributeType", "RelationType"]:
# If GSystems found, then only perform following statements
demo["property_order"] = []
type_of_set = []
#temp. variables which stores the lookup for append method
type_of_set_append_temp=type_of_set.append
demo_prop_append_temp=demo["property_order"].append
gst_nodes = node_collection.find({'_type': "GSystemType", '_id': {'$in': demo["member_of"]}}, {'type_of': 1, 'property_order': 1})
for gst in gst_nodes:
for type_of in gst["type_of"]:
if type_of not in type_of_set:
type_of_set_append_temp(type_of)
for po in gst["property_order"]:
if po not in demo["property_order"]:
demo_prop_append_temp(po)
demo.get_neighbourhood(node["member_of"])
#a temp. variable which stores the lookup for append method
new_property_order_append_temp=new_property_order.append
for tab_name, list_field_id_or_name in demo['property_order']:
list_field_set = get_widget_built_up_data(list_field_id_or_name, demo, type_of_set)
new_property_order_append_temp([tab_name, list_field_set])
demo["property_order"] = new_property_order
else:
# Otherwise (if GSystemType found) depending upon whether type_of exists or not returns property_order.
if not demo["property_order"] and demo.has_key("_id"):
type_of_nodes = node_collection.find({'_type': "GSystemType", '_id': {'$in': demo["type_of"]}}, {'property_order': 1})
if type_of_nodes.count():
demo["property_order"] = []
#a temp. variable which stores the lookup for append method
demo_prop_append_temp=demo["property_order"].append
for to in type_of_nodes:
for po in to["property_order"]:
demo_prop_append_temp(po)
node_collection.collection.update({'_id': demo._id}, {'$set': {'property_order': demo["property_order"]}}, upsert=False, multi=False)
new_property_order = demo['property_order']
if demo.has_key('_id'):
node = node_collection.one({'_id': demo._id})
else:
node = eval("node_collection.collection"+"."+demo['_type'])()
node["member_of"] = demo["member_of"]
node['property_order'] = new_property_order
return node['property_order']
@get_execution_time
def parse_template_data(field_data_type, field_value, **kwargs):
"""
Parses the value fetched from request (GET/POST) object based on the data-type of the given field.
Arguments:
field_data_type -- data-type of the field
field_value -- value of the field retrieved from GET/POST object
Returns:
Parsed value based on the data-type of the field
"""
'''
kwargs_keys_list = [
"date_format_string", # date-format in string representation
"field_instance" # dict-object reperesenting AT/RT node
]
'''
DATA_TYPE_STR_CHOICES = [
"unicode", "basestring",
"int", "float", "long",
"list", "dict",
"datetime",
"bool",
"ObjectId"
]
try:
if type(field_data_type) == type:
field_data_type = field_data_type.__name__
if not field_value:
if field_data_type == "dict":
return {}
elif field_data_type == "list":
return []
else:
return None
if field_data_type == "unicode":
field_value = unicode(field_value)
elif field_data_type == "basestring":
field_value = field_value
elif field_data_type == "int":
field_value = int(field_value)
elif field_data_type == "float":
field_value = float(field_value)
elif field_data_type == "long":
field_value = long(field_value)
elif field_data_type == "list":
if ("[" in field_value) and ("]" in field_value):
field_value = json.loads(field_value)
else:
lr = field_value.replace(" ,", ",")
rr = lr.replace(", ", ",")
field_value = rr.split(",")
elif field_data_type == "dict":
field_value = "???"
elif field_data_type == "datetime":
field_value = datetime.strptime(field_value, kwargs["date_format_string"])
elif field_data_type == "bool":
if field_value == "Yes" or field_value == "yes" or field_value == "1":
if field_value == "1":
field_value = bool(int(field_value))
else:
field_value = True
elif field_value == "No" or field_value == "no" or field_value == "0":
if field_value == "0":
field_value = bool(int(field_value))
else:
field_value = False
elif field_data_type == "ObjectId":
field_value = ObjectId(field_value)
else:
error_message = "Unknown data-type ("+field_data_type+") found"
raise Exception(error_message)
elif type(field_data_type) == list:
if "field_instance" in kwargs:
if kwargs["field_instance"]["_type"] == RelationType or kwargs["field_instance"]["_type"] == "RelationType":
# Write RT related code
if not field_value:
return None
if field_value:
field_value = ObjectId(field_value)
else:
error_message = "This ObjectId("+field_type+") doesn't exists"
raise Exception(error_message)
else:
if not field_value:
return []
if ("[" in field_value) and ("]" in field_value):
field_value = json.loads(field_value)
else:
lr = field_value.replace(" ,", ",")
rr = lr.replace(", ", ",")
field_value = rr.split(",")
return field_value
elif type(field_data_type) == dict:
# Write code...
if not field_value:
return {}
elif type(field_data_type) == mongokit.operators.IS:
# Write code...
if not field_value:
return None
field_value = unicode(field_value) if type(field_value) != unicode else field_value
elif type(field_data_type) == mongokit.document.R:
# Write code...
if kwargs["field_instance"]["_type"] == AttributeType or kwargs["field_instance"]["_type"] == "AttributeType":
# Write AT related code
if not field_value:
if field_data_type == "dict":
return {}
elif field_data_type == "list":
return []
else:
return None
else:
error_message = "Neither AttributeType nor RelationType found"
raise Exception(error_message)
else:
error_message = "Unknown data-type found"
raise Exception(error_message)
return field_value
except Exception as e:
error_message = "\n TemplateDataParsingError: "+str(e)+" !!!\n"
raise Exception(error_message)
@get_execution_time
def create_gattribute(subject_id, attribute_type_node, object_value=None, **kwargs):
ga_node = None
info_message = ""
old_object_value = None
ga_node = triple_collection.one({'_type': "GAttribute", 'subject': subject_id, 'attribute_type.$id': attribute_type_node._id})
if ga_node is None:
# Code for creation
try:
ga_node = triple_collection.collection.GAttribute()
ga_node.subject = subject_id
ga_node.attribute_type = attribute_type_node
if (not object_value) and type(object_value) != bool:
object_value = u"None"
ga_node.status = u"DELETED"
else:
ga_node.status = u"PUBLISHED"
ga_node.object_value = object_value
ga_node.save()
if object_value == u"None":
info_message = " GAttribute ("+ga_node.name+") created successfully with status as 'DELETED'!\n"
else:
info_message = " GAttribute ("+ga_node.name+") created successfully.\n"
# Fetch corresponding document & append into it's attribute_set
node_collection.collection.update({'_id': subject_id},
{'$addToSet': {'attribute_set': {attribute_type_node.name: object_value}}},
upsert=False, multi=False
)
is_ga_node_changed = True
except Exception as e:
error_message = "\n GAttributeCreateError: " + str(e) + "\n"
raise Exception(error_message)
else:
# Code for updation
is_ga_node_changed = False
try:
if (not object_value) and type(object_value) != bool:
old_object_value = ga_node.object_value
ga_node.status = u"DELETED"
ga_node.save()
info_message = " GAttribute ("+ga_node.name+") status updated from 'PUBLISHED' to 'DELETED' successfully.\n"
# Fetch corresponding document & update it's attribute_set with proper value
node_collection.collection.update({'_id': subject_id, 'attribute_set.'+attribute_type_node.name: old_object_value},
{'$pull': {'attribute_set': {attribute_type_node.name: old_object_value}}},
upsert=False, multi=False)
else:
if type(ga_node.object_value) == list:
if type(ga_node.object_value[0]) == dict:
old_object_value = ga_node.object_value
if len(old_object_value) != len(object_value):
ga_node.object_value = object_value
is_ga_node_changed = True
else:
pairs = zip(old_object_value, object_value)
if any(x != y for x, y in pairs):
ga_node.object_value = object_value
is_ga_node_changed = True
elif set(ga_node.object_value) != set(object_value):
old_object_value = ga_node.object_value
ga_node.object_value = object_value
is_ga_node_changed = True
elif type(ga_node.object_value) == dict:
if cmp(ga_node.object_value, object_value) != 0:
old_object_value = ga_node.object_value
ga_node.object_value = object_value
is_ga_node_changed = True
else:
if ga_node.object_value != object_value:
old_object_value = ga_node.object_value
ga_node.object_value = object_value
is_ga_node_changed = True
if is_ga_node_changed or ga_node.status == u"DELETED":
if ga_node.status == u"DELETED":
ga_node.status = u"PUBLISHED"
ga_node.save()
info_message = " GAttribute ("+ga_node.name+") status updated from 'DELETED' to 'PUBLISHED' successfully.\n"
# Fetch corresponding document & append into it's attribute_set
node_collection.collection.update({'_id': subject_id},
{'$addToSet': {'attribute_set': {attribute_type_node.name: object_value}}},
upsert=False, multi=False)
else:
ga_node.status = u"PUBLISHED"
ga_node.save()
info_message = " GAttribute ("+ga_node.name+") updated successfully.\n"
# Fetch corresponding document & update it's attribute_set with proper value
node_collection.collection.update({'_id': subject_id, 'attribute_set.'+attribute_type_node.name: {"$exists": True}},
{'$set': {'attribute_set.$.'+attribute_type_node.name: ga_node.object_value}},
upsert=False, multi=False)
else:
info_message = " GAttribute ("+ga_node.name+") already exists (Nothing updated) !\n"
except Exception as e:
error_message = "\n GAttributeUpdateError: " + str(e) + "\n"
raise Exception(error_message)
# print "\n\t is_ga_node_changed: ", is_ga_node_changed
if "is_changed" in kwargs:
ga_dict = {}
ga_dict["is_changed"] = is_ga_node_changed
ga_dict["node"] = ga_node
ga_dict["before_obj_value"] = old_object_value
return ga_dict
else:
return ga_node
# @get_execution_time
def create_grelation(subject_id, relation_type_node, right_subject_id_or_list, **kwargs):
"""Creates single or multiple GRelation documents (instances) based on given
RelationType's cardinality (one-to-one / one-to-many).
Arguments:
subject_id -- ObjectId of the subject-node
relation_type_node -- Document of the RelationType node (Embedded document)
right_subject_id_or_list --
- When one to one relationship: Single ObjectId of the right_subject node
- When one to many relationship: List of ObjectId(s) of the right_subject node(s)
Returns:
- When one to one relationship: Created/Updated/Existed document.
- When one to many relationship: Created/Updated/Existed list of documents.
"""
gr_node = None
multi_relations = False
try:
subject_id = ObjectId(subject_id)
def _create_grelation_node(subject_id, relation_type_node, right_subject_id_or_list, relation_type_text):
# Code for creating GRelation node
gr_node = triple_collection.collection.GRelation()
gr_node.subject = subject_id
gr_node.relation_type = relation_type_node
gr_node.right_subject = right_subject_id_or_list
gr_node.status = u"PUBLISHED"
gr_node.save()
gr_node_name = gr_node.name
info_message = "%(relation_type_text)s: GRelation (%(gr_node_name)s) " % locals() \
+ "created successfully.\n"
relation_type_node_name = relation_type_node.name
relation_type_node_inverse_name = relation_type_node.inverse_name
left_subject = node_collection.one({
"_id": subject_id,
"relation_set." + relation_type_node_name: {"$exists": True}
})
if left_subject:
# Update value of grelation in existing as key-value pair value in
# given node's "relation_set" field
node_collection.collection.update({
"_id": subject_id,
"relation_set." + relation_type_node_name: {'$exists': True}
}, {
"$addToSet": {"relation_set.$." + relation_type_node_name: right_subject_id_or_list}
},
upsert=False, multi=False
)
else:
# Add grelation as new key-value pair value in given node's
# relation_set field
node_collection.collection.update({
"_id": subject_id
}, {
"$addToSet": {"relation_set": {relation_type_node_name: [right_subject_id_or_list]}}
},
upsert=False, multi=False
)
right_subject = node_collection.one({
'_id': right_subject_id_or_list,
"relation_set." + relation_type_node_inverse_name: {"$exists": True}
}, {
'relation_set': 1
})
if right_subject:
# Update value of grelation in existing as key-value pair value in
# given node's "relation_set" field
node_collection.collection.update({
"_id": right_subject_id_or_list, "relation_set." + relation_type_node_inverse_name: {'$exists': True}
}, {
"$addToSet": {"relation_set.$." + relation_type_node_inverse_name: subject_id}
},
upsert=False, multi=False
)
else:
# Add grelation as new key-value pair value in given node's
# relation_set field
node_collection.collection.update({
"_id": right_subject_id_or_list
}, {
"$addToSet": {"relation_set": {relation_type_node_inverse_name: [subject_id]}}
},
upsert=False, multi=False
)
return gr_node
def _update_deleted_to_published(gr_node, relation_type_node, relation_type_text):
gr_node.status = u"PUBLISHED"
gr_node.save()
gr_node_name = gr_node.name
relation_type_node_name = relation_type_node.name
relation_type_node_inverse_name = relation_type_node.inverse_name
subject_id = gr_node.subject
right_subject = gr_node.right_subject
info_message = " %(relation_type_text)s: GRelation (%(gr_node_name)s) " % locals() \
+ "status updated from 'DELETED' to 'PUBLISHED' successfully.\n"
node_collection.collection.update({
"_id": subject_id, "relation_set." + relation_type_node_name: {'$exists': True}
}, {
"$addToSet": {"relation_set.$." + relation_type_node_name: right_subject}
},
upsert=False, multi=False
)
node_collection.collection.update({
"_id": right_subject, "relation_set." + relation_type_node_inverse_name: {'$exists': True}
}, {
"$addToSet": {'relation_set.$.' + relation_type_node_inverse_name: subject_id}
},
upsert=False, multi=False
)
return gr_node
if relation_type_node["object_cardinality"]:
# If object_cardinality value exists and greater than 1 (or eaqual to 100)
# Then it signifies it's a one to many type of relationship
# assign multi_relations = True
type_of_relationship = relation_type_node.member_of_names_list
if relation_type_node["object_cardinality"] > 1:
multi_relations = True
if META_TYPE[3] in type_of_relationship:
# If Binary relationship found
# Check whether right_subject_id_or_list is list or not
# If not convert it to list
if not isinstance(right_subject_id_or_list, list):
right_subject_id_or_list = [right_subject_id_or_list]
# Check whether all values of a list are of ObjectId data-type or not
# If not convert them to ObjectId
for i, each in enumerate(right_subject_id_or_list):
right_subject_id_or_list[i] = ObjectId(each)
else:
# Relationship Other than Binary one found; e.g, Triadic
if right_subject_id_or_list:
if not isinstance(right_subject_id_or_list[0], list):
right_subject_id_or_list = [right_subject_id_or_list]
# right_subject_id_or_list: [[id, id, ...], [id, id, ...], ...]
for i, each_list in enumerate(right_subject_id_or_list):
# each_list: [id, id, ...]
for j, each in enumerate(each_list):
right_subject_id_or_list[i][j] = ObjectId(each)
else:
if META_TYPE[3] in type_of_relationship:
# If Binary relationship found
if isinstance(right_subject_id_or_list, list):
right_subject_id_or_list = ObjectId(right_subject_id_or_list[0])
else:
right_subject_id_or_list = ObjectId(right_subject_id_or_list)
else:
# Relationship Other than Binary one found; e.g, Triadic
# right_subject_id_or_list: [[id, id, ...], [id, id, ...], ...]
if right_subject_id_or_list:
if isinstance(right_subject_id_or_list[0], list):
# Reduce it to [id, id, id, ...]
right_subject_id_or_list = right_subject_id_or_list[0]
for i, each_id in enumerate(right_subject_id_or_list):
right_subject_id_or_list[i] = ObjectId(each_id)
if multi_relations:
# For dealing with multiple relations (one to many)
# Iterate and find all relationships (including DELETED ones' also)
nodes = triple_collection.find({
'_type': "GRelation", 'subject': subject_id,
'relation_type.$id': relation_type_node._id
})
gr_node_list = []
for n in nodes:
if n.right_subject in right_subject_id_or_list:
if n.status != u"DELETED":
# If match found with existing one's, then only remove that ObjectId from the given list of ObjectIds
# Just to remove already existing entries (whose status is PUBLISHED)
right_subject_id_or_list.remove(n.right_subject)
gr_node_list.append(n)
node_collection.collection.update(
{'_id': subject_id, 'relation_set.'+relation_type_node.name: {'$exists': True}},
{'$addToSet': {'relation_set.$.'+relation_type_node.name: n.right_subject}},
upsert=False, multi=False
)
node_collection.collection.update(
{'_id': n.right_subject, 'relation_set.'+relation_type_node.inverse_name: {'$exists': True}},
{'$addToSet': {'relation_set.$.'+relation_type_node.inverse_name: subject_id}},
upsert=False, multi=False
)
else:
# Case: When already existing entry doesn't exists in newly come list of right_subject(s)
# So change their status from PUBLISHED to DELETED
# right_subject_id_or_list.remove(n.right_subject)
n.status = u"DELETED"
n.save()
info_message = " MultipleGRelation: GRelation ("+n.name+") status updated from 'PUBLISHED' to 'DELETED' successfully.\n"
node_collection.collection.update({
'_id': subject_id, 'relation_set.' + relation_type_node.name: {'$exists': True}
}, {
'$pull': {'relation_set.$.' + relation_type_node.name: n.right_subject}
},
upsert=False, multi=False
)
res = node_collection.collection.update({
'_id': n.right_subject, 'relation_set.' + relation_type_node.inverse_name: {'$exists': True}
}, {
'$pull': {'relation_set.$.' + relation_type_node.inverse_name: subject_id}
},
upsert=False, multi=False
)
if right_subject_id_or_list:
# If still ObjectId list persists, it means either they are new ones'
# or from deleted ones'
# For deleted one's, find them and modify their status to PUBLISHED
# For newer one's, create them as new document
for nid in right_subject_id_or_list:
gr_node = triple_collection.one({
'_type': "GRelation", 'subject': subject_id,
'relation_type.$id': relation_type_node._id, 'right_subject': nid
})
if gr_node is None:
# New one found so create it
gr_node = _create_grelation_node(subject_id, relation_type_node, nid, "MultipleGRelation")
gr_node_list.append(gr_node)
else:
# Deleted one found so change it's status back to Published
if gr_node.status == u'DELETED':
gr_node = _update_deleted_to_published(gr_node, relation_type_node, "MultipleGRelation")
gr_node_list.append(gr_node)
else:
error_message = " MultipleGRelation: Corrupt value found - GRelation ("+gr_node.name+")!!!\n"
# raise Exception(error_message)
return gr_node_list
else:
# For dealing with single relation (one to one)
gr_node = None
relation_type_node_id = relation_type_node._id
relation_type_node_name = relation_type_node.name
relation_type_node_inverse_name = relation_type_node.inverse_name
gr_node_cur = triple_collection.find({
"_type": "GRelation", "subject": subject_id,
"relation_type.$id": relation_type_node_id
})
for node in gr_node_cur:
node_name = node.name
node_status = node.status
node_right_subject = node.right_subject
if node_right_subject == right_subject_id_or_list:
# If match found, it means it could be either DELETED one or PUBLISHED one
if node_status == u"DELETED":
# If deleted, change it's status back to Published from Deleted
node = _update_deleted_to_published(node, relation_type_node, "SingleGRelation")
elif node_status == u"PUBLISHED":
node_collection.collection.update({
"_id": subject_id, "relation_set." + relation_type_node_name: {'$exists': True}
}, {
"$addToSet": {"relation_set.$." + relation_type_node_name: node_right_subject}
},
upsert=False, multi=False
)
node_collection.collection.update({
"_id": node_right_subject, "relation_set." + relation_type_node_inverse_name: {'$exists': True}
}, {
"$addToSet": {"relation_set.$." + relation_type_node_inverse_name: subject_id}
},
upsert=False, multi=False
)
info_message = " SingleGRelation: GRelation (%(node_name)s) already exists !\n" % locals()
# Set gr_node value as matched value, so that no need to create new one
node.reload()
gr_node = node
else:
# If match not found and if it's PUBLISHED one, modify it to DELETED
if node.status == u'PUBLISHED':
node.status = u"DELETED"
node.save()
node_collection.collection.update({
'_id': subject_id, 'relation_set.' + relation_type_node_name: {'$exists': True}
}, {
'$pull': {'relation_set.$.' + relation_type_node_name: node_right_subject}
},
upsert=False, multi=False
)
node_collection.collection.update({
'_id': node_right_subject, 'relation_set.' + relation_type_node_inverse_name: {'$exists': True}
}, {
'$pull': {'relation_set.$.' + relation_type_node_inverse_name: subject_id}
},
upsert=False, multi=False
)
info_message = " SingleGRelation: GRelation (%(node_name)s) status " % locals() \
+ "updated from 'PUBLISHED' to 'DELETED' successfully.\n"
if gr_node is None:
# Code for creation
gr_node = _create_grelation_node(subject_id, relation_type_node, right_subject_id_or_list, "SingleGRelation")
return gr_node
except Exception as e:
error_message = "\n GRelationError (line #" + str(exc_info()[-1].tb_lineno) + "): " + str(e) + "\n"
raise Exception(error_message)
############################################### ##############################################
@get_execution_time
def set_all_urls(member_of):
Gapp_obj = node_collection.one({"_type":"MetaType", "name":"GAPP"})
factory_obj = node_collection.one({"_type":"MetaType", "name":"factory_types"})
url = ""
gsType = member_of[0]
gsType_obj = node_collection.one({"_id":ObjectId(gsType)})
if Gapp_obj._id in gsType_obj.member_of:
if gsType_obj.name == u"Quiz":
url = u"quiz/details"
else:
url = gsType_obj.name.lower()
elif factory_obj._id in gsType_obj.member_of:
if gsType_obj.name == u"QuizItem":
url = u"quiz/details"
elif gsType_obj.name == u"Twist":
url = u"forum/thread"
else:
url = gsType_obj.name.lower()
else:
url = u"None"
return url
############################################### ###############################################
@login_required
@get_execution_time
def create_discussion(request, group_id, node_id):
'''
Method to create discussion thread for File and Page.
'''
try:
twist_st = node_collection.one({'_type':'GSystemType', 'name':'Twist'})
node = node_collection.one({'_id': ObjectId(node_id)})
# group = node_collection.one({'_id':ObjectId(group_id)})
thread = node_collection.one({ "_type": "GSystem", "name": node.name, "member_of": ObjectId(twist_st._id), "prior_node": ObjectId(node_id) })
if not thread:
# retriving RelationType
# relation_type = node_collection.one({ "_type": "RelationType", "name": u"has_thread", "inverse_name": u"thread_of" })
# Creating thread with the name of node
thread_obj = node_collection.collection.GSystem()
thread_obj.name = unicode(node.name)
thread_obj.status = u"PUBLISHED"
thread_obj.created_by = int(request.user.id)
thread_obj.modified_by = int(request.user.id)
thread_obj.contributors.append(int(request.user.id))
thread_obj.member_of.append(ObjectId(twist_st._id))
thread_obj.prior_node.append(ObjectId(node_id))
thread_obj.group_set.append(ObjectId(group_id))
thread_obj.save()
# creating GRelation
# create_grelation(node_id, relation_type, twist_st)
response_data = [ "thread-created", str(thread_obj._id) ]
return HttpResponse(json.dumps(response_data))
else:
response_data = [ "Thread-exist", str(thread._id) ]
return HttpResponse(json.dumps(response_data))
except Exception as e:
error_message = "\n DiscussionThreadCreateError: " + str(e) + "\n"
raise Exception(error_message)
# return HttpResponse("server-error")
# to add discussion replie
@get_execution_time
def discussion_reply(request, group_id, node_id):
try:
prior_node = request.POST.get("prior_node_id", "")
content_org = request.POST.get("reply_text_content", "") # reply content
# process and save node if it reply has content
if content_org:
user_id = int(request.user.id)
user_name = unicode(request.user.username)
# auth = node_collection.one({'_type': 'Author', 'name': user_name })
reply_st = node_collection.one({ '_type': 'GSystemType', 'name': 'Reply'})
# creating empty GST and saving it
reply_obj = node_collection.collection.GSystem()
reply_obj.name = unicode("Reply of:" + str(prior_node))
reply_obj.status = u"PUBLISHED"
reply_obj.created_by = user_id
reply_obj.modified_by = user_id
reply_obj.contributors.append(user_id)
reply_obj.member_of.append(ObjectId(reply_st._id))
reply_obj.prior_node.append(ObjectId(prior_node))
reply_obj.group_set.append(ObjectId(group_id))
reply_obj.content_org = unicode(content_org)
filename = slugify(unicode("Reply of:" + str(prior_node))) + "-" + user_name + "-"
reply_obj.content = org2html(content_org, file_prefix=filename)
# saving the reply obj
reply_obj.save()
formated_time = reply_obj.created_at.strftime("%B %d, %Y, %I:%M %p")
# ["status_info", "reply_id", "prior_node", "html_content", "org_content", "user_id", "user_name", "created_at" ]
reply = json.dumps( [ "reply_saved", str(reply_obj._id), str(reply_obj.prior_node[0]), reply_obj.content, reply_obj.content_org, user_id, user_name, formated_time], cls=DjangoJSONEncoder )
# ---------- mail/notification sending -------
node = node_collection.one({"_id": ObjectId(node_id)})
node_creator_user_obj = User.objects.get(id=node.created_by)
node_creator_user_name = node_creator_user_obj.username
site = Site.objects.get(pk=1)
site = site.name.__str__()
from_user = user_name
to_user_list = [node_creator_user_obj]
msg = "\n\nDear " + node_creator_user_name + ",\n\n" + \
"A reply has been added in discussion under the " + \
node.member_of_names_list[0] + " named: '" + \
node.name + "' by '" + user_name + "'."
activity = "Discussion Reply"
render_label = render_to_string(
"notification/label.html",
{
# "sender": from_user,
"activity": activity,
"conjunction": "-",
"link": "url_link"
}
)
notification.create_notice_type(render_label, msg, "notification")
notification.send(to_user_list, render_label, {"from_user": from_user})
# ---------- END of mail/notification sending ---------
return HttpResponse( reply )
else: # no reply content
return HttpResponse(json.dumps(["no_content"]))
except Exception as e:
error_message = "\n DiscussionReplyCreateError: " + str(e) + "\n"
raise Exception(error_message)
return HttpResponse(json.dumps(["Server Error"]))
@get_execution_time
def discussion_delete_reply(request, group_id):
nodes_to_delete = json.loads(request.POST.get("nodes_to_delete", "[]"))
reply_st = node_collection.one({ '_type':'GSystemType', 'name':'Reply'})
deleted_replies = []
for each_reply in nodes_to_delete:
temp_reply = node_collection.one({"_id": ObjectId(each_reply)})
if temp_reply:
deleted_replies.append(temp_reply._id.__str__())
temp_reply.delete()
return HttpResponse(json.dumps(deleted_replies))
@get_execution_time
def get_user_group(userObject):
'''
methods for getting user's belongs to group.
input (userObject) is user object
output list of dict, dict contain groupname, access, group_type, created_at and created_by
'''
blank_list = []
cur_groups_user = node_collection.find({'_type': "Group",
'$or': [
{'created_by': userObject.id},
{'group_admin': userObject.id},
{'author_set': userObject.id},
]
}).sort('last_update', -1)
for eachgroup in cur_groups_user :
access = ""
if eachgroup.created_by == userObject.id:
access = "owner"
elif userObject.id in eachgroup.group_admin :
access = "admin"
elif userObject.id in eachgroup.author_set :
access = "member"
else :
access = "member"
user = User.objects.get(id=eachgroup.created_by)
blank_list.append({'id':str(eachgroup._id), 'name':eachgroup.name, 'access':access, 'group_type':eachgroup.group_type, 'created_at':eachgroup.created_at, 'created_by':user.username})
return blank_list
@get_execution_time
def get_user_task(userObject):
'''
methods for getting user's assigned task.
input (userObject) is user object
output list of dict, dict contain taskname, status, due_time, created_at and created_by, group_name
'''
blank_list = []
attributetype_assignee = node_collection.find_one({"_type":'AttributeType', 'name':'Assignee'})
attributetype_status = node_collection.find_one({"_type":'AttributeType', 'name':'Status'})
attributetype_end_time = node_collection.find_one({"_type":'AttributeType', 'name':'end_time'})
attr_assignee = triple_collection.find({"_type":"GAttribute", "attribute_type.$id":attributetype_assignee._id, "object_value":userObject.username})
for attr in attr_assignee :
blankdict = {}
task_node = node_collection.find_one({'_id':attr.subject})
attr_status = triple_collection.find_one({"_type":"GAttribute", "attribute_type.$id":attributetype_status._id, "subject":task_node._id})
attr_end_time = triple_collection.find_one({"_type":"GAttribute", "attribute_type.$id":attributetype_end_time._id, "subject":task_node._id})
if attr_status.object_value is not "closed":
group = node_collection.find_one({"_id":task_node.group_set[0]})
user = User.objects.get(id=task_node.created_by)
blankdict.update({'name':task_node.name, 'created_at':task_node.created_at, 'created_by':user.username, 'group_name':group.name, 'id':str(task_node._id)})
if attr_status:
blankdict.update({'status':attr_status.object_value})
if attr_end_time:
blankdict.update({'due_time':attr_end_time.object_value})
blank_list.append(blankdict)
return blank_list
@get_execution_time
def get_user_notification(userObject):
'''
methods for getting user's notification.
input (userObject) is user object
output list of dict, dict contain notice label, notice display
'''
blank_list = []
notification_object = notification.NoticeSetting.objects.filter(user_id=userObject.id)
for each in notification_object.reverse():
ntid = each.notice_type_id
ntype = notification.NoticeType.objects.get(id=ntid)
label = ntype.label.split("-")[0]
blank_list.append({'label':label, 'display': ntype.display})
blank_list.reverse()
return blank_list
@get_execution_time
def get_user_activity(userObject):
'''
methods for getting user's activity.
input (userObject) is user object
output list of dict, dict
'''
blank_list = []
activity = ""
activity_user = node_collection.find({'$and':[{'$or':[{'_type':'GSystem'},{'_type':'Group'},{'_type':'File'}]},
{'$or':[{'created_by':userObject.id}, {'modified_by':userObject.id}]}] }).sort('last_update', -1).limit(10)
for each in activity_user:
if each.created_by == each.modified_by :
if each.last_update == each.created_at:
activity = 'created'
else :
activity = 'modified'
else :
activity = 'created'
if each._type == 'Group':
blank_list.append({'id':str(each._id), 'name':each.name, 'date':each.last_update, 'activity': activity, 'type': each._type})
else :
member_of = node_collection.find_one({"_id":each.member_of[0]})
blank_list.append({'id':str(each._id), 'name':each.name, 'date':each.last_update, 'activity': activity, 'type': each._type, 'group_id':str(each.group_set[0]), 'member_of':member_of.name.lower()})
return blank_list
@get_execution_time
def get_file_node(file_name=""):
# if cached result exists return it
cache_key = u'get_file_node' + slugify(unicode(file_name))
cache_result = cache.get(cache_key)
if cache_result:
return cache_result
# ---------------------------------
file_list=[]
new=[]
a=str(file_name).split(',')
for i in a:
k=str(i.strip(' [](\'u\' '))
new.append(k)
ins_objectid = ObjectId()
for i in new:
if ins_objectid.is_valid(i) is False:
filedoc = node_collection.find({'_type':'File','name':unicode(i)})
else:
filedoc = node_collection.find({'_type':'File','_id':ObjectId(i)})
if filedoc:
for i in filedoc:
file_list.append(i.name)
cache.set(cache_key, file_list, 60*15)
return file_list
@get_execution_time
def create_task(task_dict, task_type_creation="single"):
"""Creates task with required attribute(s) and relation(s).
task_dict
- Required keys: _id[optional], name, group_set, created_by, modified_by, contributors, content_org,
created_by_name, Status, Priority, start_time, end_time, Assignee, has_type
task_type_creation
- Valid input values: "single", "multiple", "group"
"""
# Fetch Task GSystemType document
task_gst = node_collection.one(
{'_type': "GSystemType", 'name': "Task"}
)
# List of keys of "task_dict" dictionary
task_dict_keys = task_dict.keys()
if "_id" in task_dict:
task_node = node_collection.one({'_id': task_dict["_id"]})
task_dict["name"] = task_node.name
else:
task_node = node_collection.find_one({"member_of": task_gst._id, "name": task_dict["name"], "attribute_set.Status": {"$nin": ["Closed"]}})
if task_node is None:
task_node = node_collection.collection.GSystem()
task_node["member_of"] = [task_gst._id]
# Store built in variables of task node
# Iterate task_node using it's keys
for key in task_node:
if key in ["Status", "Priority", "start_time", "end_time", "Assignee", "has_type"]:
# Required because these values might come as key in node's document
continue
if key in task_dict_keys:
if key == "content_org":
# org-content
task_node[key] = task_dict[key]
# Required to link temporary files with the current user who is modifying this document
filename = slugify(task_dict["name"]) + "-" + task_dict["created_by_name"] + "-" + ObjectId().__str__()
task_dict_keys.remove("created_by_name")
task_node.content = org2html(task_dict[key], file_prefix=filename)
else:
task_node[key] = task_dict[key]
task_dict_keys.remove(key)
# Save task_node with built-in variables as required for creating GAttribute(s)/GRelation(s)
task_node.status = u"PUBLISHED"
task_node.save()
# Create GAttribute(s)/GRelation(s)
for attr_or_rel_name in task_dict_keys:
attr_or_rel_node = node_collection.one(
{'_type': {'$in': ["AttributeType", "RelationType"]}, 'name': unicode(attr_or_rel_name)}
)
if attr_or_rel_node:
if attr_or_rel_node._type == "AttributeType":
ga_node = create_gattribute(task_node._id, attr_or_rel_node, task_dict[attr_or_rel_name])
elif attr_or_rel_node._type == "RelationType":
gr_node = create_grelation(task_node._id, attr_or_rel_node, task_dict[attr_or_rel_name])
task_node.reload()
else:
raise Exception("\n No AttributeType/RelationType exists with given name("+attr_or_rel_name+") !!!")
# If given task is a group task (create a task for each Assignee from the list)
# Iterate Assignee list & create separate tasks for each Assignee
# with same set of attribute(s)/relation(s)
if task_type_creation == "group":
mutiple_assignee = task_dict["Assignee"]
collection_set = []
for each in mutiple_assignee:
task_dict["Assignee"] = [each]
task_sub_node = create_task(task_dict)
collection_set.append(task_sub_node._id)
node_collection.collection.update({'_id': task_node._id}, {'$set': {'collection_set': collection_set}}, upsert=False, multi=False)
else:
# Send notification for each each Assignee of the task
# Only be done in case when task_type_creation is not group,
# i.e. either single or multiple
if not task_dict.has_key("_id"):
site = Site.objects.get(pk=1)
site = site.name.__str__()
from_user = task_node.user_details_dict["created_by"] # creator of task
group_name = node_collection.one(
{'_type': {'$in': ["Group", "Author"]}, '_id': task_node.group_set[0]},
{'name': 1}
).name
url_link = "http://" + site + "/" + group_name.replace(" ","%20").encode('utf8') + "/task/" + str(task_node._id)
to_user_list = []
for index, user_id in enumerate(task_dict["Assignee"]):
user_obj = User.objects.get(id=user_id)
task_dict["Assignee"][index] = user_obj.username
if user_obj not in to_user_list:
to_user_list.append(user_obj)
msg = "Task '" + task_node.name + "' has been reported by " + from_user + \
"\n - Status: " + task_dict["Status"] + \
"\n - Priority: " + task_dict["Priority"] + \
"\n - Assignee: " + ", ".join(task_dict["Assignee"]) + \
"\n - For more details, please click here: " + url_link
activity = "reported task"
render_label = render_to_string(
"notification/label.html",
{
"sender": from_user,
"activity": activity,
"conjunction": "-",
"link": url_link
}
)
notification.create_notice_type(render_label, msg, "notification")
notification.send(to_user_list, render_label, {"from_user": from_user})
return task_node
@get_execution_time
def get_student_enrollment_code(college_id, node_id_to_ignore, registration_date, college_group_id=None):
"""Returns new student's enrollment code
Enrollment Code is combination of following values:
Code: (MH/SNG/15/xxxx)
- 2-letters state code
- 3-letters college code
- 2-digits year of registration
- 4-digits Auto-generated No.
Arguments:
college_id: ObjectId of college's node
node_id_to_ignore: ObjectId of the student node for which this function generates enrollment_code
registration_date: Date of registration of student node for which this function generates enrollment_code
college_group_id [Optional]: ObjectId of college's group node
Returns:
Newly created enrollment code for student node
"""
new_student_enrollment_code = u""
last_enrollment_code = u""
enrollment_code_except_num = u""
college_id = ObjectId(college_id)
if college_group_id:
college_group_id = ObjectId(college_group_id)
college_name = u""
college_code = u""
state_code = u""
two_digit_year_code = u""
student_count = 0
four_digit_num = u""
# If registering very first student, then create enrollment code
# Else fetch enrollment code from last registered student node
# Fetch college enrollemnt code
college_node = node_collection.collection.aggregate([{
"$match": {
"_id": college_id
}
}, {
"$project": {
"college_name": "$name",
"college_code": "$attribute_set.enrollment_code",
"college_group_id": "$relation_set.has_group",
"state_id": "$relation_set.organization_belongs_to_state"
}
}])
college_node = college_node["result"]
if college_node:
college_node = college_node[0]
colg_group_id = college_node["college_group_id"]
if colg_group_id:
colg_group_id = ObjectId(colg_group_id[0][0])
if colg_group_id != college_group_id:
# Reset college_group_id as passed is not given
# college node's group id
college_group_id = colg_group_id
college_name = college_node["college_name"]
# Set College enrollment code
college_code = college_node["college_code"]
if college_code:
college_code = college_code[0]
if not college_name or not college_code:
raise Exception("Either name or enrollment code is not set for given college's ObjectId(" + str(college_id) + ") !!!")
# Set Last two digits of current year
# current_year = str(datetime.today().year)
current_year = str(registration_date.year)
two_digit_year_code = current_year[-2:]
# Fetch total no. of students registered for current year
# Along with enrollment code of last registered student
date_gte = datetime.strptime("1/1/" + current_year, "%d/%m/%Y")
date_lte = datetime.strptime("31/12/" + current_year, "%d/%m/%Y")
student_gst = node_collection.one({
"_type": "GSystemType",
"name": "Student"
})
res = node_collection.collection.aggregate([{
"$match": {
"_id": {"$nin": [ObjectId(node_id_to_ignore)]},
"member_of": student_gst._id,
"group_set": college_group_id,
"relation_set.student_belongs_to_college": college_id,
"attribute_set.registration_date": {'$gte': date_gte, '$lte': date_lte},
"status": u"PUBLISHED"
}
}, {
"$sort": {
"created_at": 1
}
}, {
"$group": {
"_id": {
"college": college_name,
"registration_year": current_year
},
"count": {
"$sum": 1
},
"last_enrollment_code": {
"$last": "$attribute_set.enrollment_code"
}
}
}])
student_data_result = res["result"]
if student_data_result:
student_count = student_data_result[0]["count"]
last_enrollment_code = student_data_result[0]["last_enrollment_code"]
if last_enrollment_code:
last_enrollment_code = last_enrollment_code[0]
if last_enrollment_code and student_count:
# Fetch student enrollment code of last registered student node
enrollment_code_except_num, four_digit_num = last_enrollment_code.rsplit("/", 1)
four_digit_num = int(last_enrollment_code[-4:])
if four_digit_num == student_count:
# 4. Four digit number of student's count (new) set
four_digit_num = "%04d" % (student_count + 1)
new_student_enrollment_code = u"%(enrollment_code_except_num)s/%(four_digit_num)s" % locals()
else:
raise Exception("Inconsistent Data Found (Student count & last enrollment code's number doesn't match) !!!")
else:
raise Exception("Invalid Data Found (Student count & no last enrollment code) !!!")
else:
# Registering very first student node
# Create enrollment code, hence fetch state node's state-code
state_id = college_node["state_id"]
if state_id:
state_id = ObjectId(state_id[0][0])
# Fetch state code
state_node = node_collection.collection.aggregate([{
"$match": {
"_id": state_id
}
}, {
"$project": {
"state_name": "$name",
"state_code": "$attribute_set.state_code"
}
}])
# 2. State code set
state_node = state_node["result"]
if state_node:
state_node = state_node[0]
state_name = state_node["state_name"]
state_code = state_node["state_code"]
if state_code:
state_code = state_code[0]
else:
raise Exception("No state code found for given state(" + state_name + ")")
# 4. Four digit number of student's count (new) set
four_digit_num = "%04d" % (student_count + 1)
new_student_enrollment_code = u"%(state_code)s/%(college_code)s/%(two_digit_year_code)s/%(four_digit_num)s" % locals()
else:
raise Exception("Either state not set or inconsistent state value found for given college(" + college_name + ") !!!")
return new_student_enrollment_code
else:
raise Exception("No college node exists with given college's ObjectId(" + str(college_id) + ") !!!")
@get_execution_time
def create_college_group_and_setup_data(college_node):
"""
Creates private group for given college; establishes relationship
between them via "has_group" RelationType.
Also populating data into it needed for registrations.
Arguments:
college_node -- College node (or document)
Returns:
College group node
GRelation node
"""
gfc = None
gr_gfc = None
# [A] Creating group
group_gst = node_collection.one(
{'_type': "GSystemType", 'name': "Group"},
{'_id': 1}
)
creator_and_modifier = college_node.created_by
gfc = node_collection.one(
{'_type': "Group", 'name': college_node.name},
{'_id': 1, 'name': 1, 'group_type': 1}
)
if not gfc:
gfc = node_collection.collection.Group()
gfc._type = u"Group"
gfc.name = college_node.name
gfc.altnames = college_node.name
gfc.member_of = [group_gst._id]
gfc.group_type = u"PRIVATE"
gfc.created_by = creator_and_modifier
gfc.modified_by = creator_and_modifier
gfc.contributors = [creator_and_modifier]
gfc.status = u"PUBLISHED"
gfc.save()
if "_id" in gfc:
has_group_rt = node_collection.one(
{'_type': "RelationType", 'name': "has_group"}
)
gr_gfc = create_grelation(college_node._id, has_group_rt, gfc._id)
# [B] Setting up data into college group
if gr_gfc:
# List of Types (names) whose data needs to be populated
# in college group
gst_list = [
"Country", "State", "District", "University",
"College", "Caste", "NUSSD Course"
]
gst_cur = node_collection.find(
{'_type': "GSystemType", 'name': {'$in': gst_list}}
)
# List of Types (ObjectIds)
gst_list = []
for each in gst_cur:
gst_list.append(each._id)
mis_admin = node_collection.one(
{
'_type': "Group",
'$or': [
{'name': {'$regex': u"MIS_admin", '$options': 'i'}},
{'altnames': {'$regex': u"MIS_admin", '$options': 'i'}}
],
'group_type': "PRIVATE"
},
{'_id': 1}
)
# Update GSystem node(s) of GSystemType(s) specified in gst_list
# Append newly created college group's ObjectId in group_set field
node_collection.collection.update(
{
'_type': "GSystem", 'member_of': {'$in': gst_list},
'group_set': mis_admin._id
},
{'$addToSet': {'group_set': gfc._id}},
upsert=False, multi=True
)
return gfc, gr_gfc
def get_published_version_list(request,document_object):
"""Returns the list of revision numbers of published nodes.
"""
published_node_version = []
rcs = RCS()
fp = history_manager.get_file_path(document_object)
cmd= 'rlog %s' % \
(fp)
rev_no =""
proc1=subprocess.Popen(cmd,shell=True,
stdout=subprocess.PIPE)
for line in iter(proc1.stdout.readline,b''):
if line.find('revision')!=-1 and line.find('selected') == -1:
rev_no=string.split(line,'revision')
rev_no=rev_no[1].strip( '\t\n\r')
rev_no=rev_no.strip(' ')
if line.find('PUBLISHED')!=-1:
published_node_version.append(rev_no)
return published_node_version
def parse_data(doc):
'''Section to parse node '''
user_idlist = ['modified_by','created_by','author_set','contributors']
date_typelist = ['last_update','created_at']
objecttypelist = ['member_of','group_set', 'collection_set','prior_node']
languagelist = ['language']
content = ['content']
keys_by_dict = ['attribute_set', 'relation_set']
keys_by_filesize = ['file_size']
for i in doc:
if i in content:
doc[i] = str(doc[i])
if i in user_idlist:
if type(doc[i]) == list :
temp = ""
for userid in doc[i]:
if User.objects.filter(id = userid).exists():
user = User.objects.get(id = userid)
if user:
if temp:
temp =temp + "," + (str(user.get_username()) )
else:
temp = str(user.get_username())
doc[i] = temp
else:
if User.objects.filter(id = doc[i]).exists():
user = User.objects.get(id = doc[i])
if user:
doc[i] = str(user.get_username())
elif i in date_typelist:
doc[i] = datetime.strftime(doc[i],"%d %B %Y %H:%M")
elif i in objecttypelist:
for j in doc[i]:
node = node_collection.one({"_id":ObjectId(j)})
doc[i] = node.name
if i in keys_by_dict:
att_dic = {}
if "None" not in doc[i]:
if type(doc[i]) != str and i == "attribute_set":
str1 =""
for att in doc[i]:
for k1, v1 in att.items():
if type(v1) == list:
str1 = ""
if type(v1[0]) in [OrderedDict, dict]:
for each in v1:
str1 += str(each["name"]) + ", "
else:
str1 = ",".join(v1)
att_dic[k1] = str1
else:
att_dic[k1] = str(v1)
for att,value in att_dic.items():
str1 = str1 + att + " : " + value + " "+"\n"
doc[i] = str1
if i == "relation_set":
str1 =""
for each in doc[i]:
for k1, v1 in each.items():
for rel in v1:
rel = node_collection.one({'_id':ObjectId(rel)})
att_dic[k1] = rel.name
for att,value in att_dic.items():
str1 = str1 + att + " : " + value + " "+"\n"
doc[i] = str1
elif i == "rating":
new_str = ""
if doc[i]:
for k in doc[i]:
userid = k['user_id']
score = k['score']
if User.objects.filter(id = userid).exists():
user = User.objects.get(id = userid)
if user:
new_str = new_str + "User" +":" + str(user.get_username()) + " " + "Score" + str (score) + "\n"
if not doc[i]:
doc[i] = ""
else:
doc[i] = new_str
elif i == "location":
coordinates = []
parsed_string = ""
for j in doc[i]:
coordinates = j['geometry']['coordinates']
if coordinates:
for j in coordinates:
if parsed_string:
parsed_string = str(parsed_string) + "," + str(j)
else:
parsed_string = str(j)
if not doc[i]:
doc[i] = ""
else:
doc[i] = parsed_string
elif not doc[i]:
doc[i] = ""
def delete_gattribute(subject_id=None, deletion_type=0, **kwargs):
"""This function deletes GAttribute node(s) of Triples collection.
Keyword arguments:
subject_id -- (Optional argument)
- Specify this argument if you need to delete/purge GAttribute(s)
related to given node belonging to Nodes collection
- ObjectId of the node whose GAttribute node(s) need(s) to be deleted,
accepted in either format String or ObjectId
- Default value is set to None
kwargs["node_id"] -- (Optional argument)
- Specify this argument if you need to delete/purge only a given
GAttribute node
- ObjectId of the GAttribute node to be deleted/purged, accepted in
either format String or ObjectId
- If this argument is specified, subject_id will work as an optional
argument and even query variable would be overridden by node_id's
query variable
deletion_type -- (Optional argument)
- Specify this to signify which type of deletion you need to perform
- Accepts only either of the following values:
(a) 0 (zero, i.e. Normal delete)
- Process in which node exists in database; only status field's
value is set to "DELETED"
(b) 1 (one, i.e. Purge)
- Process in which node is deleted from the database
- Default value is set to 0 (zero)
Returns:
A tuple with following values:
First-element: Boolean value
Second-element: Message
If deletion is successful, then (True, "Success message.")
Otherwise, (False, "Error message !")
Examples:
del_status, del_status_msg = delete_attribute(
subject_id=ObjectId("...")
[, deletion_type=0[/1]]
)
del_status, del_status_msg = delete_attribute(
node_id=ObjectId("...")
[, deletion_type=0[/1]]
)
"""
node_id = None
str_node_id = ""
str_subject_id = ""
str_deletion_type = "deleted" # As default value of deletion_type is 0
# Below variable holds list of ObjectId (string format)
# of GAttribute node(s) which is/are going to be deleted
gattribute_deleted_id = []
# Below variable holds list of ObjectId (string format)
# of GAttribute node(s) whose object_value field is/are going to be updated
gattribute_updated_id = []
query = OrderedDict()
try:
# print "\n 1 >> Begin..."
if deletion_type not in [0, 1]:
delete_status_message = "Must pass \"deletion_type\" agrument's " \
+ "value as either 0 (Normal delete) or 1 (Purge)"
raise Exception(delete_status_message)
# print "\n 2 >> looking for node_id..."
if "node_id" in kwargs:
# Typecast node_id from string into ObjectId,
# if found in string format
node_id = kwargs["node_id"]
# print "\t 2a >> convert node_id..."
if node_id:
if type(node_id) == ObjectId:
str_node_id = str(node_id)
# print "\t 2b >> node_id -- O to s: ", type(str_subject_id), " -- ", str_subject_id
else:
str_node_id = node_id
if ObjectId.is_valid(node_id):
node_id = ObjectId(node_id)
# print "\t 2c >> node_id -- s to O: ", type(str_subject_id), " -- ", str_subject_id
else:
delete_status_message = "Invalid value found for node_id " \
+ "(%(str_node_id)s)... [Expected value in" % locals() \
+ " ObjectId format] !!!"
raise Exception(delete_status_message)
# Forming query to delete a specific GAtribute node
query = {"_id": node_id}
# print "\t 2d >> query... ", query
# print "\n 3 >> looking for subject_id..."
if not node_id:
# Perform check for subject_id
# print "\t 3 >> found subject_id..."
if not subject_id:
delete_status_message = "Either specify subject_id " \
+ "or node_id [Expected value in ObjectId format] !!!"
raise Exception(delete_status_message)
# print "\t 3a >> convert subject_id..."
if subject_id:
# Typecast subject_id from string into ObjectId,
# if found in string format
if type(subject_id) == ObjectId:
str_subject_id = str(subject_id)
# print "\t 3b >> subject_id -- O to s: ", type(str_subject_id), " -- ", str_subject_id
else:
str_subject_id = subject_id
if ObjectId.is_valid(subject_id):
subject_id = ObjectId(subject_id)
# print "\t 3c >> subject_id -- s to O: ", type(str_subject_id), " -- ", str_subject_id
else:
if not node_id:
delete_status_message = "Invalid value found for subject_id " \
+ "(%(str_subject_id)s)... [Expected value in" % locals() \
+ " ObjectId format] !!!"
raise Exception(delete_status_message)
# Check first whether request is
# for single GAttribute node delete or not
# print "\t 3d >> Override query... ???"
if not node_id:
# Form this query only when you need to
# delete GAttribute(s) related to a given node
query = {"_type": "GAttribute", "subject": subject_id}
# print "\t 3e >> query (YES)... ", query
# Based on what you need to perform (query)
# Delete single GAttribute node, or
# Delete GAttribute node(s) related to a given node (subject_id)
# Find the required GAttribute node(s)
gattributes = triple_collection.find(query)
# print "\n 4 >> gattributes.count()... ", gattributes.count()
# Perform normal delete operation (i.e. deletion_type == 0)
for each_ga in gattributes:
gattribute_deleted_id.append(each_ga._id.__str__())
if each_ga.status != u"DELETED":
create_gattribute(each_ga.subject, each_ga.attribute_type)
# print "\t 4 >> each_ga (0) ... ", each_ga._id
# print "\n 5 >> gattribute_deleted_id... " + ", ".join(gattribute_deleted_id)
# Perform purge operation
if deletion_type == 1:
# Remove from database
str_deletion_type = "purged"
triple_collection.collection.remove(query)
# print "\n 6 >> purged also... " + ", ".join(gattribute_deleted_id)
# Formulate delete-status-message
if gattribute_deleted_id:
delete_status_message = "\tFollowing are the list of ObjectId(s) of " \
+ "%(str_deletion_type)s GAttribute node(s):- \n\t" % locals() \
+ ", ".join(gattribute_deleted_id)
else:
delete_status_message = "\tNo GAttribute nodes have been " \
+ "%(str_deletion_type)s !!!" % locals()
# print "\n 7 >> special use-case... "
# Special use-case
if subject_id:
# Find GAttribute node(s) whose object_value field (i.e. a list)
# has subject_id as one of it's value
gattributes = None
gattributes = triple_collection.find({
"_type": "GAttribute", "object_value": subject_id
})
# print "\n 8 >> gattributes.count()... ", gattributes.count()
for each_ga in gattributes:
# (a) Update GAttribute node's object_value field
# Remove subject_id from object_value
# (b) Update subject node's attribute_set field
# Remove subject_id from the value corresponding to
# subject node's "attribute-name" key referenced
# in attribute_set field
# Expecting object_value as list of ObjectIds
obj_val = []
# print "\n 8a >> each_ga... ", each_ga._id
if type(each_ga.object_value) == list:
obj_val = each_ga.object_value
# Declaration required to avoid list-copy by reference
prev_obj_val = []
prev_obj_val.extend(obj_val)
if subject_id in obj_val:
obj_val.remove(subject_id)
# print "\t 8b >> obj_val... ", obj_val
# print "\t 8c >> prev_obj_val... ", prev_obj_val
if prev_obj_val != obj_val:
# Update only when there is any change found
# Below call will perform (a) & (b) collectively
create_gattribute(
each_ga.subject, each_ga.attribute_type, obj_val
)
gattribute_updated_id.append(each_ga._id.__str__())
# print "\t 8d >> updated..."
if gattribute_updated_id:
delete_status_message += "\tFollowing are the list of ObjectId(s) of " \
+ "GAttribute node(s) whose object_value field is updated:- \n\t" \
+ ", ".join(gattribute_updated_id)
# Return output of the function
# print "\n 9 >> ", delete_status_message
return (True, delete_status_message)
except Exception as e:
delete_status_message = "DeleteGAttributeError: " + str(e)
return (False, delete_status_message)
def delete_grelation(subject_id=None, deletion_type=0, **kwargs):
"""This function deletes GRelation node(s) of Triples collection.
Keyword arguments:
subject_id -- (Optional argument)
- Specify this argument if you need to delete/purge GRelation(s)
related to given node belonging to Nodes collection
- ObjectId of the node whose GRelation node(s) need(s) to be deleted,
accepted in either format String or ObjectId
- Default value is set to None
kwargs["node_id"] -- (Optional argument)
- Specify this argument if you need to delete/purge only a given
GRelation node
- ObjectId of the GRelation node to be deleted/purged, accepted in
either format String or ObjectId
- If this argument is specified, subject_id will work as an optional
argument and even query variable would be overridden by node_id's
query variable
deletion_type -- (Optional argument)
- Specify this to signify which type of deletion you need to perform
- Accepts only either of the following values:
(a) 0 (zero, i.e. Normal delete)
- Process in which node exists in database; only status field's
value is set to "DELETED"
(b) 1 (one, i.e. Purge)
- Process in which node is deleted from the database
- Default value is set to 0 (zero)
Returns:
A tuple with following values:
First-element: Boolean value
Second-element: Message
If deletion is successful, then (True, "Success message.")
Otherwise, (False, "Error message !")
Examples:
del_status, del_status_msg = delete_grelation(
subject_id=ObjectId("...")
[, deletion_type=0[/1]]
)
del_status, del_status_msg = delete_grelation(
node_id=ObjectId("...")
[, deletion_type=0[/1]]
)
"""
node_id = None
str_node_id = ""
str_subject_id = ""
str_deletion_type = "deleted" # As default value of deletion_type is 0
# Below variable holds list of ObjectId (string format)
# of GRelation node(s) which is/are going to be deleted
grelation_deleted_id = []
# Below variable holds list of ObjectId (string format)
# of GRelation node(s) [inverse-relation] which is/are going to be deleted
inverse_grelation_deleted_id = []
query_by_id = {} # Search by _id field
query_for_relation = OrderedDict() # Search by subject field
query_for_inverse_relation = OrderedDict() # Search by right_subject field
def _perform_delete_updates_on_node(gr_node):
rel_name = gr_node.relation_type.name
inv_rel_name = gr_node.relation_type.inverse_name
subj = gr_node.subject
right_subj = gr_node.right_subject
# Remove right-subject-node's ObjectId from the value
# corresponding to subject-node's "relation-name" key
# referenced in relation_set field
res = node_collection.collection.update({
'_id': subj,
'relation_set.' + rel_name: {'$exists': True}
}, {
'$pull': {'relation_set.$.' + rel_name: right_subj}
},
upsert=False, multi=False
)
# print "\n 5 -- subject node's (", subj, ") relation-name key (", rel_name, ") referenced in relation_set field updated -- \n", res
# Remove subject-node's ObjectId from the value corresponding
# to right-subject-node's "inverse-relation-name" key
# referenced in relation_set field
res = node_collection.collection.update({
'_id': right_subj,
'relation_set.' + inv_rel_name: {'$exists': True}
}, {
'$pull': {'relation_set.$.' + inv_rel_name: subj}
},
upsert=False, multi=False
)
# print " 5 -- right_subject node's (", right_subj, ") inverse-relation-name key (", inv_rel_name, ") referenced in relation_set field updated -- \n", res
gr_node.status = u"DELETED"
gr_node.save()
try:
# print "\n 1 >> Begin..."
if deletion_type not in [0, 1]:
delete_status_message = "Must pass \"deletion_type\" agrument's " \
+ "value as either 0 (Normal delete) or 1 (Purge) !!!"
raise Exception(delete_status_message)
# print "\n 2 >> looking for node_id..."
if "node_id" in kwargs:
# Typecast node_id from string into ObjectId,
# if found in string format
node_id = kwargs["node_id"]
# print "\t 2a >> convert node_id..."
if node_id:
if type(node_id) == ObjectId:
str_node_id = str(node_id)
# print "\t 2b >> node_id -- O to s: ", type(str_subject_id), " -- ", str_subject_id
else:
str_node_id = node_id
if ObjectId.is_valid(node_id):
node_id = ObjectId(node_id)
# print "\t 2c >> node_id -- s to O: ", type(str_subject_id), " -- ", str_subject_id
else:
delete_status_message = "Invalid value found for node_id " \
+ "(%(str_node_id)s)... [Expected value in" % locals() \
+ " ObjectId format] !!!"
raise Exception(delete_status_message)
# Forming query to delete a specific GRelation node
query_by_id = {"_id": node_id}
# print "\t 2d >> query... ", query_by_id
# print "\n 3 >> looking for subject_id..."
if not node_id:
# Perform check for subject_id
# print "\t 3 >> found subject_id..."
if not subject_id:
delete_status_message = "Either specify subject_id " \
+ "or node_id [Expected value in ObjectId format] !!!"
raise Exception(delete_status_message)
# print "\t 3a >> convert subject_id..."
if subject_id:
# Typecast subject_id from string into ObjectId,
# if found in string format
if type(subject_id) == ObjectId:
str_subject_id = str(subject_id)
# print "\t 3b >> subject_id -- O to s: ", type(str_subject_id), " -- ", str_subject_id
else:
str_subject_id = subject_id
if ObjectId.is_valid(subject_id):
subject_id = ObjectId(subject_id)
# print "\t 3c >> subject_id -- s to O: ", type(str_subject_id), " -- ", str_subject_id
else:
if not node_id:
delete_status_message = "Invalid value found for subject_id " \
+ "(%(str_subject_id)s)... [Expected value in" % locals() \
+ " ObjectId format] !!!"
raise Exception(delete_status_message)
# Check first whether request is
# for single GRelation node delete or not
# print "\t 3d >> Override query... ???"
if not node_id:
# Form this query only when you need to
# delete/purge GRelation(s) related to a given node
query_for_relation = {"_type": "GRelation", "subject": subject_id}
query_for_inverse_relation = {"_type": "GRelation", "right_subject": subject_id}
# print "\t 3e >> query (YES)... \n\t", query_for_relation, "\n\t", query_for_inverse_relation
# Based on what you need to perform
# Delete single GRelation node (query_by_id), or
# Delete GRelation node(s) related to a given node (subject_id)
# (i.e, query_for_relation and query_for_inverse_relation)
# Find the required GRelation node(s) & perform required operation(s)
if query_by_id:
# print "\n delete single GRelation node"
grelations = triple_collection.find(query_by_id)
for each_rel in grelations:
if each_rel.status != u"DELETED":
_perform_delete_updates_on_node(each_rel)
grelation_deleted_id.append(each_rel._id.__str__())
# print "\n 5 >> grelation_deleted_id... " + ", ".join(grelation_deleted_id)
# Perform purge operation
if deletion_type == 1:
# Remove from database
str_deletion_type = "purged"
triple_collection.collection.remove(query_by_id)
# print "\n 6 >> purged (relation) also... " + ", ".join(grelation_deleted_id)
else:
# print "\n handle query_for_relation, query_for_inverse_relation"
grelations = None
inv_grelations = None
# (1) Find relation(s) of given node (subject_id)
# i.e, GRelation node where given node's ObjectId resides
# in subject field
grelations = triple_collection.find(query_for_relation)
for each_rel in grelations:
if each_rel.status != u"DELETED":
_perform_delete_updates_on_node(each_rel)
grelation_deleted_id.append(each_rel._id.__str__())
# (2) Find inverse-relation(s) of given node (subject_id)
# i.e, GRelation node where given node's ObjectId resides
# in right_subject field
inv_grelations = triple_collection.find(query_for_inverse_relation)
for each_inv_rel in inv_grelations:
if each_inv_rel.status != u"DELETED":
_perform_delete_updates_on_node(each_inv_rel)
inverse_grelation_deleted_id.append(each_inv_rel._id.__str__())
# print "\n 5 >> grelation_deleted_id... " + ", ".join(grelation_deleted_id)
# print "\n 5 >> inverse_grelation_deleted_id... " + ", ".join(inverse_grelation_deleted_id)
# Perform purge operation
if deletion_type == 1:
# Remove from database
str_deletion_type = "purged"
triple_collection.collection.remove(query_for_relation)
triple_collection.collection.remove(query_for_inverse_relation)
# print "\n 6 >> purged (relation) also... " + ", ".join(grelation_deleted_id)
# print "\n 6 >> purged (inverse-relation) also... " + ", ".join(inverse_grelation_deleted_id)
# Formulate delete-status-message
if grelation_deleted_id:
delete_status_message = "\tFollowing are the list of ObjectId(s) of " \
+ "%(str_deletion_type)s GRelation [Normal relation] node(s):- \n\t" % locals() \
+ ", ".join(grelation_deleted_id)
else:
delete_status_message = "\tNo GRelation [Normal relation] nodes have been " \
+ "%(str_deletion_type)s !!!" % locals()
if inverse_grelation_deleted_id:
delete_status_message += "\n\n\tFollowing are the list of ObjectId(s) of " \
+ "%(str_deletion_type)s GRelation [Inverse relation] node(s):- \n\t" % locals() \
+ ", ".join(inverse_grelation_deleted_id)
else:
delete_status_message += "\n\n\tNo GRelation [Inverse relation] nodes have been " \
+ "%(str_deletion_type)s !!!" % locals()
# Return output of the function
# print "\n 9 >> ", delete_status_message
return (True, delete_status_message)
except Exception as e:
delete_status_message = "DeleteGRelationError: " + str(e)
return (False, delete_status_message)
def delete_node(
node_id=None, collection_name=node_collection.collection_name,
deletion_type=0, **kwargs):
"""This function deletes node belonging to either Nodes collection or
Triples collection.
Keyword Arguments:
node_id -- (Optional argument)
- Specify this argument if you need to delete/purge only a given
node from Nodes/Triples collection
- ObjectId of the node to be deleted/purged, accepted in
either format String or ObjectId
- If this argument is specified, then subject_id parameter will be
ignored (if specified) in case of deleting node from Triples collection
- If this argument is ignored, then you must specify subject_id as a
parameter (mandatory in case of deleting node from Triples collection).
- Default value is set to None
collection_name -- (Optional argument)
- Specify this to signify from which collection you need to delete node
i.e. helpful in setting-up the collection-variable
- Name of the collection you need to refer for performing deletion
- Accepts only either of the following values:
(a) node_collection.collection_name/"Nodes"
(b) triple_collection.collection_name/"Triples"
- Default set to node_collection.collection_name (i.e. "Nodes")
deletion_type -- (Optional argument)
- Specify this to signify which type of deletion you need to perform
- Accepts only either of the following values:
(a) 0 (zero, i.e. Normal delete)
- Process in which node exists in database; only status field's
value is set to "DELETED"
(b) 1 (one, i.e. Purge)
- Process in which node is deleted from the database
- Default value is set to 0 (zero)
kwargs["subject_id"] -- (Optional argument)
- Specify this argument if you need to delete/purge GRelation(s) and/or
GAttribute(s) related to given node belonging to Nodes collection
- ObjectId of the node whose GAttribute(s) and/or GRelation node(s)
need(s) to be deleted, accepted in either format String or ObjectId
- Default value is set to None
kwargs["_type"] -- (Optional argument)
- Specify this argument if you need to delete/purge specifically either
only GAttribute node(s) or GRelation node(s)
- If ignored, then by default node(s) belonging to both types
(GAttribute and GRelation) will be considered for deleting/purging
- Can also be specified in case of delete/purge Nodes collection node
Returns:
A tuple with following values:
First-element: Boolean value
Second-element: Message
If deletion is successful, then (True, "Success message.")
Otherwise, (False, "Error message !")
If you need to delete node of Nodes collection, then you only need to
specify node_id, collection_name, and deletion_type as parameters.
Examples:
del_status, del_status_msg = delete_node(
node_id=ObjectId("...")
[, collection_name=node_collection.collection_name]
[, deletion_type=0[/1]]
)
If you need to delete node(s) of Triples collection, then you need to
specify node_id/subject_id [depending on whether you need to delete single
node or multiple nodes which are related to given node of Nodes collection]
, collection_name, _type, and deletion_type as parameters.
Examples:
del_status, del_status_msg = delete_node(
subject_id=ObjectId("..."),
collection_name=triple_collection.collection_name
[, _type="GAttribute"[/"GRelation"]]
[, deletion_type=0[/1]]
)
del_status, del_status_msg = delete_node(
node_id=ObjectId("..."),
collection_name=triple_collection.collection_name
[, _type="GAttribute"[/"GRelation"]]
[, deletion_type=0[/1]]
)
"""
try:
# print "\n 1 >> Entered in delete_node() function..."
# Convert into string format if value of some other data-type is passed
collection_name = collection_name.__str__()
# Check from which collection you need to delete node from
if collection_name == node_collection.collection_name:
# Perform deletion operation on Nodes collection
str_node_id = ""
query = {}
node_to_be_deleted = None
node_name = ""
# As default value of deletion_type is 0
str_deletion_type = "deleted"
delete_status_message = ""
# print "\n 2 >> Nodes collection..."
if deletion_type not in [0, 1]:
delete_status_message = "Must pass \"deletion_type\" agrument's " \
+ "value as either 0 (Normal delete) or 1 (Purge) !!!"
raise Exception(delete_status_message)
# print "\t 3 >> found node_id..."
if not node_id:
delete_status_message = "No value found for node_id" \
+ "... [Expected value in ObjectId format] !!!"
raise Exception(delete_status_message)
# print "\t 3a >> convert node_id..."
# Typecast node_id from string into ObjectId,
# if found in string format
if type(node_id) == ObjectId:
str_node_id = str(node_id)
# print "\t 3b >> node_id -- O to s: ", type(str_node_id), " -- ", str_node_id
else:
str_node_id = node_id
if ObjectId.is_valid(node_id):
node_id = ObjectId(node_id)
# print "\t 3c >> node_id -- s to O: ", type(str_node_id), " -- ", str_node_id
else:
delete_status_message = "Invalid value found for node_id " \
+ "(%(str_node_id)s)... [Expected value in" % locals() \
+ " ObjectId format] !!!"
raise Exception(delete_status_message)
# Forming query to delete a specific node from Nodes collection
query = {"_id": node_id}
# print "\t 3d >> query... ", query
# Fetch the deleting-node from given node_id
node_to_be_deleted = node_collection.find_one(query)
if not node_to_be_deleted:
delete_status_message = "Node with given ObjectId " \
+ "(%(str_node_id)s) doesn't exists " % locals() \
+ "in Nodes collection !!!"
raise Exception(delete_status_message)
node_name = node_to_be_deleted.name
if node_to_be_deleted.status == u"DELETED" and deletion_type == 0:
delete_status_message = "%(node_name)s (%(str_node_id)s) has " % locals() \
+ "already been deleted (using normal delete)." \
+ "\n\nIf required, you can still purge this node !"
return (True, delete_status_message)
# print "\n 4 >> node to be deleted fetched successfully... ", node_to_be_deleted.name
if ((node_to_be_deleted.status == u"DELETED" and
deletion_type == 1) or
(node_to_be_deleted.status != u"DELETED")):
# Perform delete/purge operation for
# deleting-node's GAttribute(s)
# print "\n\n 5 >> node's (", node_to_be_deleted.name,") GAttribute... "
del_status, del_status_msg = delete_gattribute(
subject_id=node_to_be_deleted._id,
deletion_type=deletion_type
)
if not del_status:
raise Exception(del_status_msg)
delete_status_message = del_status_msg
# print "\n 5* >> delete_status_message... \n", delete_status_message
# Required as below this node is getting saved and
# in above delete_gattribute() function, it's getting updated
node_to_be_deleted.reload()
# Perform delete/purge operation
# for deleting-node's GRelation(s)
# print "\n\n 6 >> node's (", node_to_be_deleted.name,") GRelation... "
del_status, del_status_msg = delete_grelation(
subject_id=node_to_be_deleted._id,
deletion_type=deletion_type
)
if not del_status:
raise Exception(del_status_msg)
delete_status_message += "\n\n" + del_status_msg
# print "\n 6* >> delete_status_message... \n", delete_status_message
# Required as below this node is getting saved and
# in above delete_gattribute() function, it's getting updated
node_to_be_deleted.reload()
# Search deleting-node's ObjectId in collection_set field and
# remove from it, if found any
res = node_collection.collection.update({
"_type": "GSystem",
"collection_set": node_to_be_deleted._id
}, {
"$pull": {"collection_set": node_to_be_deleted._id}
},
upsert=False, multi=True
)
# print "\n 7 >> collection_set : \n", res
# Search deleting-node's ObjectId in prior_node field and
# remove from it, if found any
res = node_collection.collection.update({
"_type": "GSystem", "prior_node": node_to_be_deleted._id
}, {
"$pull": {"prior_node": node_to_be_deleted._id}
},
upsert=False, multi=True
)
# print "\n 8 >> prior_node : \n", res
# Search deleting-node's ObjectId in post_node field and
# remove from it, if found any
res = node_collection.collection.update({
"_type": "GSystem", "post_node": node_to_be_deleted._id
}, {
"$pull": {"post_node": node_to_be_deleted._id}
},
upsert=False, multi=True
)
# print "\n 9 >> post_node : \n", res
# Perform normal delete on deleting-node
# Only changes the status of given node to DELETED
node_to_be_deleted.status = u"DELETED"
node_to_be_deleted.save()
# Perform Purge operation on deleting-node
if deletion_type == 1:
# Remove from database
str_deletion_type = "purged"
# If given node is of member-of File GApp
# Then remove it's references from GridFS as well
# Consider File GApp's ObjectId is there in member_of field
# print "\n node_to_be_deleted.member_of_names_list: ", node_to_be_deleted.member_of_names_list
if "File" in node_to_be_deleted.member_of_names_list:
# print "\n 10 >> node found as File; nodes in GridFS : ", len(node_to_be_deleted.fs_file_ids)
if node_to_be_deleted.fs_file_ids:
for each in node_to_be_deleted.fs_file_ids:
if node_to_be_deleted.fs.files.exists(each):
# print "\tdeleting node in GridFS : ", each
node_to_be_deleted.fs.files.delete(each)
# Finally delete the node
node_to_be_deleted.delete()
delete_status_message += "\n\n %(node_name)s (%(str_node_id)s) " % locals() \
+ "%(str_deletion_type)s successfully." % locals()
# print delete_status_message
return (True, delete_status_message)
elif collection_name == triple_collection.collection_name:
# Perform deletion operation on Triples collection
subject_id = None
underscore_type = ""
str_node_id = ""
query = {}
delete_status_message = ""
# print "\n 3 >> Triples collection..."
if deletion_type not in [0, 1]:
delete_status_message = "Must pass \"deletion_type\" agrument's " \
+ "value as either 0 (Normal delete) or 1 (Purge) !!!"
raise Exception(delete_status_message)
# print "\n 4 >> look out for subject_id..."
if "subject_id" in kwargs:
subject_id = kwargs["subject_id"]
# print "\t4a >> found subject_id...", subject_id
if (not node_id) and (not subject_id):
delete_status_message = "Value not found for neither node_id nor " \
+ "subject_id... [Expected value(s) in ObjectId format]"
raise Exception(delete_status_message)
# print "\n 5 >> look out for _type..."
if "_type" in kwargs:
underscore_type = kwargs["_type"].__str__()
# print "\t5a >> found _type...", underscore_type
if underscore_type not in ["GAttribute", "GRelation"]:
delete_status_message = "Invalid value found for _type parameter " \
+ "%(underscore_type)s... " % locals() \
+ "Please pass either GAttribute or GRelation"
raise Exception(delete_status_message)
# print "\n 5b >> convert node_id..."
if node_id:
if type(node_id) == ObjectId:
str_node_id = str(node_id)
# print "\t 5ba >> node_id -- O to s: ", type(str_node_id), " -- ", str_node_id
else:
str_node_id = node_id
if ObjectId.is_valid(node_id):
node_id = ObjectId(node_id)
# print "\t 5bb >> node_id -- s to O: ", type(str_node_id), " -- ", str_node_id
else:
delete_status_message = "Invalid value found for node_id " \
+ "(%(str_node_id)s)... [Expected value in" % locals() \
+ " ObjectId format] !!!"
raise Exception(delete_status_message)
# Forming query to delete a specific node from Triples collection
query = {"_id": node_id}
# print "\t 5bc >> query... ", query
# Fetch the deleting-node from given node_id
node_to_be_deleted = triple_collection.find_one(query)
if not node_to_be_deleted:
delete_status_message = "Node with given ObjectId " \
+ "(%(str_node_id)s) doesn't exists in Triples collection" % locals()
raise Exception(delete_status_message)
# print "\t 5bd >> node_to_be_deleted... ", node_to_be_deleted.name, " -- ", node_to_be_deleted._type
# Resetting underscore_type
# To rectify, if by mistake wrong value is set
# That is, consider _type is set as "GAttribute" (by mistake)
# but node (with node_id) represents "GRelation"
# To avoid this kind of case(s), resetting underscore_type
underscore_type = node_to_be_deleted._type
# print "\t 5be >> underscore_type set to node_to_be_deleted's _type... ", underscore_type
if underscore_type == "GAttribute":
# Delete/Purge only GAttribute node(s)
# print "\n 6 >> Delete/Purge only GAttribute node(s)..."
del_status, del_status_msg = delete_gattribute(
node_id=node_id, subject_id=subject_id,
deletion_type=deletion_type
)
if not del_status:
raise Exception(del_status_msg)
delete_status_message = del_status_msg
# print "\n 6* >> delete_status_message... \n", delete_status_message
elif underscore_type == "GRelation":
# Delete/Purge only GRelation node(s)
# print "\n 7 >> Delete/Purge only GRelation node(s)..."
del_status, del_status_msg = delete_grelation(
node_id=node_id, subject_id=subject_id,
deletion_type=deletion_type
)
if not del_status:
raise Exception(del_status_msg)
delete_status_message = del_status_msg
# print "\n 7* >> delete_status_message... \n", delete_status_message
else:
# Delete/Purge both GAttribute & GRelation node(s)
# print "\n 8 >> Delete/Purge both GAttribute & GRelation node(s)..."
# Delete/Purge GAttribute node(s)
del_status, del_status_msg = delete_gattribute(
node_id=node_id, subject_id=subject_id,
deletion_type=deletion_type
)
if not del_status:
raise Exception(del_status_msg)
delete_status_message = del_status_msg
# print "\n 8* >> delete_status_message... \n", delete_status_message
# Delete/Purge GRelation node(s)
del_status, del_status_msg = delete_grelation(
node_id=node_id, subject_id=subject_id,
deletion_type=deletion_type
)
if not del_status:
raise Exception(del_status_msg)
delete_status_message += "\n\n" + del_status_msg
# print "\n 8* >> delete_status_message... \n", delete_status_message
# Return output of the function
# print delete_status_message
return (True, delete_status_message)
else:
delete_status_message = " Invalid value (%(collection_name)s) " % locals() \
+ "found for collection_name field. Please refer function " \
+ "details for correct value !!!"
raise Exception(delete_status_message)
except Exception as e:
delete_status_message = "Error (from delete_node) :-\n" + str(e)
return (False, delete_status_message)
def repository(request, group_id):
'''
It's an NROER repository. Which will hold the list of apps.
'''
gapp_metatype = node_collection.one({"_type": "MetaType", "name": "GAPP"})
gapps_list = [i.values()[0] for i in GSTUDIO_NROER_GAPPS]
# print gapps_list
gapps_obj_list = []
for each_gapp in gapps_list:
gapp_obj = node_collection.one({ '_type':'GSystemType',
'name': {"$regex": each_gapp, "$options": "i"},
"member_of": {"$in": [gapp_metatype._id]}
})
gapps_obj_list.append(gapp_obj)
return render_to_response("ndf/repository.html",
{ "gapps_obj_list": gapps_obj_list,
"gapps_dict" : GSTUDIO_NROER_GAPPS,
'group_id': group_id, 'groupid': group_id
},
context_instance=RequestContext(request)
)
|