Commits

b7w committed 7fa8814

Remove RaisingRange class. Improve AdminGalleryForm.

Comments (0)

Files changed (3)

bviewer/core/models.py

 
 class Gallery(models.Model):
     CACHE_SIZE_MIN = 16
-    CACHE_SIZE_MAX = 256
+    CACHE_SIZE_MAX = 512
     CACHE_ARCHIVE_SIZE_MIN = 128
     CACHE_ARCHIVE_SIZE_MAX = 2048
 

bviewer/core/utils.py

 # -*- coding: utf-8 -*-
+import time
+import logging
+
 import django_rq
 import mockredis
-import time
-import logging
 from django.conf import settings
 from django.utils.encoding import smart_text, smart_bytes
 from django.utils.functional import wraps
 logger = logging.getLogger(__name__)
 
 
-class RaisingRange(object):
-    """
-    Iterator range that double sum base value if item/base == 8
-
-        >>> list(RaisingRange(32, start=0, base=1))
-        [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32]
-    """
-
-    def __init__(self, maximum, start=None, base=None):
-        """
-        Max value, start from or 0, base or 1
-
-        :type maximum: int
-        :type start: int
-        :type base: int
-        """
-        self.max = maximum
-        self.start = start or 0
-        self.base = base or 1
-        self.value = self.start
-
-    def __iter__(self):
-        return self
-
-    def next(self):
-        if self.value <= self.max:
-            tmp = self.value
-            if (self.value - self.start) / self.base == 8:
-                self.base *= 2
-            self.value += self.base
-            return tmp
-        else:
-            raise StopIteration
-
-    def __str__(self):
-        return str(list(self))
-
-
 class ImageOptions(object):
     """
     Options for resize such as width, height,

bviewer/profile/forms.py

 from django.forms import Form, ModelForm, ChoiceField, CharField, MultipleHiddenInput
 
 from bviewer.core.models import Gallery, Album, Video
-from bviewer.core.utils import RaisingRange
 
 
 class BulkTimeUpdateForm(Form):
 
     def __init__(self, *args, **kwargs):
         super(AdminGalleryForm, self).__init__(*args, **kwargs)
-        self._set_choice('cache_size',
-                         cache_max=Gallery.CACHE_SIZE_MAX,
-                         cache_min=Gallery.CACHE_SIZE_MIN,
-                         base=16
-        )
-        self._set_choice('cache_archive_size',
-                         cache_max=Gallery.CACHE_ARCHIVE_SIZE_MAX,
-                         cache_min=Gallery.CACHE_ARCHIVE_SIZE_MIN,
-                         base=64
-        )
+        self._set_choice('cache_size', Gallery.CACHE_SIZE_MAX)
+        self._set_choice('cache_archive_size', Gallery.CACHE_ARCHIVE_SIZE_MAX)
 
-    def _set_choice(self, field_name, cache_max, cache_min, base):
-        raising = RaisingRange(cache_max, start=cache_min, base=base)
-        choice = [(i, '%s MB' % i) for i in raising]
-        self.fields[field_name] = ChoiceField(choices=choice)
+    def _set_choice(self, field_name, cache_max):
+        base = cache_max // 8
+        raising = (i * base for i in range(1, 9))
+        choice = set([(i, '{0} MB'.format(i)) for i in raising])
+        if self.instance:
+            attr = getattr(self.instance, field_name)
+            choice.add((attr, '{0} MB'.format(attr)))
+        self.fields[field_name] = ChoiceField(choices=sorted(choice))
 
     class Meta(object):
         model = Gallery