Add registration of users to the REST API

This commit is contained in:
Bradlee Speice 2013-10-19 20:31:08 -04:00
parent e1c789d8e3
commit e2161a1ebe
4 changed files with 47 additions and 2 deletions

View File

@ -8,8 +8,8 @@ class RegisteredUser(models.Model):
date_registered = models.DateTimeField(default=datetime.now) date_registered = models.DateTimeField(default=datetime.now)
first_name = models.CharField(max_length=64) first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64) last_name = models.CharField(max_length=64)
section = models.CharField(max_length=8) section = models.CharField(max_length=8, null=True, blank=True)
row = models.IntegerField() row = models.IntegerField(null=True, blank=True)
class ParkingLot(models.Model): class ParkingLot(models.Model):

View File

@ -13,3 +13,13 @@ class ParkingRatingSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ParkingRating model = ParkingRating
fields = ('rating', 'parking_lot',) fields = ('rating', 'parking_lot',)
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = RegisteredUser
fields = ('first_name', 'last_name', 'section', 'row')
class SingleUserSerializer(serializers.ModelSerializer):
class Meta:
model = RegisteredUser
fields = ('id', 'first_name', 'last_name')

View File

@ -8,4 +8,6 @@ urlpatterns = patterns('gameday.views',
url('^lots/$', views.ParkingLotList.as_view(), name='parking-lots'), url('^lots/$', views.ParkingLotList.as_view(), name='parking-lots'),
url('^lots/(?P<lot>\w+)/$', views.SingleParkingLotList.as_view(), name='parking-lot'), url('^lots/(?P<lot>\w+)/$', views.SingleParkingLotList.as_view(), name='parking-lot'),
url('^rate/$', views.RateLot.as_view(), name='parking-rating'), url('^rate/$', views.RateLot.as_view(), name='parking-rating'),
url('^register/(?P<id>\d+)/$', views.ListRegisteredUsers.as_view(), name='get-registered-user'),
url('^register/$', views.RegisterUser.as_view(), name='register-user'),
) )

View File

@ -56,3 +56,36 @@ class RateLot(APIView):
def get(self, request): def get(self, request):
'Get the rating choice options' 'Get the rating choice options'
return Response(ParkingRating.RATING_CHOICES) return Response(ParkingRating.RATING_CHOICES)
class RegisterUser(APIView):
"""
Handle Registration of users
**GET**: Get the current information of a registered user by ID
**POST**: Register a new user
"""
def get(self, request):
'Get the first and last names of all registered users'
users = RegisteredUser.objects.all()
users_s = SingleUserSerializer(users, many=True)
return Response(users_s.data)
def post(self, request):
'Register a new user'
user = UserSerializer(data=request.DATA)
if user.is_valid():
user.save()
return Response(user.data)
return Response(user.errors, status=status.HTTP_400_BAD_REQUEST)
class ListRegisteredUsers(APIView):
"""
List all registered users.
We have a separate view set up so we can list all information about one
user at a time (this one), and one view to get just id, first_name,
and last_name of all users.
"""
def get(self, request, id):
'Get the information for a registered user'
user = get_object_or_404(RegisteredUser, id=id)
return Response(UserSerializer(user).data)