def indexFingerprint(self, batch_mode=False): def is_if_yes_no(question): return question.type in 'choice-yesno' or \ question.type in 'choice-yesnocomment' or \ question.type in 'choice-yesnodontknow' d = {} # Get parameters that are only on fingerprint # type_t d['id'] = self.fingerprint_hash d['type_t'] = self.questionnaire.slug d['date_last_modification_t'] = self.last_modification.strftime( '%Y-%m-%d %H:%M:%S.%f') d['created_t'] = self.created.strftime('%Y-%m-%d %H:%M:%S.%f') d['user_t'] = self.unique_users_string() d['percentage_d'] = self.fill adicional_text = "" # Add answers answers = Answer.objects.filter(fingerprint_id=self) for answer in answers: question = answer.question # We try to get permissions preferences for this question permissions = self.getPermissions( QuestionSet.objects.get(id=question.questionset.id)) slug = question.slug_fk.slug1 if permissions.allow_indexing or slug == 'database_name': setProperFields(d, question, slug, answer.data) if is_if_yes_no(question) and 'yes' in answer.data: adicional_text += question.text + " " if answer.comment != None: d['comment_question_' + slug + '_t'] = answer.comment d['text_t'] = generateFreeText(d) + " " + adicional_text d['mlt_t'] = generateMltText(d) if batch_mode: return d else: print "-- Indexing unique fingerprint hash " + str( self.fingerprint_hash) c = CoreEngine() results = c.search_fingerprint("id:" + self.fingerprint_hash) if len(results) == 1: # Delete old entry if any c.delete(results.docs[0]['id']) c.index_fingerprint_as_json(d)
def indexFingerprint(self, batch_mode=False): def is_if_yes_no(question): return question.type in 'choice-yesno' or \ question.type in 'choice-yesnocomment' or \ question.type in 'choice-yesnodontknow' d = {} # Get parameters that are only on fingerprint # type_t d['id']=self.fingerprint_hash d['type_t'] = self.questionnaire.slug d['date_last_modification_t'] = self.last_modification.strftime('%Y-%m-%d %H:%M:%S.%f') d['created_t'] = self.created.strftime('%Y-%m-%d %H:%M:%S.%f') d['user_t'] = self.unique_users_string() d['percentage_d'] = self.fill adicional_text = "" # Add answers answers = Answer.objects.filter(fingerprint_id=self) for answer in answers: question = answer.question # We try to get permissions preferences for this question permissions = self.getPermissions(QuestionSet.objects.get(id=question.questionset.id)) slug = question.slug_fk.slug1 if permissions.allow_indexing or slug == 'database_name': setProperFields(d, question, slug, answer.data) if is_if_yes_no(question) and 'yes' in answer.data: adicional_text += question.text+ " " if answer.comment != None: d['comment_question_'+slug+'_t'] = answer.comment d['text_t']= generateFreeText(d) + " " + adicional_text d['mlt_t'] = generateMltText(d) if batch_mode: return d else: print "-- Indexing unique fingerprint hash "+str(self.fingerprint_hash) c = CoreEngine() results = c.search_fingerprint("id:"+self.fingerprint_hash) if len(results) == 1: # Delete old entry if any c.delete(results.docs[0]['id']) c.index_fingerprint_as_json(d)
def convertFieldsOnSolr(fields, new_type): c = CoreEngine() suffix = assert_suffix(new_type) if suffix == None: print '-- Invalid new type, process cancelled.' return False documents = c.search_fingerprint("*:*") print "Started converting fields on all databases, number of databases: " + str( len(documents)) for document in documents: doc = document del doc['_version_'] for field in fields: try: value = doc[str(field) + '_t'] value = convert_value(value, new_type) if value == None: print "-- Couldn't convert field " + str( field) + " for database " + doc['id'] + ". " + str( doc[str(field) + '_t']) + " is not of type " + str(new_type) else: doc[str(field) + suffix] = value except KeyError: print "-- " + str( doc['id']) + ' doesn\'t have the field ' + str( field) + ', ignoring this field on this database.' c.delete(doc['id']) c.index_fingerprint_as_json(doc) print "Done converting fields on all databases" return True
def convertFieldsOnSolr(fields, new_type): c = CoreEngine() suffix = assert_suffix(new_type) if suffix == None: print '-- Invalid new type, process cancelled.' return False documents = c.search_fingerprint("*:*") print "Started converting fields on all databases, number of databases: "+str(len(documents)) for document in documents: doc = document del doc['_version_'] for field in fields: try: value = doc[str(field)+'_t'] value = convert_value(value, new_type) if value == None: print "-- Couldn't convert field "+str(field)+" for database " + doc['id'] + ". "+str(doc[str(field)+'_t'])+" is not of type " + str(new_type) else: doc[str(field)+suffix] = value except KeyError: print "-- "+str(doc['id'])+' doesn\'t have the field '+str(field)+', ignoring this field on this database.' c.delete(doc['id']) c.index_fingerprint_as_json(doc) print "Done converting fields on all databases" return True
def validate_and_save(user, data): """ Verify if json structure is correct and create/update values of fingerprint :param user: :param data: """ result = {} fields_text = "" # Verify if json structure is valid if 'fingerprintID' in data.keys(): fingerprintID = data['fingerprintID'] # Verify if fingerprint belongs to user if validate_fingerprint(user, fingerprintID): if 'values' in data.keys(): for f in data['values']: # Check if field already exists if FingerprintAPI.objects.filter(fingerprintID=fingerprintID, field=f): try: fp = FingerprintAPI.objects.get(fingerprintID=fingerprintID, field=f) if str(fp.value) != str(data['values'][f]): # Update value fp.value += ' ' + data['values'][f] fields_text = data['values'][f] fp.save() result[f] = "Updated successfully" else: result[f] = "Not updated" except: # print "Erro a atualizar o registo" result[f] = "Error to update field" # If field does not exist else: try: fingerprint = FingerprintAPI(fingerprintID=fingerprintID, field=f, value=data['values'][f], user=user) # Create new field-value fields_text += ' ' + data['values'][f] fingerprint.save() result[f] = "Created successfully" except: # print "Erro a criar o novo registo" result[f] = "Error to create new field" # No values key in JSON structure else: # print "Não tem valores" result['error'] = "No values detected" else: result['error'] = "Error find FingerprintID" else: # print "Não tem nenhuma chave fingerprint" result['error'] = "No fingerprintID detected" c = CoreEngine() results = c.search_fingerprint("id:" + fingerprintID) _aux = None for r in results: _aux = r break if (_aux!=None): _aux['text_t'] = _aux['text_t'] + fields_text c.index_fingerprint_as_json(_aux) return result