def assign_device(request, device_id): device = Device.objects.filter(id=device_id).first() if device is None: return json_error_response('Not found', status=404) employer_id = request.POST.get('employer_id') if not employer_id: return json_error_response('Bad request', status=400) employer = Employer.objects.filter(id=employer_id).first() if employer is None: return json_error_response('Not found', status=400) if not device.assign(employer): return json_error_response( 'Cannot assign employer. Seems to assigned already', status=400) return json_success_response('Assigned')
def vote_for_update(request, request_id, vote): employer = Employer.objects.filter(user_id=request.user)[0] update_request = DeviceUpdateRequest.objects.get(id=request_id) possible_employers = [] for history_item in History.objects.filter(device=update_request.device): possible_employers.append(history_item.employer) try: if employer in possible_employers: if DeviceUpdateEmployer.objects.filter( update_request=update_request, employer=employer): raise Exception device_update_employer = DeviceUpdateEmployer( update_request=update_request, employer=employer) device_update_employer.save() update_request = DeviceUpdateRequest.objects.get(id=request_id) if int(vote) == 1: update_request.likes += 1 elif int(vote) == 0: update_request.dislikes += 1 update_request.save() else: raise Exception except Exception: return json_error_response('Something strange happend.', status=400) return json_success_response('Voted!')