Skip to content

Commit

Permalink
Merged #29: Recent history setting: set range
Browse files Browse the repository at this point in the history
Merge with 6dab664addb814bc81d004bd2d3269b96eb88b7c
  • Loading branch information
dennissiemensma committed Feb 4, 2016
2 parents f28cbf2 + d470a15 commit 0e11bcd
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
@@ -1,4 +1,9 @@

v0.7 - xxxx-xx-xx
=================
- Recent history setting: set range (Issue #29).


v0.6 - 2016-02-03
=================
- Massive refactoring: Separating apps & using signals (Issue #19).
Expand Down
19 changes: 19 additions & 0 deletions dsmr_frontend/migrations/0002_recent_history_weeks.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name='frontendsettings',
name='recent_history_weeks',
field=models.IntegerField(help_text='The number of weeks displayed in the recent history overview.', default=4, choices=[(1, 'Last week'), (2, 'Last two weeks'), (3, 'Last three weeks'), (4, 'Last four weeks'), (5, 'Last five weeks')], verbose_name='Recent history weeks'),
),
]
13 changes: 6 additions & 7 deletions dsmr_frontend/mixins.py
Expand Up @@ -96,13 +96,12 @@ def get_context_data(self, **kwargs):


class HistoryMixin(object):
days_ago = 28
days_offset = 1

def get_context_data(self, **kwargs):
frontend_settings = FrontendSettings.get_solo()

context_data = super(HistoryMixin, self).get_context_data(**kwargs)
context_data['usage'] = []
context_data['days_ago'] = self.days_ago
context_data['days_ago'] = frontend_settings.recent_history_weeks * 7
context_data['track_temperature'] = WeatherSettings.get_solo().track

# @TODO: There must be a way to make this more clean.
Expand All @@ -116,9 +115,9 @@ def get_context_data(self, **kwargs):
# Summarize stats for the past two weeks.
now = timezone.now().astimezone(settings.LOCAL_TIME_ZONE)
dates = (
now - timezone.timedelta(days=n) for n in reversed(range(
self.days_offset, self.days_ago + 1
))
now - timezone.timedelta(days=n) for n in reversed(
range(1, context_data['days_ago'] + 1)
)
)

for current_day in dates:
Expand Down
15 changes: 15 additions & 0 deletions dsmr_frontend/models/settings.py
Expand Up @@ -5,12 +5,27 @@

class FrontendSettings(SingletonModel):
""" Singleton model restricted by django-solo plugin. Settings for this application only. """
RECENT_HISTORY_RANGE_CHOICES = (
(1, _('Last week')),
(2, _('Last two weeks')),
(3, _('Last three weeks')),
(4, _('Last four weeks')),
(5, _('Last five weeks')),
)

reverse_dashboard_graphs = models.BooleanField(
default=False,
verbose_name=_('Reverse dashboard graphs'),
help_text=_('Whether graphs are rendered with an reversed X-axis')
)

recent_history_weeks = models.IntegerField(
choices=RECENT_HISTORY_RANGE_CHOICES,
default=4,
verbose_name=_('Recent history weeks'),
help_text=_('The number of weeks displayed in the recent history overview.')
)

class Meta:
default_permissions = tuple()
verbose_name = _('Frontend configuration')
3 changes: 3 additions & 0 deletions dsmr_frontend/tests/models/test_settings.py
Expand Up @@ -15,3 +15,6 @@ def test_admin(self):

def test_reverse_dashboard_graphs(self):
self.assertFalse(self.instance.reverse_dashboard_graphs)

def test_recent_history_weeks(self):
self.assertEqual(self.instance.recent_history_weeks, 4)
10 changes: 8 additions & 2 deletions dsmr_frontend/tests/test_webinterface.py
@@ -1,3 +1,4 @@
import random
import json

from django.test import TestCase, Client
Expand All @@ -6,8 +7,9 @@

from dsmr_backend.tests.mixins import CallCommandStdoutMixin
from dsmr_consumption.models.consumption import ElectricityConsumption, GasConsumption
from dsmr_stats.models.note import Note
from dsmr_weather.models.statistics import TemperatureReading
from dsmr_frontend.models.settings import FrontendSettings
from dsmr_stats.models.note import Note


class TestViews(CallCommandStdoutMixin, TestCase):
Expand Down Expand Up @@ -71,6 +73,10 @@ def test_dashboard(self):
self.assertIn('consumption', response.context)

def test_history(self):
frontend_settings = FrontendSettings.get_solo()
frontend_settings.recent_history_weeks = random.randint(1, 5)
frontend_settings.save()

# History fetches all data BEFORE today, so add a little interval to make that happen.
self._synchronize_date(interval=timezone.timedelta(hours=-24))
response = self.client.get(
Expand All @@ -79,8 +85,8 @@ def test_history(self):
self.assertIn('usage', response.context)
self.assertIn('notes', response.context['usage'][0])
self.assertEqual(response.context['usage'][0]['notes'][0], 'Gourmetten')
self.assertEqual(response.context['days_ago'], frontend_settings.recent_history_weeks * 7)
self.assertFalse(response.context['track_temperature'])

self.assertEqual(response.status_code, 200)

def test_statistics(self):
Expand Down
Binary file modified dsmrreader/locales/nl/LC_MESSAGES/django.mo
Binary file not shown.
40 changes: 23 additions & 17 deletions dsmrreader/locales/nl/LC_MESSAGES/django.po
Expand Up @@ -7,8 +7,8 @@ msgid ""
msgstr ""
"Project-Id-Version: DSMR Reader\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-02-03 21:32+0000\n"
"PO-Revision-Date: 2016-02-03 22:22+0100\n"
"POT-Creation-Date: 2016-02-03 22:53+0000\n"
"PO-Revision-Date: 2016-02-03 23:52+0100\n"
"Last-Translator: Dennis Siemensma <dsmr@dennissiemensma.nl>\n"
"Language-Team: Dennis Siemensma <dsmr@dennissiemensma.nl>\n"
"Language: nl\n"
Expand Down Expand Up @@ -165,12 +165,33 @@ msgstr "Weergave"
msgid "Clears the entire cache."
msgstr "Gooit de volledige cache leeg."

msgid "Last week"
msgstr "Afgelopen week"

msgid "Last two weeks"
msgstr "Afgelopen twee weken"

msgid "Last three weeks"
msgstr "Afgelopen drie weken"

msgid "Last four weeks"
msgstr "Afgelopen vier weken"

msgid "Last five weeks"
msgstr "Afgelopen vijf weken"

msgid "Reverse dashboard graphs"
msgstr "Keer dashboard-grafieken om"

msgid "Whether graphs are rendered with an reversed X-axis"
msgstr "Bepaalt of de x-as van grafieken zijn omgedraaid"

msgid "Recent history weeks"
msgstr "Weken in recente historie"

msgid "The number of weeks displayed in the recent history overview."
msgstr "Het aantal weken dat getoond wordt in het recente historie overzicht."

msgid "Frontend configuration"
msgstr "Weergaveconfiguratie"

Expand Down Expand Up @@ -515,18 +536,3 @@ msgstr "Nederlands"

msgid "English"
msgstr "Engels"

#~ msgid "Stats configuration"
#~ msgstr "Statistiekenconfiguratie"

#~ msgid "Cleans up any source data from the poller. PERMANENTLY DELETES DATA!"
#~ msgstr "Ruimt alle brongegevens van de lezer op. VERWIJDERT DATA PERMANENT"

#~ msgid "Any polled source data older than DAYS will be purged (default %(default)s days)."
#~ msgstr "Alle uitgelezen gegevens ouder dan DAGEN wordt verwijderd (standaard %(default)s dagen)."

#~ msgid "Tells Django to NOT prompt the user for input of any kind."
#~ msgstr "Instrueert Django om de gebruiker NIET om welke input dan ook te vragen."

#~ msgid "WARNING: This will PERMANENTLY DELETE SOURCE DATA before \"{}\", still continue? yes/no: "
#~ msgstr "WAARSCHUWING: Dit VERWIJDERT PERMANENT ALLE BRONDATA van voor \"{}\", alsnog doorgaan? yes/no: "

0 comments on commit 0e11bcd

Please sign in to comment.