def jobs_list(request, format=None): if request.method == 'GET': jobs = Jobs.objects.all() serializer = JobsSerializer(jobs, many=True) return Response(serializer.data) elif request.method == 'POST': print ("Data: " + str(request.data)) try: username = request.data["username"] user = UserProfile.objects.get(username=username) except: print ("Username is not in the system.") content = "Username is not in the system." Response(content, status=status.HTTP_400_BAD_REQUEST) try: serializer = JobsSerializer(data=request.data) except: print ("Data is wrong compared to Jobs Serializer.") content = "Data is wrong compared to Jobs Profile Serializer." return Response(content, status=status.HTTP_400_BAD_REQUEST) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def current_jobs_list(request, format=None): """ List all current Jobs, or create a new Job. Path /api/jobs/current POST PARAMETERS data = { "userID": ID number, "category": "A Category", "description": "A description", "location": "A location", "date": "2015-09-28", "duration": 0, "timeUnit": "", "price": "", "lowerBound": 0, "upperBound": 0 } """ today = date.today() if request.method == 'GET': current_jobs = Jobs.objects.filter(date__gte=today).values('id', 'title','category','userID', 'description', 'location', 'date', 'duration', 'timeUnit', 'price', 'lowerBound', 'upperBound') # serializer = JobsSerializer(jobs, many=True) return Response(list(current_jobs)) # return Response(serializer.data) elif request.method == 'POST': print ("Data: " + str(request.data)) try: userID = request.data["userID"] user = UserProfile.objects.get(id=userID) except: print ("Username is not in the system.") content = "Username is not in the system." Response(content, status=status.HTTP_400_BAD_REQUEST) try: serializer = JobsSerializer(data=request.data) except: print ("Data is wrong compared to Jobs Serializer.") content = "Data is wrong compared to Jobs Profile Serializer." return Response(content, status=status.HTTP_400_BAD_REQUEST) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_400_BAD_REQUEST)
def jobs_list(request, format=None): """ List all Jobs, or create a new Job. Path /api/jobs POST PARAMETERS data = { "userID": ID number, "category": "A Category", "description": "A description", "location": "A location", "date": "2015-09-28", "duration": 0, "timeUnit": "", "price": "", "lowerBound": 0, "upperBound": 0, "status": "A status" } """ if request.method == 'GET': jobs = Jobs.objects.all() serializer = JobsSerializer(jobs, many=True) return Response(serializer.data) elif request.method == 'POST': print ("Data: " + str(request.data)) try: userID = request.data["userID"] user = UserProfile.objects.get(id=userID) except: print ("Username is not in the system.") content = "Username is not in the system." Response(content, status=status.HTTP_400_BAD_REQUEST) try: serializer = JobsSerializer(data=request.data) except: print ("Data is wrong compared to Jobs Serializer.") content = "Data is wrong compared to Jobs Profile Serializer." return Response(content, status=status.HTTP_400_BAD_REQUEST) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_400_BAD_REQUEST)
def user_job_detail(request, pk, job_number, format=None): """ Add, Delete, or Update a User's Job. Path: /api/jobs/USER_ID_NUMBER/JOB_NUMBER PUT PARAMETERS data = { "userID": ID number, "description": "A description", "location": "A location", "date": "2015-09-28", "duration": 0, "timeUnit": "", "price": "", "lowerBound": 0, "upperBound": 0, "status": "Active, Contract, Deleted, or Expired" } """ try: userprofile = UserProfile.objects.get(pk=pk) except UserProfile.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = Jobs.objects.filter(id=job_number).values('id', 'category','userID', 'description', 'location', 'date', 'duration', 'timeUnit', 'price', 'lowerBound', 'upperBound', 'status', 'title') return Response(serializer) elif request.method == 'PUT': try: jobs = Jobs.objects.get(id=job_number) except: return Response(data="id is missing",status=status.HTTP_400_BAD_REQUEST) serializer = JobsSerializer(jobs, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': job = Jobs.objects.get(id=job_number) job.status = "Deleted" job.save() return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_400_BAD_REQUEST)