From a668d3ef341b143bc647a2f2f2d5ece566dba7c3 Mon Sep 17 00:00:00 2001
From: rr- <rr-@sakuya.pl>
Date: Mon, 9 May 2016 22:39:04 +0200
Subject: [PATCH] server/tags: output tag category usages

---
 API.md                                                      | 4 +++-
 server/szurubooru/func/tag_categories.py                    | 1 +
 server/szurubooru/tests/api/test_tag_category_creating.py   | 6 +++++-
 server/szurubooru/tests/api/test_tag_category_retrieving.py | 1 +
 server/szurubooru/tests/api/test_tag_category_updating.py   | 1 +
 5 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/API.md b/API.md
index ae10e3df..432d579e 100644
--- a/API.md
+++ b/API.md
@@ -1403,7 +1403,8 @@ experience.
 ```json5
 {
     "name":  <name>,
-    "color": <color>
+    "color": <color>,
+    "usages": <usages>
 }
 ```
 
@@ -1411,6 +1412,7 @@ experience.
 
 - `<name>`: the category name.
 - `<color>`: the category color.
+- `<usages>`: how many tags is the given category used with.
 
 ## Detailed tag category
 **Description**
diff --git a/server/szurubooru/func/tag_categories.py b/server/szurubooru/func/tag_categories.py
index de9df8b2..beeef9b8 100644
--- a/server/szurubooru/func/tag_categories.py
+++ b/server/szurubooru/func/tag_categories.py
@@ -18,6 +18,7 @@ def serialize_category(category):
     return {
         'name': category.name,
         'color': category.color,
+        'usages': category.tag_count,
     }
 
 def serialize_category_with_details(category):
diff --git a/server/szurubooru/tests/api/test_tag_category_creating.py b/server/szurubooru/tests/api/test_tag_category_creating.py
index cd3f5fbf..62fe2506 100644
--- a/server/szurubooru/tests/api/test_tag_category_creating.py
+++ b/server/szurubooru/tests/api/test_tag_category_creating.py
@@ -21,7 +21,11 @@ def test_creating_category(test_ctx):
         test_ctx.context_factory(
             input={'name': 'meta', 'color': 'black'},
             user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
-    assert result['tagCategory'] == {'name': 'meta', 'color': 'black'}
+    assert result['tagCategory'] == {
+        'name': 'meta',
+        'color': 'black',
+        'usages': 0,
+    }
     assert len(result['snapshots']) == 1
     category = db.session.query(db.TagCategory).one()
     assert category.name == 'meta'
diff --git a/server/szurubooru/tests/api/test_tag_category_retrieving.py b/server/szurubooru/tests/api/test_tag_category_retrieving.py
index 0ca52e5e..6c22dbe9 100644
--- a/server/szurubooru/tests/api/test_tag_category_retrieving.py
+++ b/server/szurubooru/tests/api/test_tag_category_retrieving.py
@@ -41,6 +41,7 @@ def test_retrieving_single(test_ctx):
         'tagCategory': {
             'name': 'cat',
             'color': 'dummy',
+            'usages': 0,
         },
         'snapshots': [],
     }
diff --git a/server/szurubooru/tests/api/test_tag_category_updating.py b/server/szurubooru/tests/api/test_tag_category_updating.py
index abd19e2f..ee182c47 100644
--- a/server/szurubooru/tests/api/test_tag_category_updating.py
+++ b/server/szurubooru/tests/api/test_tag_category_updating.py
@@ -40,6 +40,7 @@ def test_simple_updating(test_ctx):
     assert result['tagCategory'] == {
         'name': 'changed',
         'color': 'white',
+        'usages': 0,
     }
     assert len(result['snapshots']) == 1
     assert tag_categories.try_get_category_by_name('name') is None