Quantcast
Channel: Zammad - Community - Latest posts
Viewing all articles
Browse latest Browse all 6760

Export/Import Overviews using the rails console

$
0
0

I found also the solution to this.

Instead of using the rails console I can use the API.

Here some example code to import a JSON file using the API in Python

import json
import requests
from requests.exceptions import HTTPError

# Function to load JSON data from a file
def load_json_from_file(file_path):
    try:
        with open(file_path, 'r') as file:
            json_data = json.load(file)
        return json_data
    except FileNotFoundError:
        print(f"Error: The file '{file_path}' was not found.")
        return None
    except json.JSONDecodeError:
        print(f"Error: The file '{file_path}' does not contain valid JSON.")
        return None

# Function to import a JSON object to the API endpoint
def import_json_to_api(json_data, api_url, api_token):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Token token={api_token}'
    }
    
    try:
        response = requests.post(api_url, headers=headers, data=json.dumps(json_data), verify=False)
        response.raise_for_status()  # Raise an HTTPError if the HTTP request returned an unsuccessful status code
        print(f"JSON data imported successfully: {response.status_code}")
    except HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")  # HTTP error
        if response.content:
            print(f"Response content: {response.content.decode()}")  # Print response content for more details
    except Exception as err:
        print(f"An error occurred: {err}")  # Other errors

# Example usage
domain_url = 'https://example.com'  # Replace with your actual domain URL
api_endpoint = '/api/v1/overviews/'
api_url = f'{domain_url}{api_endpoint}'
api_token = 'your_api_token'  # Replace with your actual API token

# Load JSON data from file
json_file_path = 'path_to_your_json_file.json'  # Replace with your actual JSON file path
json_data = load_json_from_file(json_file_path)

if json_data:
    import_json_to_api(json_data, api_url, api_token)

The JSON file has to be built like this:

{
    "id": 28,
    "name": "Escalated Tickets Manager",
    "link": "escalated_tickets_manager",
    "prio": 17,
    "condition": {
        "ticket.escalation_at": {
            "operator": "till (relative)",
            "value": "10",
            "range": "minute"
        }
    },
    "order": {
        "by": "escalation_at",
        "direction": "ASC"
    },
    "group_by": "organization",
    "group_direction": "ASC",
    "organization_shared": false,
    "out_of_office": false,
    "view": {
        "s": [
            "title",
            "customer",
            "group",
            "owner",
            "escalation_at"
        ]
    },
    "active": true,
    "updated_by_id": 85,
    "created_by_id": 85,
    "created_at": "2024-05-27T09:25:33.357Z",
    "updated_at": "2024-05-27T09:25:33.350Z",
    "role_ids": [
        502
    ],
    "user_ids": []
},
{
    "id": 29,
    "name": "Escalated Tickets",
    "link": "escalated_tickets",
    "prio": 18,
    "condition": {
        "ticket.escalation_at": {
            "operator": "till (relative)",
            "value": "10",
            "range": "minute"
        }
    },
    "order": {
        "by": "escalation_at",
        "direction": "ASC"
    },
    "group_by": "organization",
    "group_direction": "ASC",
    "organization_shared": false,
    "out_of_office": false,
    "view": {
        "s": [
            "title",
            "customer",
            "group",
            "owner",
            "escalation_at"
        ]
    },
    "active": true,
    "updated_by_id": 85,
    "created_by_id": 85,
    "created_at": "2024-05-27T09:25:45.791Z",
    "updated_at": "2024-05-27T09:25:45.783Z",
    "role_ids": [
        504
    ],
    "user_ids": []
}

This way if role_ids and all other parameters already exists and are exactly the same, it is possible to import overviews from a staging system to a production system

Cheerio!


Viewing all articles
Browse latest Browse all 6760

Trending Articles