from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFit

class Manufacturer(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name


class ModuleImageURLField(object):
    name = 'full_image_url'
    serialize = True
    rel = None

    def _get_val_from_obj(self, obj):
        try:
            return obj.image.url
        except ValueError:
            return ''

    def value_to_string(self, obj):
        try:
            return obj.image.url
        except ValueError:
            return ''


class Module(models.Model):
    name = models.CharField(max_length=200)
    manufacturer = models.ForeignKey(Manufacturer, blank=True, null=True)
    image = models.ImageField(blank=True,null=True,upload_to="modules/images/")
    medium_image = ImageSpecField([ResizeToFit(width=800, height=325),], image_field='image', format='JPEG', options={'quality': 90})
    hp = models.IntegerField("HP", blank=True, null=True)
    depth = models.IntegerField(blank=True, null=True)
    current_12v = models.IntegerField("+12v (mA)", blank=True, null=True)
    negative_current_12v = models.IntegerField("-12v (mA)", blank=True, null=True)
    current_5v = models.IntegerField("5v (mA)", blank=True, null=True)
    msrp = models.DecimalField("MSRP ($)", max_digits=8, decimal_places=2, blank=True, null=True)
    url = models.CharField(blank=True, max_length=200)
    user = models.ForeignKey(User, blank=True, null=True)
    custom = models.BooleanField(blank=True)
    eurorackdb_id = models.IntegerField(blank=True, null=True, db_index=True)
    eurorackdb_image = models.CharField(max_length=200, blank=True)

    def __unicode__(self):
        return self.name

    def autocomplete_name(self):
        return '%s %s' % (self.manufacturer.name, self.name)

    def get_display_image_url(self):
        if self.eurorackdb_image:
            return 'http://eurorackdb.com/assets/%s' % self.eurorackdb_image
        elif self.image:
            return self.medium_image.url
        else:
            return ''

    @models.permalink
    def get_absolute_url(self):
        return ('module', [str(self.id)])

class Setup(models.Model):
    name = models.CharField(max_length=200)
    user = models.ForeignKey(User)
    preset = models.TextField()

    def __unicode__(self):
        return self.name

@@ -1,20 +1,45 @@
+import os%0Aimport urllib%0A%0A
 from django.db impor
@@ -124,16 +124,79 @@
 rt User%0A
+from django.core.files import File  # you need this somewhere%0A%0A
 from ima
@@ -2043,25 +2043,29 @@
 get_
-display
+eurorackdb
 _image_
-url
+path
 (sel
@@ -2178,16 +2178,24 @@
 db_image
+        
 %0A       
@@ -2201,99 +2201,567 @@
   el
-if self.image:%0A            return self.medium_image.url%0A        else:%0A            return ''
+se:%0A            return None%0A%0A    def get_display_image_url(self):%0A        if self.image:%0A            return self.medium_image.url%0A        elif self.eurorackdb_image:%0A            return self.get_eurorackdb_image_path()%0A        else:%0A            return ''%0A%0A    def save_eurorackdb_image(self):%0A        result = urllib.urlretrieve(self.get_eurorackdb_image_path()) %0A        if result:%0A            self.image.save(%0A                os.path.basename(self.get_eurorackdb_image_path()),%0A                File(open(result%5B0%5D))%0A                )%0A%0A            self.save()
 %0A%0A  

use our own s3