BlenderKit: improve api key saving

This commit is contained in:
Vilem Duha 2019-05-08 23:09:11 +02:00
parent 9be41665fd
commit 06ecc07ed0
7 changed files with 43 additions and 36 deletions

View File

@ -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(

View File

@ -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()

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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:
# 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()
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.'
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'''