def _text_search(self, q, fields=None): if fields is None: fields = [ x for x in META.keys() if META[x].get("text_search", False) ] w = re.compile(r'\W+', re.U) words = w.split(q) matches = [] suffix = "" if len(fields) == 1 and fields[0] == "subject": suffix = " IN BOOLEAN MODE" for k in fields: matches.append( "MATCH(`%s`) AGAINST ('%s'%s)" % (k, " ".join(words), suffix) ) m = "+".join(matches) q = "SELECT r.id, (%s) AS score FROM savoirs_record AS r \ LEFT JOIN savoirs_record_listsets AS rl ON r.id = rl.record_id \ LEFT JOIN savoirs_listset AS l ON rl.listset_id = l.spec \ WHERE (%s) AND r.validated = 1 \ AND (l.validated = 1 OR l.validated IS NULL) \ GROUP BY r.id \ HAVING score > 0 ORDER BY score DESC" % (m, m) from django.db import connection cursor = connection.cursor() cursor.execute(q) rc = cursor.fetchall() return rc
def decode(self, field, data): """ Décode la structure provenant de la BD. """ if field in META.keys() and META[field]['type'] == 'array': return data.split(self.separator) else: return data
def encode(self, field, data): """ Encode la structure de donnée moissonnée pour la BD. """ if field in META.keys() and META[field]['type'] == 'array': return self.separator.join(data) else: return data
def _load(self, id): """ Recupérer la structure de métadonnées pour un record selon un `id`. """ r = Record.all_objects.get(id=id) meta = {'id': id} for k in META.keys(): if hasattr(r, k): v = getattr(r, k) if v is not None: meta[k] = self.encoder.decode(k, v) return meta
def menage(self,): """ Applique sur tous les records, la fonction de corrections de string sur les données moissonnées. """ for r in Record.all_objects.all(): for k in META.keys(): v = getattr(r, k) if k not in ['uri', 'identifier']: v = self.encoder.propre(v) setattr(r, k, v) r.save()
def _update_record(self, r, metadata): for k in metadata.keys(): if hasattr(self, k): method = getattr(self, k) method(r, metadata[k]) else: setattr(r, k, self.encoder.encode(k, metadata[k])) r.last_checksum = hashlib.md5(str(metadata)).hexdigest() r.last_update = datetime.datetime.today() # stocke des chaînes de caractères propre en BD en provenance # des données moissonnées for k in META.keys(): v = getattr(r, k) if k not in ['uri', 'identifier']: v = self.encoder.propre(v) setattr(r, k, v) r.save()