mirror of https://github.com/snachodog/mybuddy.git
Add basic Baby and Sleep models.
This commit is contained in:
parent
fbbaa76153
commit
cafbe048cb
|
@ -1,3 +0,0 @@
|
||||||
from django.contrib import admin
|
|
||||||
|
|
||||||
# Register your models here.
|
|
|
@ -1,3 +0,0 @@
|
||||||
from django.db import models
|
|
||||||
|
|
||||||
# Create your models here.
|
|
|
@ -4,8 +4,22 @@ from __future__ import unicode_literals
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from core.models import Baby, Sleep
|
||||||
|
|
||||||
|
|
||||||
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ('url', 'username', 'email', 'is_staff')
|
fields = ('url', 'username', 'email', 'is_staff')
|
||||||
|
|
||||||
|
|
||||||
|
class BabySerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Baby
|
||||||
|
fields = ('first_name', 'last_name', 'birth_date')
|
||||||
|
|
||||||
|
|
||||||
|
class SleepSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Sleep
|
||||||
|
fields = ('baby', 'start', 'end')
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
from .views import UserViewSet
|
from .views import UserViewSet, BabyViewSet, SleepViewSet
|
||||||
|
|
||||||
# Routers provide an easy way of automatically determining the URL conf.
|
# Routers provide an easy way of automatically determining the URL conf.
|
||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
router.register(r'users', UserViewSet)
|
router.register(r'users', UserViewSet)
|
||||||
|
router.register(r'babies', BabyViewSet)
|
||||||
|
router.register(r'sleep', SleepViewSet)
|
||||||
|
|
||||||
# Wire up our API using automatic URL routing.
|
# Wire up our API using automatic URL routing.
|
||||||
# Additionally, we include login URLs for the browsable API.
|
# Additionally, we include login URLs for the browsable API.
|
||||||
|
|
13
api/views.py
13
api/views.py
|
@ -4,9 +4,20 @@ from __future__ import unicode_literals
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|
||||||
from .serializers import (UserSerializer)
|
from core.models import Baby, Sleep
|
||||||
|
from .serializers import (UserSerializer, BabySerializer, SleepSerializer)
|
||||||
|
|
||||||
|
|
||||||
class UserViewSet(viewsets.ModelViewSet):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
queryset = User.objects.all()
|
queryset = User.objects.all()
|
||||||
serializer_class = UserSerializer
|
serializer_class = UserSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class BabyViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = Baby.objects.all()
|
||||||
|
serializer_class = BabySerializer
|
||||||
|
|
||||||
|
|
||||||
|
class SleepViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = Sleep.objects.all()
|
||||||
|
serializer_class = SleepSerializer
|
||||||
|
|
|
@ -31,13 +31,17 @@ ALLOWED_HOSTS = []
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
|
'api',
|
||||||
|
'core',
|
||||||
|
|
||||||
|
'rest_framework',
|
||||||
|
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'rest_framework'
|
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|
|
@ -1,3 +1,18 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
# Register your models here.
|
from .models import Baby, Sleep
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(Baby)
|
||||||
|
class BabyAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('first_name', 'last_name', 'birth_date',)
|
||||||
|
search_fields = ('first_name', 'last_name', 'birth_date',)
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(Sleep)
|
||||||
|
class SleepAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('baby', 'start', 'end',)
|
||||||
|
search_fields = ('baby__first_name', 'baby__last_name',)
|
||||||
|
|
|
@ -1,3 +1,38 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
# Create your models here.
|
|
||||||
|
class Baby(models.Model):
|
||||||
|
first_name = models.CharField(max_length=255)
|
||||||
|
last_name = models.CharField(max_length=255)
|
||||||
|
birth_date = models.DateField(blank=False, null=False)
|
||||||
|
|
||||||
|
objects = models.Manager()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
default_permissions = ('view', 'add', 'change', 'delete')
|
||||||
|
ordering = ['last_name', 'first_name']
|
||||||
|
verbose_name_plural = 'Babies'
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return '{} {}'.format(self.first_name, self.last_name)
|
||||||
|
|
||||||
|
|
||||||
|
class Sleep(models.Model):
|
||||||
|
baby = models.ForeignKey('Baby', related_name='sleep')
|
||||||
|
start = models.DateTimeField(blank=False, null=False)
|
||||||
|
end = models.DateTimeField(blank=False, null=False)
|
||||||
|
|
||||||
|
objects = models.Manager()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
default_permissions = ('view', 'add', 'change', 'delete')
|
||||||
|
ordering = ['-start']
|
||||||
|
verbose_name_plural = 'Sleep'
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return '{} slept from {} to {} on {}'.format(
|
||||||
|
self.baby,
|
||||||
|
self.start.time(),
|
||||||
|
self.end.time(),
|
||||||
|
self.end.date(),
|
||||||
|
)
|
||||||
|
|
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
Loading…
Reference in New Issue