# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import random
import string
from functools import wraps

from suit_dashboard.views import RefreshableDataView


# https://stackoverflow.com/questions/653368/
def double_wrap(f):
    @wraps(f)
    def new_dec(*args, **kwargs):
        if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
            return f(args[0])
        else:
            return lambda real_f: f(real_f, *args, **kwargs)

    return new_dec


@double_wrap
def refreshable(func, name=None, regex=None, refresh_time=5000):
    if name is None:
        name = func.__name__

    if name in [c.name for c in RefreshableDataView.children]:
        raise ValueError('Name %s is already used by another '
                         'RefreshableDataView subclass.' % name)

    if regex is None:
        while True:
            regex = 'refreshable/' + ''.join(random.SystemRandom().choice(
                string.ascii_lowercase + string.digits) for _ in range(32))
            if regex not in [c.regex for c in RefreshableDataView.children]:
                break

    def inner_function(*args, **kwargs):
        class InnerClass(RefreshableDataView):
            def get_data(self):
                return func(*args, **kwargs)

        InnerClass.name = name
        InnerClass.regex = regex
        InnerClass.refresh_time = refresh_time

        RefreshableDataView.children.append(InnerClass)

        return InnerClass
    return inner_function

@@ -114,16 +114,43 @@
 rt wraps
+%0Afrom hashlib import sha256
 %0A%0Afrom s
@@ -844,32 +844,141 @@
  regex is None:%0A
+        regex = sha256(str(id(func)))  # Would just id be sufficient?%0A        regex = regex.hexdigest()%5B:32%5D%0A
         while Tr
@@ -988,24 +988,26 @@
 %0A           
+ #
  regex = 're
@@ -1065,24 +1065,26 @@
 %0A           
+ #
      string.
@@ -1136,16 +1136,59 @@
 ge(32))%0A
+            regex = 'refreshable/' + regex%0A
         

Fix (try to) refreshable url changing over time