def create_group_view(request): # make sure the request is a POST request and that form data has been provided if request.method == "POST" and request.POST: # Extract the necessary info from the request post_data = request.POST group_name = post_data['name'] rider_id = post_data['rider_id'] aff_code = post_data['aff_code'] # Check if group code is already in use # if group test doesn't come back empty, return a bad request error group_test = Group.objects.filter(code=aff_code) if group_test: return write_response(request, json.dumps([{"success":"false", "message":"ERROR: Group code in use"}])) # Create a new group with the requested name and affinity code group = Group(name=group_name, code=aff_code) group.save() # Get the PK of the new group and create a mapping to a rider group_id = Group.objects.get(code=aff_code).id agm = Affinity_Group_Mapping(rider_id=rider_id, affinity_group_id=group_id) agm.save() # return a success code return write_response(request, json.dumps([{"success":"true", "message":"Sucess"}])) # return an error - tells client that only POST is allowed # response = HttpResponseNotAllowed(['POST']) # response.write("ERROR: Only POST requests allowed") # return response # returning a 404 so that the link looks invalid from the outside raise Http404
def create_group_view(request): # make sure the request is a POST request and that form data has been provided if request.method == "POST" and request.POST: # Extract the necessary info from the request post_data = request.POST group_name = post_data['name'] rider_id = post_data['rider_id'] aff_code = post_data['aff_code'] # Check if group code is already in use # if group test doesn't come back empty, return a bad request error group_test = Group.objects.filter(code=aff_code) if group_test: return write_response( request, json.dumps([{ "success": "false", "message": "ERROR: Group code in use" }])) # Create a new group with the requested name and affinity code group = Group(name=group_name, code=aff_code) group.save() # Get the PK of the new group and create a mapping to a rider group_id = Group.objects.get(code=aff_code).id agm = Affinity_Group_Mapping(rider_id=rider_id, affinity_group_id=group_id) agm.save() # return a success code return write_response( request, json.dumps([{ "success": "true", "message": "Sucess" }])) # return an error - tells client that only POST is allowed # response = HttpResponseNotAllowed(['POST']) # response.write("ERROR: Only POST requests allowed") # return response # returning a 404 so that the link looks invalid from the outside raise Http404
def join_group_view(request, aff_id, r_id): # Check if the group exists group_exists_test = Group.objects.filter(code=aff_id) if not group_exists_test: return write_response(request, json.dumps([{"success": "false", "message": "ERROR: Group does not exist"}])) # Check if the rider is already in the group rider_in_group_test = Group.objects.filter(rider__id=r_id).filter(code=aff_id) # if group_test isn't empty, return an error code if rider_in_group_test: return write_response(request, json.dumps([{"success": "false", "message": "ERROR: already in group"}])) # Get the numerical ID that matches the group's affinity code # Then create a mapping from the rider to the group group_id=Group.objects.get(code=aff_id).id agm = Affinity_Group_Mapping(rider_id=r_id,affinity_group_id=group_id) agm.save() # return a success response group_name = [{'success': 'true', 'message': 'success'}, {'name' : group_exists_test[0].name}] return write_response(request, json.dumps(group_name))
def join_group_view(request, aff_id, r_id): # Check if the group exists group_exists_test = Group.objects.filter(code=aff_id) if not group_exists_test: return write_response( request, json.dumps([{ "success": "false", "message": "ERROR: Group does not exist" }])) # Check if the rider is already in the group rider_in_group_test = Group.objects.filter(rider__id=r_id).filter( code=aff_id) # if group_test isn't empty, return an error code if rider_in_group_test: return write_response( request, json.dumps([{ "success": "false", "message": "ERROR: already in group" }])) # Get the numerical ID that matches the group's affinity code # Then create a mapping from the rider to the group group_id = Group.objects.get(code=aff_id).id agm = Affinity_Group_Mapping(rider_id=r_id, affinity_group_id=group_id) agm.save() # return a success response group_name = [{ 'success': 'true', 'message': 'success' }, { 'name': group_exists_test[0].name }] return write_response(request, json.dumps(group_name))