Adding activity output
Bug fixing
This commit is contained in:
parent
7301e2a401
commit
c2e384cf7a
3 changed files with 59 additions and 5 deletions
|
@ -8,7 +8,7 @@ from dateutil.relativedelta import relativedelta
|
||||||
|
|
||||||
from .models import Status
|
from .models import Status
|
||||||
|
|
||||||
def moodstats(mindate=None, maxdate=None, days=7):
|
def moodstats(user, mindate=None, maxdate=None, days=7):
|
||||||
hv.extension('bokeh')
|
hv.extension('bokeh')
|
||||||
|
|
||||||
maxdate = maxdate or timezone.now()
|
maxdate = maxdate or timezone.now()
|
||||||
|
@ -27,7 +27,7 @@ def moodstats(mindate=None, maxdate=None, days=7):
|
||||||
|
|
||||||
pointdict = {"date": [], "value": [], "color": []}
|
pointdict = {"date": [], "value": [], "color": []}
|
||||||
|
|
||||||
for status in Status.objects.filter(timestamp__gte=mindate, timestamp__lte=maxdate):
|
for status in Status.objects.filter(user=user, timestamp__gte=mindate, timestamp__lte=maxdate):
|
||||||
if status.mood:
|
if status.mood:
|
||||||
pointdict["date"].append(status.timestamp)
|
pointdict["date"].append(status.timestamp)
|
||||||
pointdict["value"].append(status.mood.value)
|
pointdict["value"].append(status.mood.value)
|
||||||
|
@ -48,6 +48,21 @@ def moodstats(mindate=None, maxdate=None, days=7):
|
||||||
line = hv.Curve(pointtuples)
|
line = hv.Curve(pointtuples)
|
||||||
|
|
||||||
output = points * line
|
output = points * line
|
||||||
output.opts(tools=["xwheel_zoom"])
|
output.opts(tools=["xwheel_zoom"], ylim=(0, 5))
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
def activitystats(user, mindate=None, maxdate=None, days=7):
|
||||||
|
maxdate = maxdate or timezone.now()
|
||||||
|
mindate = mindate or (maxdate - relativedelta(days=days))
|
||||||
|
|
||||||
|
output = {}
|
||||||
|
|
||||||
|
for status in Status.objects.filter(user=user, timestamp__gte=mindate, timestamp__lte=maxdate):
|
||||||
|
for activity in status.activity_set:
|
||||||
|
if activity in output.keys():
|
||||||
|
output[activity] += 1
|
||||||
|
else:
|
||||||
|
output[activity] = 1
|
||||||
|
|
||||||
return output
|
return output
|
|
@ -19,6 +19,31 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="col-xl-4 col-lg-5">
|
||||||
|
<div class="card shadow mb-4">
|
||||||
|
<!-- Card Header - Dropdown -->
|
||||||
|
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
|
||||||
|
<h6 class="m-0 font-weight-bold text-primary">Activities</h6>
|
||||||
|
</div>
|
||||||
|
<!-- Card Body -->
|
||||||
|
<div class="card-body">
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Activity</th>
|
||||||
|
<th>Count</th>
|
||||||
|
</tr>
|
||||||
|
{% for activity, count in activities.items %}
|
||||||
|
<tr>
|
||||||
|
<td style="color:{{ activity.color }};"><i class="{{ activity.icon }}"></i><b> {{ activity }}</b></td>
|
||||||
|
<td style="text-align: right;">{{ count }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -9,7 +9,7 @@ from django.utils.decorators import method_decorator
|
||||||
|
|
||||||
from .models import Status, Activity, Mood, StatusMedia, StatusActivity
|
from .models import Status, Activity, Mood, StatusMedia, StatusActivity
|
||||||
from .forms import StatusForm
|
from .forms import StatusForm
|
||||||
from .statistics import moodstats
|
from .statistics import moodstats, activitystats
|
||||||
|
|
||||||
from common.helpers import get_upload_path
|
from common.helpers import get_upload_path
|
||||||
from common.templatetags.images import hvhtml
|
from common.templatetags.images import hvhtml
|
||||||
|
@ -330,8 +330,22 @@ class MoodStatisticsView(LoginRequiredMixin, TemplateView):
|
||||||
template_name = "mood/statistics.html"
|
template_name = "mood/statistics.html"
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
|
startdate = self.request.GET.get("start")
|
||||||
|
enddate = self.request.GET.get("end")
|
||||||
|
|
||||||
|
if enddate:
|
||||||
|
maxdate = datetime.strptime(enddate, "%Y-%m-%d")
|
||||||
|
else:
|
||||||
|
maxdate = timezone.now()
|
||||||
|
|
||||||
|
if startdate:
|
||||||
|
mindate = datetime.strptime(startdate, "%Y-%m-%d")
|
||||||
|
else:
|
||||||
|
mindate = maxdate - relativedelta.relativedelta(weeks=1)
|
||||||
|
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context["title"] = "Statistics"
|
context["title"] = "Statistics"
|
||||||
|
context["activities"] = activitystats(self.request.user, mindate, maxdate)
|
||||||
return context
|
return context
|
||||||
|
|
||||||
class MoodCSVView(LoginRequiredMixin, View):
|
class MoodCSVView(LoginRequiredMixin, View):
|
||||||
|
@ -383,5 +397,5 @@ class MoodPlotView(LoginRequiredMixin, View):
|
||||||
else:
|
else:
|
||||||
mindate = maxdate - relativedelta.relativedelta(weeks=1)
|
mindate = maxdate - relativedelta.relativedelta(weeks=1)
|
||||||
|
|
||||||
res.write(hvhtml(moodstats(mindate, maxdate)))
|
res.write(hvhtml(moodstats(request.user, mindate, maxdate)))
|
||||||
return res
|
return res
|
Loading…
Reference in a new issue