[Bf-extensions-cvs] [06ecc07e] master: BlenderKit: improve api key saving

Vilem Duha noreply at git.blender.org
Sun May 19 16:02:55 CEST 2019


Commit: 06ecc07ed0435400864c98ae3b0f05210d6be6ff
Author: Vilem Duha
Date:   Wed May 8 23:09:11 2019 +0200
Branches: master
https://developer.blender.org/rBA06ecc07ed0435400864c98ae3b0f05210d6be6ff

BlenderKit: improve api key saving

===================================================================

M	blenderkit/__init__.py
M	blenderkit/oauth.py
M	blenderkit/paths.py
M	blenderkit/search.py
M	blenderkit/tasks_queue.py
M	blenderkit/upload.py
M	blenderkit/utils.py

===================================================================

diff --git a/blenderkit/__init__.py b/blenderkit/__init__.py
index c9f80c1e..66d31b9c 100644
--- a/blenderkit/__init__.py
+++ b/blenderkit/__init__.py
@@ -1235,7 +1235,6 @@ class BlenderKitAddonPreferences(AddonPreferences):
         description="API key used to refresh the token regularly.",
         default="",
         subtype="PASSWORD",
-        update=utils.save_prefs
     )
 
     login_attempt: BoolProperty(
diff --git a/blenderkit/oauth.py b/blenderkit/oauth.py
index d414c5a1..2e0f76eb 100644
--- a/blenderkit/oauth.py
+++ b/blenderkit/oauth.py
@@ -119,13 +119,12 @@ def refresh_token(api_key_refresh):
 
 
 def write_tokens(auth_token, refresh_token):
-    utils.p('writing tokens?')
+    utils.p('writing tokens')
     preferences = bpy.context.preferences.addons['blenderkit'].preferences
-    preferences.api_key = auth_token
     preferences.api_key_refresh = refresh_token
+    preferences.api_key = auth_token
     preferences.login_attempt = False
     props = utils.get_search_props()
-    search.get_profile()
     props.report = 'Login success!'
     search.get_profile()
 
diff --git a/blenderkit/paths.py b/blenderkit/paths.py
index 1595ad78..310d6ec1 100644
--- a/blenderkit/paths.py
+++ b/blenderkit/paths.py
@@ -36,6 +36,7 @@ BLENDERKIT_SETTINGS_FILENAME = os.path.join(_presets, "bkit.json")
 
 
 def get_bkit_url():
+    # bpy.app.debug_value = 2
     d = bpy.app.debug_value
     # d = 2
     if d == 1:
diff --git a/blenderkit/search.py b/blenderkit/search.py
index 7faf6f18..7352fb2d 100644
--- a/blenderkit/search.py
+++ b/blenderkit/search.py
@@ -331,6 +331,8 @@ def writeblockm(tooltip, mdata, key='', pretext=None):  # for longer texts
         intext = mdata[key]
         if type(intext) == list:
             intext = list_to_str(intext)
+        if type(intext) == float:
+            intext = round(intext,3)
         intext = str(intext)
         if intext.rstrip() == '':
             return tooltip
@@ -419,7 +421,7 @@ def generate_tooltip(mdata):
     # t = writeblockm(t, mparams, key='shaders')
 
     if has(mparams, 'textureSizeMeters'):
-        t = writeblockm(t, mparams, key='textureSizeMeters', pretext='texture size in meters')
+        t += 'texture size: %s\n' % fmt_length(mparams['textureSizeMeters'])
 
     if has(mparams, 'textureResolutionMax') and mparams['textureResolutionMax'] > 0:
         if mparams['textureResolutionMin'] == mparams['textureResolutionMax']:
@@ -668,7 +670,7 @@ class Searcher(threading.Thread):
             urlquery = url + requeststring
 
         try:
-            # utils.p(urlquery)
+            utils.p(urlquery)
             r = requests.get(urlquery, headers=headers)
             reports = ''
             # utils.p(r.text)
diff --git a/blenderkit/tasks_queue.py b/blenderkit/tasks_queue.py
index 9cf3720f..b86b29da 100644
--- a/blenderkit/tasks_queue.py
+++ b/blenderkit/tasks_queue.py
@@ -28,7 +28,6 @@ def add_task(task):
 
 def queue_worker():
     q = get_queue()
-    # utils.p('queue timer')
     while not q.empty():
         utils.p('as a task:   ')
         print('window manager', bpy.context.window_manager)
@@ -48,3 +47,5 @@ def register():
 
 def unregister():
     bpy.app.handlers.load_post.remove(scene_load)
+
+
diff --git a/blenderkit/upload.py b/blenderkit/upload.py
index 30e17f27..06afd1ef 100644
--- a/blenderkit/upload.py
+++ b/blenderkit/upload.py
@@ -501,6 +501,7 @@ def get_upload_location(props):
         return None
     return None
 
+
 def check_storage_quota(props):
     if not props.is_private:
         return True
@@ -514,12 +515,13 @@ def check_storage_quota(props):
             return False
         search.write_profile(adata)
         profile = adata
-    print(profile.keys())
-    if profile['user'].get('remainingPrivateQuota')>0:
+    quota = profile['user'].get('remainingPrivateQuota')
+    if quota is None or quota > 0:
         return True
     props.report = 'Private storage quota exceeded.'
     return False
 
+
 def start_upload(self, context, asset_type, as_new, metadata_only):
     props = utils.get_upload_props()
     storage_quota_ok = check_storage_quota(props)
@@ -618,7 +620,6 @@ def start_upload(self, context, asset_type, as_new, metadata_only):
             props.uploading = False
             return {'CANCELLED'}
 
-
     # props.upload_state = 'step 1'
     if metadata_only:
         props.uploading = False
@@ -717,7 +718,7 @@ class ModelUploadOperator(Operator):
             self.metadata_only = False
             props.name_changed = False
 
-        result =    start_upload(self, context, self.asset_type, self.as_new, self.metadata_only)
+        result = start_upload(self, context, self.asset_type, self.as_new, self.metadata_only)
 
         return result
 
diff --git a/blenderkit/utils.py b/blenderkit/utils.py
index 13b11481..39439da1 100644
--- a/blenderkit/utils.py
+++ b/blenderkit/utils.py
@@ -171,34 +171,36 @@ def load_prefs():
     if os.path.exists(fpath):
         with open(fpath, 'r') as s:
             prefs = json.load(s)
-            user_preferences.api_key = prefs.get('API_key','')
+            user_preferences.api_key = prefs.get('API_key', '')
             user_preferences.global_dir = prefs.get('global_dir', paths.default_global_dict())
-            user_preferences.api_key_refresh = prefs.get('API_key_refresh','')
+            user_preferences.api_key_refresh = prefs.get('API_key_refresh', '')
+
 
 def save_prefs(self, context):
-    # print(type(context),type(bpy.context))
+    # first check context, so we don't do this on registration or blender startup
     if not bpy.app.background and hasattr(bpy.context, 'view_layer'):
         user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
-        if user_preferences.api_key != '':
-            #we test the api key for lenght, so not a random accidentaly typed sequence gets saved.
-            if len(user_preferences.api_key)>25:
-
-                prefs = {
-                    'API_key': user_preferences.api_key,
-                    'API_key_refresh': user_preferences.api_key_refresh,
-                    'global_dir': user_preferences.global_dir,
-                }
-                # user_preferences.api_key = user_preferences.api_key.strip()
-                fpath = paths.BLENDERKIT_SETTINGS_FILENAME
-                f = open(fpath, 'w')
-                with open(fpath, 'w') as s:
-                    json.dump(prefs, s)
-                bpy.ops.wm.save_userpref()
-            else:
-                # reset the api key in case the user writes some nonsense, e.g. a search string instead of the Key
-                user_preferences.api_key = ''
-                props = get_search_props()
-                props.report = 'Login failed. Please paste a correct API Key.'
+        # we test the api key for lenght, so not a random accidentaly typed sequence gets saved.
+        lk = len(user_preferences.api_key)
+        if 0 < lk < 25:
+            # reset the api key in case the user writes some nonsense, e.g. a search string instead of the Key
+            user_preferences.api_key = ''
+            props = get_search_props()
+            props.report = 'Login failed. Please paste a correct API Key.'
+
+        prefs = {
+            'API_key': user_preferences.api_key,
+            'API_key_refresh': user_preferences.api_key_refresh,
+            'global_dir': user_preferences.global_dir,
+        }
+        # user_preferences.api_key = user_preferences.api_key.strip()
+        fpath = paths.BLENDERKIT_SETTINGS_FILENAME
+        f = open(fpath, 'w')
+        with open(fpath, 'w') as s:
+            json.dump(prefs, s)
+        bpy.ops.wm.save_userpref()
+
+
 
 def load_categories():
     categories.copy_categories()
@@ -267,10 +269,12 @@ def get_brush_props(context):
         return brush.blenderkit
     return None
 
-def p(text,text1 = '',text2 = '',text3 = '',text4 = '',text5 = ''):
+
+def p(text, text1='', text2='', text3='', text4='', text5=''):
     '''debug printing depending on blender's debug value'''
-    if bpy.app.debug_value != 0:
-        print(text, text1, text2, text3, text4,text5)
+    if bpy.app.debug_value != 10:
+        print(text, text1, text2, text3, text4, text5)
+
 
 def pprint(data):
     '''pretty print jsons'''



More information about the Bf-extensions-cvs mailing list