diff --git a/zerobin/paste.py b/zerobin/paste.py index 53d26b4..f2de129 100644 --- a/zerobin/paste.py +++ b/zerobin/paste.py @@ -2,6 +2,7 @@ import os import hashlib +import locale from datetime import datetime, timedelta @@ -112,10 +113,10 @@ class Paste(object): return cls.load_from_file(cls.get_path(uuid)) - + def increment_counter(self): """ - Increment pastes counter + Increment pastes counter """ # simple counter incrementation @@ -131,7 +132,7 @@ class Paste(object): try: #make lock file flock = open(lock_file, "w") - flock.write('lock') + flock.write('lock') flock.close() # init counter (first time) @@ -205,6 +206,27 @@ class Paste(object): return self + @classmethod + def get_pastes_count(cls): + """ + Return the number of created pastes. + (must have option DISPLAY_COUNTER enabled for the pastes to be + be counted) + """ + try: + locale.setlocale(locale.LC_ALL, 'en_US') + except: + pass + counter_file = os.path.join(settings.PASTE_FILES_ROOT, 'counter') + try: + count = long(open(counter_file).read(50)) + except (IOError, OSError): + count = 0 + + return locale.format("%d", long(count), grouping=True) + + + def delete(self): """ Delete the paste file. diff --git a/zerobin/routes.py b/zerobin/routes.py index ee911b4..d166e77 100644 --- a/zerobin/routes.py +++ b/zerobin/routes.py @@ -23,16 +23,16 @@ from bottle import (Bottle, run, static_file, view, request) import clize from paste import Paste -from utils import drop_privileges, dmerge, get_pastes_count +from utils import drop_privileges, dmerge app = Bottle() GLOBAL_CONTEXT = { 'settings': settings, - 'pastes_count': get_pastes_count(), + 'pastes_count': Paste.get_pastes_count(), 'refresh_counter': datetime.now() } - + @app.route('/') @view('home') @@ -66,17 +66,20 @@ def create_paste(): expiration = request.forms.get('expiration', u'burn_after_reading') paste = Paste(expiration=expiration, content=content) paste.save() - + # display counter if settings.DISPLAY_COUNTER: #increment paste counter paste.increment_counter() - + # if refresh time elapsed pick up new counter value - if GLOBAL_CONTEXT['refresh_counter'] + timedelta(seconds=settings.REFRESH_COUNTER) < datetime.now(): - GLOBAL_CONTEXT['pastes_count'] = get_pastes_count() - GLOBAL_CONTEXT['refresh_counter'] = datetime.now() + now = datetime.now() + timeout = (GLOBAL_CONTEXT['refresh_counter'] + + timedelta(seconds=settings.REFRESH_COUNTER)) + if timeout < now: + GLOBAL_CONTEXT['pastes_count'] = Paste.get_pastes_count() + GLOBAL_CONTEXT['refresh_counter'] = now return {'status': 'ok', diff --git a/zerobin/utils.py b/zerobin/utils.py index 40b59de..8932ad7 100644 --- a/zerobin/utils.py +++ b/zerobin/utils.py @@ -5,7 +5,6 @@ import os import glob import tempfile import sys -import locale import default_settings sys.path.append(default_settings.LIBS_DIR) @@ -54,23 +53,6 @@ def dmerge(*args): return dictionary -def get_pastes_count(): - """ - Return the number of pastes created (must have option DISPLAY_COUNTER enabled) - """ - locale.setlocale(locale.LC_ALL, 'en_US') - counter_path = settings.PASTE_FILES_ROOT - counter_file = os.path.join(counter_path, 'counter') - try: - with open(counter_file, "r") as f: - count = f.read(50) - f.close - except IOError: - count = 0 - - return locale.format("%d", long(float(count)), grouping=True) - - class SettingsContainer(object): """ Singleton containing the settings for the whole app diff --git a/zerobin/views/base.tpl b/zerobin/views/base.tpl index e257789..c8b01d3 100644 --- a/zerobin/views/base.tpl +++ b/zerobin/views/base.tpl @@ -12,11 +12,13 @@ %if settings.COMPRESSED_STATIC_FILES: - + %else: - + %end @@ -25,11 +27,11 @@ %if settings.COMPRESSED_STATIC_FILES: - + %else: - + %end + %else: