Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new async Topics & Folder resources, blueprints & helper functions [NHUB-531] #1059

Merged
merged 52 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4d01fb0
Update topic resource to async
devketanpro Sep 2, 2024
777f8e2
refactore code
devketanpro Sep 2, 2024
2576a61
fix flake
devketanpro Sep 2, 2024
1170b00
Merge remote-tracking branch 'upstream/async' into NHUB-531
devketanpro Sep 2, 2024
7b09904
fix mypy
devketanpro Sep 2, 2024
e4f9838
fix folder assignment on create method
devketanpro Sep 2, 2024
a860968
update folder resource to async
devketanpro Sep 2, 2024
2745a0b
correction in folder async
devketanpro Sep 2, 2024
375e19f
use async topics func
devketanpro Sep 2, 2024
3df233a
refactore code
devketanpro Sep 2, 2024
146a8e7
fix tests
devketanpro Sep 2, 2024
c72ac2a
minor changes in init
devketanpro Sep 2, 2024
2835d8c
refactore code
devketanpro Sep 3, 2024
6d96d1b
refactore topic resource
devketanpro Sep 17, 2024
7e338bd
Merge remote-tracking branch 'upstream/async' into NHUB-531
devketanpro Sep 17, 2024
1e2eeda
refactore code and register folder module
devketanpro Sep 17, 2024
892214a
remove unwanted code
devketanpro Sep 17, 2024
815c7da
update folder resource
devketanpro Sep 18, 2024
86750fa
refactore code
devketanpro Sep 18, 2024
fc4d757
address comment
devketanpro Sep 18, 2024
fedd20e
add types
devketanpro Sep 18, 2024
8717341
remove unwanted code
devketanpro Sep 18, 2024
6abd8ab
remove unwanted code and refactore it
devketanpro Sep 19, 2024
fe7dfc7
update tests
devketanpro Sep 19, 2024
9478aa2
added some comments on issues
devketanpro Sep 19, 2024
1f89ef0
add new method and update tests
devketanpro Sep 19, 2024
4a225ec
fix advanced search e2e
devketanpro Sep 19, 2024
3510f37
fix pymongo duplicate error
devketanpro Sep 19, 2024
73b9b18
refactored tests
devketanpro Sep 20, 2024
d4df1af
Merge remote-tracking branch 'upstream/async' into NHUB-531
devketanpro Sep 20, 2024
45bf293
remove unwanted tests
devketanpro Sep 20, 2024
21ae0c7
final changes
devketanpro Sep 20, 2024
5365fc3
fix tests
devketanpro Sep 20, 2024
2275831
undo changes in test_user_dashboard
devketanpro Sep 20, 2024
9544de6
update user resource
devketanpro Sep 20, 2024
0012c96
address comment
devketanpro Sep 23, 2024
db33284
refactore code
devketanpro Sep 23, 2024
713ca69
fix server tests
devketanpro Sep 23, 2024
e48f360
minor chnage
devketanpro Sep 23, 2024
67c041d
Merge remote-tracking branch 'upstream/async' into NHUB-531
devketanpro Sep 23, 2024
d5a591a
update user receive emails tests
devketanpro Sep 23, 2024
8d10703
Fix e2e tests
eos87 Sep 23, 2024
b9eda41
Merge remote-tracking branch 'upstream/async' into NHUB-531
devketanpro Sep 24, 2024
b6448aa
fix conflicts
devketanpro Sep 24, 2024
7e66e53
Merge remote-tracking branch 'upstream/async' into NHUB-531
devketanpro Sep 25, 2024
369b99c
fix init_indexes call
MarkLark86 Sep 25, 2024
389cdeb
fix init_indexes
MarkLark86 Sep 25, 2024
8f8dd15
cleanup post_topic endpoint
MarkLark86 Sep 25, 2024
66851d5
fix lint issues
MarkLark86 Sep 25, 2024
f7eeeaf
re-add comment about _created in topic creation
MarkLark86 Sep 25, 2024
b03fbcd
Merge remote-tracking branch 'upstream/async' into NHUB-531
devketanpro Sep 25, 2024
2a515db
add tests
devketanpro Sep 25, 2024
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
12 changes: 4 additions & 8 deletions e2e/cypress/e2e/wire/topics.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,10 @@ describe('Wire - Topics', function () {
profileTopics.createNewFolder('Weather');
profileTopics.createNewFolder('Traffic');

// TODO-ASYNC: The dragTopicToFolder action triggers a PATCH action over a nested resource.
// the url of the PATCH looks something like /api/users/445460066f6a58e1c6b11541/topics/66607674e471296eb3dde17c
// I suspect this issue should be solved by https://github.com/superdesk/superdesk-core/pull/2694
// so I'm leaving it commented out for now
// profileTopics.dragTopicToFolder('Sofab Weather', 'Weather');
// profileTopics
// .getTopicCardAction('Sofab Weather', 'Remove from folder')
// .should('exist');
profileTopics.dragTopicToFolder('Sofab Weather', 'Weather');
profileTopics
.getTopicCardAction('Sofab Weather', 'Remove from folder')
.should('exist');

// Open the Topic for editing, and check the search params etc
profileTopics.getTopicCardAction('Sofab Weather', 'Edit').click();
Expand Down
21 changes: 13 additions & 8 deletions e2e/cypress/e2e/wire/wire_topic.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Wire - Topic', function () {
it('Rename folder in My Wire Topics', () => {
addResources([
{
resource: 'topic_folders',
resource: 'user_topic_folders',
devketanpro marked this conversation as resolved.
Show resolved Hide resolved
items: [
{
"_id": "652d2535b7e10e09ec704d6d",
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('Wire - Topic', function () {
it('Delete a folder with content in My Wire Topic', () => {
addResources([
{
resource: 'topic_folders',
resource: 'user_topic_folders',
items: [
{
"_id": "652d2535b7e10e09ec704d6d",
Expand All @@ -122,7 +122,7 @@ describe('Wire - Topic', function () {
resource: 'topics',
items: [
{
"_id": "672d3d26f27b4d56d8d5a27s",
"_id": "672d3d26f27b4d56d8d5a272",
"query": "Topic 1",
"topic_type": "wire",
"label": "Topic 1",
Expand Down Expand Up @@ -169,16 +169,21 @@ describe('Wire - Topic', function () {
beforeEach(() => {
addResources([
{
resource: 'topic_folders',
resource: 'user_topic_folders',
items: [
{
"_id": "652d2535b7e10e09ec704d6d",
"_id": "652d2535b7e10e09ec704d64",
"name": "user folder",
"section": "wire",
"user": USERS.foobar.admin._id,
},
],
},
{
resource: 'company_topic_folders',
items: [
{
"_id": "672d3d26f27b4d52d8d5a87s",
"_id": "672d3d26f27b4d52d8d5a874",
"section": "wire",
"name": "company folder",
"company": COMPANIES.foobar._id,
Expand Down Expand Up @@ -241,7 +246,7 @@ describe('Wire - Topic', function () {
it('Move My Topic to another folder', () => {
addResources([
{
resource: 'topic_folders',
resource: 'user_topic_folders',
items: [
{
"_id": "652d2535b7e10e09ec704d6d",
Expand Down Expand Up @@ -297,7 +302,7 @@ describe('Wire - Topic', function () {
it('Remove My Topic from folder ', () => {
addResources([
{
resource: 'topic_folders',
resource: 'user_topic_folders',
items: [
{
"_id": "652d2535b7e10e09ec704d6d",
Expand Down
9 changes: 5 additions & 4 deletions newsroom/agenda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from newsroom.auth.utils import check_user_has_products
from newsroom.products.products import get_products_by_company
from newsroom.template_filters import is_admin_or_internal, is_admin
from newsroom.topics import get_company_folders, get_user_folders, get_user_topics
from newsroom.topics import get_user_topics
from newsroom.topics_folders import get_company_folders, get_user_folders
from newsroom.navigations import get_navigations
from newsroom.auth import get_company, get_user, get_user_id, get_user_required
from newsroom.decorator import login_required, section
Expand Down Expand Up @@ -129,7 +130,7 @@ async def search():

async def get_view_data() -> Dict:
user = get_user_required()
topics = get_user_topics(user["_id"]) if user else []
topics = await get_user_topics(user["_id"]) if user else []
company = get_company(user)
products = get_products_by_company(company, product_type="agenda") if company else []

Expand All @@ -153,8 +154,8 @@ async def get_view_data() -> Dict:
"ui_config": await ui_config_service.get_section_config("agenda"),
"groups": get_groups(get_app_config("AGENDA_GROUPS", []), company),
"has_agenda_featured_items": get_resource_service("agenda_featured").find_one(req=None) is not None,
"user_folders": get_user_folders(user, "agenda") if user else [],
"company_folders": get_company_folders(company, "agenda") if company else [],
"user_folders": await get_user_folders(user, "agenda") if user else [],
"company_folders": await get_company_folders(company, "agenda") if company else [],
"date_filters": get_app_config("AGENDA_TIME_FILTERS", []),
}

Expand Down
1 change: 1 addition & 0 deletions newsroom/core/resources/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async def on_create(self, docs: list[NewshubResourceModelType]) -> None:
await super().on_create(docs)
for doc in docs:
doc.original_creator = get_user_id()
doc.version_creator = get_user_id()

async def on_update(self, updates: dict[str, Any], original: NewshubResourceModelType) -> None:
await super().on_update(updates, original)
Expand Down
2 changes: 1 addition & 1 deletion newsroom/market_place/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
async def get_view_data():
"""Get the view data"""
user = get_user()
topics = get_user_topics(user["_id"]) if user else []
topics = await get_user_topics(user["_id"]) if user else []
navigations = await get_navigations_by_company(
str(user["company"]) if user and user.get("company") else None,
product_type=SECTION_ID,
Expand Down
4 changes: 2 additions & 2 deletions newsroom/notifications/send_scheduled_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from newsroom.utils import get_user_dict, get_company_dict
from newsroom.email import send_user_email
from newsroom.celery_app import celery
from newsroom.topics.topics import get_user_id_to_topic_for_subscribers, TopicNotificationType
from newsroom.topics.topics_async import get_user_id_to_topic_for_subscribers, NotificationType
from newsroom.gettext import get_session_timezone, set_session_timezone

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -47,7 +47,7 @@ async def run_schedules(self, force: bool):
now_utc = utcnow().replace(second=0, microsecond=0)
companies = get_company_dict(False)
users = get_user_dict(False)
user_topic_map = get_user_id_to_topic_for_subscribers(TopicNotificationType.SCHEDULED.value)
user_topic_map = await get_user_id_to_topic_for_subscribers(NotificationType.SCHEDULED)

schedules: List[NotificationQueue] = get_resource_service("notification_queue").get(req=None, lookup={})
except Exception as e:
Expand Down
8 changes: 4 additions & 4 deletions newsroom/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from superdesk.lock import lock, unlock

from newsroom.notifications import push_notification, save_user_notifications, NotificationQueueService
from newsroom.topics.topics import (
from newsroom.topics.topics_async import (
get_agenda_notification_topics_for_query_by_id,
get_topics_with_subscribers,
)
Expand Down Expand Up @@ -932,7 +932,7 @@ async def send_user_notification_emails(item, user_matches, users, section):


async def notify_wire_topic_matches(item, users_dict, companies_dict) -> Set[ObjectId]:
topics = get_topics_with_subscribers("wire")
topics = await get_topics_with_subscribers("wire")
topic_matches = superdesk.get_resource_service("wire_search").get_matching_topics(
item["_id"], topics, users_dict, companies_dict
)
Expand All @@ -945,7 +945,7 @@ async def notify_wire_topic_matches(item, users_dict, companies_dict) -> Set[Obj


async def notify_agenda_topic_matches(item, users_dict, companies_dict) -> Set[ObjectId]:
topics = get_topics_with_subscribers("agenda")
topics = await get_topics_with_subscribers("agenda")
topic_matches = superdesk.get_resource_service("agenda").get_matching_topics(
item["_id"], topics, users_dict, companies_dict
)
Expand All @@ -954,7 +954,7 @@ async def notify_agenda_topic_matches(item, users_dict, companies_dict) -> Set[O
topic_matches.extend(
[
topic
for topic in get_agenda_notification_topics_for_query_by_id(item, users_dict)
for topic in await get_agenda_notification_topics_for_query_by_id(item, users_dict)
if topic.get("_id") not in topic_matches
]
)
Expand Down
4 changes: 4 additions & 0 deletions newsroom/reports/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def get_company_saved_searches():
company_topics = defaultdict(int)
companies = get_entity_dict(query_resource("companies"))
users = get_entity_dict(query_resource("users"))

# TODO-Async:- update when this reports resource convert to async
topics = query_resource("topics")

for topic in topics:
Expand Down Expand Up @@ -60,6 +62,8 @@ def get_user_saved_searches():
user_topics = defaultdict(int)
companies = get_entity_dict(query_resource("companies"))
users = get_entity_dict(query_resource("users"))

# TODO-Async:- update when this reports resource convert to async
topics = query_resource("topics")

for topic in topics:
Expand Down
44 changes: 9 additions & 35 deletions newsroom/topics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
import superdesk
from superdesk.flask import Blueprint
from superdesk.core.module import Module

from .topics import get_user_topics # noqa
from . import folders, topics
from .topics_async import topic_resource_config, topic_endpoints, get_user_topics
from . import topics


blueprint = Blueprint("topics", __name__)
__all__ = ["get_user_topics", "topic_endpoints", "topic_resource_config"]


def init_app(app):
topics.TopicsResource("topics", app, topics.topics_service)
folders.FoldersResource("topic_folders", app, folders.folders_service)

superdesk.register_resource("user_topic_folders", folders.UserFoldersResource, folders.UserFoldersService, _app=app)
superdesk.register_resource(
"company_topic_folders", folders.CompanyFoldersResource, folders.CompanyFoldersService, _app=app
)


def get_user_folders(user, section):
return list(
superdesk.get_resource_service("user_topic_folders").get(
req=None,
lookup={
"user": user["_id"],
"section": section,
},
)
)


def get_company_folders(company, section):
return list(
superdesk.get_resource_service("company_topic_folders").get(
req=None,
lookup={
"company": company["_id"],
"section": section,
},
)
)

module = Module(
name="newsroom.topics",
resources=[topic_resource_config],
endpoints=[topic_endpoints],
)

from . import views # noqa
91 changes: 0 additions & 91 deletions newsroom/topics/folders.py

This file was deleted.

3 changes: 3 additions & 0 deletions newsroom/topics/topics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO-ASYNC :- Remove this resource when Reports module is converted to async

from typing import Optional, List, Dict, Any
import enum

Expand Down Expand Up @@ -65,6 +67,7 @@ class TopicsResource(newsroom.Resource):
datasource = {"source": "topics", "default_sort": [("label", 1)]}
allowed_roles = [role for role in UserRole]
allowed_item_roles = allowed_roles
internal_resource = True
devketanpro marked this conversation as resolved.
Show resolved Hide resolved


class TopicsService(newsroom.Service):
Expand Down
Loading
Loading