def get(self, request, workspace_id, name): # resource check workspace = Workspaces.objects.get_workspace_by_id(workspace_id) if not workspace: error_msg = "Workspace %s not found." % (workspace_id, ) return api_error(status.HTTP_404_NOT_FOUND, error_msg) dtable = DTables.objects.get_dtable(workspace, name) if not dtable: error_msg = "Table %s not found." % (name, ) return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check username = request.user.username if not check_dtable_permission(username, dtable.workspace, dtable): error_msg = "Permission denied." return api_error(status.HTTP_403_FORBIDDEN, error_msg) # checkout apps tokens = DTableAPIToken.objects.list_by_dtable(dtable) # access dtable server payload = {'admin': 'dtable', 'exp': int(time.time()) + 60 * 60 * 3} access_token = jwt.encode(payload, DTABLE_PRIVATE_KEY, algorithm='HS256').decode() headers = {'authorization': 'Token ' + access_token} app_status_url = DTABLE_SERVER_URL.strip( '/') + '/api/v1/internal/' + dtable.uuid.hex + '/connected-apps/' try: resp = requests.get(app_status_url, headers=headers) except Exception as e: logger.error('request url: %s error: %s', app_status_url, e) error_msg = 'Internal Server Error.' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if resp.status_code != 200: logger.error('request url: %s status code: %s', app_status_url, resp.status_code) error_msg = 'Internal Server Error.' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) try: connected_apps = resp.json()['connected_apps'] except Exception as e: logger.error('checkout connected apps from response error: %s', e) error_msg = "Internal Server Error" return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) app_status = [{ 'app_name': token.app_name, 'connected': token.app_name in connected_apps, 'last_access': datetime_to_isoformat_timestr(token.last_access) } for token in tokens] return Response({'api_status_list': app_status})
def dtable_row_share_link_view(request, token): # resource check dtable_row_share = DTableRowShares.objects.get_dtable_row_share_by_token( token) if not dtable_row_share: return render_error(request, 'DTable row share link does not exist.') workspace_id = dtable_row_share.workspace_id workspace = Workspaces.objects.get_workspace_by_id(workspace_id) if not workspace: return render_error(request, 'Workspace does not exist.') repo_id = workspace.repo_id repo = seafile_api.get_repo(repo_id) if not repo: return render_error(request, 'Library does not exist.') dtable_uuid = dtable_row_share.dtable_uuid dtable = DTables.objects.get_dtable_by_uuid(dtable_uuid) if not dtable: return render_error(request, 'DTable %s does not exist' % dtable_uuid) # generate json web token username = request.user.username payload = { 'exp': int(time.time()) + 86400 * 3, 'dtable_uuid': dtable.uuid.hex, 'username': username, 'permission': PERMISSION_READ, } try: access_token = jwt.encode(payload, DTABLE_PRIVATE_KEY, algorithm='HS256') except Exception as e: logger.error(e) return render_error(request, _('Internal Server Error')) url_for_row = '%s/api/v1/dtables/%s/tables/%s/rows/%s/' % \ (DTABLE_SERVER_URL.strip('/'), dtable_uuid, dtable_row_share.table_id, dtable_row_share.row_id) req_for_row = requests.Request( url_for_row, headers={"Authorization": "Token %s" % access_token.decode()}) url_for_columns = '%s/api/v1/dtables/%s/tables/%s/columns/' % \ (DTABLE_SERVER_URL.strip('/'), dtable_uuid, dtable_row_share.table_id) req_for_columns = requests.Request( url_for_columns, headers={"Authorization": "Token %s" % access_token.decode()}) try: row_content = requests.urlopen(req_for_row).read().decode() columns = requests.urlopen(req_for_columns).read().decode() except Exception as e: logger.error(e) return render_error(request, _('Internal Server Error')) return_dict = { 'row_content': row_content, 'columns': columns, 'workspace_id': workspace_id, 'dtable_name': dtable.name } return render(request, 'dtable_shared_row_view_react.html', return_dict)
def dtable_form_edit(request, token): """ Permission: 1. owner 2. group member 3. shared user with `rw` permission """ # resource check form_obj = DTableForms.objects.get_form_by_token(token) if not form_obj: return render_error(request, 'Table\'s form does not exist.') workspace_id = form_obj.workspace_id workspace = Workspaces.objects.get_workspace_by_id(workspace_id) if not workspace: return render_error(request, 'Workspace does not exist.') dtable_uuid = form_obj.dtable_uuid dtable = DTables.objects.get_dtable_by_uuid(dtable_uuid) if not dtable: return render_error(request, 'Table does not exist.') # permission check username = request.user.username permission = check_dtable_permission(username, workspace, dtable) if permission != PERMISSION_READ_WRITE: return render_permission_error(request, 'Permission denied.') if not check_user_workspace_quota(workspace): return render_error(request, 'Asset quota exceeded.') # generate json web token payload = { 'exp': int(time.time()) + 60 * 5, 'dtable_uuid': dtable_uuid, 'username': "******", 'permission': permission, } try: access_token = jwt.encode(payload, DTABLE_PRIVATE_KEY, algorithm='HS256') except Exception as e: logger.error(e) return render_error(request, _('Internal Server Error')) url = '%s/api/v1/dtables/%s/metadata/' % (DTABLE_SERVER_URL.strip('/'), dtable_uuid) req = requests.Request( url, headers={"Authorization": "Token %s" % access_token.decode()}) try: dtable_metadata = requests.urlopen(req).read().decode() except Exception as e: logger.error(e) return render_error(request, _('Internal Server Error')) share_type = form_obj.share_type shared_groups = list() if share_type == SHARED_GROUPS: group_ids = DTableFormShare.objects.list_by_form(form_obj) shared_groups = [{ 'id': group_id, 'name': group_id_to_name(group_id) } for group_id in group_ids] return_dict = { 'dtable_metadata': dtable_metadata, 'dtable_name': dtable.name, 'workspace_id': workspace_id, 'form_id': form_obj.form_id, 'form_config': form_obj.form_config, 'dtable_uuid': dtable.uuid.hex, 'dtable_web_service_url': DTABLE_WEB_SERVICE_URL, 'form_token': token, 'share_type': share_type, 'shared_groups': json.dumps(shared_groups), } return render(request, 'dtable_edit_form_view_react.html', return_dict)
def dtable_form_view(request, token): # resource check form_obj = DTableForms.objects.get_form_by_token(token) if not form_obj: return render_error(request, 'Table\'s form does not exist.') workspace_id = form_obj.workspace_id workspace = Workspaces.objects.get_workspace_by_id(workspace_id) if not workspace: return render_error(request, 'Workspace does not exist.') dtable_uuid = form_obj.dtable_uuid dtable = DTables.objects.get_dtable_by_uuid(dtable_uuid) if not dtable: return render_error(request, 'Table does not exist.') # permission check if not check_form_submit_permission(request, form_obj): return render_permission_error(request, _('Permission denied.')) # asset quota check if not check_user_workspace_quota(workspace): return render_error(request, _('Asset quota exceeded.')) # generate json web token payload = { 'exp': int(time.time()) + 60 * 5, 'dtable_uuid': dtable_uuid, 'username': "******", 'permission': PERMISSION_READ, } try: access_token = jwt.encode(payload, DTABLE_PRIVATE_KEY, algorithm='HS256') except Exception as e: logger.error(e) return render_error(request, _('Internal Server Error')) url = '%s/api/v1/dtables/%s/metadata/' % (DTABLE_SERVER_URL.strip('/'), dtable_uuid) req = requests.Request( url, headers={"Authorization": "Token %s" % access_token.decode()}) try: dtable_metadata = requests.urlopen(req).read().decode() except Exception as e: logger.error(e) return render_error(request, _('Internal Server Error')) return_dict = { 'version': SEATABLE_VERSION, 'dtable_metadata': dtable_metadata, 'workspace_id': workspace_id, 'form_id': form_obj.form_id, 'form_config': form_obj.form_config, 'dtable_name': dtable.name, 'dtable_web_service_url': DTABLE_WEB_SERVICE_URL, 'form_token': token, } return render(request, 'dtable_share_form_view_react.html', return_dict)