from redis import Redis
from django.core.exceptions import ObjectDoesNotExist

from thumbnails.models import Source, ThumbnailMeta


class ImageMeta:

    def __init__(self, source_name, name, size):
        self.source_name = source_name
        self.name = name
        self.size = size

    def __eq__(self, other):
        return self.__dict__ == other.__dict__


class BaseBackend:

    def add_source(name):
        raise NotImplementedError

    def delete_source(name):
        raise NotImplementedError

    def get_thumbnails(name):
        raise NotImplementedError

    def get_thumbnail(name, size):
        raise NotImplementedError

    def add_thumbnail(name, size, filename):
        raise NotImplementedError

    def delete_thumbnail(name, size):
        raise NotImplementedError


class DatabaseBackend(BaseBackend):

    def add_source(self, name):
        return Source.objects.create(name=name)

    def get_source(self, name):
        return Source.objects.get(name=name)

    def delete_source(self, name):
        return Source.objects.filter(name=name).delete()

    def get_thumbnails(self, name):
        metas = ThumbnailMeta.objects.filter(source__name=name)
        return [ImageMeta(name, meta.name, meta.size) for meta in metas]

    def get_thumbnail(self, source_name, size):
        try:
            meta = ThumbnailMeta.objects.get(source__name=source_name, size=size)
            return ImageMeta(source_name, meta.name, meta.size)
        except ThumbnailMeta.DoesNotExist:
            return None

    def add_thumbnail(self, source_name, size, name):
        source = self.get_source(source_name)
        return ThumbnailMeta.objects.create(source=source, size=size, name=name)

    def delete_thumbnail(self, source_name, size):
        ThumbnailMeta.objects.filter(source__name=source_name, size=size).delete()


class RedisBackend(BaseBackend):
    redis = Redis()

    def add_source(self, name):
        self.redis.hset('sources', name, name)
        return name

    def get_source(self, name):
        return self.redis.hget('sources', name)

    def delete_source(self, name):
        return self.redis.hdel('sources', name)

    def get_thumbnails(self, name):
        key = "%s:thumbnails" % name
        metas = self.redis.hgetall(key)
        return [ImageMeta(name, meta[1], meta[0]) for meta in metas.iteritems()]

    def get_thumbnail(self, source_name, size):
        key = "%s:thumbnails" % source_name
        name = self.redis.hget(key, size)
        if name:
            return ImageMeta(source_name, name, size)
        return None

    def add_thumbnail(self, source_name, size, name):
        if self.redis.hexists('sources', source_name):
            key = "%s:thumbnails" % source_name
            self.redis.hset(key, size, name)
            return ImageMeta(source_name, name, size)
        raise ObjectDoesNotExist

    def delete_thumbnail(self, source_name, size):
        key = "%s:thumbnails" % source_name
        self.redis.hdel(key, size)

@@ -20,62 +20,8 @@
 edis
-%0Afrom django.core.exceptions import ObjectDoesNotExist
 %0A%0Afr
@@ -1847,24 +1847,180 @@
  = Redis()%0A%0A
+    def get_source_key(name):%0A        return %22djthumbs:sources:%25s%22 %25 name%0A%0A    def get_thumbnail_key(name):%0A        return %22djthumbs:thumbnails:%25s%22 %25 name%0A%0A
     def add_
@@ -2063,25 +2063,36 @@
 is.hset(
-'
+get_
 source
-s'
+_key(name)
 , name, 
@@ -2181,25 +2181,36 @@
 is.hget(
-'
+get_
 source
-s'
+_key(name)
 , name)%0A
@@ -2276,25 +2276,36 @@
 is.hdel(
-'
+get_
 source
-s'
+_key(name)
 , name)%0A
@@ -2347,36 +2347,36 @@
 :%0A        key = 
-%22%25s:
+get_
 thumbnails%22 %25 na
@@ -2368,25 +2368,26 @@
 humbnail
-s%22 %25 
+_key(
 name
+)
 %0A       
@@ -2455,34 +2455,54 @@
 me, 
-meta%5B1%5D, meta%5B0%5D) for meta
+thumbnail_name, size) for size, thumbnail_name
  in 
@@ -2581,78 +2581,54 @@
     
-key = %22%25s:thumbnails%22 %25 source_name%0A        name = self.redis.hget(key
+name = self.redis.hget(get_thumbnail_key(name)
 , si
@@ -2785,19 +2785,16 @@
         
-if 
 self.red
@@ -2801,119 +2801,35 @@
 is.h
-exists('sources', source_name):%0A            key = %22%25s:thumbnails%22 %25 source_name%0A            self.redis.hset(key
+set(get_thumbnail_key(name)
 , si
@@ -2838,36 +2838,32 @@
 , name)%0A        
- 
-   
 return ImageMeta
@@ -2891,41 +2891,8 @@
 ize)
-%0A        raise ObjectDoesNotExist
 %0A%0A  
@@ -2952,71 +2952,47 @@
     
-key = %22%25s:thumbnails%22 %25 source_name%0A        self.redis.hdel(key
+self.redis.hdel(get_thumbnail_key(name)
 , si

add 'djthumbs' prefix, define a method to generate source key, remove source checking on add thumbnail