from django.db import models
from tagging.fields import TagField
from django.core.files.base import ContentFile
# Create your models here.

project_list = ( 
        ('deegree', 'deegree'),
        ('mapbender', 'Mapbender'),
        ('mapbuilder', 'MapBuilder'),
        ('mapguide', 'MapGuide'),
        ('mapserver', 'MapServer'),
        ('openlayers', 'OpenLayers'),
        ('grass', 'GRASS GIS'),
        ('qgis', "Quantum GIS"),
        ('gvsig', 'gvSIG'),
        ('fdo', 'FDO'),
        ('gdal', 'GDAL/OGR'),
        ('geos', 'GEOS'),
        ('geotools', 'GeoTools'),
        ('metacrs', 'MetaCRS'),
        ('geonetwork', 'GeoNetwork')
)

class Project(models.Model):
    def __unicode__(self):
        return self.name

    key = models.CharField(max_length=255)
    name = models.CharField(max_length=255)    

class Item(models.Model):
    class Admin: pass
    name = models.CharField(max_length=255)
    description = models.TextField()
    url = models.CharField(max_length=255)
    projects = models.ManyToManyField(Project)   
    technologies = TagField( help_text="Tag list, comma or space seperated" )
    image = models.ImageField(upload_to='images/', blank=True)
    contact = models.CharField(max_length=255, blank=True, null=True, help_text="Organization or Contact information; optional; will be published")
    created = models.DateTimeField(auto_now_add=True) 
    modified = models.DateTimeField(auto_now=True) 
    thumbnail = models.ImageField(upload_to="thumbnails/", editable=False, blank=True)
    @models.permalink
    def get_absolute_url(self):
        return ('gallery.views.item', [str(self.id)]) 

    def save(self):
        if not self.thumbnail and self.image:
            # We use PIL's Image object
            # Docs: http://www.pythonware.com/library/pil/handbook/image.htm
            from PIL import Image
        
            # Set our max thumbnail size in a tuple (max width, max height)
            THUMBNAIL_SIZE = (150, 150)
        
            # Open original photo which we want to thumbnail using PIL's Image
            # object
            image = Image.open(self.image.path)
        
            # Convert to RGB if necessary
            # Thanks to Limodou on DjangoSnippets.org
            # http://www.djangosnippets.org/snippets/20/
            if image.mode not in ('L', 'RGB'):
                image = image.convert('RGB')
        
            # We use our PIL Image object to create the thumbnail, which already
            # has a thumbnail() convenience method that contrains proportions.
            # Additionally, we use Image.ANTIALIAS to make the image look better.
            # Without antialiasing the image pattern artifacts may result.
            image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
        
            # Save the thumbnail
            self.thumbnail.save(self.image.path, ContentFile(''))
            image.save(self.thumbnail.path)
        
        # Save this photo instance
        super(Item, self).save()
