diff --git a/behaviour/__init__.py b/behaviour/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/behaviour/admin.py b/behaviour/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/behaviour/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/behaviour/apps.py b/behaviour/apps.py new file mode 100644 index 0000000..0bc0234 --- /dev/null +++ b/behaviour/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class BehaviourConfig(AppConfig): + name = 'behaviour' diff --git a/behaviour/migrations/__init__.py b/behaviour/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/behaviour/models.py b/behaviour/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/behaviour/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/behaviour/tests.py b/behaviour/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/behaviour/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/behaviour/views.py b/behaviour/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/behaviour/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/diary/__init__.py b/diary/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/diary/admin.py b/diary/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/diary/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/diary/apps.py b/diary/apps.py new file mode 100644 index 0000000..8b391a3 --- /dev/null +++ b/diary/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class DiaryConfig(AppConfig): + name = 'diary' diff --git a/diary/migrations/__init__.py b/diary/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/diary/models.py b/diary/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/diary/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/diary/tests.py b/diary/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/diary/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/diary/views.py b/diary/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/diary/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/intervention/__init__.py b/intervention/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/intervention/admin.py b/intervention/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/intervention/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/intervention/apps.py b/intervention/apps.py new file mode 100644 index 0000000..3a39cea --- /dev/null +++ b/intervention/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class InterventionConfig(AppConfig): + name = 'intervention' diff --git a/intervention/migrations/__init__.py b/intervention/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/intervention/models.py b/intervention/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/intervention/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/intervention/tests.py b/intervention/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/intervention/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/intervention/views.py b/intervention/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/intervention/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..018bb44 --- /dev/null +++ b/manage.py @@ -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) diff --git a/sensibdiary/__init__.py b/sensibdiary/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sensibdiary/settings.py b/sensibdiary/settings.py new file mode 100644 index 0000000..2306fef --- /dev/null +++ b/sensibdiary/settings.py @@ -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') diff --git a/sensibdiary/urls.py b/sensibdiary/urls.py new file mode 100644 index 0000000..ea84670 --- /dev/null +++ b/sensibdiary/urls.py @@ -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), +] diff --git a/sensibdiary/wsgi.py b/sensibdiary/wsgi.py new file mode 100644 index 0000000..5c384aa --- /dev/null +++ b/sensibdiary/wsgi.py @@ -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() diff --git a/sleep/__init__.py b/sleep/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sleep/admin.py b/sleep/admin.py new file mode 100644 index 0000000..dd81c06 --- /dev/null +++ b/sleep/admin.py @@ -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) diff --git a/sleep/apps.py b/sleep/apps.py new file mode 100644 index 0000000..f08873c --- /dev/null +++ b/sleep/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class SleepConfig(AppConfig): + name = 'sleep' diff --git a/sleep/migrations/0001_initial.py b/sleep/migrations/0001_initial.py new file mode 100644 index 0000000..ac65912 --- /dev/null +++ b/sleep/migrations/0001_initial.py @@ -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')), + ], + ), + ] diff --git a/sleep/migrations/__init__.py b/sleep/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sleep/models.py b/sleep/models.py new file mode 100644 index 0000000..9dd9d9f --- /dev/null +++ b/sleep/models.py @@ -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) diff --git a/sleep/tests.py b/sleep/tests.py new file mode 100644 index 0000000..83f36d1 --- /dev/null +++ b/sleep/tests.py @@ -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) diff --git a/sleep/views.py b/sleep/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/sleep/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.