Просмотр исходного кода

Make helper-methods py3 compatible

Casper Van Gheluwe 10 лет назад
Родитель
Сommit
e6dbc5834a
3 измененных файлов с 18 добавлено и 10 удалено
  1. 2 0
      flaskbb/_compat.py
  2. 9 8
      flaskbb/utils/helpers.py
  3. 7 2
      flaskbb/utils/populate.py

+ 2 - 0
flaskbb/_compat.py

@@ -12,6 +12,7 @@ if not PY2:     # pragma: no cover
     string_types = (str,)
     integer_types = (int, )
     intern_method = sys.intern
+    range_method = range
     iterkeys = lambda d: iter(d.keys())
     itervalues = lambda d: iter(d.values())
     iteritems = lambda d: iter(d.items())
@@ -21,6 +22,7 @@ else:           # pragma: no cover
     string_types = (str, unicode)
     integer_types = (int, long)
     intern_method = intern
+    range_method = xrange
     iterkeys = lambda d: d.iterkeys()
     itervalues = lambda d: d.itervalues()
     iteritems = lambda d: d.iteritems()

+ 9 - 8
flaskbb/utils/helpers.py

@@ -20,8 +20,9 @@ from flask.ext.themes2 import render_theme_template
 from flask.ext.login import current_user
 
 from postmarkup import render_bbcode
+from flaskbb._compat import range_method
 
-from flaskbb.extensions import redis
+from flaskbb.extensions import redis_store
 from flaskbb.utils.settings import flaskbb_config
 
 _punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
@@ -39,7 +40,7 @@ def slugify(text, delim=u'-'):
         word = normalize('NFKD', word).encode('ascii', 'ignore')
         if word:
             result.append(word)
-    return unicode(delim.join(result))
+    return str(delim.join(str(result)))
 
 
 def render_template(template, **context):
@@ -222,7 +223,7 @@ def mark_online(user_id, guest=False):
     else:
         all_users_key = 'online-users/%d' % (now // 60)
         user_key = 'user-activity/%s' % user_id
-    p = redis.pipeline()
+    p = redis_store.pipeline()
     p.sadd(all_users_key, user_id)
     p.set(user_key, now)
     p.expireat(all_users_key, expires)
@@ -238,9 +239,9 @@ def get_last_user_activity(user_id, guest=False):
     :param guest: If the user is a guest (not signed in)
     """
     if guest:
-        last_active = redis.get('guest-activity/%s' % user_id)
+        last_active = redis_store.get('guest-activity/%s' % user_id)
     else:
-        last_active = redis.get('user-activity/%s' % user_id)
+        last_active = redis_store.get('user-activity/%s' % user_id)
 
     if last_active is None:
         return None
@@ -253,11 +254,11 @@ def get_online_users(guest=False):
     :param guest: If True, it will return the online guests
     """
     current = int(time.time()) // 60
-    minutes = xrange(flaskbb_config['ONLINE_LAST_MINUTES'])
+    minutes = range_method(flaskbb_config['ONLINE_LAST_MINUTES'])
     if guest:
-        return redis.sunion(['online-guests/%d' % (current - x)
+        return redis_store.sunion(['online-guests/%d' % (current - x)
                              for x in minutes])
-    return redis.sunion(['online-users/%d' % (current - x)
+    return redis_store.sunion(['online-users/%d' % (current - x)
                          for x in minutes])
 
 

+ 7 - 2
flaskbb/utils/populate.py

@@ -76,8 +76,13 @@ def create_admin_user(username, password, email):
     Creates the administrator user
     """
     admin_group = Group.query.filter_by(admin=True).first()
-    user = User(username=username, password=password, email=email,
-                date_joined=datetime.utcnow(), primary_group_id=admin_group.id)
+    user = User()
+
+    user.username = username
+    user.password = password
+    user.email = email
+    user.primary_group_id = admin_group.id
+
     user.save()