# Path: ncdjango/models.py
# class ProcessingJob(models.Model):
#     """ An active, completed, or failed geoprocessing job. """
# 
#     uuid = models.CharField(max_length=36, default=uuid.uuid4, db_index=True)
#     job = models.CharField(max_length=100)
#     user = models.ForeignKey(USER_MODEL, null=True, on_delete=models.SET_NULL)
#     user_ip = models.CharField(max_length=32)
#     created = models.DateTimeField(auto_now_add=True)
#     celery_id = models.CharField(max_length=100)
#     inputs = models.TextField(null=False, default="{}")
#     outputs = models.TextField(null=False, default="{}")
# 
#     @property
#     def status(self):
#         """ The status of the celery task for this job. """
# 
#         return AsyncResult(self.celery_id).status.lower()
# 
# Path: ncdjango/geoprocessing/serializers.py
# class ProcessingJobSerializer(serializers.ModelSerializer):
#     status = serializers.CharField(read_only=True)
#     inputs = serializers.JSONField(allow_null=True, write_only=True)
#     outputs = serializers.JSONField(read_only=True)
# 
#     class Meta:
#         model = ProcessingJob
#         fields = ('uuid', 'job', 'created', 'status', 'inputs', 'outputs')
#         read_only_fields = ('uuid', 'created', 'status')
# 
#     def validate_job(self, value):
#         if value not in REGISTERED_JOBS:
#             raise serializers.ValidationError('Invalid job name')
#         return value
# 
#     def validate_inputs(self, value):
#         if value:
#             try:
#                 return json.loads(value, strict=False)
#             except ValueError:
#                 raise serializers.ValidationError('Invalid input JSON')
# 
#         return {}
# 
#     def validate(self, data):
#         task = get_task_instance(data['job'])
#         missing_params = set(x.name for x in task.inputs if x.required).difference(set(data['inputs'].keys()))
# 
#         if missing_params:
#             raise serializers.ValidationError('Missing task inputs: {}'.format(','.join(missing_params)))
# 
#         return data
# 
#     def create(self, validated_data):
#         result = run_job.delay(validated_data['job'], validated_data['inputs'])
#         request = self.context['request']
# 
#         # Get real IP address if request has been forwarded
#         forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
#         if forwarded_for:
#             ip_address = forwarded_for.split(',', 1)[0].strip()
#         else:
#             ip_address = request.META.get('REMOTE_ADDR')
# 
#         return ProcessingJob.objects.create(
#             job=validated_data['job'], celery_id=result.id, inputs=json.dumps(validated_data['inputs']),
#             user_ip=ip_address, user=request.user if request.user.is_authenticated else None
#         )


from rest_framework import viewsets, mixins
from ncdjango.models import ProcessingJob
from .serializers import ProcessingJobSerializer



class ProcessingJobsViewset(mixins.CreateModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    queryset = ProcessingJob.objects.all()
    serializer_class = ProcessingJobSerializer

# Path: ncdjango/models.py
# class ProcessingJob(models.Model):
#     """ An active, completed, or failed geoprocessing job. """
# 
#     uuid = models.CharField(max_length=36, default=uuid.uuid4, db_index=True)
#     job = models.CharField(max_length=100)
#     user = models.ForeignKey(USER_MODEL, null=True, on_delete=models.SET_NULL)
#     user_ip = models.CharField(max_length=32)
#     created = models.DateTimeField(auto_now_add=True)
#     celery_id = models.CharField(max_length=100)
#     inputs = models.TextField(null=False, default="{}")
#     outputs = models.TextField(null=False, default="{}")
# 
#     @property
#     def status(self):
#         """ The status of the celery task for this job. """
# 
#         return AsyncResult(self.celery_id).status.lower()
# 
# Path: ncdjango/geoprocessing/serializers.py
# class ProcessingJobSerializer(serializers.ModelSerializer):
#     status = serializers.CharField(read_only=True)
#     inputs = serializers.JSONField(allow_null=True, write_only=True)
#     outputs = serializers.JSONField(read_only=True)
# 
#     class Meta:
#         model = ProcessingJob
#         fields = ('uuid', 'job', 'created', 'status', 'inputs', 'outputs')
#         read_only_fields = ('uuid', 'created', 'status')
# 
#     def validate_job(self, value):
#         if value not in REGISTERED_JOBS:
#             raise serializers.ValidationError('Invalid job name')
#         return value
# 
#     def validate_inputs(self, value):
#         if value:
#             try:
#                 return json.loads(value, strict=False)
#             except ValueError:
#                 raise serializers.ValidationError('Invalid input JSON')
# 
#         return {}
# 
#     def validate(self, data):
#         task = get_task_instance(data['job'])
#         missing_params = set(x.name for x in task.inputs if x.required).difference(set(data['inputs'].keys()))
# 
#         if missing_params:
#             raise serializers.ValidationError('Missing task inputs: {}'.format(','.join(missing_params)))
# 
#         return data
# 
#     def create(self, validated_data):
#         result = run_job.delay(validated_data['job'], validated_data['inputs'])
#         request = self.context['request']
# 
#         # Get real IP address if request has been forwarded
#         forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
#         if forwarded_for:
#             ip_address = forwarded_for.split(',', 1)[0].strip()
#         else:
#             ip_address = request.META.get('REMOTE_ADDR')
# 
#         return ProcessingJob.objects.create(
#             job=validated_data['job'], celery_id=result.id, inputs=json.dumps(validated_data['inputs']),
#             user_ip=ip_address, user=request.user if request.user.is_authenticated else None
#         )


# Path: ncdjango/geoprocessing/views.py
from rest_framework import viewsets, mixins
from ncdjango.models import ProcessingJob
from .serializers import ProcessingJobSerializer




class ProcessingJobsViewset(mixins.CreateModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    queryset = ProcessingJob.objects.all()
    serializer_class = ProcessingJobSerializer

    lookup_field = 'uuid'