# Path: zenodo/modules/spam/tasks.py
# @shared_task(ignore_result=False)
# def check_metadata_for_spam(community_id=None, dep_id=None):
#     """Checks metadata of the provided deposit for spam content."""
#     if not current_app.config.get('ZENODO_SPAM_MODEL_LOCATION'):
#         return 0
#     if community_id:
#         community = Community.query.get(community_id)
#         spam_proba = current_spam.model.predict_proba(
#             [community.title + ' ' + community.description])[0][1]
#     if dep_id:
#         deposit = RecordMetadata.query.get(dep_id)
#         spam_proba = current_spam.model.predict_proba(
#             [deposit.json['title'] + ' ' + deposit.json['description']])[0][1]
# 
#     return spam_proba


from celery.exceptions import TimeoutError
from elasticsearch_dsl import Q
from flask import current_app, render_template
from flask_babelex import gettext as _
from flask_mail import Message
from flask_principal import ActionNeed
from invenio_access import Permission
from invenio_communities.models import Community
from invenio_mail.tasks import send_email
from invenio_search.api import RecordsSearch
from werkzeug.exceptions import HTTPException
from zenodo.modules.spam.tasks import check_metadata_for_spam
        deposit=deposit
    )
    send_email.delay(msg.__dict__)


def send_spam_admin_email(user, deposit=None, community=None):
    """Send email notification to admins for a spam detection."""
    msg = Message(
        _("Zenodo activity marked as spam."),
        sender=current_app.config.get('SUPPORT_EMAIL'),
        recipients=[current_app.config.get('ZENODO_ADMIN_EMAIL')],
    )
    msg.body = render_template(
        "zenodo_spam/email/spam_admin_email.tpl",
        user=user,
        deposit=deposit,
        community=community
    )
    send_email.delay(msg.__dict__)


def check_and_handle_spam(community=None, deposit=None, retry=True):
    """Checks community/deposit metadata for spam."""
    try:
        if current_app.config.get('ZENODO_SPAM_MODEL_LOCATION'):
            if community:
                task = check_metadata_for_spam.delay(
                    community_id=community.id)
                user_id = community.id_user
            if deposit:

# Path: zenodo/modules/spam/tasks.py
# @shared_task(ignore_result=False)
# def check_metadata_for_spam(community_id=None, dep_id=None):
#     """Checks metadata of the provided deposit for spam content."""
#     if not current_app.config.get('ZENODO_SPAM_MODEL_LOCATION'):
#         return 0
#     if community_id:
#         community = Community.query.get(community_id)
#         spam_proba = current_spam.model.predict_proba(
#             [community.title + ' ' + community.description])[0][1]
#     if dep_id:
#         deposit = RecordMetadata.query.get(dep_id)
#         spam_proba = current_spam.model.predict_proba(
#             [deposit.json['title'] + ' ' + deposit.json['description']])[0][1]
# 
#     return spam_proba


# Path: zenodo/modules/spam/utils.py
from celery.exceptions import TimeoutError
from elasticsearch_dsl import Q
from flask import current_app, render_template
from flask_babelex import gettext as _
from flask_mail import Message
from flask_principal import ActionNeed
from invenio_access import Permission
from invenio_communities.models import Community
from invenio_mail.tasks import send_email
from invenio_search.api import RecordsSearch
from werkzeug.exceptions import HTTPException
from zenodo.modules.spam.tasks import check_metadata_for_spam

        deposit=deposit
    )
    send_email.delay(msg.__dict__)


def send_spam_admin_email(user, deposit=None, community=None):
    """Send email notification to admins for a spam detection."""
    msg = Message(
        _("Zenodo activity marked as spam."),
        sender=current_app.config.get('SUPPORT_EMAIL'),
        recipients=[current_app.config.get('ZENODO_ADMIN_EMAIL')],
    )
    msg.body = render_template(
        "zenodo_spam/email/spam_admin_email.tpl",
        user=user,
        deposit=deposit,
        community=community
    )
    send_email.delay(msg.__dict__)


def check_and_handle_spam(community=None, deposit=None, retry=True):
    """Checks community/deposit metadata for spam."""
    try:
        if current_app.config.get('ZENODO_SPAM_MODEL_LOCATION'):
            if community:
                task = check_metadata_for_spam.delay(
                    community_id=community.id)
                user_id = community.id_user
            if deposit:

                task = check_metadata_for_spam.delay(dep_id=str(deposit.id))