Public Member Functions | |
| def | __init__ |
| def | open |
| def | decode_row |
| def | sniff_header |
| def | run |
| def | reset |
| def | import_bool |
| def | import_date |
| def | import_row |
Public Attributes | |
| dialect | |
| decoder | |
| fd | |
| header | |
Static Public Attributes | |
| tuple | encoding = locale.getpreferredencoding() |
| do_overlay = False | |
| add_to_library = None | |
| true_values = None | |
Classes | |
| class | user_dialect |
| def py::qo::importer::comma_separated_values::__init__ | ( | self | ) |
| def py::qo::importer::comma_separated_values::open | ( | self, | ||
| filename | ||||
| ) |
| def py::qo::importer::comma_separated_values::decode_row | ( | self, | ||
| row | ||||
| ) |
| def py::qo::importer::comma_separated_values::sniff_header | ( | self | ) |
00087 : 00088 ret = [] 00089 done = [] 00090 for head in self.header : 00091 mhead = qo.utils.replace_lower_accents(head.lower()) 00092 match = None 00093 for field, trs in _tr_fields : 00094 if field in done : 00095 continue 00096 if mhead in trs : 00097 done.append(field) 00098 match = field 00099 break 00100 ret.append((head, match)) 00101 return ret 00102 def run( self, progress, head_map ) :
| def py::qo::importer::comma_separated_values::run | ( | self, | ||
| progress, | ||||
| head_map | ||||
| ) |
00103 : 00104 progress.section(_("Importing...")) 00105 head_map = dict(head_map) 00106 header = [head_map[h] or "_" for h in self.header] 00107 _stats = { 00108 "matched_rows" : [], 00109 "notmatched_rows" : [], 00110 "error_rows" : [], 00111 } 00112 i = 2 00113 for row in self.fd : 00114 try : 00115 row = self.decode_row(row) 00116 is_new, message = self.import_row(dict(zip(header, row))) 00117 _stats[is_new and "notmatched_rows" or "matched_rows"].append((i, message)) 00118 except Exception, e : 00119 traceback.print_exc() 00120 _stats["error_rows"].append((i, e)) 00121 i += 1 00122 progress.section_end() 00123 return _stats 00124 def reset( self ) :
| def py::qo::importer::comma_separated_values::reset | ( | self | ) |
| def py::qo::importer::comma_separated_values::import_bool | ( | self, | ||
| value | ||||
| ) |
| def py::qo::importer::comma_separated_values::import_date | ( | self, | ||
| value | ||||
| ) |
00131 : 00132 months = [ 'jan', 'fev', 'mars', 'avr', 'mai', 'juin', 'juil', 'aout', 'sept', 'oct', 'nov', 'dec' ] 00133 kvalue = value 00134 value = qo.utils.replace_lower_accents(value.lower()) 00135 for month in months : 00136 if month in months : 00137 value = value.replace(month, " %d " % (months.index(month)+1)) 00138 break 00139 values = map(int, qo.utils.normalize(re_uint.sub(" ", value)).split()) 00140 00141 d_day, d_month, d_year = 0, 0, 0 00142 00143 if len(values) == 1 : # year only 00144 d_year = values[0] 00145 elif len(values) == 2 : # year + month 00146 im, iy = 0, 1 00147 if values[0] > 12 : 00148 im, iy = iy, im 00149 d_year, d_month = values[iy], values[im] 00150 elif len(values) == 3 : # year, month, day 00151 success = False 00152 for id, im, iy in [[0, 1, 2], [2, 1, 0], [1, 0, 2], [1, 2, 0], [2, 0, 1], [0, 2, 1]] : 00153 try : 00154 assert 1 <= id <= 31 00155 assert 1 <= im <= 12 00156 success = True 00157 break 00158 except AssertionError : 00159 pass 00160 if success : 00161 d_year, d_month, d_day = values[iy], values[im], values[id] 00162 00163 if d_year < 1000 : 00164 if 75 <= d_year <= 99 : 00165 d_year += 1900 00166 else : 00167 d_year += 2000 00168 if d_month > 12 : 00169 d_month, d_day = 0, 0 00170 elif d_month > 0 : 00171 try : 00172 datetime.date(d_year, d_month, d_day) 00173 except ValueError : 00174 d_day = 0 00175 try : 00176 datetime.date(d_year, d_month, d_day) 00177 except ValueError : 00178 d_year, d_month, d_day = 0, 0, 0 00179 return "%d-%02d-%02d" % (d_year, d_month, d_day) 00180 def import_row( self, row ) :
| def py::qo::importer::comma_separated_values::import_row | ( | self, | ||
| row | ||||
| ) |
00181 : 00182 00183 def simple_name_search( root, name, qoid ) : 00184 entities = [] 00185 if qoid : 00186 entities = list(root.filter(qoid=qoid)) 00187 if not entities : 00188 entities = list(root.filter(name__iexact=name)) 00189 if entities : 00190 return entities.pop(0) 00191 00192 editor = None 00193 if "editor" in row : 00194 editor = simple_name_search(Editor.objects, row["editor"], Editor.create_qoid(row["editor"])) 00195 00196 collection = None 00197 if "collection" in row : 00198 q_collection = Q() 00199 if editor : 00200 q_collection |= Q(editor=editor) 00201 collection_root = Collection.objects.filter(q_collection) 00202 collection = simple_name_search(collection_root, row["collection"], editor and Collection.create_qoid(editor, row["collection"])) 00203 00204 series = None 00205 if "series" in row : 00206 q_series = Q() 00207 if collection : 00208 q_series |= Q(collection=collection) 00209 elif editor : 00210 q_series |= Q(collection__editor=editor) 00211 series_root = Series.objects.filter(q_series) 00212 series = simple_name_search(series_root, row["series"], collection and Series.create_qoid(collection, row["series"])) 00213 00214 album = None 00215 q_album = Q() 00216 if series : 00217 q_album |= Q(series=series) 00218 elif collection : 00219 q_album |= Q(series__collection=collection) 00220 elif editor : 00221 q_album |= Q(series__collection__editor=editor) 00222 album_root = Album.objects.filter(q_album) 00223 for key in ["ean", "isbn"] : 00224 if key in row : 00225 try : 00226 album = album_root.get(qoid=qo.utils.make_qoid(row[key])) 00227 break 00228 except Album.DoesNotExist : 00229 pass 00230 if not album and series : 00231 qoid = "%s%s" % (series.qoid, qo.utils.make_qoid(row["name"]) or "DEFAULT") 00232 if "volume" in row : 00233 try : 00234 album = album_root.get(qoid=qo.utils.make_qoid("%s%s" % (qoid, row["volume"].replace("-", "MINUS")))) 00235 except : 00236 try : 00237 album = album_root.get(qoid=qoid) 00238 except Album.DoesNotExist : 00239 pass 00240 00241 props = {} 00242 for key, tkey in [("name", unicode), ("volume", int), ("published_date", datetime.date), ("ean", "qoid"), ("isbn", "qoid"), \ 00243 ("guiding_price", float)] : 00244 if key in row and row[key].strip() : 00245 value = qo.utils.normalize(row[key]) 00246 if tkey is unicode : 00247 pass 00248 elif tkey is int : 00249 value = re_int.sub("", value) 00250 if not value : 00251 continue 00252 value = int(value) 00253 elif tkey is float : 00254 value = re_float.sub("", value.replace(",",".")) 00255 if not value : 00256 continue 00257 value = float(value) 00258 elif tkey == "qoid" : 00259 value = qo.utils.make_qoid(value) 00260 elif tkey is datetime.date : 00261 value = self.import_date(value) 00262 props[key] = value 00263 00264 if "name" not in props : 00265 props["name"] = "" 00266 00267 def create_obj(model, props) : 00268 qoid = model.create_qoid(**props) 00269 return model.make_overlay(qoid, **props) 00270 00271 is_new = album is None 00272 if not is_new : 00273 if self.do_overlay : 00274 album.overlay(**props) 00275 else : 00276 if not series : 00277 if not collection : 00278 if not editor : 00279 editor = create_obj(Editor, {"name": row.get("editor", "")}) 00280 collection = create_obj(Collection, {"editor": editor, "name": row.get("collection", "")}) 00281 series = create_obj(Series, {"collection": collection, "name": row.get("series", "")}) 00282 props["series"] = series 00283 album = create_obj(Album, props) 00284 00285 if ("authors" in row or "drawers" in row) and (is_new or self.do_overlay) : 00286 data = { 00287 "authors" : [], 00288 "drawers" : [], 00289 } 00290 for key in ["authors", "drawers"] : 00291 for author_name in map(qo.utils.normalize, row.get(key, "").split(",")) : 00292 if not author_name : 00293 continue 00294 data[key].append(Author.find_matching(author_name) or create_obj(Author, {"name": author_name})) 00295 album.authors_entities = data["authors"] 00296 album.drawers_entities = data["drawers"] 00297 album.save() 00298 00299 for library in Library.objects.all() : 00300 add_it, remove_it = False, False 00301 key = "in_library%d" % library.id 00302 if library.id in self.add_to_library : 00303 add_it = self.add_to_library[library.id] 00304 elif key in row : 00305 add_it = self.import_bool(row[key]) 00306 remove_it = not add_it 00307 00308 if add_it and not album.belongs_to_library(library.id) : 00309 library.albums.create(album=album) 00310 elif remove_it : 00311 album.albumcopy_set.filter(library=library).delete() 00312 00313 return is_new, None 00314 00315
tuple py::qo::importer::comma_separated_values::encoding = locale.getpreferredencoding() [static] |
py::qo::importer::comma_separated_values::do_overlay = False [static] |
py::qo::importer::comma_separated_values::add_to_library = None [static] |
py::qo::importer::comma_separated_values::true_values = None [static] |
1.5.3