Skip to content

Commit

Permalink
add ability to add new chats
Browse files Browse the repository at this point in the history
  • Loading branch information
dphuang2 committed Apr 21, 2024
1 parent ccb9550 commit 735aeec
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 81 deletions.
33 changes: 33 additions & 0 deletions enterprise/chatbot_app/migrations/0002_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 5.0.4 on 2024-04-21 07:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("chatbot_app", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="Chat",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"messages",
models.ManyToManyField(
related_name="chat", to="chatbot_app.message"
),
),
],
),
]
18 changes: 18 additions & 0 deletions enterprise/chatbot_app/migrations/0003_chat_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-04-21 07:57

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("chatbot_app", "0002_chat"),
]

operations = [
migrations.AddField(
model_name="chat",
name="name",
field=models.TextField(default="2024/04/21 00:00"),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.0.4 on 2024-04-21 08:37

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("chatbot_app", "0003_chat_name"),
]

operations = [
migrations.RemoveField(
model_name="chat",
name="messages",
),
migrations.AddField(
model_name="message",
name="chat",
field=models.ForeignKey(
default=0,
on_delete=django.db.models.deletion.CASCADE,
related_name="messages",
to="chatbot_app.chat",
),
preserve_default=False,
),
]
18 changes: 18 additions & 0 deletions enterprise/chatbot_app/migrations/0005_alter_chat_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-04-21 08:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("chatbot_app", "0004_remove_chat_messages_message_chat"),
]

operations = [
migrations.AlterField(
model_name="chat",
name="name",
field=models.TextField(default="2024/04/21 00:00:00"),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.0.4 on 2024-04-21 09:02

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("chatbot_app", "0005_alter_chat_name"),
]

operations = [
migrations.AddField(
model_name="chat",
name="created_at",
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
migrations.AlterField(
model_name="chat",
name="name",
field=models.TextField(),
),
]
16 changes: 13 additions & 3 deletions enterprise/chatbot_app/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from django.db import models
from datetime import datetime

class Chat(models.Model):
name = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)

def save(self, *args, **kwargs):
if not self.name:
self.name = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
super(Chat, self).save(*args, **kwargs)

class Message(models.Model):
user_message = models.TextField()
bot_message = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
user_message = models.TextField()
bot_message = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name='messages')
17 changes: 17 additions & 0 deletions enterprise/chatbot_app/templates/_chat_container_inner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div id="container">
{% include 'chat_select.html' %} {% include 'chatbox.html' %}

<form
hx-post="{% url 'chatbox_view' %}"
hx-target="#chatbox"
hx-swap="outerHTML"
>
{% csrf_token %}
<input type="hidden" name="chat_id" value="{{ current_chat.id }}" />
<div class="my-indicator"></div>
<div class="input-fields">
<input type="text" name="message" />
<button type="submit">Send</button>
</div>
</form>
</div>
103 changes: 46 additions & 57 deletions enterprise/chatbot_app/templates/chat.html
Original file line number Diff line number Diff line change
@@ -1,65 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<head>
<meta charset="UTF-8" />
<title>AI Chatbot</title>
<script src="https://unpkg.com/[email protected]" integrity="sha384-zUfuhFKKZCbHTY6aRR46gxiqszMk5tcHjsVFxnUo8VMus4kHGVdIYVbOYYNlKmHV" crossorigin="anonymous"></script>
<script
src="https://unpkg.com/[email protected]"
integrity="sha384-zUfuhFKKZCbHTY6aRR46gxiqszMk5tcHjsVFxnUo8VMus4kHGVdIYVbOYYNlKmHV"
crossorigin="anonymous"
></script>
<style>
.input-fields{
display: flex;
flex-direction: row;
}
.input-fields {
display: flex;
flex-direction: row;
}

/* Style for the loading spinner */
.my-indicator {
display: none;
border: 2px solid #f3f3f3;
border-top: 2px solid #3498db;
border-radius: 50%;
width: 20px;
height: 20px;
animation: spin .5s linear infinite;
}
.htmx-request .my-indicator {
display: inline-block;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.user-message{
color: #b83eff;
padding: 5px;
/* Style for the loading spinner */
.my-indicator {
display: none;
border: 2px solid #f3f3f3;
border-top: 2px solid #3498db;
border-radius: 50%;
width: 20px;
height: 20px;
animation: spin 0.5s linear infinite;
}
.htmx-request .my-indicator {
display: inline-block;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
.bot-message {
padding: 5px;
color: darkblue;
100% {
transform: rotate(360deg);
}
input[type=text] {
width: 50%;
}

}
.user-message {
color: #b83eff;
padding: 5px;
}
.bot-message {
padding: 5px;
color: darkblue;
}
input[type="text"] {
width: 50%;
}
</style>
</head>
<body>
<div id="container">
<div id="chatbox">
{% for message in messages %}
<div>User: {{ message.user_message }}</div>
<div>Bot: {{ message.bot_message }}</div>
{% endfor %}
</div>

<form hx-post="{% url 'chat_view' %}" hx-target="#container" hx-swap="innerHTML" >
{% csrf_token %}
<div class="my-indicator"></div>
<div class="input-fields">
<input type="text" name="message">
<button type="submit">
Send
</button>
</div>
</form>
</div>
</body>
</html>
</head>
<body>
{% include 'chat_container.html' %}
</body>
</html>
1 change: 1 addition & 0 deletions enterprise/chatbot_app/templates/chat_container.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include '_chat_container_inner.html' %}
9 changes: 9 additions & 0 deletions enterprise/chatbot_app/templates/chat_select.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<select name="chat_id" hx-get="{% url 'chat_select_view' %}" hx-target="#container">
{% for chat in chats %}
<option value="{{ chat.id }}" {% if chat.id == current_chat.id %}selected{% endif %}>{{ chat.name }}</option>
{% endfor %}
</select>
<form hx-post="{% url 'create_chat_view' %}" hx-target="#container">
{% csrf_token %}
<button>Create Chat</button>
</form>
6 changes: 6 additions & 0 deletions enterprise/chatbot_app/templates/chatbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div id="chatbox">
{% for message in messages %}
<div>User: {{ message.user_message }}</div>
<div>Bot: {{ message.bot_message }}</div>
{% endfor %}
</div>
14 changes: 11 additions & 3 deletions enterprise/chatbot_app/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from django.urls import path
from .views import chat_view
from .views import (
chat_view,
chatbox_view,
create_chat_view,
chat_select_view,
)

urlpatterns = [
path('', chat_view, name='chat_view'),
]
path("", chat_view, name="chat_view"),
path("chatbox", chatbox_view, name="chatbox_view"),
path("create_chat", create_chat_view, name="create_chat_view"),
path("chat_select", chat_select_view, name="chat_select_view"),
]
Loading

0 comments on commit 735aeec

Please sign in to comment.