Skip to content

Commit

Permalink
Merge 642f281 into 3755328
Browse files Browse the repository at this point in the history
  • Loading branch information
Rineee committed Apr 27, 2021
2 parents 3755328 + 642f281 commit ae89c26
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions adhocracy-plus/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,4 @@

AI_API_AUTH_TOKEN = ''
AI_API_URL = 'https://kosmo-api-dev.liqd.net/api/classify/'
AI_USAGE = True
29 changes: 18 additions & 11 deletions apps/classifications/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@

@receiver(signals.post_save, sender=Comment)
def get_ai_classification(sender, instance, **kwargs):
try:
response = call_ai_api(comment=instance)
if response.status_code == 200 \
and response.json()['classification'] == 'OFFENSE':

classification = AIClassification(
comment=instance,
classification='OFFENSIVE')
classification.save()
except httpx.HTTPError:
logger.error('Error connecting to %s', settings.AI_API_URL)
if hasattr(settings, 'AI_USAGE') and settings.AI_USAGE:
if hasattr(settings, 'AI_API_AUTH_TOKEN') and \
settings.AI_API_AUTH_TOKEN:
try:
response = call_ai_api(comment=instance)
if response.status_code == 200 \
and response.json()['classification'] == 'OFFENSE':

classification = AIClassification(
comment=instance,
classification='OFFENSIVE')
classification.save()
except httpx.HTTPError as e:
logger.error('Error connecting to %s: %s',
settings.AI_API_URL, str(e))
else:
logger.error('No ai api auth token provided. '
'Disable ai usage or provide token.')


def skip_retry(e):
Expand Down
2 changes: 1 addition & 1 deletion tests/classifications/test_classification_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def test_ai_classification_receives_post_save_comment_signal(idea,
assert(get_ai_classification in signals.post_save._live_receivers(Comment))
comment_factory(content_object=idea,
comment='lala')
assert('Error connecting to %s' in str(caplog.records[-1]))
assert('No ai api auth token provided.' in str(caplog.records[-1]))

0 comments on commit ae89c26

Please sign in to comment.