From 4059d5db461aeeb89c91d80bde7a367292243b03 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sat, 1 Apr 2017 19:59:02 +0000 Subject: [PATCH] first pass rest framework --- stagestatus/apps/machines/models.py | 11 +++++ stagestatus/apps/machines/routers.py | 6 +++ stagestatus/apps/machines/serializers.py | 41 +++++++++++++++++++ .../apps/machines/tests/test_serializers.py | 34 +++++++++++++++ stagestatus/apps/machines/urls.py | 7 +++- stagestatus/apps/machines/views.py | 7 +++- stagestatus/apps/machines/viewsets.py | 31 ++++++++++++++ stagestatus/settings/base.py | 2 +- .../templates/machines/machine_detail.html | 14 +++++++ .../templates/machines/machine_list.html | 31 +++++++------- 10 files changed, 165 insertions(+), 19 deletions(-) create mode 100644 stagestatus/apps/machines/routers.py create mode 100644 stagestatus/apps/machines/serializers.py create mode 100644 stagestatus/apps/machines/tests/test_serializers.py create mode 100644 stagestatus/apps/machines/viewsets.py create mode 100644 stagestatus/templates/machines/machine_detail.html diff --git a/stagestatus/apps/machines/models.py b/stagestatus/apps/machines/models.py index e14b659..0364fd4 100644 --- a/stagestatus/apps/machines/models.py +++ b/stagestatus/apps/machines/models.py @@ -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) diff --git a/stagestatus/apps/machines/routers.py b/stagestatus/apps/machines/routers.py new file mode 100644 index 0000000..be21c86 --- /dev/null +++ b/stagestatus/apps/machines/routers.py @@ -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) diff --git a/stagestatus/apps/machines/serializers.py b/stagestatus/apps/machines/serializers.py new file mode 100644 index 0000000..b5cbf29 --- /dev/null +++ b/stagestatus/apps/machines/serializers.py @@ -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', + ) diff --git a/stagestatus/apps/machines/tests/test_serializers.py b/stagestatus/apps/machines/tests/test_serializers.py new file mode 100644 index 0000000..cbe2d01 --- /dev/null +++ b/stagestatus/apps/machines/tests/test_serializers.py @@ -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) diff --git a/stagestatus/apps/machines/urls.py b/stagestatus/apps/machines/urls.py index 1d40c87..68e7fd4 100644 --- a/stagestatus/apps/machines/urls.py +++ b/stagestatus/apps/machines/urls.py @@ -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\w+)/$', MachineDetail.as_view(), name='machine-detail'), url(r'^$', MachineList.as_view()), ] diff --git a/stagestatus/apps/machines/views.py b/stagestatus/apps/machines/views.py index 4bde3c1..4a4ca7c 100644 --- a/stagestatus/apps/machines/views.py +++ b/stagestatus/apps/machines/views.py @@ -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 diff --git a/stagestatus/apps/machines/viewsets.py b/stagestatus/apps/machines/viewsets.py new file mode 100644 index 0000000..3639513 --- /dev/null +++ b/stagestatus/apps/machines/viewsets.py @@ -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() diff --git a/stagestatus/settings/base.py b/stagestatus/settings/base.py index 84fd669..6228a71 100644 --- a/stagestatus/settings/base.py +++ b/stagestatus/settings/base.py @@ -27,7 +27,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', ] PROJECT_APPS = [ - 'machines', + 'machines.apps.MachinesConfig', ] THIRD_PARTY_APPS = [ 'django_nose', diff --git a/stagestatus/templates/machines/machine_detail.html b/stagestatus/templates/machines/machine_detail.html new file mode 100644 index 0000000..2e51f33 --- /dev/null +++ b/stagestatus/templates/machines/machine_detail.html @@ -0,0 +1,14 @@ +{{machine.hostname}} + diff --git a/stagestatus/templates/machines/machine_list.html b/stagestatus/templates/machines/machine_list.html index ac97edd..2246fac 100644 --- a/stagestatus/templates/machines/machine_list.html +++ b/stagestatus/templates/machines/machine_list.html @@ -2,25 +2,26 @@ Staging Machines +