Welcome to mirror list, hosted at ThFree Co, Russian Federation.

comments_utils.py « blenderkit - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 121beeb14da43f207a9377cc845c016dbca7f8a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# mainly update functions and callbacks for ratings properties, here to avoid circular imports.
import bpy
from blenderkit import utils, paths, tasks_queue, rerequests

import threading
import requests
import logging

bk_logger = logging.getLogger('blenderkit')


def upload_comment_thread(url, comment='', headers=None):
    ''' Upload rating thread function / disconnected from blender data.'''
    bk_logger.debug('upload comment ' + comment)

    # rating_url = url + rating_name + '/'
    data = {
        "content_type": "",
        "object_pk": "",
        "timestamp": "",
        "security_hash": "",
        "honeypot": "",
        "name": "",
        "email": "",
        "url": "",
        "comment": comment,
        "followup": False,
        "reply_to": None
    }

    # try:
    r = rerequests.put(url, data=data, verify=True, headers=headers)
    print(r)
    print(dir(r))
    print(r.text)
    # except requests.exceptions.RequestException as e:
    #     print('ratings upload failed: %s' % str(e))


def upload_comment_flag_thread(url, comment_id='', flag='like', headers=None):
    ''' Upload rating thread function / disconnected from blender data.'''
    bk_logger.debug('upload comment flag' + str(comment_id))

    # rating_url = url + rating_name + '/'
    data = {
        "comment": comment_id,
        "flag": flag,
    }

    # try:
    r = rerequests.post(url, data=data, verify=True, headers=headers)
    print(r)
    print(dir(r))
    print(r.text)
    # except requests.exceptions.RequestException as e:
    #     print('ratings upload failed: %s' % str(e))


def send_comment_to_thread(url, comment, headers):
    '''Sens rating into thread rating, main purpose is for tasks_queue.
    One function per property to avoid lost data due to stashing.'''
    thread = threading.Thread(target=upload_comment_thread, args=(url, comment, headers))
    thread.start()


def store_comments_local(asset_id, comments):
    context = bpy.context
    ac = context.window_manager.get('asset comments', {})
    ac[asset_id] = comments
    context.window_manager['asset comments'] = ac


def get_comments_local(asset_id):
    context = bpy.context
    context.window_manager['asset comments'] = context.window_manager.get('asset comments', {})
    comments = context.window_manager['asset comments'].get(asset_id)
    if comments:
        return comments
    return None


def get_comments(asset_id, headers):
    '''
    Retrieve ratings from BlenderKit server. Can be run from a thread
    Parameters
    ----------
    asset_id
    headers

    Returns
    -------
    ratings - dict of type:value ratings
    '''
    url = paths.get_api_url() + 'comments/assets-uuidasset/' + asset_id + '/'
    params = {}
    r = rerequests.get(url, params=params, verify=True, headers=headers)
    if r is None:
        return
    if r.status_code == 200:
        rj = r.json()
        comments = []
        # store comments - send them to task queue

        tasks_queue.add_task((store_comments_local, (asset_id, rj['results'])))

        # if len(rj['results'])==0:
        #     # store empty ratings too, so that server isn't checked repeatedly
        #     tasks_queue.add_task((store_rating_local_empty,(asset_id,)))
        # return ratings