Add user association to Timer (WIP - adding timer broken)

This commit is contained in:
Christopher Charbonneau Wells 2017-08-17 14:42:44 -04:00
parent d89fa9df11
commit 1b015d4dde
7 changed files with 34 additions and 10 deletions

View File

@ -49,7 +49,7 @@ class SleepSerializer(serializers.HyperlinkedModelSerializer):
class TimerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Timer
fields = ('name', 'start', 'end', 'duration', 'active')
fields = ('name', 'start', 'end', 'duration', 'active', 'user')
class TummyTimeSerializer(serializers.HyperlinkedModelSerializer):

View File

@ -62,7 +62,7 @@ class TimerViewSet(viewsets.ModelViewSet):
serializer_class = TimerSerializer
def get_queryset(self):
params = ['name', 'active']
params = ['name', 'active', 'user']
return filter_by_params(self.request, Timer, params)

View File

@ -46,9 +46,9 @@ class SleepAdmin(admin.ModelAdmin):
@admin.register(Timer)
class TimerAdmin(admin.ModelAdmin):
list_display = ('name', 'start', 'end', 'duration', 'active',)
list_filter = ('active',)
search_fields = ('name',)
list_display = ('name', 'start', 'end', 'duration', 'active', 'user')
list_filter = ('active', 'user')
search_fields = ('name', 'user')
@admin.register(TummyTime)

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-08-17 18:26
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('core', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='timer',
name='user',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='timers', to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
]

View File

@ -115,6 +115,7 @@ class Timer(models.Model):
start = models.DateTimeField(auto_now=True)
end = models.DateTimeField(blank=True, null=True, editable=False)
active = models.BooleanField(default=True, editable=False)
user = models.ForeignKey('auth.User', related_name='timers')
objects = models.Manager()

View File

@ -9,9 +9,10 @@ from core.models import Timer
register = template.Library()
@register.inclusion_tag('timer_list.html')
def list_timers(active=True):
timers = Timer.objects.filter(active=active)
@register.inclusion_tag('timer_list.html', takes_context=True)
def list_timers(context, active=True):
request = context['request'] or None
timers = Timer.objects.filter(user=request.user, active=active)
return {'timers': timers}

View File

@ -169,12 +169,10 @@ class TummyTimeAdd(PermissionRequiredMixin, CreateView):
class TummyTimeUpdate(PermissionRequiredMixin, UpdateView):
model = TummyTime
permission_required = ('core.change_tummytime',)
form_class = TummyTimeForm
success_url = '/tummy-time'
class TummyTimeDelete(PermissionRequiredMixin, DeleteView):
model = TummyTime
permission_required = ('core.delete_tummytime',)
success_url = '/tummy-time'