-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticleMod.py
185 lines (160 loc) · 5.51 KB
/
articleMod.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# 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_ARTICLE_V == 2;
db = dotsi.fy({"articleBox": mongo.db.articleBox}); # Isolate
############################################################
# Article building and validation: #
############################################################
validateArticle = vf.dictOf({
"_id": utils.isObjectId,
"_v": lambda x: x == K.CURRENT_ARTICLE_V,
#
# Intro'd in _v0:
#
"title": vf.typeIs(str),
#"scratchpad": vf.typeIs(str), -- Renamed in _v1 to 'body'
"creatorId": utils.isObjectId,
"createdAt": utils.isInty,
#
# Intro'd in _v1:
#
"body": vf.typeIs(str),
"categoryId": utils.isBlankOrObjectId,
#"sectionId": utils.isBlankOrObjectId, -- Removed in _v2
#
# Intro'd in _v2:
#
"status": lambda x: x in K.ARTICLE_STATUS_LIST,
});
def buildArticle (creatorId, title=""):
assert K.CURRENT_ARTICLE_V == 2;
return dotsi.fy({
"_id": utils.objectId(),
"_v": K.CURRENT_ARTICLE_V,
#
# Intro'd in _v0:
#
"title": title,
#"scratchpad": "", -- Renamed in _v1 to 'body'
"creatorId": creatorId,
"createdAt": utils.now(),
#
# Intro'd in _v1:
#
"body": "",
"categoryId": "", # blank => Untitled, topmost category.
#"sectionId": "", -- Removed in _v2
#
# Intro'd in _v2:
#
"status": "draft",
});
############################################################
# Adapting:
############################################################
articleAdp = stdAdpBuilder.buildStdAdp(
str_fooBox = "articleBox",
str_CURRENT_FOO_V = "CURRENT_ARTICLE_V",
int_CURRENT_FOO_V = K.CURRENT_ARTICLE_V,
func_validateFoo = validateArticle,
);
@articleAdp.addStepAdapter
def stepAdapterCore_from_0_to_1 (articleY): # Note: This _CANNOT_ be a lambda as `addStepAdapter` relies on .__name__
# article._v: 0 --> 1
# Renamed:
# ~ scratchpad --> body
# Added:
# + categoryId
# + sectionId
articleY.update({
"body": articleY.pop("scratchpad"),
"categoryId": "",
"sectionId": "",
});
@articleAdp.addStepAdapter
def stepAdapterCore_from_1_to_2 (articleY): # Note: This _CANNOT_ be a lambda as `addStepAdapter` relies on .__name__
# article._v: 1 --> 2
# Added:
# + status
# Removed
# - sectionId
articleY.update({"status": "draft"});
articleY.pop("sectionId");
#@articleAdp.addStepAdapter
#def stepAdapterCore_from_X_to_Y (articleY): # Note: This _CANNOT_ be a lambda as `addStepAdapter` relies on .__name__
# # article._v: X --> Y
# # Added:
# # + foo
# articleY.update({
# "foo": "foobar",
# });
assert articleAdp.getStepCount() == K.CURRENT_ARTICLE_V;
# Adaptation Checklist:
# Assertions will help you.
# You'll need to look at:
# + constants.py
# + articleMod.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
# + articleCon.py and others:
# + modify funcs that call articleMod's funcs.
############################################################
# Getting:
############################################################
def getArticle (q, shouldUpdateDb=True):
"Query traditionally for a single article.";
assert type(q) in [str, dict];
article = db.articleBox.find_one(q);
if article is None:
return None;
return articleAdp.adapt(article, shouldUpdateDb);
def getArticleList (q=None, shouldUpdateDb=True):
"Query traditionally for multiple articles.";
q = q or {};
assert type(q) is dict;
adaptWrapper = lambda article: ( # A wrapper around `adapt`, aware of `shouldUpdateDb`.
articleAdp.adapt(article, shouldUpdateDb)#, # NO COMMA
);
return utils.map(adaptWrapper, db.articleBox.find(q));
def getArticleCount (q=None):
return db.articleBox.count_documents(q or {});
############################################################
# Inserting, Updating & Deleting:
############################################################
def insertArticle (article):
"More or less blindly INSERTS article to db."; # Used primarily for inviting articles.
assert validateArticle(article);
#print("inserting article: ", article);
dbOut = db.articleBox.insert_one(article);
assert dbOut.inserted_id == article._id;
return dbOut;
def replaceArticle (article):
"More or less blindly REPLACES article in db."; # Used primarily for updating fname/lname/password etc of logged-in articles.
assert validateArticle(article);
dbOut = db.articleBox.replace_one({"_id": article._id}, article);
assert dbOut.matched_count == 1 == dbOut.modified_count;
return dbOut;
def deleteArticle (article):
"Deletes an unverified, invited.";
assert validateArticle(article);
dbOut = db.articleBox.delete_one({"_id": article._id});
assert dbOut.deleted_count == 1;
return True;
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx