Add incomplete CBT component

This commit is contained in:
Kumi 2020-12-31 22:18:39 +01:00
parent 9dc5e1bfd6
commit f5ce10ef93
9 changed files with 105 additions and 1 deletions

0
cbt/__init__.py Normal file
View file

3
cbt/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
cbt/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class CbtConfig(AppConfig):
name = 'cbt'

8
cbt/forms.py Normal file
View file

@ -0,0 +1,8 @@
from django.forms import ModelForm
from .models import ThoughtRecord
class ThoughtRecordStepOneForm(ModelForm):
class Meta:
model = ThoughtRecord
fields = ["title", "situation"]

22
cbt/models.py Normal file
View file

@ -0,0 +1,22 @@
from django.db import models
from django.contrib.auth import get_user_model
from common.fields import PercentageField
class EmotionRecord(models.Model):
emotion = models.CharField(max_length=128)
percentage = PercentageField()
description = models.TextField()
class ThoughtRecord(models.Model):
user = models.ForeignKey(get_user_model(), models.CASCADE)
title = models.CharField(blank=True, null=True, max_length=128)
situation = models.TextField(blank=True, null=True)
emotions = models.ManyToManyField(EmotionRecord)
thoughts = models.TextField(blank=True, null=True)
pro_facts = models.TextField(blank=True, null=True)
con_facts = models.TextField(blank=True, null=True)
realistic = models.TextField(blank=True, null=True)
outcome = models.TextField(blank=True, null=True)
emotions_now = models.ManyToManyField(EmotionRecord, related_name="emotions_now")
complete = models.BooleanField(default=False)

View file

@ -0,0 +1,40 @@
{% extends "frontend/base.html" %}
{% block "content" %}
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger" role="alert">
{{ field.name }}: {{ error }}
</div>
{% endfor %}
{% endfor %}
{% endif %}
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Edit {{ object.name }}</h6>
</div>
<div class="card-body">
<form action="" method="POST">
{% csrf_token %}
<div class="table-responsive">
<table>
<tr><th><label for="id_title">Title:</label></th><td><input type="text" name="name" value="{{ object.title }}" maxlength="64" required id="id_name"></td></tr>
<tr><th><label for="id_icon">Icon:</label></th><td><input style="visibility: hidden;" class="icp icp-auto" type="text" name="icon" value="{{ object.icon }}" maxlength="64" required id="id_icon"></td></tr>
<tr><th><label for="id_color">Color:</label></th><td><input type="text"
id="id_color"
class="form-control colorfield_field jscolor"
name="color"
value="{{ object.color }}"
placeholder="{{ object.color }}"
data-jscolor="{hash:true,width:225,height:150,required:true}"
required /></td></tr>
<tr><th><label for="id_value">Value:</label></th><td><input name="value" type="number" step="1" value="{{ object.value }}" required id="id_value"></td></tr>
</table>
</div>
<button style="margin-top: 20px;" class="form-control btn-primary" type="submit">Save</button>
</form>
</div>
</div>
{% endblock %}

3
cbt/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

22
cbt/views.py Normal file
View file

@ -0,0 +1,22 @@
from django.views.generic import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from .forms import ThoughtRecordStepOneForm
from .models import ThoughtRecord
class ThoughtRecordStepOneView(LoginRequiredMixin, CreateView):
form_class = ThoughtRecordStepOneForm
template_name = "cbt/thoughtrecord1.html"
model = ThoughtRecord
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "Create Thought Record"
return context
def form_valid(self, form):
form.instance.user = self.request.user
ret = super().form_valid(form)
return ret

View file

@ -21,6 +21,7 @@ INSTALLED_APPS = [
'mood',
'msgio',
'cronhandler',
'cbt',
]
MIDDLEWARE = [
@ -124,4 +125,4 @@ LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = "/"
DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage' if not AWS_ACCESS_KEY_ID else 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage' if AWS_ACCESS_KEY_ID else 'django.contrib.staticfiles.storage.StaticFilesStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage' if AWS_ACCESS_KEY_ID else 'django.contrib.staticfiles.storage.StaticFilesStorage'