Merge branch 'basic_rest_framework' into 'master'

first pass rest framework

See merge request !7
This commit is contained in:
Tyrel Souza 2017-04-01 19:59:03 +00:00
commit e57e0100ea
10 changed files with 165 additions and 19 deletions

View File

@ -18,6 +18,12 @@ class Machine(models.Model):
max_length=256,
help_text='Database server host')
def is_reserved(self):
try:
return bool(self.machinenote)
except MachineNote.DoesNotExist:
return False
def __unicode__(self):
return self.hostname
@ -57,3 +63,8 @@ class MachineNote(models.Model):
user = models.ForeignKey(
User,
help_text='Who checked out the server?')
def __unicode__(self):
return u'Note for User <{}> Machine: <{}>'.format(
self.user,
self.machine)

View File

@ -0,0 +1,6 @@
import machines.viewsets
from rest_framework import routers
router = routers.SimpleRouter()
router.register('machine', machines.viewsets.MachineViewSet)
router.register('machine_note', machines.viewsets.MachineNoteViewSet)
router.register('machine_status', machines.viewsets.MachineStatusViewSet)

View File

@ -0,0 +1,41 @@
from rest_framework import serializers
from machines.models import Machine, MachineNote, MachineStatus
class MachineNoteSerializer(serializers.ModelSerializer):
class Meta:
model = MachineNote
fields = (
'machine',
'user',
'notes',
)
class MachineStatusSerializer(serializers.ModelSerializer):
class Meta:
model = MachineStatus
fields = (
'machine',
'git_branch',
'git_version',
'last_db_refresh',
'last_updated'
)
class MachineSerializer(serializers.ModelSerializer):
machine_note = MachineNoteSerializer(read_only=True)
machine_status = MachineStatusSerializer(read_only=True)
class Meta:
model = Machine
fields = (
'hostname',
'db_server',
'machine_note',
'machine_status',
)

View File

@ -0,0 +1,34 @@
from __future__ import print_function
from django.test import TestCase
from model_mommy import mommy
from rest_framework.renderers import JSONRenderer
# Models
from django.contrib.auth.models import User
from machines.models import Machine, MachineStatus, MachineNote
from machines.serializers import MachineSerializer
class SerializerTest(TestCase):
def setUp(self):
self.user = mommy.make(User)
self.machine = mommy.make(Machine)
self.machine_status = mommy.make(MachineStatus,
machine=self.machine
)
self.machine_note = mommy.make(MachineNote,
user=self.user,
machine=self.machine
)
def test_serializer(self):
"""
Test to make sure the serializer returns data in proper format
"""
ms = MachineSerializer(self.machine)
ms_json = JSONRenderer().render(ms)
self.assertEqual("", ms_json)

View File

@ -1,6 +1,9 @@
from django.conf.urls import url
from machines.views import MachineList
from machines.views import MachineList, MachineDetail
from django.conf.urls import url, include
from machines.routers import router
urlpatterns = [
url(r'^api/', include(router.urls)),
url(r'^(?P<pk>\w+)/$', MachineDetail.as_view(), name='machine-detail'),
url(r'^$', MachineList.as_view()),
]

View File

@ -1,7 +1,12 @@
from django.views.generic import ListView
from django.views.generic import ListView, DetailView
from machines.models import Machine
class MachineList(ListView):
context_object_name = 'machines'
queryset = Machine.objects.all()
class MachineDetail(DetailView):
context_object_name = 'machine'
model = Machine

View File

@ -0,0 +1,31 @@
from rest_framework import mixins
from rest_framework import viewsets
import machines.serializers
import machines.models
class MachineViewSet(mixins.UpdateModelMixin,
mixins.CreateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
serializer_class = machines.serializers.MachineSerializer
queryset = machines.models.Machine.objects.all()
class MachineNoteViewSet(mixins.UpdateModelMixin,
mixins.CreateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
serializer_class = machines.serializers.MachineNoteSerializer
queryset = machines.models.MachineNote.objects.all()
class MachineStatusViewSet(mixins.UpdateModelMixin,
mixins.CreateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
serializer_class = machines.serializers.MachineStatusSerializer
queryset = machines.models.MachineStatus.objects.all()

View File

@ -27,7 +27,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
]
PROJECT_APPS = [
'machines',
'machines.apps.MachinesConfig',
]
THIRD_PARTY_APPS = [
'django_nose',

View File

@ -0,0 +1,14 @@
<span>{{machine.hostname}}</span>
<ul>
<li>DB Server: {{ machine.db_server }}</li>
<li>DB Refresh: {{machine.machinestatus.last_db_refresh }}</li>
<li>Git Branch: {{ machine.machinestatus.git_branch }}</li>
<li>Git Version: {{ machine.machinestatus.git_version }}</li>
{% if machine.machinenote %}
<li>User: {{ machine.machinenote.user }}</li>
<li>Notes: <br>
<span>{{ machine.machinenote.notes|linebreaksbr }}</span>
</li>
{% endif %}
</ul>

View File

@ -2,25 +2,26 @@
<html>
<head>
<title>Staging Machines</title>
<style>
.machine_available {
background-color: green;
}
.machine_reserved{
background-color: red;
}
</style>
</head>
<body>
<ul>
{% for machine in machines %}
<li>
<span>{{machine.hostname}}</span>
<ul>
<li>DB Server: {{ machine.db_server }}</li>
<li>DB Refresh: {{machine.machinestatus.last_db_refresh }}</li>
<li>Git Branch: {{ machine.machinestatus.git_branch }}</li>
<li>Git Version: {{ machine.machinestatus.git_version }}</li>
{% if machine.machinenote %}
<li>User: {{ machine.machinenote.user }}</li>
<li>Notes: <br>
<span>{{ machine.machinenote.notes|linebreaksbr }}</span>
</li>
{% endif %}
</ul>
<li
{% if machine.is_reserved %}
class="machine_reserved"
{% else %}
class="machine_available"
{% endif %}"
>
{% include "machines/machine_detail.html" with machine=machine %}
</li>
{% endfor %}
</ul>