Skip to content

Commit

Permalink
Created basic sleep model and admin view, started test creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftypanda committed Feb 3, 2018
0 parents commit 256015d
Show file tree
Hide file tree
Showing 34 changed files with 362 additions and 0 deletions.
Empty file added behaviour/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions behaviour/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions behaviour/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BehaviourConfig(AppConfig):
name = 'behaviour'
Empty file.
3 changes: 3 additions & 0 deletions behaviour/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions behaviour/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions behaviour/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Empty file added diary/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions diary/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions diary/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class DiaryConfig(AppConfig):
name = 'diary'
Empty file added diary/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions diary/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions diary/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions diary/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Empty file added intervention/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions intervention/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions intervention/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class InterventionConfig(AppConfig):
name = 'intervention'
Empty file.
3 changes: 3 additions & 0 deletions intervention/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions intervention/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions intervention/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
15 changes: 15 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sensibdiary.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
Empty file added sensibdiary/__init__.py
Empty file.
125 changes: 125 additions & 0 deletions sensibdiary/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"""
Django settings for sensibdiary project.
Generated by 'django-admin startproject' using Django 2.0.2.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'svub8ac+9%__xu(&$_+&vec&(=xtufs&*!6_!e71i0+7r(s=sm'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'diary',
'intervention',
'behaviour',
'sleep',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'sensibdiary.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'sensibdiary.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
21 changes: 21 additions & 0 deletions sensibdiary/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""sensibdiary URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions sensibdiary/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for sensibdiary project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sensibdiary.settings")

application = get_wsgi_application()
Empty file added sleep/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions sleep/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.contrib import admin

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

class SleepInterruptionInline(admin.TabularInline):
model = SleepInterruption
extra = 1

class SleepAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['sleep_date']}),
('Getting to sleep', {'fields': ['time_start_preparing_for_sleep', 'time_went_into_bed']}),
('Actually asleep', {'fields': ['time_went_to_sleep']}),
('Time woke in morning', {'fields': ['time_woke_in_morning']}),
]
inlines = [SleepInterruptionInline]
list_display = ('sleep_date', 'time_start_preparing_for_sleep', 'time_went_into_bed', 'time_went_to_sleep', 'time_woke_in_morning', 'total_time_asleep')
list_filter = ['sleep_date']
search_fields = ['sleep_date']

admin.site.register(Sleep, SleepAdmin)
5 changes: 5 additions & 0 deletions sleep/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class SleepConfig(AppConfig):
name = 'sleep'
36 changes: 36 additions & 0 deletions sleep/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 2.0.2 on 2018-02-03 13:39

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


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Sleep',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('sleep_date', models.DateField(verbose_name='sleep date')),
('time_start_preparing_for_sleep', models.DateTimeField(verbose_name='time started getting ready for bed')),
('time_went_into_bed', models.DateTimeField(verbose_name='time in bed')),
('time_went_to_sleep', models.DateTimeField(verbose_name='time asleep')),
('time_woke_in_morning', models.DateTimeField(verbose_name='time woke in morning')),
],
),
migrations.CreateModel(
name='SleepInterruption',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('interruption_time', models.TimeField(verbose_name='time woke during sleep')),
('intervention', models.CharField(max_length=200)),
('time_taken_to_sleep', models.IntegerField(default=10)),
('sleep_date', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sleep.Sleep')),
],
),
]
Empty file added sleep/migrations/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions sleep/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import datetime

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

# Create your models here.

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_woke_in_morning = models.DateTimeField('time woke in morning')



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

return timediff


class SleepInterruption(models.Model):
sleep_date = models.ForeignKey(Sleep, on_delete=models.CASCADE)

interruption_time = models.TimeField('time woke during sleep')

intervention = models.CharField(max_length=200)
intervention.short_description= 'What intervention happened?'

time_taken_to_sleep = models.IntegerField(default=10)
28 changes: 28 additions & 0 deletions sleep/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import datetime

from django.utils import timezone
from django.test import TestCase
from django.urls import reverse

from .models import Sleep
# Create your tests here.


def create_sleep(sleep_date, time_went_to_sleep, time_woke_in_morning):
"""
Create a sleep with given start time and end time in datetimeformat
used for further testing below
"""
return Sleep.objects.create(sleep_date=sleep_date, time_went_to_sleep=time_went_to_sleep, time_woke_in_morning=time_woke_in_morning)

class SleepModelTests(TestCase):
def test_sleep_with_past_wake_time(self):
"""
total_time_asleep() should if trying to create
a wake time that is before a sleep time
"""
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)
3 changes: 3 additions & 0 deletions sleep/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

0 comments on commit 256015d

Please sign in to comment.