11import logging
22
3- from concepts .utils import UnionFind
3+ from concepts .utils import UnionFind , normalize_concept_name
44from django .db import models
55from django .db .models .functions import Lower
66from django .db .utils import IntegrityError
77
88
99class Concept (models .Model ):
1010 name = models .CharField (max_length = 200 , null = True )
11+ normal_name = models .CharField (max_length = 200 , null = True )
1112 description = models .TextField (null = True )
1213
1314 class Meta :
1415 ordering = ["name" , "description" ]
1516 constraints = [
16- models .UniqueConstraint (Lower ( "name" ). desc () , name = "unique_lower_name " )
17+ models .UniqueConstraint (fields = [ "normal_name" ] , name = "unique_normal_name " )
1718 ]
1819
1920
@@ -34,7 +35,7 @@ def create_singleton_concepts(self):
3435 logging .WARNING ,
3536 f" A concept named '{ new_concept .name } ' already exists." ,
3637 )
37- new_concept = Concept .objects .get (name__iexact = new_concept .name )
38+ new_concept = Concept .objects .get (normal_name = new_concept .normal_name )
3839 item .concept = new_concept
3940 item .save ()
4041
@@ -45,16 +46,17 @@ def take_first(lst):
4546 components = UnionFind (self .all (), Link .objects .all ().to_tuples ())
4647 for concept_items in components .get_item_components (sort_key = Item .Source .key ()):
4748 name = take_first ([item .name for item in concept_items ])
49+ normal_name = normalize_concept_name (name )
4850 description = take_first ([item .description for item in concept_items ])
49- new_concept = Concept (name = name , description = description )
51+ new_concept = Concept (name = name , normal_name = normal_name , description = description )
5052 try :
5153 new_concept .save ()
5254 except IntegrityError :
5355 logging .log (
5456 logging .WARNING ,
5557 f" A concept named '{ new_concept .name } ' already exists." ,
5658 )
57- new_concept = Concept .objects .get (name = name )
59+ new_concept = Concept .objects .get (normal_name = normal_name )
5860 for item in concept_items :
5961 item .concept = new_concept
6062 item .save ()
@@ -127,7 +129,7 @@ def get_linked_item_urls(self):
127129 return [i .get_url () for i in self .get_linked_items ()]
128130
129131 def to_concept (self ):
130- return Concept (name = self .name , description = self .description )
132+ return Concept (name = self .name , normal_name = normalize_concept_name ( self . name ), description = self .description )
131133
132134 def __str__ (self ):
133135 if self .name :
0 commit comments