def data_get(request, *args,**kargs): data_uuid = request.query_params.get('uuid') print(data_uuid) data = Data.objects.get(uuid = data_uuid) if not is_data_related(request.user, data): raise exceptions.PermissionDenied() data = DataSerializer(data).data return response_gen(data=data)
def sensor_get(request, *args, **kargs): sensor_uuid = request.query_params.get('uuid') sensor = Sensor.objects.get(uuid=sensor_uuid) if not is_sensor_related(request.user, sensor): raise exceptions.PermissionDenied() data = SensorSerilizer(sensor).data return response_gen(data=data)
def sensor_delete(request, *args, **kargs): sensor_uuid = request.query_params.get('uuid') sensor: Sensor = Sensor.objects.get(uuid=sensor_uuid) if not is_sensor_related(request.user, sensor): raise exceptions.PermissionDenied() sensor.delete() return response_gen()
def crop_get(request, *args, **kargs): crop_uuid = request.query_params.get('uuid') crop = Crop.objects.get(uuid=crop_uuid) if not is_crop_related(request.user, crop): raise exceptions.PermissionDenied() data = CropProductionSerialzier(crop).data return response_gen(data=data)
def sensor_create(request, *args, **kargs): data = request.data data['field'] = Field.objects.get(uuid=data.get('field', None)) data['feed'] = Feed.objects.get(uuid=data.get('feed', None)) if not is_field_related(request.user, data['field']): raise exceptions.PermissionDenied() sensor = Sensor.objects.create(**data) return response_gen(SensorSerilizer(sensor).data)
def sensor_matrix(request, *args, **kargs): field_uuid = request.data.get('field_uuid', None) field = Field.objects.get(uuid=field_uuid) if not is_field_related(request.user, field): raise exceptions.PermissionDenied() sensors = field.sensor_field.all().order_by('-created_at') data = SensorSerilizer(sensors, many=True).data return response_gen(data)
def crop_matrix(request, *args, **kargs): field_uuid = request.data.get('field_uuid', None) field = Field.objects.get(uuid=field_uuid) if not is_field_related(request.user, field): raise exceptions.PermissionDenied() crops = field.crop_field.all().order_by('-created_at') data = CropProductionSerialzier(crops, many=True).data return response_gen(data)
def crop_create(request, *args, **kargs): data = request.data data['field'] = Field.objects.get(uuid=data.get('field', None)) data['production'] = Production.objects.get( uuid=data.get('production', None)) if not is_field_related(request.user, data['field']): raise exceptions.PermissionDenied() crop = Crop.objects.create(**data) return response_gen(CropProductionSerialzier(crop).data)
def field_active_crop(request, *args, **kargs): field_uuid = request.query_params.get('field_uuid', None) field: Field = Field.objects.get(uuid=field_uuid) if not is_field_related(request.user, field): raise exceptions.PermissionDenied crop = field.current_crop() data = CropProductionSerialzier(crop).data if crop else None return response_gen(data=data)
def harvest_crop(request, *args, **kargs): crop_uuid = request.query_params.get('uuid', None) crop: Crop = Crop.objects.get(uuid=crop_uuid) if not is_crop_related(request.user, crop): raise exceptions.PermissionDenied() state = crop.state if state == CropState.HARVESTED: raise exceptions.PermissionDenied() crop.state = CropState.HARVESTED crop.harvested_at = datetime.datetime.now() crop.save() return response_gen(CropProductionSerialzier(crop).data)
def data_matrix(request, *args, **kargs): field_uuid = request.data.get('field_uuid', None) field = Field.objects.filter(uuid = field_uuid).first() crop_uuid = request.data.get('crop_uuid', None) crop = Crop.objects.filter(uuid = crop_uuid).first() if field and not is_field_related(request.user, field): raise exceptions.PermissionDenied() if crop and not is_crop_related(request.user, crop): raise exceptions.PermissionDenied() data = { 'crop':crop, 'field':field, } qs = DataFilter(data = data,queryset=Data.objects.all()).qs qs = qs.order_by('-created_at') data = DataSerializer(qs, many = True).data return response_gen(data)