# Path: sendit/apps/main/models.py
# class Batch(models.Model):
#     '''A batch has one or more images for some number of patients, each of which
#     is associated with a Study or Session. A batch maps cleanly to a folder that is
#     dropped into data for processing, and the application moves through tasks based
#     on batches.
#     '''
#     uid = models.CharField(max_length=200, null=False, unique=True)
# 
#     status = models.CharField(choices=BATCH_STATUS,
#                               default="NEW",
#                               max_length=250)
# 
#     add_date = models.DateTimeField('date added', auto_now_add=True)
#     has_error = models.BooleanField(choices=ERROR_STATUS, 
#                                     default=False,
#                                     verbose_name="HasError")
# 
#     qa = JSONField(default=dict())
#     logs = JSONField(default=dict())
#     modify_date = models.DateTimeField('date modified', auto_now=True)
#     tags = TaggableManager()
# 
#     def change_images_status(self,status):
#         '''change all images to have the same status'''
#         for dcm in self.image_set.all():
#             dcm.status = status
#             dcm.save()
#     
# 
#     def get_image_paths(self):
#         '''return file paths for all images associated
#         with a batch'''
#         image_files = []
#         for dcm in self.image_set.all():
#             try:
#                 if hasattr(dcm.image, 'file'):
#                     dicom_file = dcm.image.path
#                     if os.path.exists(dicom_file):
#                         image_files.append(dicom_file)
# 
#             # Image object has no file associated with it
#             except ValueError:
#                 pass
# 
#         return image_files
# 
#     def get_finished(self):
#         '''return file paths that aren't in PHI folder'''
#         return [ x for x in self.get_image_paths() if "/PHI/" not in x ]
# 
#     def get_path(self):
#         return "%s/%s" %(MEDIA_ROOT,self.id)
# 
#     def get_absolute_url(self):
#         return reverse('batch_details', args=[str(self.id)])
# 
#     def __str__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def __unicode__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def get_label(self):
#         return "batch"
# 
#     class Meta:
#         app_label = 'main'
# 
# class Image(models.Model):
#     '''An image maps to one dicom file, usually in a series
#     '''
#     uid = models.CharField(max_length=250, null=False, blank=False)
#     name = models.CharField(max_length=250, null=False, blank=False)
# 
#     image = models.FileField(upload_to=get_upload_folder,null=True,blank=False)
#     add_date = models.DateTimeField('date added', auto_now_add=True)
#     modify_date = models.DateTimeField('date modified', auto_now=True)
#     batch = models.ForeignKey(Batch,null=False,blank=False)
# 
#     def __str__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def __unicode__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def load_dicom(self):
#         '''easily load an Image if dicom file'''
#         from pydicom import read_file
#         return(read_file(self.image.path,
#                          force=True))
# 
#     def rename(self,new_basename,uid=None):
#         ''' rename will name the image path of an already uploaded
#         file to some new_basename.'''
#         if uid is not None:
#             self.uid = uid
#         new_dir = os.path.dirname(self.image.name)
#         initial_path = self.image.path
#         if self.name is not None:
#             new_basename = "%s_%s" %(new_basename,self.name)
#         new_name = "%s/%s" %(new_dir,new_basename)
#         self.image.name = new_name
#         new_path = os.path.join(settings.MEDIA_ROOT, new_name)
#         shutil.move(initial_path, new_path)
#         self.image.file.name = self.image.path
#         self.save()
#         return self
# 
# 
#     def get_label(self):
#         return "image"
# 
#     class Meta:
#         app_label = 'main'
#         unique_together = ('uid','batch',)
#  
#     # Get the url for a report collection
#     def get_absolute_url(self):
#         return reverse('image_details', args=[str(self.id)])


from django.contrib.auth.models import User
from sendit.apps.main.models import (
    Batch,
    Image
)
from rest_framework import serializers
'''
Copyright (c) 2017 Vanessa Sochat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

'''



class BatchSerializer(serializers.ModelSerializer):
    image_set = serializers.PrimaryKeyRelatedField(many=True, 

# Path: sendit/apps/main/models.py
# class Batch(models.Model):
#     '''A batch has one or more images for some number of patients, each of which
#     is associated with a Study or Session. A batch maps cleanly to a folder that is
#     dropped into data for processing, and the application moves through tasks based
#     on batches.
#     '''
#     uid = models.CharField(max_length=200, null=False, unique=True)
# 
#     status = models.CharField(choices=BATCH_STATUS,
#                               default="NEW",
#                               max_length=250)
# 
#     add_date = models.DateTimeField('date added', auto_now_add=True)
#     has_error = models.BooleanField(choices=ERROR_STATUS, 
#                                     default=False,
#                                     verbose_name="HasError")
# 
#     qa = JSONField(default=dict())
#     logs = JSONField(default=dict())
#     modify_date = models.DateTimeField('date modified', auto_now=True)
#     tags = TaggableManager()
# 
#     def change_images_status(self,status):
#         '''change all images to have the same status'''
#         for dcm in self.image_set.all():
#             dcm.status = status
#             dcm.save()
#     
# 
#     def get_image_paths(self):
#         '''return file paths for all images associated
#         with a batch'''
#         image_files = []
#         for dcm in self.image_set.all():
#             try:
#                 if hasattr(dcm.image, 'file'):
#                     dicom_file = dcm.image.path
#                     if os.path.exists(dicom_file):
#                         image_files.append(dicom_file)
# 
#             # Image object has no file associated with it
#             except ValueError:
#                 pass
# 
#         return image_files
# 
#     def get_finished(self):
#         '''return file paths that aren't in PHI folder'''
#         return [ x for x in self.get_image_paths() if "/PHI/" not in x ]
# 
#     def get_path(self):
#         return "%s/%s" %(MEDIA_ROOT,self.id)
# 
#     def get_absolute_url(self):
#         return reverse('batch_details', args=[str(self.id)])
# 
#     def __str__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def __unicode__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def get_label(self):
#         return "batch"
# 
#     class Meta:
#         app_label = 'main'
# 
# class Image(models.Model):
#     '''An image maps to one dicom file, usually in a series
#     '''
#     uid = models.CharField(max_length=250, null=False, blank=False)
#     name = models.CharField(max_length=250, null=False, blank=False)
# 
#     image = models.FileField(upload_to=get_upload_folder,null=True,blank=False)
#     add_date = models.DateTimeField('date added', auto_now_add=True)
#     modify_date = models.DateTimeField('date modified', auto_now=True)
#     batch = models.ForeignKey(Batch,null=False,blank=False)
# 
#     def __str__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def __unicode__(self):
#         return "%s-%s" %(self.id,self.uid)
# 
#     def load_dicom(self):
#         '''easily load an Image if dicom file'''
#         from pydicom import read_file
#         return(read_file(self.image.path,
#                          force=True))
# 
#     def rename(self,new_basename,uid=None):
#         ''' rename will name the image path of an already uploaded
#         file to some new_basename.'''
#         if uid is not None:
#             self.uid = uid
#         new_dir = os.path.dirname(self.image.name)
#         initial_path = self.image.path
#         if self.name is not None:
#             new_basename = "%s_%s" %(new_basename,self.name)
#         new_name = "%s/%s" %(new_dir,new_basename)
#         self.image.name = new_name
#         new_path = os.path.join(settings.MEDIA_ROOT, new_name)
#         shutil.move(initial_path, new_path)
#         self.image.file.name = self.image.path
#         self.save()
#         return self
# 
# 
#     def get_label(self):
#         return "image"
# 
#     class Meta:
#         app_label = 'main'
#         unique_together = ('uid','batch',)
#  
#     # Get the url for a report collection
#     def get_absolute_url(self):
#         return reverse('image_details', args=[str(self.id)])


# Path: sendit/apps/api/serializers.py
from django.contrib.auth.models import User
from sendit.apps.main.models import (
    Batch,
    Image
)
from rest_framework import serializers

'''
Copyright (c) 2017 Vanessa Sochat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

'''



class BatchSerializer(serializers.ModelSerializer):
    image_set = serializers.PrimaryKeyRelatedField(many=True, 

                                                queryset=Image.objects.all())