Skip to content

Commit

Permalink
Merge 77e7d1b into 4ff188b
Browse files Browse the repository at this point in the history
  • Loading branch information
dyohan9 committed Oct 1, 2019
2 parents 4ff188b + 77e7d1b commit 7e4001a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ You can set environment variables in your OS, write on ```.env``` file or pass v
| BOTHUB_ENGINE_AWS_ACCESS_KEY_ID | ```string``` | ```None``` |
| BOTHUB_ENGINE_AWS_SECRET_ACCESS_KEY | ```string``` | ```None``` |
| BOTHUB_ENGINE_AWS_REGION_NAME | ```string``` | ```None``` |
| BOTHUB_ENGINE_AWS_SEND | ```bool``` | ```False``` |
34 changes: 27 additions & 7 deletions bothub/api/v2/nlp/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import base64
import json
import re
import requests

from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -417,11 +419,26 @@ class RepositoryUpdateInterpretersViewSet(
def retrieve(self, request, *args, **kwargs):
check_auth(request)
update = self.get_object()
try:
download = requests.get(update.bot_data)
bot_data = base64.b64encode(download.content)
except Exception:
bot_data = b""

regex = re.compile(
r"^(?:http|ftp)s?://" # http:// or https://
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|"
r"[A-Z0-9-]{2,}\.?)|"
r"localhost|"
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
r"(?::\d+)?"
r"(?:/?|[/?]\S+)$",
re.IGNORECASE,
)

if re.match(regex, update.bot_data) is not None:
try:
download = requests.get(update.bot_data)
bot_data = base64.b64encode(download.content)
except Exception:
bot_data = b""
else:
bot_data = update.bot_data
return Response(
{
"update_id": update.id,
Expand All @@ -434,6 +451,9 @@ def create(self, request, *args, **kwargs):
check_auth(request)
id = request.data.get("id")
repository = get_object_or_404(RepositoryUpdate, pk=id)
bot_data = base64.b64decode(request.data.get("bot_data"))
repository.save_training(send_bot_data_file_aws(id, bot_data))
if settings.AWS_SEND:
bot_data = base64.b64decode(request.data.get("bot_data"))
repository.save_training(send_bot_data_file_aws(id, bot_data))
else:
repository.save_training(request.data.get("bot_data"))
return Response({})
17 changes: 9 additions & 8 deletions bothub/common/migrations/0035_auto_20190902_1455.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# Generated by Django 2.1.5 on 2019-09-02 14:55

from django.conf import settings
from django.db import migrations, models
from bothub.utils import send_bot_data_file_aws
from bothub.common.models import RepositoryUpdate


def update_repository(apps, schema_editor):
for update in RepositoryUpdate.objects.all().exclude(bot_data__exact=""):
url = send_bot_data_file_aws(update.pk, update.bot_data)
repository_update = RepositoryUpdate.objects.get(pk=update.pk)
repository_update.bot_data = url
repository_update.save(update_fields=["bot_data"])
print("Updating bot_data repository_update {}".format(str(update.pk)))
if settings.AWS_SEND:
for update in RepositoryUpdate.objects.all().exclude(bot_data__exact=""):
repository_update = RepositoryUpdate.objects.get(pk=update.pk)
bot_data = send_bot_data_file_aws(update.pk, update.bot_data)
repository_update.bot_data = bot_data
repository_update.save(update_fields=["bot_data"])
print("Updating bot_data repository_update {}".format(str(update.pk)))


class Migration(migrations.Migration):
Expand All @@ -23,6 +24,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name="repositoryupdate",
name="bot_data",
field=models.URLField(blank=True, verbose_name="bot data"),
field=models.TextField(blank=True, verbose_name="bot data"),
),
]
2 changes: 1 addition & 1 deletion bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class Meta:
use_competing_intents = models.BooleanField(default=False)
use_name_entities = models.BooleanField(default=False)
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
bot_data = models.URLField(_("bot data"), blank=True)
bot_data = models.TextField(_("bot data"), blank=True)
by = models.ForeignKey(User, models.CASCADE, blank=True, null=True)
training_started_at = models.DateTimeField(
_("training started at"), blank=True, null=True
Expand Down
3 changes: 2 additions & 1 deletion bothub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
BOTHUB_ENGINE_AWS_SECRET_ACCESS_KEY=(str, ""),
BOTHUB_ENGINE_AWS_S3_BUCKET_NAME=(str, ""),
BOTHUB_ENGINE_AWS_REGION_NAME=(str, "us-east-1"),
BOTHUB_ENGINE_AWS_SEND=(bool, False),
)

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Expand Down Expand Up @@ -257,7 +258,7 @@


# AWS

AWS_SEND = env.bool("BOTHUB_ENGINE_AWS_SEND")
AWS_ACCESS_KEY_ID = env.str("BOTHUB_ENGINE_AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env.str("BOTHUB_ENGINE_AWS_SECRET_ACCESS_KEY")
AWS_BUCKET_NAME = env.str("BOTHUB_ENGINE_AWS_S3_BUCKET_NAME")
Expand Down

0 comments on commit 7e4001a

Please sign in to comment.