-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategoryMod.py
170 lines (144 loc) · 5.29 KB
/
categoryMod.py
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
# std:
# n/a
# pip-ext:
# n/a
# pip-int:
import dotsi;
import vf;
# loc:
from constants import K;
import mongo;
import utils;
import bu;
import stdAdpBuilder;
############################################################
# Assertions & prelims: #
############################################################
assert K.CURRENT_CATEGORY_V == 0;
db = dotsi.fy({"categoryBox": mongo.db.categoryBox}); # Isolate
############################################################
# Category building and validation: #
############################################################
validateCategory = vf.dictOf({
"_id": utils.isObjectId,
"_v": lambda x: x == K.CURRENT_CATEGORY_V,
#
# Intro'd in _v0:
#
"name": vf.typeIs(str),
"rank": utils.isNonNegativeNumber, # 0+, inty or float.
"parentId": utils.isBlankOrObjectId, # "" => no parent => top
"creatorId": utils.isObjectId,
"createdAt": utils.isInty,
});
def buildCategory (creatorId, name="", rank=0, parentId=""):
assert K.CURRENT_CATEGORY_V == 0;
return dotsi.fy({
"_id": utils.objectId(),
"_v": K.CURRENT_CATEGORY_V,
#
# Intro'd in _v0:
#
"name": name,
"rank": rank,
"parentId": parentId,
"creatorId": creatorId,
"createdAt": utils.now(),
});
def checkCircularParentage (unsavedCategory):
# `unsavedCategory` should ideally be unsaved, but needn't be.
# A few examples of circular parentage:
# ~ catA.parent -> catA
# ~ catA.parent -> catB & catB.parent -> catA X --> Y --> Z
# ~ X.parent -> Y, Y.parent -> Z, Z.parent -> X ^-----<-----v
# ~ etc.
currentCategory = unsavedCategory;
seenCategoryIds = set();
while currentCategory:
if currentCategory._id in seenCategoryIds:
# ==> Circular
return True; # Short ckt.
# otherwise ...
seenCategoryIds.add(currentCategory._id);
# Loop:
currentCategory = getCategory(currentCategory.parentId);
# ==> Non-circular.
return False;
def validateNonCircularParentage (unsavedCategory):
if checkCircularParentage(unsavedCategory):
raise bu.abort("Circular parentage detected.");
# ==> Non-circular, i.e. valid.
return True;
############################################################
# Adapting:
############################################################
categoryAdp = stdAdpBuilder.buildStdAdp(
str_fooBox = "categoryBox",
str_CURRENT_FOO_V = "CURRENT_CATEGORY_V",
int_CURRENT_FOO_V = K.CURRENT_CATEGORY_V,
func_validateFoo = validateCategory,
);
#@categoryAdp.addStepAdapter
#def stepAdapterCore_from_X_to_Y (categoryY): # Note: This _CANNOT_ be a lambda as `addStepAdapter` relies on .__name__
# # category._v: X --> Y
# # Added:
# # + foo
# categoryY.update({
# "foo": "foobar",
# });
assert categoryAdp.getStepCount() == K.CURRENT_CATEGORY_V;
# Adaptation Checklist:
# Assertions will help you.
# You'll need to look at:
# + constants.py
# + categoryMod.py
# + top (K) assertion
# + define stepAdapterCore_from_X_to_Y
# + modify builder/s as needed
# + modify validator/s as needed
# + modify snip/s if any, as needed
# + categoryCon.py and others:
# + modify funcs that call categoryMod's funcs.
############################################################
# Getting:
############################################################
def getCategory (q, shouldUpdateDb=True):
"Query traditionally for a single category.";
assert type(q) in [str, dict];
category = db.categoryBox.find_one(q);
if category is None:
return None;
return categoryAdp.adapt(category, shouldUpdateDb);
def getCategoryList (q=None, shouldUpdateDb=True):
"Query traditionally for multiple categorys.";
q = q or {};
assert type(q) is dict;
adaptWrapper = lambda category: ( # A wrapper around `adapt`, aware of `shouldUpdateDb`.
categoryAdp.adapt(category, shouldUpdateDb)#, # NO COMMA
);
return utils.map(adaptWrapper, db.categoryBox.find(q));
def getCategoryCount (q=None):
return db.categoryBox.count_documents(q or {});
############################################################
# Inserting, Updating & Deleting:
############################################################
def insertCategory (category):
"More or less blindly INSERTS category to db.";
assert validateCategory(category);
#print("inserting category: ", category);
dbOut = db.categoryBox.insert_one(category);
assert dbOut.inserted_id == category._id;
return dbOut;
def replaceCategory (category):
"More or less blindly REPLACES category in db.";
assert validateCategory(category);
dbOut = db.categoryBox.replace_one({"_id": category._id}, category);
assert dbOut.matched_count == 1 == dbOut.modified_count;
return dbOut;
def deleteCategory (category):
"Deletes an unverified, invited.";
assert validateCategory(category);
dbOut = db.categoryBox.delete_one({"_id": category._id});
assert dbOut.deleted_count == 1;
return True;
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx