Пример #1
0
def _get_dtable_metadata(dtable_uuid):
    access_token = get_dtable_server_token(dtable_uuid)
    metadata_url = DTABLE_SERVER_URL.rstrip(
        '/') + '/api/v1/dtables/' + dtable_uuid + '/metadata/'
    headers = {'Authorization': 'Token ' + access_token.decode()}
    response = requests.get(metadata_url, headers=headers)
    return response.json().get('metadata')
Пример #2
0
def is_row_satisfy_filters(row_id,
                           filters,
                           filter_conjuntion,
                           dtable_uuid,
                           table_id,
                           dtable_server_access_token,
                           rule_id=None,
                           db_session=None):
    url = DTABLE_SERVER_URL.rstrip(
        '/'
    ) + '/api/v1/dtables/' + dtable_uuid + '/tables/' + table_id + '/is-row-satisfy-filters/'
    headers = {
        'Authorization': 'Token ' + dtable_server_access_token.decode('utf-8')
    }
    data = {
        'row_id': row_id,
        'filters': filters,
        'filter_conjunction': filter_conjuntion
    }
    res = requests.get(url, headers=headers, json=data)

    if res.status_code == 404:
        deal_invalid_rule(rule_id, db_session)
    if res.status_code != 200:
        logger.error(res.text)
        return False
    return json.loads(res.content).get('is_row_satisfy_filters')
Пример #3
0
def is_row_in_view(row_id,
                   view_id,
                   dtable_uuid,
                   table_id,
                   dtable_server_access_token,
                   rule_id=None,
                   db_session=None):
    url = DTABLE_SERVER_URL.rstrip(
        '/'
    ) + '/api/v1/dtables/' + dtable_uuid + '/tables/' + table_id + '/is-row-in-view/'
    headers = {
        'Authorization': 'Token ' + dtable_server_access_token.decode('utf-8')
    }
    params = {
        'row_id': row_id,
        'view_id': view_id,
    }
    res = requests.get(url, headers=headers, params=params)

    if res.status_code == 404:
        # perhaps 404 is reason for row_id, we only deal with 'view not found','table not found' and 'dtable not found'
        if not is_view_in_table(view_id, dtable_uuid, table_id,
                                dtable_server_access_token):
            deal_invalid_rule(rule_id, db_session)
    if res.status_code != 200:
        logger.error(res.text)
        return False
    return json.loads(res.content).get('is_row_in_view')
Пример #4
0
def send_notification(dtable_uuid, user_msg_list, dtable_server_access_token):
    url = DTABLE_SERVER_URL.rstrip(
        '/') + '/api/v1/dtables/' + dtable_uuid + '/notifications-batch/'
    headers = {
        'Authorization': 'Token ' + dtable_server_access_token.decode('utf-8')
    }
    body = {
        'user_messages': user_msg_list,
    }
    res = requests.post(url, headers=headers, json=body)

    if res.status_code != 200:
        logger.error(f'failed to send_notification {res.text}')
Пример #5
0
def list_rows_near_deadline(dtable_uuid,
                            table_id,
                            view_id,
                            date_column_name,
                            alarm_days,
                            dtable_server_access_token,
                            rule_id=None,
                            db_session=None):
    url = DTABLE_SERVER_URL.rstrip(
        '/') + '/api/v1/dtables/' + dtable_uuid + '/rows/'
    headers = {
        'Authorization': 'Token ' + dtable_server_access_token.decode('utf-8')
    }
    query_params = {
        'table_id': table_id,
        'view_id': view_id,
        'convert_link_id': True
    }
    try:
        res = requests.get(url, headers=headers, params=query_params)
    except Exception as e:
        logger.error(e)
        return []

    if res.status_code == 404:
        deal_invalid_rule(rule_id, db_session)
    if res.status_code != 200:
        logger.error(res.text)
        return []

    rows = json.loads(res.content).get('rows', [])
    rows_near_deadline = []
    for row in rows:
        deadline_date_date_str = row.get(date_column_name, '')
        if not deadline_date_date_str:
            continue
        if ' ' in deadline_date_date_str:
            deadline_date_date_str = deadline_date_date_str.split(' ')[0]
        try:
            deadline_date = datetime.strptime(deadline_date_date_str,
                                              '%Y-%m-%d').date()
        except Exception as e:
            # perhaps result-type of fomular column has been changed to non-date
            logger.warning(
                'date_column_name: %s value: %s, transfer to date error: %s',
                date_column_name, deadline_date_date_str, e)
            continue
        now_plus_alarm_date = date.today() + timedelta(days=int(alarm_days))
        if date.today() <= deadline_date <= now_plus_alarm_date:
            rows_near_deadline.append(row)
    return rows_near_deadline
Пример #6
0
def get_table_view_columns(dtable_uuid, table_id, view_id,
                           dtable_server_access_token):
    url = DTABLE_SERVER_URL.rstrip(
        '/') + '/api/v1/dtables/' + dtable_uuid + '/columns/'
    headers = {
        'Authorization': 'Token ' + dtable_server_access_token.decode('utf-8')
    }
    query_params = {'table_id': table_id, 'view_id': view_id}
    try:
        response = requests.get(url, params=query_params, headers=headers)
        columns = response.json()['columns']
    except Exception as e:
        logger.error(
            'dtable_uuid: %s, table_id: %s, view_id: %s request columns error: %s',
            dtable_uuid, table_id, view_id, e)
        return []
    return columns
Пример #7
0
def is_view_in_table(view_id, dtable_uuid, table_id,
                     dtable_server_access_token):
    url = DTABLE_SERVER_URL.rstrip(
        '/') + '/api/v1/dtables/' + dtable_uuid + '/metadata/'
    headers = {
        'Authorization': 'Token ' + dtable_server_access_token.decode('utf-8')
    }
    res = requests.get(url, headers=headers)
    # dtable not found
    if res.status_code == 404:
        return False
    if res.status_code != 200:
        return True
    tables = json.loads(res.content).get('metadata', {}).get('tables', {})
    for table in tables:
        if table['_id'] == table_id:
            for view in table['views']:
                if view['_id'] == view_id:
                    return True
    return False
Пример #8
0
def gen_notification_msg_with_row_id(dtable_uuid, table_id, view_id, row_id,
                                     msg, dtable_server_access_token,
                                     db_session):
    if not msg:
        return msg

    # checkout all blanks to fill in
    # if no blanks, just return msg
    blanks = set(re.findall(r'\{([^{]*?)\}', msg))
    if not blanks:
        return msg

    columns = get_table_view_columns(dtable_uuid, table_id, view_id,
                                     dtable_server_access_token)

    column_blanks, col_name_dict = _get_column_blanks(blanks, columns)

    if not column_blanks:
        return msg

    # get row of table-view-row
    row_url = DTABLE_SERVER_URL.rstrip(
        '/') + '/api/v1/dtables/{dtable_uuid}/rows/{row_id}/'.format(
            dtable_uuid=dtable_uuid, row_id=row_id)
    headers = {
        'Authorization': 'Token ' + dtable_server_access_token.decode('utf-8')
    }
    params = {'table_id': table_id, 'convert_link_id': True}
    try:
        response = requests.get(row_url, params=params, headers=headers)
        row = response.json()
    except Exception as e:
        logger.error(
            'dtable_uuid: %s, table_id: %s, row_id: %s, request row error: %s',
            dtable_uuid, table_id, row_id, e)
        return msg, {}

    msg = _fill_msg_blanks(dtable_uuid, msg, column_blanks, col_name_dict, row,
                           db_session)

    return msg
Пример #9
0
def list_users_by_column_key(dtable_uuid, table_id, view_id, row_id,
                             column_key, dtable_server_access_token):
    url = DTABLE_SERVER_URL.rstrip(
        '/') + '/api/v1/dtables/' + dtable_uuid + '/rows/' + row_id + '/'
    headers = {
        'Authorization': 'Token ' + dtable_server_access_token.decode('utf-8')
    }
    params = {
        'table_id': table_id,
        'view_id': view_id,
        'convert': False,
    }
    res = requests.get(url, headers=headers, params=params)

    if res.status_code != 200:
        logger.error(f'failed to list_users_by_column_key {res.text}')

    rowdict = json.loads(res.content)
    user_list = rowdict.get(column_key, [])
    if isinstance(user_list, str):
        return [user_list]
    return user_list
Пример #10
0
import time
import logging

import jwt
import requests
from django.conf import settings

from seahub.settings import DTABLE_PRIVATE_KEY, DTABLE_SERVER_URL

logger = logging.getLogger(__name__)

DTABLE_SERVER_SYS_INFO_URL = DTABLE_SERVER_URL.rstrip('/') + getattr(
    settings, 'DTABLE_SERVER_INFO_API', '/api/v1/admin/sys-info')


def gen_dtable_server_admin_token(username):
    payload = {
        'exp': int(time.time()) + 86400 * 3,
        'admin': username,
    }
    access_token = jwt.encode(payload, DTABLE_PRIVATE_KEY, algorithm='HS256')
    return access_token.decode()


def get_dtable_server_info(username):
    infos = {
        "web_socket_count": 0,
        "operation_count_since_up": 0,
        "loaded_dtables_count": 0,
        "last_period_operations_count": 0,
        "app_connection_count": 0