mirror of
https://github.com/Tygs/0bin.git
synced 2023-08-10 21:13:00 +03:00
Prevent 0bin to crash if the locale formatting is not supported. Moved get_pastes_count into the Paste class. Added 0bin version as query parameter in the scrip and css tags URL
This commit is contained in:
parent
780c50f971
commit
6860778f47
@ -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.
|
||||
|
@ -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',
|
||||
|
@ -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
|
||||
|
@ -12,11 +12,13 @@
|
||||
<link rel="shortcut icon" href="/favicon.ico">
|
||||
|
||||
%if settings.COMPRESSED_STATIC_FILES:
|
||||
<link href="/static/css/style.min.css" rel="stylesheet" />
|
||||
<link href="/static/css/style.min.css?{{ settings.VERSION }}"
|
||||
rel="stylesheet" />
|
||||
%else:
|
||||
<link href="/static/css/prettify.css" rel="stylesheet" />
|
||||
<link href="/static/css/bootstrap.css" rel="stylesheet">
|
||||
<link href="/static/css/style.css" rel="stylesheet">
|
||||
<link href="/static/css/style.css?{{ settings.VERSION }}"
|
||||
rel="stylesheet">
|
||||
%end
|
||||
|
||||
<!-- Le HTML5 shim, for IE7-8 support of HTML5 elements -->
|
||||
@ -25,11 +27,11 @@
|
||||
<![endif]-->
|
||||
|
||||
%if settings.COMPRESSED_STATIC_FILES:
|
||||
<script src="/static/js/main.min.js"></script>
|
||||
<script src="/static/js/main.min.js?{{ settings.VERSION }}"></script>
|
||||
%else:
|
||||
<script src="/static/js/jquery-1.7.2.min.js"></script>
|
||||
<script src="/static/js/sjcl.js"></script>
|
||||
<script src="/static/js/behavior.js"></script>
|
||||
<script src="/static/js/behavior.js?{{ settings.VERSION }}"></script>
|
||||
%end
|
||||
|
||||
<script type="text/javascript">
|
||||
@ -126,14 +128,14 @@
|
||||
<small>Edgar Allan Poe</small>
|
||||
</blockquote>
|
||||
|
||||
|
||||
|
||||
%if settings.DISPLAY_COUNTER:
|
||||
<h4 id="pixels-total" >
|
||||
<p>ø</p>
|
||||
<strong>{{ pastes_count }}</strong> </br>pastes øbinned
|
||||
</h4>
|
||||
%end
|
||||
|
||||
|
||||
|
||||
</br>
|
||||
<p class="greetings span12">
|
||||
@ -144,7 +146,7 @@
|
||||
</footer>
|
||||
|
||||
%if settings.COMPRESSED_STATIC_FILES:
|
||||
<script src="/static/js/additional.min.js"></script>
|
||||
<script src="/static/js/additional.min.js?{{ settings.VERSION }}"></script>
|
||||
%else:
|
||||
<script src="/static/js/jquery.elastic.source.js"></script>
|
||||
<script src="/static/js/lzw.js"></script>
|
||||
|
Loading…
Reference in New Issue
Block a user