def autocomplete(request): drugs = Drug.objects.all() entries = [drug.name for drug in drugs] for drug in drugs: for alias in drug.aliases: if not any([normalize(alias) in normalize(entry) for entry in entries]): entries.append(alias) return render( request, 'drugcombinator/autocomplete.js', locals(), content_type='text/javascript' )
def get_from_name(self, name): """ Return a drug based on its name. Search criterias priorities: - equal to slug (case insensitive, accentuation insensitive) - equal to name (case insentitive) - included in aliases (case insentitive) Throw DoesNotExist if no drug is found. """ try: return self.get(slug=normalize(name)) except self.model.DoesNotExist: pass try: return self.filter(name__iexact=name)[0] except IndexError: pass reg = rf'(^|\r|\n){name}(\r|\n|$)' try: return self.filter(_aliases__iregex=reg)[0] except IndexError: raise self.model.DoesNotExist
def test_normalize(self): self.assertEqual( normalize("Je bois du café. Ça vous va ?"), "je bois du cafe. ca vous va ?" )
def test_normalize_noop(self): data = "c'est vraiment pas de bol !" self.assertEqual(normalize(data), data)