Skip to content

Commit

Permalink
Added an endpoint for adding new badges. Also, hubs will now only see…
Browse files Browse the repository at this point in the history
… badges from the assosiated project
  • Loading branch information
OrenLederman committed Jun 10, 2017
1 parent 0b9a4f0 commit 39ce99f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
7 changes: 4 additions & 3 deletions openbadge-server/openbadge/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import random
import json as simplejson
import string
import time


from decimal import Decimal
Expand Down Expand Up @@ -212,12 +213,12 @@ class Member(BaseModel):

email = models.EmailField(unique=True)
name = models.CharField(max_length=64)
badge = models.CharField(max_length=64)
badge = models.CharField(max_length=64, unique=True)
"""Some sort of hub-readable ID for the badge, similar to a MAC, but accessible from iPhone"""

last_audio_ts = models.DecimalField(max_digits=20, decimal_places=3, default=Decimal(0))
last_audio_ts = models.DecimalField(max_digits=20, decimal_places=3, default=round(Decimal(time.time()), 0))
last_audio_ts_fract = models.DecimalField(max_digits=20, decimal_places=3, default=Decimal(0))
last_proximity_ts = models.DecimalField(max_digits=20, decimal_places=3, default=Decimal(0))
last_proximity_ts = models.DecimalField(max_digits=20, decimal_places=3, default=round(Decimal(time.time()), 0))
last_voltage = models.DecimalField(max_digits=5, decimal_places=3, default=Decimal(0))
last_seen_ts = models.DecimalField(max_digits=20, decimal_places=3, default=Decimal(0))

Expand Down
3 changes: 2 additions & 1 deletion openbadge-server/openbadge/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Meta:
model = Member
fields = ('id', 'project', 'name', 'email', 'badge', 'last_seen_ts', 'last_audio_ts',
'last_audio_ts_fract', 'last_proximity_ts', 'last_voltage', 'key')
read_only_fields = ('project', 'id', 'key', 'badge', 'name', 'email')
read_only_fields = ('project', 'id', 'key')

def update(self, instance, validated_data):

Expand All @@ -34,6 +34,7 @@ def update(self, instance, validated_data):
instance.save()
return instance


class HubSerializer(serializers.ModelSerializer):
project = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())

Expand Down
3 changes: 2 additions & 1 deletion openbadge-server/openbadge/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

badges_list = views.MemberViewSet.as_view({
'get': 'list',
# 'post': 'create',
'post': 'create',
})

badges_details = views.MemberViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
#'post': 'create',
})

hubs_list = views.HubViewSet.as_view({
Expand Down
36 changes: 35 additions & 1 deletion openbadge-server/openbadge/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rest_framework.decorators import api_view
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework import status

from .decorators import app_view, is_god, is_own_project, require_hub_uuid
from .models import Meeting, Project, Hub, DataFile # Chunk # ActionDataChunk, SamplesDataChunk
Expand All @@ -37,11 +38,25 @@ def context(**extra):


class MemberViewSet(viewsets.ModelViewSet):
queryset = Member.objects.all()
#queryset = Member.objects.all()
serializer_class = MemberSerializer
permission_classes = [AppkeyRequired, HubUuidRequired]
lookup_field = 'key'

def get_queryset(self):
"""
Filters the members list based on the hub's project
:return:
"""

# hub information is validated in the permission class
hub_uuid = self.request.META.get("HTTP_X_HUB_UUID")
hub = Hub.objects.prefetch_related("project").get(uuid=hub_uuid)
project = hub.project

# Return only badges from the relevant project
return Member.objects.filter(project=project)

def retrieve(self, request, *args, **kwargs):
"""
Get the badge specified by the provided key
Expand All @@ -52,6 +67,25 @@ def retrieve(self, request, *args, **kwargs):
serializer = self.get_serializer(badge)
return Response(serializer.data)

def create(self, request, *args, **kwargs):
"""
Creates a new member under the call hub project
"""
hub_uuid = request.META.get("HTTP_X_HUB_UUID")
hub = Hub.objects.prefetch_related("project").get(uuid=hub_uuid)
project = hub.project

# request.data is from the POST object. Adding the project id
data = request.data.dict()
data['project'] = project.id

serializer = MemberSerializer(data=data)
if serializer.is_valid():
serializer.save() # will call .create()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class HubViewSet(viewsets.ModelViewSet):
queryset = Hub.objects.all()
Expand Down

0 comments on commit 39ce99f

Please sign in to comment.