Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spindle
1 change: 1 addition & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async def test_build(self):
"report_sentence_hits_initial",
"original_html_initial",
"categories",
"categories_auto_add",
"report_categories",
"keywords",
"report_keywords",
Expand Down
12 changes: 12 additions & 0 deletions tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,18 @@ async def test_adding_report_categories(self):
current = await self.data_svc.get_report_category_keynames(report_id)
self.assertEqual(set(current), {"aerospace", "music"}, msg="Categories were not added.")

async def test_adding_report_categories_auto_add(self):
"""Function to test successfully adding categories to a report which auto-adds another category."""
report_id, report_title = str(uuid4()), "Auto-Add Categories to Me!"
await self.submit_test_report(dict(uid=report_id, title=report_title, url="auto.add.categories"))

data = dict(index="set_report_keywords", report_title=report_title, victims=dict(category=["rockets"]))
resp = await self.client.post("/rest", json=data)
self.assertTrue(resp.status < 300, msg="Adding categories resulted in a non-200 response.")

current = await self.data_svc.get_report_category_keynames(report_id)
self.assertEqual({"rockets", "aerospace"}, set(current), msg="Categories were not auto-added.")

async def test_removing_report_categories(self):
"""Function to test successfully removing categories from a report."""
report_id, report_title = str(uuid4()), "Remove Categories From Me!"
Expand Down
13 changes: 13 additions & 0 deletions tests/thread_app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ async def setUpAsync(self):
cat_1 = dict(uid="c010101", keyname="aerospace", name="Aerospace")
cat_2 = dict(uid="c123456", keyname="music", name="Music")
cat_3 = dict(uid="c898989", keyname="film", name="Film")
cat_4 = dict(uid="c000001", keyname="rockets", name="Rockets")
group_1 = dict(uid="apt1", name="APT1")
group_2 = dict(uid="apt2", name="APT2")
group_3 = dict(uid="apt3", name="APT3")
Expand All @@ -144,6 +145,18 @@ async def setUpAsync(self):
await self.db.insert("keywords", group)
self.web_svc.categories_dict[cat["keyname"]] = dict(name=cat["name"], sub_categories=[])

self.web_svc.categories_dict["rockets"] = dict(name="Rockets", sub_categories=[], auto_select=["aerospace"])
with suppress(sqlite3.IntegrityError):
await self.db.insert("categories", cat_4)
await self.db.insert(
"categories_auto_add",
dict(
uid="aa000001",
selected="rockets",
auto_add="aerospace",
),
)

# Carry out pre-launch tasks except for prepare_queue(): replace the call of this to return (and do) nothing
# We don't want multiple prepare_queue() calls so the queue does not accumulate between tests
with patch.object(RestService, "prepare_queue", return_value=None):
Expand Down
40 changes: 39 additions & 1 deletion threadcomponents/conf/categories/industry.json
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,51 @@
"name": "Federal Government",
"sub_categories": []
},
"diplomatic_services": {
"name": "Diplomatic Services",
"sub_categories": []
},
"government": {
"name": "Government",
"sub_categories": ["defence", "law_enforcement", "emergency_services", "courts", "local_gov", "state_gov",
"federal_gov"]
"federal_gov", "diplomatic_services"]
},
"general_public": {
"name": "General Public",
"sub_categories": []
},
"critical_infrastructure": {
"name": "Critical Infrastructure",
"sub_categories": [],
"auto_select": ["government", "financial", "agriculture", "food", "energy", "info_tech", "aerospace", "education",
"transport", "telecom", "water", "health"]
},
"political_orgs": {
"name": "Political Organisations",
"sub_categories": []
},
"retail": {
"name": "General Retail",
"sub_categories": []
},
"travel": {
"name": "International and Domestic Travel",
"sub_categories": []
},
"maritime_freight": {
"name": "Maritime Freight",
"sub_categories": []
},
"dissidents": {
"name": "Dissidents",
"sub_categories": []
},
"terrorist_groups": {
"name": "Terrorist Groups",
"sub_categories": []
},
"ngo": {
"name": "Non-Governmental Organisations",
"sub_categories": []
}
}
12 changes: 12 additions & 0 deletions threadcomponents/conf/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ CREATE TABLE IF NOT EXISTS categories (
display_name VARCHAR(200)
);

CREATE TABLE IF NOT EXISTS categories_auto_add (
-- For categories, the category to automatically add when adding a given category
uid VARCHAR(60) PRIMARY KEY,
-- The keyname of the category being selected
selected VARCHAR(40),
-- The keyname of the category to auto-add
auto_add VARCHAR(40),
UNIQUE (selected, auto_add),
FOREIGN KEY(selected) REFERENCES categories(keyname) ON DELETE CASCADE,
FOREIGN KEY(auto_add) REFERENCES categories(keyname) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS report_categories (
uid VARCHAR(60) PRIMARY KEY,
-- The UID of the report
Expand Down
10 changes: 10 additions & 0 deletions threadcomponents/managers/report_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ async def set_report_categories(self, request, criteria=None):
# Retrieve current report categories
current = await self.data_svc.get_report_category_keynames(report_id)
valid_categories = set(self.web_svc.categories_dict.keys()).intersection(categories)

auto_add = await self.data_svc.get_auto_selected_for_category(valid_categories)
valid_categories.update(auto_add)

to_add = valid_categories - set(current)
to_delete = set(current) - valid_categories

Expand Down Expand Up @@ -210,6 +214,12 @@ async def set_report_keywords(self, request, criteria=None):
categories = await self.data_svc.get_report_category_keynames(report_id)
current["victims"]["categories"] = categories or []

requested_categories = victims.get("category", [])
auto_add = await self.data_svc.get_auto_selected_for_category(requested_categories)
if auto_add:
success.update(refresh_page=True)
requested_categories += auto_add

# For each aggressor and victim, have the current-data and request-data ready to compare
aggressor_assoc = [AssociationWith.CN.value, AssociationWith.RG.value, AssociationWith.GR.value]
victim_assoc = [
Expand Down
Loading