# Category Trees

Category Tree is the structure where categories/subcategories are created for the products to go on sale. Category tree in Omnitron is formed by the combination of two different structures: `CategoryTree` and `CategoryNode`.

## Campaigns

On the Catalogs page **Products and Catalogs > Catalogs**, the detail page is entered through a catalog.

The **pen** icon that appears when the relevant category point is hovered with the cursor is clicked on. This opens the *Category Edit* form. Clicking on the **Campaigns** tab, and then the **Add Campaign** button, allows adding campaigns. After filling in the required information, the **Save** button at the bottom is clicked on. *Campaigns* added here are reflected on the page of the relevant sales category.

## Large Image

On the Catalogs page **Products and Catalogs > Catalogs**, the detail page is entered through a catalog.

The pen icon that appears when the relevant category point is hovered with the cursor is clicked on. This opens the Category Edit form. The **Large Image** tab is clicked on for the addition of large images. After filling in the required information, the **Save** button at the bottom is clicked on. The **Large Image** added here is reflected on the page of the relevant sales category.

## Small Image

On the Catalogs page (**Products and Catalogs > Catalogs**), the detail page is entered through a catalog.

The **pen** icon that appears when the relevant category point is hovered with the cursor is clicked on. This opens the **Category Edit Form**. The **Small Image** tab is clicked on for the addition of small images. After filling in the required information, the **Save** button at the bottom is clicked on. The **Small Image** added here is reflected on the page of the relevant sales category.

## Explanation

On the Catalogs page **Products and Catalogs > Catalogs**, the detail page is entered through a catalog.

The pen icon that appears when the relevant category point is hovered with the cursor is clicked on. This opens up the **Category Edit Form**. The details about the relevant category are entered in the description field and the **Save** button at the bottom is clicked on. The description added here is reflected on the page of the relevant sales category.

## Category Tree API Methods

### `GET` All Category Trees

Sample HTTP request to get all category trees lists from the system.

‘content\_type’ header represents the response type.

The ‘Authorization’ header is required for authentication. You can retrieve `api_token` by logging in.

**Request**

Path: `/api/v1/category_tree/`

```python
import requests

url = "https://{customer_api_url}/api/v1/category_tree/"
api_token = "API TOKEN"

headers = {
   'content-type': 'application/json',
   'Authorization': 'Token {}'.format(api_token)
}

response = requests.get(url, headers=headers)
print(response.text)

```

**Filters**

All fields except the existing “category\_root” can be used in the `CategoryTree` model.

**Response**

```json
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"pk": 1,
"name": "test catalog",
"category_root": {
"pk": 246,
"name": "test catalog",
"order": 1,
"numchild": 0,
"depth": 1
"attributes": {},
"attributes_kwargs": {},
"sort_option": null,
"uuid": "2798d37e-7a04-487f-a693-fa0c4b174656",
"path": "0001",
"created_date": "2021-10-06T13:48:07.643569Z",
"modified_date": "2021-11-01T13:54:52.526750Z"
}
}
]
}
```

### `POST` Create Category Tree

Records new objects in the CategoryTree table. The CategoryTreeSerializer class defined at `omnitron.catalogs.resources.serializers` is used to validate the data.

‘content\_type’ header represents the response type.

The ‘Authorization’ header is required for authentication. You can retrieve `api_token` by logging in.

**Request**

Path: `/api/v1/category_tree/`

```python
import requests

url = "https://{customer_api_url}/api/v1/category_tree/"
api_token = "API TOKEN"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}

data = {
    "name" : "new_tree"
}

response = requests.post(url, headers=headers, data=data)
print(response.text)

```

If there is another category tree with the same name, a 400 response status code is returned.

**Response**

Information of the created `CategoryTree` object is returned. The features are described above. The status code will be 201.

```json
{
    "pk": 2,
    "name": "new_tree",
    "category_root": {
        "pk": 247,
        "name": "new_tree",
        "order": 1,
        "numchild": 0,
        "depth": 1,
        "attributes": {},
        "attributes_kwargs": {},
        "sort_option": null,
        "uuid": "06e18786-74c6-4c3c-a8dc-60e7a5dd494a",
        "path": "0002",
        "created_date": "2022-12-21T07:50:08.386058Z",
        "modified_date": "2022-12-21T07:50:08.386095Z"
    }
}

```

**Incorrect Response Example**

CategoryTree model has a name field and its property is unique=True, which means the category name field must be unique. Otherwise it will display an error as shown below.

```json
{
    "name": [
        "category tree with this name already exists."
    ]
}
```

### `PATCH` - `PUT` Update Category Tree

If not found, it returns a 404 response status code.

If a record with an existing “name” is being attempted, this returns a 400 response status code.

Path: `/api/v1/category_tree/{pk}/`

```json
{
 "name" : "new_tree__"
}
```

**Response**

```json

{
   "pk": 6,
   "name": "new_tree__",
   "category_root": {
       "pk": 247,
       "name": "new_tree",
       "order": 1,
       "numchild": 0,
       "depth": 1,
       "attributes": {},
       "attributes_kwargs": {},
       "sort_option": null,
       "uuid": "06e18786-74c6-4c3c-a8dc-60e7a5dd494a",
       "path": "0003",
       "created_date": "2022-12-21T07:50:08.386058Z",
       "modified_date": "2022-12-21T07:50:08.386095Z"
    }

}
```

**Incorrect Response Example**

If an existing tree name is inserted during the update, it will again throw a unique error for the name field.

```json
{
    "name": [
         "category tree with this name already exists."
    ]
}
```

### `DELETE` Category Tree

If the category tree is not found, it returns a 404 response status code.

If the category tree with provided ID is linked to a catalog or if there are products linked to the category tree, a 400 response status code will be returned.

Path: `/api/v1/category_tree/{pk}/`

**Response**

```
Status: 204
```

**Incorrect Response Example**

If the shop is still used by some catalog(s), the status code (406) is returned. This means Not Acceptable for deletion.

```json
{
    "non_field_errors": "Shop is still being used by some catalog(s).",
    "error_code": "category_600_6"
}
```

### `GET` Category Tree

Tree returns the relevant category tree with its sub branches. If the category tree with provided ID is not available, it returns a 404 response status code.

Path: `/api/v1/category_tree/{pk}/tree/`

**Response**

```json
[
    {
        "pk": 1,
        "name": "new_tree",
        "order": 1,
        "numchild": 8,
        "depth": 1,
        "attributes": {},
        "attributes_kwargs": {},
        "sort_option": 1,
         "uuid": "cf108732-fa7c-4f0f-9bea-fb59691a9d7c",
         "path": "0001"
    },
    {
        "pk": 4,
        "name": "Test Child",
        "order": 1,
        "numchild": 12,
        "depth": 2,
        "attributes": {
            "campaigns":  [],
            "big_image": {
                 "url": "test-child",
                 "image": "cms/2021/02/08/
                 e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",
                 "title": "Test Child"
            },
            "small_image": {
                "url": "test-child",
                "image":
                "cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",
                "title": "Household Linen"
            },
            "description": ""
            },
            "attributes_kwargs": {
                 "campaigns": [],
                     "big_image": {
                        "value": {
                            "image":{
                                "url": "/media/cms/2021/02/08/e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",
                                "value": "cms/2021/02/08/e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",
                        "data_type":"image"
    }

    },
    "data_type": "nested"
},
"small_image": {
     "value": {
          "image": {
              "url": "/media/cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",
              "value": "cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",
            "data_type": "image"                   
            }

        },
}]
```

### `GET` Category Tree Detail

Retrieves detailed information about `category_tree`. Shows details of sort option information and sorting algorithm information, as well as the associated child information in a list.

Returns the relevant category tree with its sub branches in a more detailed manner. If the category tree is not found, it returns a 404 response status code.

**Request**

```python

import requests

url = "https://{customer_api_url}/api/v1/category_tree/{PK}/tree_detailed/"
api_token = "API TOKEN"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}

response = requests.get(url, headers=headers)
print(response.text)

```

Path: `/api/v1/category_tree/{pk}/tree_detailed/`

**Response**

```json

[
    {
        "pk": 1,
        "name": "Shop",
        "order": 1,
        "numchild": 8,
        "depth": 1,
         "attributes": {},
          "attributes_kwargs": {},
          "sort_option": {},
           "uuid": "cf108732-fa7c-4f0f-9bea-fb59691a9d7c",
           "path": "0001",
            "children": [
                {
                    "pk": 43,
                    "name": "Test Child",
                    "order": 1,
                    "numchild": 12,
                    "depth": 2,
                    "attributes": {
                    "campaigns": [],
                    "big_image": {
                        "url": "test-child",
                         "image": "cms/2021/02/08/e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",
                         "title": "Test Child"
                    },
                    "small_image": {
                         "url": "test-child",
                         "image": "cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",
                         "title": "Test Child"
                    },
                    "description": ""
                },
                "attributes_kwargs": {
                     "campaigns": [],
                     "big_image": {
                         "value": {
                             "image": {
                                 "url":
                                    "/media/cms/2021/02/08/e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",
                                "value": "cms/2021/02/08/e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",

                        "data_type": "image"}
                    },
                    "data_type": "nested"
                },
                "small_image": {
                    "value":{
                        "image": {
                            "url": "/media/cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",
                            "value": "cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",

                        "data_type": "image"

                }
                "data_type": "nested"
                }
            },
            "sort_option": 12,
             "uuid": "9ea60027-c3a0-4cc3-9acc-6ecc462b1589",
              "path": "00010001"
            }
        ]
    },
    {

        "pk": 43,
        "name": "Test Child",
         "order": 1,
         "numchild": 12,
         "depth": 2,
          "attributes": {
               "campaigns": [],
               "big_image": {
                   "url": "test-child",
                   "image": "cms/2021/02/08/e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",
                    "title": "Test Child"
                },
                "small_image": {
                      "url":"test-child",
                      "image": "cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",
                       "title": "Test Child"
                },
                "description": ""
        },
        "attributes_kwargs": {
             "campaigns": [],
             "big_image": {
                      "value": {
                          "image": {
                              "url": "/media/cms/2021/02/08/e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",
                               "value": "cms/2021/02/08/e1f3fb64-98f2-4f8e-ad0a-4178a775c087.jpg",

                  "data_type":"image"}
            },
            "data_type": "nested"
            },
             "small_image": {
                  "value": {
                       "image": {
                           "url": "/media/cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",
                           "value": "cms/2021/02/08/7bcbcec5-b468-475b-b45e-e9f567151a94.jpg",
                           "data_type": "image"}
                        },
                         "data_type":"nested"}
                },
                    "sort_option": {},
                     "uuid": "9ea60027-c3a0-4cc3-9acc-6ecc462b1589",
                    "path": "00010001",
                    "children": []
        }
]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apidocs.akinon.com/omnitron/catalogue/category-trees.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
