def get(request): if request.method == 'GET': status, response_data = DepartmentController.get() else: # POST coordinates = request.data['area']['features'][0]['geometry'][ 'coordinates'] if coordinates[0] is not coordinates[-1]: coordinates += [coordinates[0]] status, response_data = DepartmentController.get(coordinates) response_json = json.dumps(response_data) return HttpResponse(response_json, content_type="application/json", status=status)
def delete(request): id = request.query_params.get('id') status, response_data = DepartmentController.delete(id) response_json = json.dumps(response_data) return HttpResponse(response_json, content_type="application/json", status=status)
def message_jurisdiction(request): user = request.user body = request.data department_name = body['department_name'] gov_official = DepartmentController.get_user_department_query(user) if not gov_official: response_json = { 'message': 'Must be government official to use this endpoint.' } return HttpResponse(json.dumps(response_json), content_type='application/json', status=400) DepartmentController.message_pilots(department_name, body['message']) response_json = {'message': 'Messages have successfully begun sending.'} return HttpResponse(json.dumps(response_json), content_type='application/json')
def get_user_departments(request): user = request.user status, response_data = DepartmentController.get_user_department(user) response_json = json.dumps(response_data) return HttpResponse(response_json, content_type="application/json", status=status)
def get_flights(user, method, body): user_owned_flights = Flight.objects department_flights = Flight.objects if method == 'POST': filters = body['filters'] for _filter in filters: if _filter['title'] == 'before': user_owned_flights = user_owned_flights.filter(ends_at__lt=parse_datetime(_filter['datetime'])) department_flights = department_flights.filter(ends_at__lt=parse_datetime(_filter['datetime'])) if _filter['title'] == 'after': user_owned_flights = user_owned_flights.filter(starts_at__gt=parse_datetime(_filter['datetime'])) department_flights = department_flights.filter(starts_at__gt=parse_datetime(_filter['datetime'])) user_departments = DepartmentController.get_user_department_query(user) for user_department in user_departments: user_owned_flights = user_owned_flights.filter(~Q(area__intersects=user_department.area)) department_flights = department_flights.filter(area__intersects=user_department.area) if len(user_departments) == 0: department_flights = [] else: department_flights = department_flights.all() user_owned_flights = user_owned_flights.filter(created_by=user.id).all() dictionaries = [] for flights, can_edit_clearance in [(department_flights, True), (user_owned_flights, False)]: for flight in flights: flight_dict = flight.as_dict() flight_dict['num_drones'] = Asset.objects.filter(flight=flight).count() flight_dict['can_edit_clearance'] = can_edit_clearance dictionaries += [flight_dict] return dictionaries
def add_airboss(request): body = request.data airboss_id = body['airboss_id'] name = body['name'] status, response_data = DepartmentController.add_airboss(name, airboss_id) response_json = json.dumps(response_data) return HttpResponse(response_json, content_type="application/json", status=status)
def remove_watch_commander(request): body = request.data watch_commander_id = body['watch_commander_id'] name = body['name'] status, response_data = DepartmentController.remove_watch_commander( name, watch_commander_id) response_json = json.dumps(response_data) return HttpResponse(response_json, content_type="application/json", status=status)
def get_flight_info(flight_id, user) -> (int, dict): flight = Flight.objects.filter(id=flight_id).first() if not flight: return 400, {"message": "No mission exists with that id."} flight_dict = flight.as_dict() user_departments = DepartmentController.get_user_department_query(user) flight_dict['can_edit_clearance'] = False for user_department in user_departments: if flight.area.intersects(user_department.area): flight_dict['can_edit_clearance'] = True return 200, flight_dict
def edit_clearance(user_id, flight_id, state, message) -> (int, dict): user = User.objects.filter(id=user_id).first() flight = Flight.objects.get(id=flight_id) if not DepartmentController.can_edit(user, flight): return 400, {'message': 'You do not have permissions to edit this flights clearance.'} object = flight.clearance object.created_by = user.username object.state = state object.message = message object.save() user = flight.created_by clearance_update_email.delay(user.username, user.email, user.username, flight.title, message, state) notify.send(user, recipient=flight.created_by, verb='updated flight clearance', action_object=flight) return 200, {'message': 'Successfully edited the clearance.'}
def flight_histogram(request): body = request.data start_day = parse_datetime(body['start_day']) end_day = parse_datetime(body['end_day']) department_id = body['department_id'] if start_day is None or end_day is None: response_json = { 'message': 'One or more datetime could not be parsed.' } return HttpResponse(json.dumps(response_json), content_type='application/json', status=400) status, flight_histogram_data = DepartmentController.flight_histogram( department_id, start_day, end_day, request.user) return HttpResponse(json.dumps(flight_histogram_data), content_type='application/json', status=status)
def edit(request): current_user = User.objects.filter(id=request.user.id).first() if not current_user.is_staff: response_data = {'message': 'Must be admin to use this endpoint.'} response_json = json.dumps(response_data) return HttpResponse(response_json, status=403, content_type="application/json") body = request.data name = body['name'] new_name = body['new_name'] coordinates = body['area']['features'][0]['geometry']['coordinates'] if coordinates[0] is not coordinates[-1]: coordinates += [coordinates[0]] status, response_data = DepartmentController.edit(name, new_name, coordinates) response_json = json.dumps(response_data) return HttpResponse(response_json, content_type="application/json", status=status)
def is_government_official(request): response_json = json.dumps( DepartmentController.is_department_member(request.user)) return HttpResponse(response_json, content_type="application/json")