/[glsr]/trunk/site/script.py
Gentoo

Diff of /trunk/site/script.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 185 Revision 186
16from time import strftime, gmtime 16from time import strftime, gmtime
17import types 17import types
18 18
19from glsr.setup import config 19from glsr.setup import config
20from glsr.core.db.mysql import SQLdb 20from glsr.core.db.mysql import SQLdb
21from glsr.site import user
21 22
22def create_script(submitter_id, name = "", 23def create_script(submitter_id, name = "",
23 descr = "", version = "1.0", language = 0, 24 descr = "", version = "1.0", language = 0,
24 category = 0, body = "", changelog = ""): 25 category = 0, body = "", changelog = ""):
25 """Add a new script to the database.""" 26 """Add a new script to the database."""
129 130
130 return retval 131 return retval
131 132
132def search(columns = None, terms = None): 133def search(columns = None, terms = None):
133 """Search for scripts in the DB. 134 """Search for scripts in the DB.
134 135
135 Terms should be a dictionary of (field, value) pairs. If 'value' is a 136 Terms should be a dictionary of (field, value) pairs. If 'value' is a
136 ListType then they will be OR'd 137 ListType then they will be OR'd
137 (i.e. field = value[0] OR field = value[1]...) 138 (i.e. field = value[0] OR field = value[1]...)
138 139
139 Searchable values include: language, category, status, name, descr, 140 Searchable values include: language, category, status, name, descr,
140 submitter 141 submitter
141 """ 142 """
142 143
143 # FIXME: Needs some more sanity checking on incoming columns and terms. 144 # FIXME: Needs some more sanity checking on incoming columns and terms.
159 scripts = SQLdb(config.db).query( 160 scripts = SQLdb(config.db).query(
160 "SELECT * FROM %(prefix)sscript %(where)s" % 161 "SELECT * FROM %(prefix)sscript %(where)s" %
161 {'columns': columns, 'prefix': config.db["prefix"], 162 {'columns': columns, 'prefix': config.db["prefix"],
162 'where': where_clause}) 163 'where': where_clause})
163 164
164 # FIXME: This won't work because submitter is not in the script table,
165 # submitter_id is. Thus we can't obtain a partial match on the
166 # submitter name so easily.
167 found = [] 165 found = []
168 for script in scripts: 166 for script in scripts:
169 for key in ["name", "descr", "submitter"]: 167 for key in ["name", "descr"]:
170 if key in terms.keys(): 168 if (terms.has_key(key)
171 if script["script_" + key].find(terms[key]) != -1: 169 and script["script_" + key].find(terms[key]) != -1):
170 if script not in found:
172 found.append(script) 171 found.append(script)
172
173 for script in scripts:
174 if terms.has_key("submitter_id"):
175 search_user = user.User(terms["submitter_id"])
176 script_user = user.User(script["script_submitter_id"])
177 if search_user.get_alias().find(script_user.get_alias()) != -1:
178 if script not in found:
179 found.append(script)
180
181 for script in scripts:
182 if (terms.has_key("category_id") and
183 script["script_category_id"] == int(terms["category_id"])):
184 if script not in found:
185 found.append(script)
186
187 for script in scripts:
188 if (terms.has_key("language_id") and
189 script["script_language_id"] == int(terms["language_id"])):
190 if script not in found:
191 found.append(script)
173 192
174 # Get the subscripts 193 # Get the subscripts
175 where_clause = __mk_query(["status"], terms) 194 where_clause = __mk_query(["status"], terms)
176 if where_clause != "": 195 if where_clause != "":
177 where_clause = "AND " + where_clause 196 where_clause = "AND " + where_clause
178 197
179 # Since 'found' will be modified in the loop, we need to copy it's size.
180 found_len = len(found)
181 for i in range(found_len): 198 for i in range(len(found)):
182 199
183 qstr = ("SELECT subscript_version, subscript_date, subscript_status," + 200 qstr = ("SELECT subscript_version, subscript_date, subscript_status," +
184 " subscript_approvedby" + 201 " subscript_approvedby " +
185 "FROM %(prefix)ssubscript " % {'prefix': config.db["prefix"]} + 202 "FROM %(prefix)ssubscript " % {'prefix': config.db["prefix"]} +
186 "WHERE status != 'draft'" + 203 "WHERE subscript_status != 'draft'" +
187 " AND parent = %s " % found[i]["subscript_id"] + 204 " AND subscript_parent_id = %s " % found[i]["script_id"] +
188 where_clause) 205 where_clause)
189 206
190 if "current" in terms.keys(): 207 if "current" in terms.keys():
191 subscript = SQLdb(config.db).query(qstr + " ORDER BY version", 208 subscript = SQLdb(config.db).query(
192 fetch = "one") 209 qstr + " ORDER BY subscript_version", fetch = "one")
193 found[i].update(subscript) 210 found[i].update(subscript)
194 211
195 else: 212 else:
196 subscripts = SQLdb(config.db).query(qstr, fetch = "all") 213 subscripts = SQLdb(config.db).query(qstr, fetch = "all")
197 214
198 for subscript in subscripts[1:]: 215 for subscript in subscripts[1:]:
216 # Since we're selecting all subscripts we need to copy the
217 # script again for each additional subscript.
199 found.append(found[i]) 218 found.append(found[i])
200 found[len(found)].update(subscript) 219 found[len(found)].update(subscript)
201 220
202 found[i].update(subscripts[0]) 221 found[i].update(subscripts[0])
203 222

Legend:
Removed from v.185  
changed lines
  Added in v.186

  ViewVC Help
Powered by ViewVC 1.1.20