Best Python code snippet using playwright-python
00016_20181011-174652_vocabularies.py
Source:00016_20181011-174652_vocabularies.py
1# -*- coding: utf-8; -*-2# This file is part of Superdesk.3# For the full copyright and license information, please see the4# AUTHORS and LICENSE files distributed with this source code, or5# at https://www.sourcefabric.org/superdesk/license6#7# Author : superdesk8# Creation: 2018-10-11 17:469from superdesk.commands.data_updates import DataUpdate10class DataUpdate(DataUpdate):11 resource = 'vocabularies'12 def forwards(self, mongodb_collection, mongodb_database):13 mongodb_collection.insert(14 [15 {16 "_id": "regions",17 "helper_text": "The administrative state or region of an address",18 "schema": {19 "name": {20 },21 "qcode": {22 }23 },24 "type": "manageable",25 "display_name": "State / Region",26 "unique_field": "qcode",27 "items": [28 {29 "qcode": "NSW",30 "name": "NSW",31 "is_active": True32 },33 {34 "qcode": "VIC",35 "name": "VIC",36 "is_active": True37 },38 {39 "qcode": "TAS",40 "name": "TAS",41 "is_active": True42 },43 {44 "qcode": "WA",45 "name": "WA",46 "is_active": True47 },48 {49 "qcode": "QLD",50 "name": "QLD",51 "is_active": True52 },53 {54 "qcode": "NT",55 "name": "NT",56 "is_active": True57 },58 {59 "qcode": "ACT",60 "name": "ACT",61 "is_active": True62 }63 ]64 },65 {66 "_id": "countries",67 "schema": {68 "name": {69 },70 "qcode": {71 }72 },73 "type": "manageable",74 "display_name": "Countries",75 "unique_field": "qcode",76 "items": [77 {78 "qcode": "AFG",79 "name": "Afghanistan",80 "is_active": True81 },82 {83 "qcode": "ALA",84 "name": "Aland Islands",85 "is_active": True86 },87 {88 "qcode": "ALB",89 "name": "Albania",90 "is_active": True91 },92 {93 "qcode": "DZA",94 "name": "Algeria",95 "is_active": True96 },97 {98 "qcode": "ASM",99 "name": "American Samoa",100 "is_active": True101 },102 {103 "qcode": "AND",104 "name": "Andorra",105 "is_active": True106 },107 {108 "qcode": "AGO",109 "name": "Angola",110 "is_active": True111 },112 {113 "qcode": "AIA",114 "name": "Anguilla",115 "is_active": True116 },117 {118 "qcode": "ATA",119 "name": "Antarctica",120 "is_active": True121 },122 {123 "qcode": "ATG",124 "name": "Antigua and Barbuda",125 "is_active": True126 },127 {128 "qcode": "ARG",129 "name": "Argentina",130 "is_active": True131 },132 {133 "qcode": "ARM",134 "name": "Armenia",135 "is_active": True136 },137 {138 "qcode": "ABW",139 "name": "Aruba",140 "is_active": True141 },142 {143 "qcode": "AUS",144 "name": "Australia",145 "is_active": True146 },147 {148 "qcode": "AUT",149 "name": "Austria",150 "is_active": True151 },152 {153 "qcode": "AZE",154 "name": "Azerbaijan",155 "is_active": True156 },157 {158 "qcode": "BHS",159 "name": "Bahamas",160 "is_active": True161 },162 {163 "qcode": "BHR",164 "name": "Bahrain",165 "is_active": True166 },167 {168 "qcode": "BGD",169 "name": "Bangladesh",170 "is_active": True171 },172 {173 "qcode": "BRB",174 "name": "Barbados",175 "is_active": True176 },177 {178 "qcode": "BLR",179 "name": "Belarus",180 "is_active": True181 },182 {183 "qcode": "BEL",184 "name": "Belgium",185 "is_active": True186 },187 {188 "qcode": "BLZ",189 "name": "Belize",190 "is_active": True191 },192 {193 "qcode": "BEN",194 "name": "Benin",195 "is_active": True196 },197 {198 "qcode": "BMU",199 "name": "Bermuda",200 "is_active": True201 },202 {203 "qcode": "BTN",204 "name": "Bhutan",205 "is_active": True206 },207 {208 "qcode": "BOL",209 "name": "Bolivia",210 "is_active": True211 },212 {213 "qcode": "BIH",214 "name": "Bosnia and Herzegovina",215 "is_active": True216 },217 {218 "qcode": "BWA",219 "name": "Botswana",220 "is_active": True221 },222 {223 "qcode": "BVT",224 "name": "Bouvet Island",225 "is_active": True226 },227 {228 "qcode": "BRA",229 "name": "Brazil",230 "is_active": True231 },232 {233 "qcode": "VGB",234 "name": "British Virgin Islands",235 "is_active": True236 },237 {238 "qcode": "IOT",239 "name": "British Indian Ocean Territory",240 "is_active": True241 },242 {243 "qcode": "BRN",244 "name": "Brunei Darussalam",245 "is_active": True246 },247 {248 "qcode": "BGR",249 "name": "Bulgaria",250 "is_active": True251 },252 {253 "qcode": "BFA",254 "name": "Burkina Faso",255 "is_active": True256 },257 {258 "qcode": "BDI",259 "name": "Burundi",260 "is_active": True261 },262 {263 "qcode": "KHM",264 "name": "Cambodia",265 "is_active": True266 },267 {268 "qcode": "CMR",269 "name": "Cameroon",270 "is_active": True271 },272 {273 "qcode": "CAN",274 "name": "Canada",275 "is_active": True276 },277 {278 "qcode": "CPV",279 "name": "Cape Verde",280 "is_active": True281 },282 {283 "qcode": "CYM",284 "name": "Cayman Islands",285 "is_active": True286 },287 {288 "qcode": "CAF",289 "name": "Central African Republic",290 "is_active": True291 },292 {293 "qcode": "TCD",294 "name": "Chad",295 "is_active": True296 },297 {298 "qcode": "CHL",299 "name": "Chile",300 "is_active": True301 },302 {303 "qcode": "CHN",304 "name": "China",305 "is_active": True306 },307 {308 "qcode": "HKG",309 "name": "Hong Kong, SAR China",310 "is_active": True311 },312 {313 "qcode": "MAC",314 "name": "Macao, SAR China",315 "is_active": True316 },317 {318 "qcode": "CXR",319 "name": "Christmas Island",320 "is_active": True321 },322 {323 "qcode": "CCK",324 "name": "Cocos (Keeling) Islands",325 "is_active": True326 },327 {328 "qcode": "COL",329 "name": "Colombia",330 "is_active": True331 },332 {333 "qcode": "COM",334 "name": "Comoros",335 "is_active": True336 },337 {338 "qcode": "COG",339 "name": "Congo (Brazzaville)",340 "is_active": True341 },342 {343 "qcode": "COD",344 "name": "Congo, (Kinshasa)",345 "is_active": True346 },347 {348 "qcode": "COK",349 "name": "Cook Islands",350 "is_active": True351 },352 {353 "qcode": "CRI",354 "name": "Costa Rica",355 "is_active": True356 },357 {358 "qcode": "CIV",359 "name": "Côte d'Ivoire",360 "is_active": True361 },362 {363 "qcode": "HRV",364 "name": "Croatia",365 "is_active": True366 },367 {368 "qcode": "CUB",369 "name": "Cuba",370 "is_active": True371 },372 {373 "qcode": "CYP",374 "name": "Cyprus",375 "is_active": True376 },377 {378 "qcode": "CZE",379 "name": "Czech Republic",380 "is_active": True381 },382 {383 "qcode": "DNK",384 "name": "Denmark",385 "is_active": True386 },387 {388 "qcode": "DJI",389 "name": "Djibouti",390 "is_active": True391 },392 {393 "qcode": "DMA",394 "name": "Dominica",395 "is_active": True396 },397 {398 "qcode": "DOM",399 "name": "Dominican Republic",400 "is_active": True401 },402 {403 "qcode": "ECU",404 "name": "Ecuador",405 "is_active": True406 },407 {408 "qcode": "EGY",409 "name": "Egypt",410 "is_active": True411 },412 {413 "qcode": "SLV",414 "name": "El Salvador",415 "is_active": True416 },417 {418 "qcode": "GNQ",419 "name": "Equatorial Guinea",420 "is_active": True421 },422 {423 "qcode": "ERI",424 "name": "Eritrea",425 "is_active": True426 },427 {428 "qcode": "EST",429 "name": "Estonia",430 "is_active": True431 },432 {433 "qcode": "ETH",434 "name": "Ethiopia",435 "is_active": True436 },437 {438 "qcode": "FLK",439 "name": "Falkland Islands (Malvinas)",440 "is_active": True441 },442 {443 "qcode": "FRO",444 "name": "Faroe Islands",445 "is_active": True446 },447 {448 "qcode": "FJI",449 "name": "Fiji",450 "is_active": True451 },452 {453 "qcode": "FIN",454 "name": "Finland",455 "is_active": True456 },457 {458 "qcode": "FRA",459 "name": "France",460 "is_active": True461 },462 {463 "qcode": "GUF",464 "name": "French Guiana",465 "is_active": True466 },467 {468 "qcode": "PYF",469 "name": "French Polynesia",470 "is_active": True471 },472 {473 "qcode": "ATF",474 "name": "French Southern Territories",475 "is_active": True476 },477 {478 "qcode": "GAB",479 "name": "Gabon",480 "is_active": True481 },482 {483 "qcode": "GMB",484 "name": "Gambia",485 "is_active": True486 },487 {488 "qcode": "GEO",489 "name": "Georgia",490 "is_active": True491 },492 {493 "qcode": "DEU",494 "name": "Germany",495 "is_active": True496 },497 {498 "qcode": "GHA",499 "name": "Ghana",500 "is_active": True501 },502 {503 "qcode": "GIB",504 "name": "Gibraltar",505 "is_active": True506 },507 {508 "qcode": "GRC",509 "name": "Greece",510 "is_active": True511 },512 {513 "qcode": "GRL",514 "name": "Greenland",515 "is_active": True516 },517 {518 "qcode": "GRD",519 "name": "Grenada",520 "is_active": True521 },522 {523 "qcode": "GLP",524 "name": "Guadeloupe",525 "is_active": True526 },527 {528 "qcode": "GUM",529 "name": "Guam",530 "is_active": True531 },532 {533 "qcode": "GTM",534 "name": "Guatemala",535 "is_active": True536 },537 {538 "qcode": "GGY",539 "name": "Guernsey",540 "is_active": True541 },542 {543 "qcode": "GIN",544 "name": "Guinea",545 "is_active": True546 },547 {548 "qcode": "GNB",549 "name": "Guinea-Bissau",550 "is_active": True551 },552 {553 "qcode": "GUY",554 "name": "Guyana",555 "is_active": True556 },557 {558 "qcode": "HTI",559 "name": "Haiti",560 "is_active": True561 },562 {563 "qcode": "HMD",564 "name": "Heard and Mcdonald Islands",565 "is_active": True566 },567 {568 "qcode": "VAT",569 "name": "Holy See (Vatican City State)",570 "is_active": True571 },572 {573 "qcode": "HND",574 "name": "Honduras",575 "is_active": True576 },577 {578 "qcode": "HUN",579 "name": "Hungary",580 "is_active": True581 },582 {583 "qcode": "ISL",584 "name": "Iceland",585 "is_active": True586 },587 {588 "qcode": "IND",589 "name": "India",590 "is_active": True591 },592 {593 "qcode": "IDN",594 "name": "Indonesia",595 "is_active": True596 },597 {598 "qcode": "IRN",599 "name": "Iran, Islamic Republic of",600 "is_active": True601 },602 {603 "qcode": "IRQ",604 "name": "Iraq",605 "is_active": True606 },607 {608 "qcode": "IRL",609 "name": "Ireland",610 "is_active": True611 },612 {613 "qcode": "IMN",614 "name": "Isle of Man",615 "is_active": True616 },617 {618 "qcode": "ISR",619 "name": "Israel",620 "is_active": True621 },622 {623 "qcode": "ITA",624 "name": "Italy",625 "is_active": True626 },627 {628 "qcode": "JAM",629 "name": "Jamaica",630 "is_active": True631 },632 {633 "qcode": "JPN",634 "name": "Japan",635 "is_active": True636 },637 {638 "qcode": "JEY",639 "name": "Jersey",640 "is_active": True641 },642 {643 "qcode": "JOR",644 "name": "Jordan",645 "is_active": True646 },647 {648 "qcode": "KAZ",649 "name": "Kazakhstan",650 "is_active": True651 },652 {653 "qcode": "KEN",654 "name": "Kenya",655 "is_active": True656 },657 {658 "qcode": "KIR",659 "name": "Kiribati",660 "is_active": True661 },662 {663 "qcode": "PRK",664 "name": "Korea (North)",665 "is_active": True666 },667 {668 "qcode": "KOR",669 "name": "Korea (South)",670 "is_active": True671 },672 {673 "qcode": "KWT",674 "name": "Kuwait",675 "is_active": True676 },677 {678 "qcode": "KGZ",679 "name": "Kyrgyzstan",680 "is_active": True681 },682 {683 "qcode": "LAO",684 "name": "Lao PDR",685 "is_active": True686 },687 {688 "qcode": "LVA",689 "name": "Latvia",690 "is_active": True691 },692 {693 "qcode": "LBN",694 "name": "Lebanon",695 "is_active": True696 },697 {698 "qcode": "LSO",699 "name": "Lesotho",700 "is_active": True701 },702 {703 "qcode": "LBR",704 "name": "Liberia",705 "is_active": True706 },707 {708 "qcode": "LBY",709 "name": "Libya",710 "is_active": True711 },712 {713 "qcode": "LIE",714 "name": "Liechtenstein",715 "is_active": True716 },717 {718 "qcode": "LTU",719 "name": "Lithuania",720 "is_active": True721 },722 {723 "qcode": "LUX",724 "name": "Luxembourg",725 "is_active": True726 },727 {728 "qcode": "MKD",729 "name": "Macedonia, Republic of",730 "is_active": True731 },732 {733 "qcode": "MDG",734 "name": "Madagascar",735 "is_active": True736 },737 {738 "qcode": "MWI",739 "name": "Malawi",740 "is_active": True741 },742 {743 "qcode": "MYS",744 "name": "Malaysia",745 "is_active": True746 },747 {748 "qcode": "MDV",749 "name": "Maldives",750 "is_active": True751 },752 {753 "qcode": "MLI",754 "name": "Mali",755 "is_active": True756 },757 {758 "qcode": "MLT",759 "name": "Malta",760 "is_active": True761 },762 {763 "qcode": "MHL",764 "name": "Marshall Islands",765 "is_active": True766 },767 {768 "qcode": "MTQ",769 "name": "Martinique",770 "is_active": True771 },772 {773 "qcode": "MRT",774 "name": "Mauritania",775 "is_active": True776 },777 {778 "qcode": "MUS",779 "name": "Mauritius",780 "is_active": True781 },782 {783 "qcode": "MYT",784 "name": "Mayotte",785 "is_active": True786 },787 {788 "qcode": "MEX",789 "name": "Mexico",790 "is_active": True791 },792 {793 "qcode": "FSM",794 "name": "Micronesia, Federated States of",795 "is_active": True796 },797 {798 "qcode": "MDA",799 "name": "Moldova",800 "is_active": True801 },802 {803 "qcode": "MCO",804 "name": "Monaco",805 "is_active": True806 },807 {808 "qcode": "MNG",809 "name": "Mongolia",810 "is_active": True811 },812 {813 "qcode": "MNE",814 "name": "Montenegro",815 "is_active": True816 },817 {818 "qcode": "MSR",819 "name": "Montserrat",820 "is_active": True821 },822 {823 "qcode": "MAR",824 "name": "Morocco",825 "is_active": True826 },827 {828 "qcode": "MOZ",829 "name": "Mozambique",830 "is_active": True831 },832 {833 "qcode": "MMR",834 "name": "Myanmar",835 "is_active": True836 },837 {838 "qcode": "NAM",839 "name": "Namibia",840 "is_active": True841 },842 {843 "qcode": "NRU",844 "name": "Nauru",845 "is_active": True846 },847 {848 "qcode": "NPL",849 "name": "Nepal",850 "is_active": True851 },852 {853 "qcode": "NLD",854 "name": "Netherlands",855 "is_active": True856 },857 {858 "qcode": "ANT",859 "name": "Netherlands Antilles",860 "is_active": True861 },862 {863 "qcode": "NCL",864 "name": "New Caledonia",865 "is_active": True866 },867 {868 "qcode": "NZL",869 "name": "New Zealand",870 "is_active": True871 },872 {873 "qcode": "NIC",874 "name": "Nicaragua",875 "is_active": True876 },877 {878 "qcode": "NER",879 "name": "Niger",880 "is_active": True881 },882 {883 "qcode": "NGA",884 "name": "Nigeria",885 "is_active": True886 },887 {888 "qcode": "NIU",889 "name": "Niue",890 "is_active": True891 },892 {893 "qcode": "NFK",894 "name": "Norfolk Island",895 "is_active": True896 },897 {898 "qcode": "MNP",899 "name": "Northern Mariana Islands",900 "is_active": True901 },902 {903 "qcode": "NOR",904 "name": "Norway",905 "is_active": True906 },907 {908 "qcode": "OMN",909 "name": "Oman",910 "is_active": True911 },912 {913 "qcode": "PAK",914 "name": "Pakistan",915 "is_active": True916 },917 {918 "qcode": "PLW",919 "name": "Palau",920 "is_active": True921 },922 {923 "qcode": "PSE",924 "name": "Palestinian Territory",925 "is_active": True926 },927 {928 "qcode": "PAN",929 "name": "Panama",930 "is_active": True931 },932 {933 "qcode": "PNG",934 "name": "Papua New Guinea",935 "is_active": True936 },937 {938 "qcode": "PRY",939 "name": "Paraguay",940 "is_active": True941 },942 {943 "qcode": "PER",944 "name": "Peru",945 "is_active": True946 },947 {948 "qcode": "PHL",949 "name": "Philippines",950 "is_active": True951 },952 {953 "qcode": "PCN",954 "name": "Pitcairn",955 "is_active": True956 },957 {958 "qcode": "POL",959 "name": "Poland",960 "is_active": True961 },962 {963 "qcode": "PRT",964 "name": "Portugal",965 "is_active": True966 },967 {968 "qcode": "PRI",969 "name": "Puerto Rico",970 "is_active": True971 },972 {973 "qcode": "QAT",974 "name": "Qatar",975 "is_active": True976 },977 {978 "qcode": "REU",979 "name": "Réunion",980 "is_active": True981 },982 {983 "qcode": "ROU",984 "name": "Romania",985 "is_active": True986 },987 {988 "qcode": "RUS",989 "name": "Russian Federation",990 "is_active": True991 },992 {993 "qcode": "RWA",994 "name": "Rwanda",995 "is_active": True996 },997 {998 "qcode": "BLM",999 "name": "Saint-Barthélemy",1000 "is_active": True1001 },1002 {1003 "qcode": "SHN",1004 "name": "Saint Helena",1005 "is_active": True1006 },1007 {1008 "qcode": "KNA",1009 "name": "Saint Kitts and Nevis",1010 "is_active": True1011 },1012 {1013 "qcode": "LCA",1014 "name": "Saint Lucia",1015 "is_active": True1016 },1017 {1018 "qcode": "MAF",1019 "name": "Saint-Martin (French part)",1020 "is_active": True1021 },1022 {1023 "qcode": "SPM",1024 "name": "Saint Pierre and Miquelon",1025 "is_active": True1026 },1027 {1028 "qcode": "VCT",1029 "name": "Saint Vincent and Grenadines",1030 "is_active": True1031 },1032 {1033 "qcode": "WSM",1034 "name": "Samoa",1035 "is_active": True1036 },1037 {1038 "qcode": "SMR",1039 "name": "San Marino",1040 "is_active": True1041 },1042 {1043 "qcode": "STP",1044 "name": "Sao Tome and Principe",1045 "is_active": True1046 },1047 {1048 "qcode": "SAU",1049 "name": "Saudi Arabia",1050 "is_active": True1051 },1052 {1053 "qcode": "SEN",1054 "name": "Senegal",1055 "is_active": True1056 },1057 {1058 "qcode": "SRB",1059 "name": "Serbia",1060 "is_active": True1061 },1062 {1063 "qcode": "SYC",1064 "name": "Seychelles",1065 "is_active": True1066 },1067 {1068 "qcode": "SLE",1069 "name": "Sierra Leone",1070 "is_active": True1071 },1072 {1073 "qcode": "SGP",1074 "name": "Singapore",1075 "is_active": True1076 },1077 {1078 "qcode": "SVK",1079 "name": "Slovakia",1080 "is_active": True1081 },1082 {1083 "qcode": "SVN",1084 "name": "Slovenia",1085 "is_active": True1086 },1087 {1088 "qcode": "SLB",1089 "name": "Solomon Islands",1090 "is_active": True1091 },1092 {1093 "qcode": "SOM",1094 "name": "Somalia",1095 "is_active": True1096 },1097 {1098 "qcode": "ZAF",1099 "name": "South Africa",1100 "is_active": True1101 },1102 {1103 "qcode": "SGS",1104 "name": "South Georgia and the South Sandwich Islands",1105 "is_active": True1106 },1107 {1108 "qcode": "SSD",1109 "name": "South Sudan",1110 "is_active": True1111 },1112 {1113 "qcode": "ESP",1114 "name": "Spain",1115 "is_active": True1116 },1117 {1118 "qcode": "LKA",1119 "name": "Sri Lanka",1120 "is_active": True1121 },1122 {1123 "qcode": "SDN",1124 "name": "Sudan",1125 "is_active": True1126 },1127 {1128 "qcode": "SUR",1129 "name": "Suriname",1130 "is_active": True1131 },1132 {1133 "qcode": "SJM",1134 "name": "Svalbard and Jan Mayen Islands",1135 "is_active": True1136 },1137 {1138 "qcode": "SWZ",1139 "name": "Swaziland",1140 "is_active": True1141 },1142 {1143 "qcode": "SWE",1144 "name": "Sweden",1145 "is_active": True1146 },1147 {1148 "qcode": "CHE",1149 "name": "Switzerland",1150 "is_active": True1151 },1152 {1153 "qcode": "SYR",1154 "name": "Syrian Arab Republic (Syria)",1155 "is_active": True1156 },1157 {1158 "qcode": "TWN",1159 "name": "Taiwan, Republic of China",1160 "is_active": True1161 },1162 {1163 "qcode": "TJK",1164 "name": "Tajikistan",1165 "is_active": True1166 },1167 {1168 "qcode": "TZA",1169 "name": "Tanzania, United Republic of",1170 "is_active": True1171 },1172 {1173 "qcode": "THA",1174 "name": "Thailand",1175 "is_active": True1176 },1177 {1178 "qcode": "TLS",1179 "name": "Timor-Leste",1180 "is_active": True1181 },1182 {1183 "qcode": "TGO",1184 "name": "Togo",1185 "is_active": True1186 },1187 {1188 "qcode": "TKL",1189 "name": "Tokelau",1190 "is_active": True1191 },1192 {1193 "qcode": "TON",1194 "name": "Tonga",1195 "is_active": True1196 },1197 {1198 "qcode": "TTO",1199 "name": "Trinidad and Tobago",1200 "is_active": True1201 },1202 {1203 "qcode": "TUN",1204 "name": "Tunisia",1205 "is_active": True1206 },1207 {1208 "qcode": "TUR",1209 "name": "Turkey",1210 "is_active": True1211 },1212 {1213 "qcode": "TKM",1214 "name": "Turkmenistan",1215 "is_active": True1216 },1217 {1218 "qcode": "TCA",1219 "name": "Turks and Caicos Islands",1220 "is_active": True1221 },1222 {1223 "qcode": "TUV",1224 "name": "Tuvalu",1225 "is_active": True1226 },1227 {1228 "qcode": "UGA",1229 "name": "Uganda",1230 "is_active": True1231 },1232 {1233 "qcode": "UKR",1234 "name": "Ukraine",1235 "is_active": True1236 },1237 {1238 "qcode": "ARE",1239 "name": "United Arab Emirates",1240 "is_active": True1241 },1242 {1243 "qcode": "GBR",1244 "name": "United Kingdom",1245 "is_active": True1246 },1247 {1248 "qcode": "USA",1249 "name": "United States of America",1250 "is_active": True1251 },1252 {1253 "qcode": "UMI",1254 "name": "US Minor Outlying Islands",1255 "is_active": True1256 },1257 {1258 "qcode": "URY",1259 "name": "Uruguay",1260 "is_active": True1261 },1262 {1263 "qcode": "UZB",1264 "name": "Uzbekistan",1265 "is_active": True1266 },1267 {1268 "qcode": "VUT",1269 "name": "Vanuatu",1270 "is_active": True1271 },1272 {1273 "qcode": "VEN",1274 "name": "Venezuela (Bolivarian Republic)",1275 "is_active": True1276 },1277 {1278 "qcode": "VNM",1279 "name": "Viet Nam",1280 "is_active": True1281 },1282 {1283 "qcode": "VIR",1284 "name": "Virgin Islands, US",1285 "is_active": True1286 },1287 {1288 "qcode": "WLF",1289 "name": "Wallis and Futuna Islands",1290 "is_active": True1291 },1292 {1293 "qcode": "ESH",1294 "name": "Western Sahara",1295 "is_active": True1296 },1297 {1298 "qcode": "YEM",1299 "name": "Yemen",1300 "is_active": True1301 },1302 {1303 "qcode": "ZMB",1304 "name": "Zambia",1305 "is_active": True1306 },1307 {1308 "qcode": "ZWE",1309 "name": "Zimbabwe",1310 "is_active": True1311 }1312 ]1313 }1314 ]1315 )1316 mongodb_collection.remove("contact_states")1317 def backwards(self, mongodb_collection, mongodb_database):1318 mongodb_collection.remove("regions")1319 mongodb_collection.remove("countries")1320 mongodb_collection.insert(1321 [1322 {1323 "_id": "contact_states",1324 "helper_text": "The administrative state or region of an address",1325 "schema": {1326 "name": {1327 },1328 "qcode": {1329 }1330 },1331 "type": "manageable",1332 "display_name": "State / Region",1333 "unique_field": "qcode",1334 "items": [1335 {1336 "qcode": "NSW",1337 "name": "NSW",1338 "is_active": True1339 },1340 {1341 "qcode": "VIC",1342 "name": "VIC",1343 "is_active": True1344 },1345 {1346 "qcode": "TAS",1347 "name": "TAS",1348 "is_active": True1349 },1350 {1351 "qcode": "WA",1352 "name": "WA",1353 "is_active": True1354 },1355 {1356 "qcode": "QLD",1357 "name": "QLD",1358 "is_active": True1359 },1360 {1361 "qcode": "NT",1362 "name": "NT",1363 "is_active": True1364 },1365 {1366 "qcode": "ACT",1367 "name": "ACT",1368 "is_active": True1369 }1370 ]1371 }1372 ]...
binary.py
Source:binary.py
...44 return self.__class__(self.a.copy(), self.b.copy())45 def depth(self):46 return 1 + max(self.a.depth(), self.b.depth())47 def skip_to(self, id):48 if not self.is_active():49 raise mcore.ReadTooFar50 ra = self.a.skip_to(id)51 rb = self.b.skip_to(id)52 return ra or rb53 def supports_block_quality(self):54 return (self.a.supports_block_quality()55 and self.b.supports_block_quality())56 def supports(self, astype):57 return self.a.supports(astype) and self.b.supports(astype)58class AdditiveBiMatcher(BiMatcher):59 """Base class for binary matchers where the scores of the sub-matchers are60 added together.61 """62 def max_quality(self):63 q = 0.064 if self.a.is_active():65 q += self.a.max_quality()66 if self.b.is_active():67 q += self.b.max_quality()68 return q69 def block_quality(self):70 bq = 0.071 if self.a.is_active():72 bq += self.a.block_quality()73 if self.b.is_active():74 bq += self.b.block_quality()75 return bq76 def weight(self):77 return (self.a.weight() + self.b.weight())78 def score(self):79 return (self.a.score() + self.b.score())80 def __eq__(self, other):81 return self.__class__ is type(other)82 def __lt__(self, other):83 return type(other) is self.__class__84 def __ne__(self, other):85 return not self.__eq__(other)86 def __gt__(self, other):87 return not (self.__lt__(other) or self.__eq__(other))88 def __le__(self, other):89 return self.__eq__(other) or self.__lt__(other)90 def __ge__(self, other):91 return self.__eq__(other) or self.__gt__(other)92class UnionMatcher(AdditiveBiMatcher):93 """Matches the union (OR) of the postings in the two sub-matchers.94 """95 _id = None96 def replace(self, minquality=0):97 a = self.a98 b = self.b99 a_active = a.is_active()100 b_active = b.is_active()101 # If neither sub-matcher on its own has a high enough max quality to102 # contribute, convert to an intersection matcher103 if minquality and a_active and b_active:104 a_max = a.max_quality()105 b_max = b.max_quality()106 if a_max < minquality and b_max < minquality:107 return IntersectionMatcher(a, b).replace(minquality)108 elif a_max < minquality:109 return AndMaybeMatcher(b, a)110 elif b_max < minquality:111 return AndMaybeMatcher(a, b)112 # If one or both of the sub-matchers are inactive, convert113 if not (a_active or b_active):114 return mcore.NullMatcher()115 elif not a_active:116 return b.replace(minquality)117 elif not b_active:118 return a.replace(minquality)119 a = a.replace(minquality - b.max_quality() if minquality else 0)120 b = b.replace(minquality - a.max_quality() if minquality else 0)121 # If one of the sub-matchers changed, return a new union122 if a is not self.a or b is not self.b:123 return self.__class__(a, b)124 else:125 self._id = None126 return self127 def is_active(self):128 return self.a.is_active() or self.b.is_active()129 def skip_to(self, id):130 self._id = None131 ra = rb = False132 if self.a.is_active():133 ra = self.a.skip_to(id)134 if self.b.is_active():135 rb = self.b.skip_to(id)136 return ra or rb137 def id(self):138 _id = self._id139 if _id is not None:140 return _id141 a = self.a142 b = self.b143 if not a.is_active():144 _id = b.id()145 elif not b.is_active():146 _id = a.id()147 else:148 _id = min(a.id(), b.id())149 self._id = _id150 return _id151 # Using sets is faster in most cases, but could potentially use a lot of152 # memory. Comment out this method override to not use sets.153 #def all_ids(self):154 # return iter(sorted(set(self.a.all_ids()) | set(self.b.all_ids())))155 def next(self):156 self._id = None157 a = self.a158 b = self.b159 a_active = a.is_active()160 b_active = b.is_active()161 # Shortcut when one matcher is inactive162 if not (a_active or b_active):163 raise mcore.ReadTooFar164 elif not a_active:165 return b.next()166 elif not b_active:167 return a.next()168 a_id = a.id()169 b_id = b.id()170 ar = br = None171 # After all that, here's the actual implementation172 if a_id <= b_id:173 ar = a.next()174 if b_id <= a_id:175 br = b.next()176 return ar or br177 def spans(self):178 if not self.a.is_active():179 return self.b.spans()180 if not self.b.is_active():181 return self.a.spans()182 id_a = self.a.id()183 id_b = self.b.id()184 if id_a < id_b:185 return self.a.spans()186 elif id_b < id_a:187 return self.b.spans()188 else:189 return sorted(set(self.a.spans()) | set(self.b.spans()))190 def weight(self):191 a = self.a192 b = self.b193 if not a.is_active():194 return b.weight()195 if not b.is_active():196 return a.weight()197 id_a = a.id()198 id_b = b.id()199 if id_a < id_b:200 return a.weight()201 elif id_b < id_a:202 return b.weight()203 else:204 return (a.weight() + b.weight())205 def score(self):206 a = self.a207 b = self.b208 if not a.is_active():209 return b.score()210 if not b.is_active():211 return a.score()212 id_a = a.id()213 id_b = b.id()214 if id_a < id_b:215 return a.score()216 elif id_b < id_a:217 return b.score()218 else:219 return (a.score() + b.score())220 def skip_to_quality(self, minquality):221 self._id = None222 a = self.a223 b = self.b224 if not (a.is_active() or b.is_active()):225 raise mcore.ReadTooFar226 # Short circuit if one matcher is inactive227 if not a.is_active():228 return b.skip_to_quality(minquality)229 elif not b.is_active():230 return a.skip_to_quality(minquality)231 skipped = 0232 aq = a.block_quality()233 bq = b.block_quality()234 while a.is_active() and b.is_active() and aq + bq <= minquality:235 if aq < bq:236 skipped += a.skip_to_quality(minquality - bq)237 aq = a.block_quality()238 else:239 skipped += b.skip_to_quality(minquality - aq)240 bq = b.block_quality()241 return skipped242class DisjunctionMaxMatcher(UnionMatcher):243 """Matches the union (OR) of two sub-matchers. Where both sub-matchers244 match the same posting, returns the weight/score of the higher-scoring245 posting.246 """247 # TODO: this class inherits from AdditiveBiMatcher (through UnionMatcher)248 # but it does not add the scores of the sub-matchers together (it249 # overrides all methods that perform addition). Need to clean up the250 # inheritance.251 def __init__(self, a, b, tiebreak=0.0):252 super(DisjunctionMaxMatcher, self).__init__(a, b)253 self.tiebreak = tiebreak254 def copy(self):255 return self.__class__(self.a.copy(), self.b.copy(),256 tiebreak=self.tiebreak)257 def replace(self, minquality=0):258 a = self.a259 b = self.b260 a_active = a.is_active()261 b_active = b.is_active()262 # DisMax takes the max of the sub-matcher qualities instead of adding263 # them, so we need special logic here264 if minquality and a_active and b_active:265 a_max = a.max_quality()266 b_max = b.max_quality()267 if a_max < minquality and b_max < minquality:268 # If neither sub-matcher has a high enough max quality to269 # contribute, return an inactive matcher270 return mcore.NullMatcher()271 elif b_max < minquality:272 # If the b matcher can't contribute, return a273 return a.replace(minquality)274 elif a_max < minquality:275 # If the a matcher can't contribute, return b276 return b.replace(minquality)277 if not (a_active or b_active):278 return mcore.NullMatcher()279 elif not a_active:280 return b.replace(minquality)281 elif not b_active:282 return a.replace(minquality)283 # We CAN pass the minquality down here, since we don't add the two284 # scores together285 a = a.replace(minquality)286 b = b.replace(minquality)287 a_active = a.is_active()288 b_active = b.is_active()289 # It's kind of tedious to check for inactive sub-matchers all over290 # again here after we replace them, but it's probably better than291 # returning a replacement with an inactive sub-matcher292 if not (a_active and b_active):293 return mcore.NullMatcher()294 elif not a_active:295 return b296 elif not b_active:297 return a298 elif a is not self.a or b is not self.b:299 # If one of the sub-matchers changed, return a new DisMax300 return self.__class__(a, b)301 else:302 return self303 def score(self):304 if not self.a.is_active():305 return self.b.score()306 elif not self.b.is_active():307 return self.a.score()308 else:309 return max(self.a.score(), self.b.score())310 def max_quality(self):311 return max(self.a.max_quality(), self.b.max_quality())312 def block_quality(self):313 return max(self.a.block_quality(), self.b.block_quality())314 def skip_to_quality(self, minquality):315 a = self.a316 b = self.b317 # Short circuit if one matcher is inactive318 if not a.is_active():319 sk = b.skip_to_quality(minquality)320 return sk321 elif not b.is_active():322 return a.skip_to_quality(minquality)323 skipped = 0324 aq = a.block_quality()325 bq = b.block_quality()326 while a.is_active() and b.is_active() and max(aq, bq) <= minquality:327 if aq <= minquality:328 skipped += a.skip_to_quality(minquality)329 aq = a.block_quality()330 if bq <= minquality:331 skipped += b.skip_to_quality(minquality)332 bq = b.block_quality()333 return skipped334class IntersectionMatcher(AdditiveBiMatcher):335 """Matches the intersection (AND) of the postings in the two sub-matchers.336 """337 def __init__(self, a, b):338 super(IntersectionMatcher, self).__init__(a, b)339 self._find_first()340 def reset(self):341 self.a.reset()342 self.b.reset()343 self._find_first()344 def _find_first(self):345 if (self.a.is_active()346 and self.b.is_active()347 and self.a.id() != self.b.id()):348 self._find_next()349 def replace(self, minquality=0):350 a = self.a351 b = self.b352 a_active = a.is_active()353 b_active = b.is_active()354 if not (a_active and b_active):355 # Intersection matcher requires that both sub-matchers be active356 return mcore.NullMatcher()357 if minquality:358 a_max = a.max_quality()359 b_max = b.max_quality()360 if a_max + b_max < minquality:361 # If the combined quality of the sub-matchers can't contribute,362 # return an inactive matcher363 return mcore.NullMatcher()364 # Require that the replacements be able to contribute results365 # higher than the minquality366 a_min = minquality - b_max367 b_min = minquality - a_max368 else:369 a_min = b_min = 0370 a = a.replace(a_min)371 b = b.replace(b_min)372 a_active = a.is_active()373 b_active = b.is_active()374 if not (a_active or b_active):375 return mcore.NullMatcher()376 elif not a_active:377 return b378 elif not b_active:379 return a380 elif a is not self.a or b is not self.b:381 return self.__class__(a, b)382 else:383 return self384 def is_active(self):385 return self.a.is_active() and self.b.is_active()386 def _find_next(self):387 a = self.a388 b = self.b389 a_id = a.id()390 b_id = b.id()391 assert a_id != b_id392 r = False393 while a.is_active() and b.is_active() and a_id != b_id:394 if a_id < b_id:395 ra = a.skip_to(b_id)396 if not a.is_active():397 return398 r = r or ra399 a_id = a.id()400 else:401 rb = b.skip_to(a_id)402 if not b.is_active():403 return404 r = r or rb405 b_id = b.id()406 return r407 def id(self):408 return self.a.id()409 # Using sets is faster in some cases, but could potentially use a lot of410 # memory411 def all_ids(self):412 return iter(sorted(set(self.a.all_ids()) & set(self.b.all_ids())))413 def skip_to(self, id):414 if not self.is_active():415 raise mcore.ReadTooFar416 ra = self.a.skip_to(id)417 rb = self.b.skip_to(id)418 if self.is_active():419 rn = False420 if self.a.id() != self.b.id():421 rn = self._find_next()422 return ra or rb or rn423 def skip_to_quality(self, minquality):424 a = self.a425 b = self.b426 minquality = minquality427 skipped = 0428 aq = a.block_quality()429 bq = b.block_quality()430 while a.is_active() and b.is_active() and aq + bq <= minquality:431 if aq < bq:432 # If the block quality of A is less than B, skip A ahead until433 # it can contribute at least the balance of the required min434 # quality when added to B435 sk = a.skip_to_quality(minquality - bq)436 skipped += sk437 if not sk and a.is_active():438 # The matcher couldn't skip ahead for some reason, so just439 # advance and try again440 a.next()441 else:442 # And vice-versa443 sk = b.skip_to_quality(minquality - aq)444 skipped += sk445 if not sk and b.is_active():446 b.next()447 if not a.is_active() or not b.is_active():448 # One of the matchers is exhausted449 break450 if a.id() != b.id():451 # We want to always leave in a state where the matchers are at452 # the same document, so call _find_next() to sync them453 self._find_next()454 # Get the block qualities at the new matcher positions455 aq = a.block_quality()456 bq = b.block_quality()457 return skipped458 def next(self):459 if not self.is_active():460 raise mcore.ReadTooFar461 # We must assume that the ids are equal whenever next() is called (they462 # should have been made equal by _find_next), so advance them both463 ar = self.a.next()464 if self.is_active():465 nr = self._find_next()466 return ar or nr467 def spans(self):468 return sorted(set(self.a.spans()) | set(self.b.spans()))469class AndNotMatcher(BiMatcher):470 """Matches the postings in the first sub-matcher that are NOT present in471 the second sub-matcher.472 """473 def __init__(self, a, b):474 super(AndNotMatcher, self).__init__(a, b)475 self._find_first()476 def reset(self):477 self.a.reset()478 self.b.reset()479 self._find_first()480 def _find_first(self):481 if (self.a.is_active()482 and self.b.is_active()483 and self.a.id() == self.b.id()):484 self._find_next()485 def is_active(self):486 return self.a.is_active()487 def _find_next(self):488 pos = self.a489 neg = self.b490 if not neg.is_active():491 return492 pos_id = pos.id()493 r = False494 if neg.id() < pos_id:495 neg.skip_to(pos_id)496 while pos.is_active() and neg.is_active() and pos_id == neg.id():497 nr = pos.next()498 if not pos.is_active():499 break500 r = r or nr501 pos_id = pos.id()502 neg.skip_to(pos_id)503 return r504 def supports_block_quality(self):505 return self.a.supports_block_quality()506 def replace(self, minquality=0):507 if not self.a.is_active():508 # The a matcher is required, so if it's inactive, return an509 # inactive matcher510 return mcore.NullMatcher()511 elif (minquality512 and self.a.max_quality() < minquality):513 # If the quality of the required matcher isn't high enough to514 # contribute, return an inactive matcher515 return mcore.NullMatcher()516 elif not self.b.is_active():517 # If the prohibited matcher is inactive, convert to just the518 # required matcher519 return self.a.replace(minquality)520 a = self.a.replace(minquality)521 b = self.b.replace()522 if a is not self.a or b is not self.b:523 # If one of the sub-matchers was replaced, return a new AndNot524 return self.__class__(a, b)525 else:526 return self527 def max_quality(self):528 return self.a.max_quality()529 def block_quality(self):530 return self.a.block_quality()531 def skip_to_quality(self, minquality):532 skipped = self.a.skip_to_quality(minquality)533 self._find_next()534 return skipped535 def id(self):536 return self.a.id()537 def next(self):538 if not self.a.is_active():539 raise mcore.ReadTooFar540 ar = self.a.next()541 nr = False542 if self.a.is_active() and self.b.is_active():543 nr = self._find_next()544 return ar or nr545 def skip_to(self, id):546 if not self.a.is_active():547 raise mcore.ReadTooFar548 if id < self.a.id():549 return550 self.a.skip_to(id)551 if self.b.is_active():552 self.b.skip_to(id)553 self._find_next()554 def weight(self):555 return self.a.weight()556 def score(self):557 return self.a.score()558 def supports(self, astype):559 return self.a.supports(astype)560 def value(self):561 return self.a.value()562 def value_as(self, astype):563 return self.a.value_as(astype)564class AndMaybeMatcher(AdditiveBiMatcher):565 """Matches postings in the first sub-matcher, and if the same posting is566 in the second sub-matcher, adds their scores.567 """568 def __init__(self, a, b):569 AdditiveBiMatcher.__init__(self, a, b)570 self._first_b()571 def reset(self):572 self.a.reset()573 self.b.reset()574 self._first_b()575 def _first_b(self):576 a = self.a577 b = self.b578 if a.is_active() and b.is_active() and a.id() != b.id():579 b.skip_to(a.id())580 def is_active(self):581 return self.a.is_active()582 def id(self):583 return self.a.id()584 def next(self):585 if not self.a.is_active():586 raise mcore.ReadTooFar587 ar = self.a.next()588 br = False589 if self.a.is_active() and self.b.is_active():590 br = self.b.skip_to(self.a.id())591 return ar or br592 def skip_to(self, id):593 if not self.a.is_active():594 raise mcore.ReadTooFar595 ra = self.a.skip_to(id)596 rb = False597 if self.a.is_active() and self.b.is_active():598 rb = self.b.skip_to(id)599 return ra or rb600 def replace(self, minquality=0):601 a = self.a602 b = self.b603 a_active = a.is_active()604 b_active = b.is_active()605 if not a_active:606 return mcore.NullMatcher()607 elif minquality and b_active:608 if a.max_quality() + b.max_quality() < minquality:609 # If the combined max quality of the sub-matchers isn't high610 # enough to possibly contribute, return an inactive matcher611 return mcore.NullMatcher()612 elif a.max_quality() < minquality:613 # If the max quality of the main sub-matcher isn't high enough614 # to ever contribute without the optional sub- matcher, change615 # into an IntersectionMatcher616 return IntersectionMatcher(self.a, self.b)617 elif not b_active:618 return a.replace(minquality)619 new_a = a.replace(minquality - b.max_quality())620 new_b = b.replace(minquality - a.max_quality())621 if new_a is not a or new_b is not b:622 # If one of the sub-matchers changed, return a new AndMaybe623 return self.__class__(new_a, new_b)624 else:625 return self626 def skip_to_quality(self, minquality):627 a = self.a628 b = self.b629 minquality = minquality630 if not a.is_active():631 raise mcore.ReadTooFar632 if not b.is_active():633 return a.skip_to_quality(minquality)634 skipped = 0635 aq = a.block_quality()636 bq = b.block_quality()637 while a.is_active() and b.is_active() and aq + bq <= minquality:638 if aq < bq:639 skipped += a.skip_to_quality(minquality - bq)640 aq = a.block_quality()641 else:642 skipped += b.skip_to_quality(minquality - aq)643 bq = b.block_quality()644 return skipped645 def weight(self):646 if self.a.id() == self.b.id():647 return self.a.weight() + self.b.weight()648 else:649 return self.a.weight()650 def score(self):651 if self.b.is_active() and self.a.id() == self.b.id():652 return self.a.score() + self.b.score()653 else:654 return self.a.score()655 def supports(self, astype):656 return self.a.supports(astype)657 def value(self):658 return self.a.value()659 def value_as(self, astype):...
test_sa_transaction.py
Source:test_sa_transaction.py
1import functools2import unittest3from unittest import mock4import pytest5from sqlalchemy import MetaData, Table, Column, Integer, String6from aiomysql import sa7meta = MetaData()8tbl = Table('sa_tbl2', meta,9 Column('id', Integer, nullable=False,10 primary_key=True),11 Column('name', String(255)))12def check_prepared_transactions(func):13 @functools.wraps(func)14 async def wrapper(self):15 conn = await self.loop.run_until_complete(self._connect())16 val = await conn.scalar('show max_prepared_transactions')17 if not val:18 raise unittest.SkipTest('Twophase transacions are not supported. '19 'Set max_prepared_transactions to '20 'a nonzero value')21 return func(self)22 return wrapper23async def start(conn):24 await conn.execute("DROP TABLE IF EXISTS sa_tbl2")25 await conn.execute("CREATE TABLE sa_tbl2 "26 "(id serial, name varchar(255))")27 await conn.execute("INSERT INTO sa_tbl2 (name)"28 "VALUES ('first')")29 await conn._connection.commit()30@pytest.fixture()31def sa_connect(connection, connection_creator):32 async def _connect(**kwargs):33 conn = await connection_creator(**kwargs)34 # TODO: fix this, should autocommit be enabled by default?35 await conn.autocommit(True)36 engine = mock.Mock()37 engine.dialect = sa.engine._dialect38 def release(*args):39 return40 engine.release = release41 ret = sa.SAConnection(conn, engine)42 return ret43 return _connect44async def test_without_transactions(sa_connect):45 conn1 = await sa_connect()46 await start(conn1)47 conn2 = await sa_connect()48 res1 = await conn1.scalar(tbl.count())49 assert 1 == res150 await conn2.execute(tbl.delete())51 res2 = await conn1.scalar(tbl.count())52 assert 0 == res253 await conn1.close()54 await conn2.close()55async def test_connection_attr(sa_connect):56 conn = await sa_connect()57 await start(conn)58 tr = await conn.begin()59 assert tr.connection is conn60 await conn.close()61async def test_root_transaction(sa_connect):62 conn1 = await sa_connect()63 await start(conn1)64 conn2 = await sa_connect()65 tr = await conn1.begin()66 assert tr.is_active67 await conn1.execute(tbl.delete())68 res1 = await conn2.scalar(tbl.count())69 assert 1 == res170 await tr.commit()71 assert not tr.is_active72 assert not conn1.in_transaction73 res2 = await conn2.scalar(tbl.count())74 assert 0 == res275 await conn1.close()76 await conn2.close()77async def test_root_transaction_rollback(sa_connect):78 conn1 = await sa_connect()79 await start(conn1)80 conn2 = await sa_connect()81 tr = await conn1.begin()82 assert tr.is_active83 await conn1.execute(tbl.delete())84 res1 = await conn2.scalar(tbl.count())85 assert 1 == res186 await tr.rollback()87 assert not tr.is_active88 res2 = await conn2.scalar(tbl.count())89 assert 1 == res290 await conn1.close()91 await conn2.close()92async def test_root_transaction_close(sa_connect):93 conn1 = await sa_connect()94 await start(conn1)95 conn2 = await sa_connect()96 tr = await conn1.begin()97 assert tr.is_active98 await conn1.execute(tbl.delete())99 res1 = await conn2.scalar(tbl.count())100 assert 1 == res1101 await tr.close()102 assert not tr.is_active103 res2 = await conn2.scalar(tbl.count())104 assert 1 == res2105 await conn1.close()106 await conn2.close()107async def test_rollback_on_connection_close(sa_connect):108 conn1 = await sa_connect()109 await start(conn1)110 conn2 = await sa_connect()111 tr = await conn1.begin()112 await conn1.execute(tbl.delete())113 res1 = await conn2.scalar(tbl.count())114 assert 1 == res1115 await conn1.close()116 res2 = await conn2.scalar(tbl.count())117 assert 1 == res2118 del tr119 await conn1.close()120 await conn2.close()121async def test_root_transaction_commit_inactive(sa_connect):122 conn = await sa_connect()123 await start(conn)124 tr = await conn.begin()125 assert tr.is_active126 await tr.commit()127 assert not tr.is_active128 with pytest.raises(sa.InvalidRequestError):129 await tr.commit()130 await conn.close()131async def test_root_transaction_rollback_inactive(sa_connect):132 conn = await sa_connect()133 await start(conn)134 tr = await conn.begin()135 assert tr.is_active136 await tr.rollback()137 assert not tr.is_active138 await tr.rollback()139 assert not tr.is_active140 await conn.close()141async def test_root_transaction_double_close(sa_connect):142 conn = await sa_connect()143 await start(conn)144 tr = await conn.begin()145 assert tr.is_active146 await tr.close()147 assert not tr.is_active148 await tr.close()149 assert not tr.is_active150 await conn.close()151async def test_inner_transaction_commit(sa_connect):152 conn = await sa_connect()153 await start(conn)154 tr1 = await conn.begin()155 tr2 = await conn.begin()156 assert tr2.is_active157 await tr2.commit()158 assert not tr2.is_active159 assert tr1.is_active160 await tr1.commit()161 assert not tr2.is_active162 assert not tr1.is_active163 await conn.close()164async def test_inner_transaction_rollback(sa_connect):165 conn = await sa_connect()166 await start(conn)167 tr1 = await conn.begin()168 tr2 = await conn.begin()169 assert tr2.is_active170 await conn.execute(tbl.insert().values(name='aaaa'))171 await tr2.rollback()172 assert not tr2.is_active173 assert not tr1.is_active174 res = await conn.scalar(tbl.count())175 assert 1 == res176 await conn.close()177async def test_inner_transaction_close(sa_connect):178 conn = await sa_connect()179 await start(conn)180 tr1 = await conn.begin()181 tr2 = await conn.begin()182 assert tr2.is_active183 await conn.execute(tbl.insert().values(name='aaaa'))184 await tr2.close()185 assert not tr2.is_active186 assert tr1.is_active187 await tr1.commit()188 res = await conn.scalar(tbl.count())189 assert 2 == res190 await conn.close()191async def test_nested_transaction_commit(sa_connect):192 conn = await sa_connect()193 await start(conn)194 tr1 = await conn.begin_nested()195 tr2 = await conn.begin_nested()196 assert tr1.is_active197 assert tr2.is_active198 await conn.execute(tbl.insert().values(name='aaaa'))199 await tr2.commit()200 assert not tr2.is_active201 assert tr1.is_active202 res = await conn.scalar(tbl.count())203 assert 2 == res204 await tr1.commit()205 assert not tr2.is_active206 assert not tr1.is_active207 res = await conn.scalar(tbl.count())208 assert 2 == res209 await conn.close()210async def test_nested_transaction_commit_twice(sa_connect):211 conn = await sa_connect()212 await start(conn)213 tr1 = await conn.begin_nested()214 tr2 = await conn.begin_nested()215 await conn.execute(tbl.insert().values(name='aaaa'))216 await tr2.commit()217 assert not tr2.is_active218 assert tr1.is_active219 await tr2.commit()220 assert not tr2.is_active221 assert tr1.is_active222 res = await conn.scalar(tbl.count())223 assert 2 == res224 await tr1.close()225 await conn.close()226async def test_nested_transaction_rollback(sa_connect):227 conn = await sa_connect()228 await start(conn)229 tr1 = await conn.begin_nested()230 tr2 = await conn.begin_nested()231 assert tr1.is_active232 assert tr2.is_active233 await conn.execute(tbl.insert().values(name='aaaa'))234 await tr2.rollback()235 assert not tr2.is_active236 assert tr1.is_active237 res = await conn.scalar(tbl.count())238 assert 1 == res239 await tr1.commit()240 assert not tr2.is_active241 assert not tr1.is_active242 res = await conn.scalar(tbl.count())243 assert 1 == res244 await conn.close()245async def test_nested_transaction_rollback_twice(sa_connect):246 conn = await sa_connect()247 await start(conn)248 tr1 = await conn.begin_nested()249 tr2 = await conn.begin_nested()250 await conn.execute(tbl.insert().values(name='aaaa'))251 await tr2.rollback()252 assert not tr2.is_active253 assert tr1.is_active254 await tr2.rollback()255 assert not tr2.is_active256 assert tr1.is_active257 await tr1.commit()258 res = await conn.scalar(tbl.count())259 assert 1 == res260 await conn.close()261async def test_twophase_transaction_commit(sa_connect):262 conn = await sa_connect()263 await start(conn)264 tr = await conn.begin_twophase('sa_twophase')265 assert tr.xid == 'sa_twophase'266 await conn.execute(tbl.insert().values(name='aaaa'))267 await tr.prepare()268 assert tr.is_active269 await tr.commit()270 assert not tr.is_active271 res = await conn.scalar(tbl.count())272 assert 2 == res273 await conn.close()274async def test_twophase_transaction_twice(sa_connect):275 conn = await sa_connect()276 await start(conn)277 tr = await conn.begin_twophase()278 with pytest.raises(sa.InvalidRequestError):279 await conn.begin_twophase()280 assert tr.is_active281 await tr.prepare()282 await tr.commit()283 await conn.close()284async def test_transactions_sequence(sa_connect):285 conn = await sa_connect()286 await start(conn)287 await conn.execute(tbl.delete())288 assert conn._transaction is None289 tr1 = await conn.begin()290 assert tr1 is conn._transaction291 await conn.execute(tbl.insert().values(name='a'))292 res1 = await conn.scalar(tbl.count())293 assert 1 == res1294 await tr1.commit()295 assert conn._transaction is None296 tr2 = await conn.begin()297 assert tr2 is conn._transaction298 await conn.execute(tbl.insert().values(name='b'))299 res2 = await conn.scalar(tbl.count())300 assert 2 == res2301 await tr2.rollback()302 assert conn._transaction is None303 tr3 = await conn.begin()304 assert tr3 is conn._transaction305 await conn.execute(tbl.insert().values(name='b'))306 res3 = await conn.scalar(tbl.count())307 assert 2 == res3308 await tr3.commit()309 assert conn._transaction is None...
admin.py
Source:admin.py
1from django.contrib import admin2from .models import Categories, DebitCards, CreditCards, PotrebCredits, MFO, Mortgages, RKO, Refinancing, IDOrders3from django import forms4from ckeditor_uploader.widgets import CKEditorUploadingWidget5admin.site.site_header = '"ÐÑ ÐмеÑÑе" Ð¿Ð°Ð½ÐµÐ»Ñ ÑпÑавлениÑ'6admin.site.site_title = 'ÐÑÐмеÑÑе'7class AdminDebitCardsForm(forms.ModelForm):8 condition = forms.CharField(label='ÐпиÑание', widget=CKEditorUploadingWidget)9 class Meta:10 model: DebitCards11 fields = '__all__'12class AdminCreditCardsForm(forms.ModelForm):13 condition = forms.CharField(widget=CKEditorUploadingWidget)14 class Meta:15 model: CreditCards16 fields = '__all__'17class AdminPotrebCreditsForm(forms.ModelForm):18 condition = forms.CharField(widget=CKEditorUploadingWidget)19 class Meta:20 model: PotrebCredits21 fields = '__all__'22class AdminMortgagesForm(forms.ModelForm):23 condition = forms.CharField(widget=CKEditorUploadingWidget)24 class Meta:25 model: Mortgages26 fields = '__all__'27class AdminRefinancingForm(forms.ModelForm):28 condition = forms.CharField(widget=CKEditorUploadingWidget)29 class Meta:30 model: Refinancing31 fields = '__all__'32class AdminMFOForm(forms.ModelForm):33 condition = forms.CharField(widget=CKEditorUploadingWidget)34 class Meta:35 model: MFO36 fields = '__all__'37class AdminRKOForm(forms.ModelForm):38 condition = forms.CharField(widget=CKEditorUploadingWidget)39 class Meta:40 model: RKO41 fields = '__all__'42@admin.register(Categories)43class CategoriesAdmin(admin.ModelAdmin):44 list_display = ['name', 'is_active']45 list_editable = ['is_active']46 prepopulated_fields = {'slug': ('name',)}47@admin.register(DebitCards)48class DebitCardsAdmin(admin.ModelAdmin):49 form = AdminDebitCardsForm50 list_display = ['bank_name', 'card_name', 'reward', 'is_active']51 list_filter = ['bank_name', 'is_active']52 list_editable = ['reward', 'is_active']53 prepopulated_fields = {'slug': ('card_name',)}54 fieldsets = (55 ('ÐбÑее', {56 'fields': ('category', 'offer_id', 'bank_name', 'card_name', 'image', 'slug', 'main_characteristics',57 'condition', 'short_condition', 'referral_slug', 'referral_slug_2', 'reward'),58 }),59 ('ХаÑакÑеÑиÑÑики', {60 'fields': ('service_cost', 'age', 'cash_back', 'miles', 'delivery', 'demands', 'recommend', 'max_pay',61 'popular', 'is_active'),62 }),63 )64@admin.register(CreditCards)65class CreditCardsAdmin(admin.ModelAdmin):66 form = AdminCreditCardsForm67 list_display = ['bank_name', 'card_name', 'reward', 'is_active']68 list_filter = ['bank_name', 'is_active']69 list_editable = ['reward', 'is_active']70 prepopulated_fields = {'slug': ('card_name',)}71 fieldsets = (72 ('ÐбÑее', {73 'fields': ('category', 'offer_id', 'bank_name', 'card_name', 'image', 'slug', 'condition',74 'short_condition', 'referral_slug', 'referral_slug_2', 'reward'),75 }),76 ('ХаÑакÑеÑиÑÑики', {77 'fields': ('grace_period', 'installment_plan', 'age', 'limit', 'delivery', 'demands', 'recommend',78 'max_pay', 'popular', 'is_active'),79 }),80 )81@admin.register(PotrebCredits)82class PotrebCreditsAdmin(admin.ModelAdmin):83 form = AdminPotrebCreditsForm84 list_display = ['bank_name', 'type', 'reward', 'is_active']85 list_filter = ['bank_name', 'is_active', 'type']86 list_editable = ['reward', 'is_active']87 prepopulated_fields = {'slug': ('bank_name',)}88 fieldsets = (89 ('ÐбÑее', {90 'fields': ('category', 'offer_id', 'bank_name', 'type', 'image', 'slug', 'main_characteristics',91 'condition', 'short_condition', 'referral_slug', 'referral_slug_2', 'reward'),92 }),93 ('ХаÑакÑеÑиÑÑики', {94 'fields': ('age', 'limit', 'percents', 'documents', 'demands', 'recommend', 'max_pay', 'popular',95 'is_active'),96 }),97 )98@admin.register(MFO)99class MFOAdmin(admin.ModelAdmin):100 form = AdminMFOForm101 list_display = ['bank_name', 'reward', 'is_active']102 list_filter = ['bank_name', 'is_active']103 list_editable = ['reward', 'is_active']104 prepopulated_fields = {'slug': ('bank_name',)}105 fieldsets = (106 ('ÐбÑее', {107 'fields': ('category', 'offer_id', 'bank_name', 'image', 'slug', 'main_characteristics',108 'condition', 'short_condition', 'referral_slug', 'referral_slug_2', 'reward'),109 }),110 ('ХаÑакÑеÑиÑÑики', {111 'fields': ('term', 'age', 'sum', 'percents', 'demands', 'recommend', 'max_pay', 'popular', 'is_active'),112 }),113 )114@admin.register(Mortgages)115class MortgagesAdmin(admin.ModelAdmin):116 form = AdminMortgagesForm117 list_display = ['bank_name', 'type', 'reward', 'is_active']118 list_filter = ['bank_name', 'is_active']119 list_editable = ['reward', 'is_active']120 prepopulated_fields = {'slug': ('bank_name',)}121 fieldsets = (122 ('ÐбÑее', {123 'fields': ('category', 'offer_id', 'bank_name', 'type', 'image', 'slug', 'main_characteristics',124 'condition', 'short_condition', 'referral_slug', 'referral_slug_2', 'reward'),125 }),126 ('ХаÑакÑеÑиÑÑики', {127 'fields': ('age', 'limit', 'percents', 'documents', 'demands', 'recommend', 'max_pay', 'popular',128 'is_active'),129 }),130 )131@admin.register(RKO)132class RKOAdmin(admin.ModelAdmin):133 form = AdminRKOForm134 list_display = ['bank_name', 'reward', 'is_active']135 list_filter = ['bank_name', 'is_active']136 list_editable = ['reward', 'is_active']137 prepopulated_fields = {'slug': ('bank_name',)}138 fieldsets = (139 ('ÐбÑее', {140 'fields': ('category', 'offer_id', 'bank_name', 'image', 'slug', 'main_characteristics',141 'condition', 'short_condition', 'referral_slug', 'referral_slug_2', 'reward'),142 }),143 ('ХаÑакÑеÑиÑÑики', {144 'fields': ('payments', 'cash_deposit', 'service_cost', 'cash_withdrawal', 'demands', 'recommend', 'max_pay',145 'popular', 'is_active'),146 }),147 )148@admin.register(Refinancing)149class RefinancingAdmin(admin.ModelAdmin):150 form = AdminRefinancingForm151 list_display = ['bank_name', 'type', 'reward', 'is_active']152 list_filter = ['bank_name', 'is_active']153 list_editable = ['reward', 'is_active']154 prepopulated_fields = {'slug': ('bank_name',)}155 fieldsets = (156 ('ÐбÑее', {157 'fields': ('category', 'offer_id', 'bank_name', 'type', 'image', 'slug', 'main_characteristics',158 'condition', 'short_condition', 'referral_slug', 'referral_slug_2', 'reward'),159 }),160 ('ХаÑакÑеÑиÑÑики', {161 'fields': ('age', 'limit', 'percents', 'documents', 'demands', 'recommend', 'max_pay', 'popular',162 'is_active'),163 }),164 )165@admin.register(IDOrders)166class IDOrdersAdmin(admin.ModelAdmin):167 list_display = ['user', 'order_id', 'offer_id', 'status', 'broker']...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!