1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

counter first version

This commit is contained in:
max 2012-05-21 22:14:01 +07:00
parent 2fb38d0c00
commit 26fd2d0913
10 changed files with 76 additions and 13 deletions

View File

@ -53,6 +53,10 @@ PORT = "8000"
USER = None USER = None
GROUP = None GROUP = None
# Display a tiny counter for pastes created
# Be carreful if your site have to many pastes this can hurt your hard drive performances.
DISPLAY_COUNTER = True
# Names/links to insert in the menu bar. # Names/links to insert in the menu bar.
# Any link with "mailto:" will be escaped to prevent spam # Any link with "mailto:" will be escaped to prevent spam
MENU = ( MENU = (

View File

@ -1,8 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import fcntl
import sys
import hashlib import hashlib
from datetime import datetime, timedelta from datetime import datetime, timedelta
from utils import settings from utils import settings
@ -112,6 +115,36 @@ class Paste(object):
return cls.load_from_file(cls.get_path(uuid)) return cls.load_from_file(cls.get_path(uuid))
def increment_counter(self):
"""
Increment pastes counter
"""
# simple counter incrementation
# using lock file to prevent multi access to the file
# could be improved.
path = settings.PASTE_FILES_ROOT
counter_file = os.path.join(path, 'counter')
fd = os.open(counter_file, os.O_RDWR | os.O_CREAT)
fcntl.lockf(fd, fcntl.LOCK_EX)
s = os.read(fd, 4096)
try:
n = long(float(s))
except ValueError:
raise ValueError(u"Couldn't read value from counter file " + counter_file + ", assuming 0")
n = 0
fnn = counter_file + ".new"
f = open(fnn, "w")
f.write(str(n + 1))
f.close()
os.rename(fnn, counter_file)
os.close(fd)
def save(self): def save(self):
""" """
Save the content of this paste to a file. Save the content of this paste to a file.

View File

@ -23,12 +23,13 @@ from bottle import (Bottle, run, static_file, view, request)
import clize import clize
from paste import Paste from paste import Paste
from utils import drop_privileges, dmerge from utils import drop_privileges, dmerge, get_pastes_count
app = Bottle() app = Bottle()
GLOBAL_CONTEXT = { GLOBAL_CONTEXT = {
'settings': settings 'settings': settings,
'pastes_count': get_pastes_count()
} }
@ -40,7 +41,7 @@ def index():
@app.route('/faq/') @app.route('/faq/')
@view('faq') @view('faq')
def index(): def faq():
return GLOBAL_CONTEXT return GLOBAL_CONTEXT
@ -64,6 +65,10 @@ def create_paste():
expiration = request.forms.get('expiration', u'burn_after_reading') expiration = request.forms.get('expiration', u'burn_after_reading')
paste = Paste(expiration=expiration, content=content) paste = Paste(expiration=expiration, content=content)
paste.save() paste.save()
if settings.DISPLAY_COUNTER:
paste.increment_counter()
return {'status': 'ok', return {'status': 'ok',
'paste': paste.uuid} 'paste': paste.uuid}

View File

@ -31,6 +31,10 @@
margin-bottom: 0px !important; margin-bottom: 0px !important;
} }
.center {
text-align: center;
}
.about span{ .about span{
font-size: 10px; font-size: 10px;
} }

View File

@ -5,6 +5,7 @@ import os
import glob import glob
import tempfile import tempfile
import sys import sys
import locale
import default_settings import default_settings
sys.path.append(default_settings.LIBS_DIR) sys.path.append(default_settings.LIBS_DIR)
@ -53,6 +54,21 @@ def dmerge(*args):
return dictionary 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 = default_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): class SettingsContainer(object):

View File

@ -33,4 +33,4 @@
</form> </form>
%rebase base settings=settings %rebase base settings=settings, pastes_count=pastes_count

View File

@ -126,12 +126,13 @@
<small>Edgar Allan Poe</small> <small>Edgar Allan Poe</small>
</blockquote> </blockquote>
<!-- %if settings.DISPLAY_COUNTER:
<h4 id="pixels-total" > <h4 id="pixels-total" >
<p>ø</p> <p>ø</p>
<strong>41,017,923,819</strong> pastes øbinned <strong>{{ pastes_count }}</strong> </br>pastes øbinned
</h4> </h4>
--> %end
</br> </br>
<p class="greetings span12"> <p class="greetings span12">

View File

@ -32,4 +32,4 @@
%rebase base settings=settings %rebase base settings=settings, pastes_count=pastes_count

View File

@ -25,4 +25,4 @@
</form> </form>
%rebase base settings=settings %rebase base settings=settings, pastes_count=pastes_count

View File

@ -78,4 +78,4 @@
</div> </div>
%rebase base settings=settings %rebase base settings=settings, pastes_count=pastes_count