Skip to content

Commit

Permalink
Updated model to prevent showing sleep errors and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftypanda committed Feb 6, 2018
1 parent 256015d commit 41b9932
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 16 deletions.
3 changes: 2 additions & 1 deletion sleep/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin

# Register your models here.
from .models import Sleep, SleepInterruption
from .models import Participant, Sleep, SleepInterruption

class SleepInterruptionInline(admin.TabularInline):
model = SleepInterruption
Expand All @@ -20,3 +20,4 @@ class SleepAdmin(admin.ModelAdmin):
search_fields = ['sleep_date']

admin.site.register(Sleep, SleepAdmin)
admin.site.register(Participant)
33 changes: 33 additions & 0 deletions sleep/migrations/0002_auto_20180203_1953.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.0.2 on 2018-02-03 19:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sleep', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='sleep',
name='time_start_preparing_for_sleep',
field=models.DateTimeField(blank=True, null=True, verbose_name='time started getting ready for bed'),
),
migrations.AlterField(
model_name='sleep',
name='time_went_into_bed',
field=models.DateTimeField(blank=True, null=True, verbose_name='time in bed'),
),
migrations.AlterField(
model_name='sleep',
name='time_went_to_sleep',
field=models.DateTimeField(blank=True, null=True, verbose_name='time asleep'),
),
migrations.AlterField(
model_name='sleep',
name='time_woke_in_morning',
field=models.DateTimeField(blank=True, verbose_name='time woke in morning'),
),
]
18 changes: 18 additions & 0 deletions sleep/migrations/0003_auto_20180203_1954.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.2 on 2018-02-03 19:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sleep', '0002_auto_20180203_1953'),
]

operations = [
migrations.AlterField(
model_name='sleep',
name='time_woke_in_morning',
field=models.DateTimeField(blank=True, null=True, verbose_name='time woke in morning'),
),
]
26 changes: 26 additions & 0 deletions sleep/migrations/0004_participant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 2.0.2 on 2018-02-05 23:11

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('sleep', '0003_auto_20180203_1954'),
]

operations = [
migrations.CreateModel(
name='Participant',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('individual_name', models.CharField(blank=True, max_length=100, null=True)),
('age', models.IntegerField(blank=True, max_length=3, null=True)),
('favourite_color', models.CharField(blank=True, max_length=60, null=True)),
('login_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
18 changes: 18 additions & 0 deletions sleep/migrations/0005_auto_20180205_2312.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.2 on 2018-02-05 23:12

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sleep', '0004_participant'),
]

operations = [
migrations.AlterField(
model_name='participant',
name='age',
field=models.IntegerField(blank=True, null=True),
),
]
59 changes: 51 additions & 8 deletions sleep/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,69 @@

# Create your models here.

class Participant(models.Model):
login_user = models.ForeignKey ('auth.User', on_delete=models.CASCADE)
individual_name = models.CharField (max_length=100, blank=True, null=True)
age = models.IntegerField (blank=True, null=True)
favourite_color = models.CharField (max_length=60, blank=True, null=True)
# TODO: Generate random unique_name that can be shared to discuss participant
"""
def generate_unique_name(self):
with open('/usr/share/dict/words') as f:
words = [word.strip() for word in f]
random_name = ' '.join(choice(words) for i in range(4))
return random_name
self.unique_name = generate_unique_name()
"""
def __str__(self):
return str(self.login_user)

class Sleep(models.Model):
sleep_date = models.DateField('sleep date')

time_start_preparing_for_sleep = models.DateTimeField('time started getting ready for bed')
time_went_into_bed = models.DateTimeField('time in bed')
time_went_to_sleep = models.DateTimeField('time asleep')
time_start_preparing_for_sleep = models.DateTimeField('time started getting ready for bed', blank=True, null=True)
time_went_into_bed = models.DateTimeField('time in bed', blank=True, null=True)
time_went_to_sleep = models.DateTimeField('time asleep', blank=True, null=True)

time_woke_in_morning = models.DateTimeField('time woke in morning')
time_woke_in_morning = models.DateTimeField('time woke in morning', blank=True, null=True)


def check_time_went_to_sleep_is_before_time_woke_in_morning(self):
"""
Checks that time went to sleep is before waking time in the morning
"""
# TODO: ? add value error check as well
if self.time_woke_in_morning < self.time_went_to_sleep:
return False
else:
return True
check_time_went_to_sleep_is_before_time_woke_in_morning.boolean = True
#TODO: Run function to check and raise error if entered incorrectly

def total_time_asleep(self):
"""
Calculates the total sleep time based on time went to sleep
compared to time woke in the time woke in morning
This does NOT include any interruptions.
"""
wake = self.time_woke_in_morning
sleep = self.time_went_to_sleep
timediff = wake - sleep
# TODO: Add in calculation of sum of Sleep interruptions ? need to add time back to sleep
if ((self.time_went_to_sleep != None) and (self.time_woke_in_morning != None)):

wake = self.time_woke_in_morning
sleep = self.time_went_to_sleep
timediff = wake - sleep
if (timediff > datetime.timedelta(0) ):
return timediff
else:
return ValueError, "Time waking must be AFTER time going to sleep"
else:
response = "Missing data: Please make sure time went to sleep and time woke in morning are entered"
return response

return timediff
def __str__(self):
return str(self.sleep_date)


class SleepInterruption(models.Model):
Expand All @@ -38,3 +79,5 @@ class SleepInterruption(models.Model):
intervention.short_description= 'What intervention happened?'

time_taken_to_sleep = models.IntegerField(default=10)

# TODO: Missing variable to track total amount of time taken to get to sleep
60 changes: 60 additions & 0 deletions sleep/static/templates/css/sleep.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.page-header {
background-color: teal;
margin-top: 0;
padding: 20px 20px 20px 40px;
}

.page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active {
color: #ffffff;
font-size: 36pt;
text-decoration: none;
}

.content {
margin-left: 40px;
}

h1, h2, h3, h4 {
font-family: 'lobster', cursive;
}

.date {
color: #828282;
}

.save {
float: right;
}

.post-form textarea, .post-form input {
width: 100%;
}

.top-menu, .top-menu:hover, .top-menu:visited {
color: #ffffff;
float: right;
font-size: 26pt;
margin-right: 20px;
}

.post {
margin-bottom: 70px;
}

.post h1 a, .post h1 a:visited {
color: #000000;
}

h1 a {
color: #FCA205;
font-family: 'Lobster';
transition: .5;
}

body {
padding-left: 15px;
}

.comment {
margin: 20px 0px 20px 20px;
}
27 changes: 27 additions & 0 deletions sleep/static/templates/sleep/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% load staticfiles %}
<!Doctype: html>
<html>
<head>
<title>SenSibDiary</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="{% static 'css/sleep.css' %}">
</head>
<body>
<div class"page-header">
#<!-- TODO: Nav menu here for authenticated users -->
<!-- TODO: Nav menu here for anonymous users / login redirect -->

<h1><a href="/">SenSibDiary</a></h1>
</div>
<div class="content container">
<div class="row">
<div class="col-md-8">
{% blockcontent %}
{% endblock%}
</div>
</div>
</div>
</body>
</html>
50 changes: 43 additions & 7 deletions sleep/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,50 @@ def create_sleep(sleep_date, time_went_to_sleep, time_woke_in_morning):
"""
return Sleep.objects.create(sleep_date=sleep_date, time_went_to_sleep=time_went_to_sleep, time_woke_in_morning=time_woke_in_morning)

#Placeholder for use in comparison checks datetime.timedelta(0)


class SleepModelTests(TestCase):
def test_sleep_with_past_wake_time(self):
def test_check_time_went_to_sleep_is_before_time_woke_in_morning_returns_false_for_past_sleep(self):
"""
check_time_went_to_sleep_is_before_time_woke_in_morning should
returns False for wake time before sleep time,
"""
time = timezone.now()
time_went_to_sleep = time
time_woke_in_morning = time - datetime.timedelta(hours=1)
past_sleep = create_sleep(time, time_went_to_sleep, time_woke_in_morning)
self.assertIs((past_sleep.check_time_went_to_sleep_is_before_time_woke_in_morning()), False)

def test_check_time_went_to_sleep_is_before_time_woke_in_morning_returns_true_for_correct_sleep(self):
"""
check_time_went_to_sleep_is_before_time_woke_in_morning should
returns True if sleep time is before wake time
"""
time = timezone.now()
time_went_to_sleep = time
time_woke_in_morning = time + datetime.timedelta(hours=6)
correct_sleep = create_sleep(time, time_went_to_sleep, time_woke_in_morning)
self.assertIs((correct_sleep.check_time_went_to_sleep_is_before_time_woke_in_morning()), True)


def test_total_time_asleep_is_not_negative(self):
"""
total_time_asleep should not be able to be negative,
returning an error if it is negative.
"""
time = timezone.now()
time_went_to_sleep = time
time_woke_in_morning = time - datetime.timedelta(hours=1)
past_sleep = create_sleep(time, time_went_to_sleep, time_woke_in_morning)
self.assertRaises(ValueError)

def test_total_time_asleep_is_returns_positive_if_correct(self):
"""
total_time_asleep() should if trying to create
a wake time that is before a sleep time
total_time_asleep returns positive datetime for correct sleep
"""
time = timezone.now()
sleep_start = time
sleep_end = time - datetime.timedelta(hours=1)
past_sleep = create_sleep(time, sleep_start, sleep_end)
self.assertIs((past_sleep.sleep_start >= past_sleep.sleep_end), False)
time_went_to_sleep = time
time_woke_in_morning = time + datetime.timedelta(hours=7)
correct_sleep = create_sleep(time, time_went_to_sleep, time_woke_in_morning)
self.assertIs((correct_sleep.total_time_asleep() > datetime.timedelta(0)), True)

0 comments on commit 41b9932

Please sign in to comment.