View Single Post
Old 01-30-2022, 05:20 AM   #9
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,482
Karma: 8025704
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
The problem happens because the template function uses lstrip() to remove the prefix. That function doesn't look for a string but instead strips out each character (case sensitive). Your user category is Pseudo so if an author begins with any of those letters (note that P is one of them) then it will be stripped. If an author name started with d then the same thing will happen.

This fixes it by changing lstrip() to a slice, removing N characters from the beginning where N is the length of the outer user category name passed to the function (user_cat_prefix). As a side benefit it will be a bit faster.
Code:
def evaluate(self, formatter, kwargs, mi, locals, val, col_name, user_cat_prefix):
    new_val = ''
    if hasattr(mi, '_proxy_metadata'):
        all_cats = mi.user_categories
        cats = {k:v for k,v in all_cats.items() if k.startswith(user_cat_prefix)}
        SEP = mi.metadata_for_field(col_name)['is_multiple'].get('list_to_ui', '')
        new_val = set()
        if SEP:
            val_ = val.split(SEP)
        else:
            val_ = [val]  
        prefix_length = len(user_cat_prefix)
        for user_cat, v in cats.items():
            repl = user_cat[prefix_length:]
            for user_cat_item, src_cat in v:
                if src_cat == col_name:
                    for item in val_:
                        if item == user_cat_item:
                            new_val.add(repl)
                            val_.remove(item)
                        else:
                            new_val.add(item)
        if new_val:
            return ', '.join(list(new_val))
    return val
chaley is offline   Reply With Quote