Skip to content

Commit

Permalink
Allow Django app settings to override ChatterBot settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Sep 9, 2016
1 parent 7b85cc3 commit adc1ed8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
4 changes: 0 additions & 4 deletions chatterbot/ext/django_chatterbot/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@ class DjangoChatterBotConfig(AppConfig):
name = 'chatterbot.ext.django_chatterbot'
label = 'django_chatterbot'
verbose_name = 'Django ChatterBot'

def ready(self):
from chatterbot.ext.django_chatterbot import settings
settings.patch_all()
14 changes: 10 additions & 4 deletions chatterbot/ext/django_chatterbot/settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from django.conf import settings


CHATTERBOT = {
'NAME': 'ChatterBot'
CHATTERBOT_SETTINGS = getattr(settings, 'CHATTERBOT', {})

CHATTERBOT_DEFAULTS = {
'name': 'ChatterBot',
'storage_adapter': 'chatterbot.adapters.storage.DjangoStorageAdapter',
'input_adapter': 'chatterbot.adapters.input.VariableInputTypeAdapter',
'output_adapter': 'chatterbot.adapters.output.OutputFormatAdapter',
'output_format': 'json'
}

def patch_all():
setattr(settings, 'CHATTERBOT', CHATTERBOT)
CHATTERBOT = CHATTERBOT_DEFAULTS.copy()
CHATTERBOT.update(CHATTERBOT_SETTINGS)
13 changes: 4 additions & 9 deletions chatterbot/ext/django_chatterbot/views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
from django.views.generic import View
from django.http import JsonResponse
from django.conf import settings
from chatterbot import ChatBot
from chatterbot.ext.django_chatterbot import settings


class ChatterBotView(View):

chatterbot = ChatBot(
settings.CHATTERBOT['NAME'],
storage_adapter='chatterbot.adapters.storage.DjangoStorageAdapter',
input_adapter='chatterbot.adapters.input.VariableInputTypeAdapter',
output_adapter='chatterbot.adapters.output.OutputFormatAdapter',
output_format='json'
)
chatterbot = ChatBot(**settings.CHATTERBOT)

def post(self, request, *args, **kwargs):
input_statement = request.POST.get('text')
Expand All @@ -23,7 +17,8 @@ def post(self, request, *args, **kwargs):

def get(self, request, *args, **kwargs):
data = {
'detail': 'You should make a POST request to this endpoint.'
'detail': 'You should make a POST request to this endpoint.',
'name': self.chatterbot.name
}

# Return a method not allowed response
Expand Down
6 changes: 6 additions & 0 deletions examples/django_app/example_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
'example_app',
)

# ChatterBot settings

CHATTERBOT = {
'name': 'Django ChatterBot Example'
}

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
Expand Down
21 changes: 21 additions & 0 deletions examples/django_app/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.test import TestCase
from django.conf import settings


class SettingsTestCase(TestCase):

def test_modified_settings(self):
with self.settings(CHATTERBOT={'name': 'Jim'}):
self.assertIn('name', settings.CHATTERBOT)
self.assertEqual('Jim', settings.CHATTERBOT['name'])

def test_name_setting(self):
from django.core.urlresolvers import reverse

api_url = reverse('chatterbot:chatterbot')
response = self.client.get(api_url)

self.assertEqual(response.status_code, 405)
self.assertIn('detail', response.json())
self.assertIn('name', response.json())
self.assertEqual('Django ChatterBot Example', response.json()['name'])

0 comments on commit adc1ed8

Please sign in to comment.