python add new categories to appear in the filter

Hi,

I wrote a script that synchronizes contacts from a SQL-Database to Kopano using python3-kopano.

I also added categories to these contacts.
However, these categories are not listed, when you want to search and filter.

So, how to check if any given category is already there and how to add it, if not?

It’s a public contact folder by the way, so all users should be able to filter with these categories.

Thanks for reading,
Roland.

I got some help from a kopano developer to get this running. Maybe the result is interesting for someone here.

First thing to know is that the categories are stored for each user separately. So you need to do this for everyone, who should have access to the new categories. My example script here does it just for one, but can easily adopted to run for several or all users.

What you need to get is a special property. If it’s not there, then the user never changed something in his or her category settings and the property needs to be created.

import kopano
from MAPI.Tags import PR_EC_WEBAPP_PERSISTENT_SETTINGS_JSON
import jsbeautifier
import json
import io
import copy

server = kopano.Server()
user = server.user('kopano-user')

list_cats = list()
kopano_cats = list()

cat_language = 'english'

cat = user.store.get_prop(PR_EC_WEBAPP_PERSISTENT_SETTINGS_JSON)
if cat:
    cat = user.store.prop(PR_EC_WEBAPP_PERSISTENT_SETTINGS_JSON).value
    cat = cat.decode()

    parsed_json_cat = json.loads(cat)
    list_cats = parsed_json_cat['settings']['kopano']['main']['categories']
else:
    print('No custom setting done yet')
    print('Setting default values')

    if cat_language == 'german':
        with io.open('default_cats_german.json', mode='r', encoding='utf-8') as f:
            parsed_json_cat = json.load(f)
        print('Using german category names')
    else:
        with open('default_cats_english.json') as f:
            parsed_json_cat = json.load(f)
        print('Using english category names')
    user.store.create_prop(PR_EC_WEBAPP_PERSISTENT_SETTINGS_JSON, json.dumps(parsed_json_cat).encode())

new_cats = ['01/Custom Category', '02/Another custom category', 'utf8 Example: Aufzüge']

print(' ')
print(' ')
print('Kopano Categories before: ')
print(' ')
for item in list_cats:
    print(item['name'])
    kopano_cats.append(item['name'])

print(' ')
print(' ')
print('JSON before: ')
print(' ')
pretty_cat = jsbeautifier.beautify(str(parsed_json_cat))
print(pretty_cat)

json_cat = json.dumps(parsed_json_cat)
old_parsed_json_cat = copy.deepcopy(parsed_json_cat)

for new_cat in new_cats:
    if new_cat not in kopano_cats:
        parsed_json_cat['settings']['kopano']['main']['categories'].append({"name": new_cat, "color": "#BDC3C7", "quickAccess": False})

print(' ')
print(' ')
print('Kopano Categories after: ')
print(' ')
list_cats = parsed_json_cat['settings']['kopano']['main']['categories']
for item in list_cats:
    print(item['name'])

print(' ')
print(' ')
print('JSON after: ')
print(' ')
pretty_cat = jsbeautifier.beautify(str(parsed_json_cat))
print(pretty_cat)

if old_parsed_json_cat != parsed_json_cat:
    json_cat = json.dumps(parsed_json_cat)
    json_cat = json_cat.encode()
    user.store.prop(PR_EC_WEBAPP_PERSISTENT_SETTINGS_JSON).value = json_cat
else:
    print('Nothing changed, exiting.')

And these are the default categories in english:
(so, just put the content in a file called default_cats_english.json before running the script on users, who didn’t change the cat settings)

{
    "settings": {
        "kopano": {
            "main": {
                "categories": [{
                    "name": "Red",
                    "used": false,
                    "quickAccess": true,
                    "sortIndex": 0,
                    "color": "#e40023",
                    "standardIndex": 6
                }, {
                    "name": "Orange",
                    "used": false,
                    "quickAccess": true,
                    "sortIndex": 1,
                    "color": "#f99406",
                    "standardIndex": 2
                }, {
                    "name": "Yellow",
                    "used": false,
                    "quickAccess": true,
                    "sortIndex": 2,
                    "color": "#f7ca17",
                    "standardIndex": 4
                }, {
                    "name": "Green",
                    "used": false,
                    "quickAccess": true,
                    "sortIndex": 3,
                    "color": "#5ab556",
                    "standardIndex": 3
                }, {
                    "name": "Blue",
                    "used": false,
                    "quickAccess": true,
                    "sortIndex": 4,
                    "color": "#0f70bd",
                    "standardIndex": 5
                }, {
                    "name": "Purple",
                    "used": false,
                    "quickAccess": true,
                    "sortIndex": 5,
                    "color": "#912887",
                    "standardIndex": 1
                }, {
                    "name": "Birthday",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#DCB6E9",
                    "standardIndex": ""
                }, {
                    "name": "Business",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Competition",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Favorites",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Gifts",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Goals/Objectives",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Holiday",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#EBDA6C",
                    "standardIndex": ""
                }, {
                    "name": "Holiday Cards",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Hot Contacts",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Ideas",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Important",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#F4B7B5",
                    "standardIndex": ""
                }, {
                    "name": "International",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Key Customer",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Miscellaneous",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Personal",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#D9E9B6",
                    "standardIndex": ""
                }, {
                    "name": "Phone Calls",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Phone Interview",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#C1E9B6",
                    "standardIndex": ""
                }, {
                    "name": "Preparation Required",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#B6B6E9",
                    "standardIndex": ""
                }, {
                    "name": "Required",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#E9DAB6",
                    "standardIndex": ""
                }, {
                    "name": "Special Date",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#E9BBB6",
                    "standardIndex": ""
                }, {
                    "name": "Status",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Strategies",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Suppliers",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Time & Expenses",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Travel Required",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#B6DDE9",
                    "standardIndex": ""
                }, {
                    "name": "VIP",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Waiting",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#BDC3C7",
                    "standardIndex": ""
                }, {
                    "name": "Work",
                    "used": "",
                    "quickAccess": false,
                    "sortIndex": 100000,
                    "color": "#B6CAE9",
                    "standardIndex": ""
                }]
            }
        }
    }
}

My example script does not delete categories, if you make the new_cats list smaller at any time. If you want that, you need to make some changes.