From d02a68a85932c89d1151409673cfbf3b51d8d863 Mon Sep 17 00:00:00 2001 From: Shiftypanda Date: Tue, 6 Feb 2018 23:20:05 +0000 Subject: [PATCH] Updated html and added views for sleep details on frontend --- sensibdiary/settings.py | 4 +-- sensibdiary/urls.py | 3 +- sleep/models.py | 2 +- sleep/static/templates/css/sleep.css | 6 ++-- sleep/{static => }/templates/sleep/base.html | 8 ++--- sleep/templates/sleep/detail.html | 24 ++++++++++++++ sleep/templates/sleep/index.html | 10 ++++++ sleep/urls.py | 9 ++++++ sleep/views.py | 33 +++++++++++++++++++- 9 files changed, 88 insertions(+), 11 deletions(-) rename sleep/{static => }/templates/sleep/base.html (79%) create mode 100644 sleep/templates/sleep/detail.html create mode 100644 sleep/templates/sleep/index.html create mode 100644 sleep/urls.py diff --git a/sensibdiary/settings.py b/sensibdiary/settings.py index 2306fef..38a5d79 100644 --- a/sensibdiary/settings.py +++ b/sensibdiary/settings.py @@ -25,7 +25,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['localhost', '127.0.0.1', '.pythonanywhere.com'] # Application definition @@ -109,7 +109,7 @@ LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE = 'GB' USE_I18N = True diff --git a/sensibdiary/urls.py b/sensibdiary/urls.py index ea84670..82e1e86 100644 --- a/sensibdiary/urls.py +++ b/sensibdiary/urls.py @@ -14,8 +14,9 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), + path('sleep-diary/', include('sleep.urls')), ] diff --git a/sleep/models.py b/sleep/models.py index 9cc81f6..9d17919 100644 --- a/sleep/models.py +++ b/sleep/models.py @@ -71,7 +71,7 @@ def __str__(self): class SleepInterruption(models.Model): - sleep_date = models.ForeignKey(Sleep, on_delete=models.CASCADE) + sleep_date = models.ForeignKey(Sleep, on_delete=models.CASCADE, related_name='interruptions') interruption_time = models.TimeField('time woke during sleep') diff --git a/sleep/static/templates/css/sleep.css b/sleep/static/templates/css/sleep.css index f88cba6..1f1f5a8 100644 --- a/sleep/static/templates/css/sleep.css +++ b/sleep/static/templates/css/sleep.css @@ -1,13 +1,15 @@ -.page-header { - background-color: teal; +.base-header { + margin-top: 0; padding: 20px 20px 20px 40px; + background-color: teal; } .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 { diff --git a/sleep/static/templates/sleep/base.html b/sleep/templates/sleep/base.html similarity index 79% rename from sleep/static/templates/sleep/base.html rename to sleep/templates/sleep/base.html index 36e7a30..3f26093 100644 --- a/sleep/static/templates/sleep/base.html +++ b/sleep/templates/sleep/base.html @@ -6,11 +6,11 @@ - + -
- # +
+

SenSibDiary

@@ -18,7 +18,7 @@

SenSibDiary

- {% blockcontent %} + {% block content %} {% endblock%}
diff --git a/sleep/templates/sleep/detail.html b/sleep/templates/sleep/detail.html new file mode 100644 index 0000000..1e138af --- /dev/null +++ b/sleep/templates/sleep/detail.html @@ -0,0 +1,24 @@ +{% extends 'sleep/base.html' %} + {% block content %} + +

{{ sleep.sleep_date|date:'d-m-Y'}}

+{% if error_message %}

{{ error_message }}

{% endif %} +
+

Sleep date: {{ sleep.sleep_date|date:'d-m-Y'}}

+
+ + + + + + + +
+ + +
Time started preparing for bed: {{ sleep.time_start_preparing_for_sleep|date:'H:i e'}}
Time went to bed: {{ sleep.time_went_into_bed|date:'H:i e' }}
Time went to sleep: {{ sleep.time_went_to_sleep|date:'H:i e'}}
Time woke in the morning: {{ sleep.time_woke_in_morning|date:'H:i e'}}
+
+
+ + + {% endblock %} diff --git a/sleep/templates/sleep/index.html b/sleep/templates/sleep/index.html new file mode 100644 index 0000000..fe2448c --- /dev/null +++ b/sleep/templates/sleep/index.html @@ -0,0 +1,10 @@ +{% extends 'sleep/base.html' %} + {% block content %} + {% for sleep in latest_sleeps_lists %} + +

Sleep date: {{ sleep.sleep_date|date:'d-m-Y'}}

+
+ + {% endfor %} + + {% endblock %} diff --git a/sleep/urls.py b/sleep/urls.py new file mode 100644 index 0000000..06d6fc1 --- /dev/null +++ b/sleep/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from . import views + +app_name = 'sleep' +urlpatterns = [ + path('', views.IndexView.as_view(), name='index'), + path('/', views.DetailView.as_view(), name='detail') +] diff --git a/sleep/views.py b/sleep/views.py index 91ea44a..3dc5a94 100644 --- a/sleep/views.py +++ b/sleep/views.py @@ -1,3 +1,34 @@ -from django.shortcuts import render +from django.shortcuts import get_object_or_404, render +from django.http import HttpResponseRedirect +from django.utils import timezone +from django.contrib.auth.decorators import login_required +from django.views import generic +from django.urls import reverse + +from .models import Sleep, SleepInterruption # Create your views here. + + +class IndexView(generic.ListView): + template_name = 'sleep/index.html' + context_object_name = 'latest_sleeps_list' + + def get_queryset(self): + """ + Return the last seven sleeps + """ + return Sleep.objects.filter( + sleep_date__lte=timezone.now() + ).order_by('-sleep_date')[:7] + + +class DetailView(generic.DetailView): + model = Sleep + template_name ='sleep/detail.html' + + def get_queryset(self): + """ + Excludes any sleeps that haven't completed yet + """ + return Sleep.objects.filter(sleep_date__lte=timezone.now())