@@ -18,11 +18,13 @@ def onAdvBrowserLoad(self, advBrowser):
1818 # We build this dictionary once to avoid needlessly finding the
1919 # field order for every single row when sorting. It's
2020 # significantly faster that way.
21- # { mid -> {fldName -> pos}}
21+ # {mid -> {fldName -> pos}}
2222 self .modelFieldPos = {}
2323
24- # List of our columns
25- self .customColumns = []
24+ # Dictionary of columns. A column can exist in multiple places, because
25+ # different note types may have the same field name.
26+ # {fld['name'] -> CustomColumn}}
27+ self .customColumns = {}
2628
2729 self .buildMappings ()
2830
@@ -33,18 +35,18 @@ def fldOnData(c, n, t):
3335 field = self .fieldTypes [t ]
3436 if field in c .note ().keys ():
3537 return anki .utils .stripHTML (c .note ()[field ])
36-
38+
3739 for type , name in self .fieldTypes .iteritems ():
3840 srt = ("(select valueForField(mid, flds, '%s') "
3941 "from notes where id = c.nid)" % name )
40-
42+
4143 cc = advBrowser .newCustomColumn (
42- type = type ,
43- name = name ,
44- onData = fldOnData ,
45- onSort = getOnSort (srt )
44+ type = type ,
45+ name = name ,
46+ onData = fldOnData ,
47+ onSort = getOnSort (srt )
4648 )
47- self .customColumns . append ( cc )
49+ self .customColumns [ name ] = cc
4850
4951 def onBuildContextMenu (self , contextMenu ):
5052 # Models might have changed so rebuild our mappings.
@@ -53,10 +55,14 @@ def onBuildContextMenu(self, contextMenu):
5355
5456 # Create a new sub-menu for our columns
5557 fldGroup = contextMenu .newSubMenu ("Fields" )
56- for column in self .customColumns :
57- fldGroup .addItem (column )
58+ # And a sub-menu for each note type
59+ for model in mw .col .models .models .itervalues ():
60+ modelGroup = fldGroup .newSubMenu (model ['name' ])
61+ for fld in model ['flds' ]:
62+ modelGroup .addItem (self .customColumns [fld ['name' ]])
63+
5864
59- def buildMappings (self ):
65+ def buildMappings (self ):
6066 for model in mw .col .models .all ():
6167 # For some reason, some mids return as unicode, so convert to int
6268 mid = int (model ['id' ])
@@ -70,10 +76,10 @@ def buildMappings(self):
7076 for field in model ['flds' ]:
7177 name = field ['name' ]
7278 ord = field ['ord' ]
73- type = "_field_" + name #prefix to avoid potential clashes
79+ type = "_field_" + name # prefix to avoid potential clashes
7480 self .modelFieldPos [mid ][name ] = ord
7581 self .modelFieldPos [mid32 ][name ] = ord
76- if type not in self .fieldTypes : #avoid dupes
82+ if type not in self .fieldTypes : # avoid dupes
7783 self .fieldTypes [type ] = name
7884
7985 def valueForField (self , mid , flds , fldName ):
@@ -102,7 +108,7 @@ def valueForField(self, mid, flds, fldName):
102108 print "_modelFieldPos" + self .modelFieldPos
103109 print "Error was: " + e .message
104110
105- def myLoadCollection (self , _self ):
111+ def myLoadCollection (self , _self ):
106112 # Create a new SQL function that we can use in our queries.
107113 mw .col .db ._db .create_function ("valueForField" , 3 , self .valueForField )
108114
0 commit comments