Skip to content
Permalink
Browse files

early implementation of #26 (redis-based user storage)

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jan 11, 2015
1 parent 8d3117b commit f3e8da4104fd4ab3b7b3b8d77fb57419bd125529
Showing with 59 additions and 43 deletions.
  1. +30 −42 comet/web.py
  2. +29 −1 requirements.txt
@@ -32,6 +32,8 @@
import pkg_resources
import nikola.__main__
import logbook
import kombu
import redis
from nikola.utils import unicode_str, get_logger, ColorfulStderrHandler
import nikola.plugins.command.new_post
from flask import Flask, request, redirect, send_from_directory, g, session
@@ -42,6 +44,7 @@

site = None
app = None
db = None


def scan_site():
@@ -59,18 +62,24 @@ def configure_url(url):

def configure_site():
"""Configure the site for Comet."""
global site
global site, db

nikola.__main__._RETURN_DOITNIKOLA = True
_dn = nikola.__main__.main([])
_dn.sub_cmds = _dn.get_commands()
site = _dn.nikola
app.config['BCRYPT_LOG_ROUNDS'] = 12
app.config['NIKOLA_ROOT'] = os.getcwd()
app.config['DEBUG'] = False
# TODO REMOVE THE TWO SETTINGS
# TODO REMOVE read_users write_users
# TODO DOCUMENT REDIS
app.config['USERS'] = {}
app.config['USERS_PATH'] = os.path.join(app.config['NIKOLA_ROOT'],
'comet_users.json')

# Logging configuration

logf = (u'[{record.time:%Y-%m-%dT%H:%M:%SZ}] {record.level_name}: '
u'{record.channel}: {record.message}')
logh = (u'[{record.time:%Y-%m-%dT%H:%M:%SZ}] {record.channel} '
@@ -108,8 +117,14 @@ def configure_site():

app.secret_key = site.config.get('COMET_SECRET_KEY')
app.config['COMET_URL'] = site.config.get('COMET_URL')

read_users()
app.config['REDIS_URL'] = site.config.get('COMET_REDIS_URL', 'redis://')
# Redis configuration
redis_raw = kombu.parse_url(app.config['REDIS_URL'])
redis_conn = {'host': redis_raw['hostname'] or 'localhost',
'port': redis_raw['port'] or 6379,
'db': int(redis_raw['virtual_host'] or 0),
'password': redis_raw['password']}
db = redis.StrictRedis(**redis_conn)

site.template_hooks['menu_alt'].append(generate_menu_alt)

@@ -277,8 +292,6 @@ def find_post(path):


app = Flask('comet')
app.config['BCRYPT_LOG_ROUNDS'] = 12


@app.after_request
def log_request(resp):
@@ -358,7 +371,13 @@ def get_user(uid):
:raises ValueError: uid is not an integer
:raises KeyError: if user does not exist
"""
return app.config['USERS'][int(uid)]
d = db.hgetall('user:{0}'.format(uid))
if d:
for p in PERMISSIONS:
d[p] = d[p] == '1'
return User(uid=uid, **d)
else:
return None


def find_user_by_name(username):
@@ -368,42 +387,11 @@ def find_user_by_name(username):
:return: the user
:rtype: User object or None
"""
for uid, u in app.config['USERS'].items():
if u.username == username:
return u
break


def read_users():
"""Read user data from the JSON file."""
app.config['USERS'] = {}

if not os.path.exists(app.config['USERS_PATH']):
app.logger.error("Cannot find comet_users.json.")
return

with io.open(app.config['USERS_PATH'], 'r', encoding='utf-8') as fh:
udict = json.load(fh)
for uid, data in udict.items():
uid = int(uid)
app.config['USERS'][uid] = User(uid, **data)


def write_users():
"""Write user data to the JSON file."""
udict = {}
for uid, user in app.config['USERS'].items():
uid = unicode_str(uid)
udict[uid] = {
'username': user.username,
'realname': user.realname,
'password': user.password,
'active': user.active,
}
for p in PERMISSIONS:
udict[uid][p] = getattr(user, p)
with open(app.config['USERS_PATH'], 'w') as fh:
json.dump(udict, fh, indent=4, sort_keys=True, separators=(',', ': '))
uid = db.hget('users', username)
if uid:
return get_user(uid)
else:
return None


@app.route('/login', methods=['GET', 'POST'])
@@ -1,6 +1,34 @@
git+https://github.com/getnikola/nikola#egg=Nikola
redis==2.10.3
amqp==1.4.6
anyjson==0.3.3
billiard==3.3.0.19
blinker==1.3
celery==3.1.17
docopt==0.6.1
docutils==0.12
doit==0.26.0
Flask==0.10.1
Flask-Login==0.2.11
Flask-Bcrypt==0.6.0
Flask-Login==0.2.11
itsdangerous==0.24
Jinja2==2.7.3
kombu==3.0.24
Logbook==0.8.1
lxml==3.4.1
Mako==1.0.0
MarkupSafe==0.23
natsort==3.5.1
Pillow==2.7.0
py-bcrypt==0.4
Pygments==2.0.1
pyinotify==0.9.5
PyRSS2Gen==1.1
python-dateutil==2.4.0
pytz==2014.10
redis==2.10.3
six==1.9.0
Unidecode==0.4.17
webassets==0.10.1
Werkzeug==0.9.6
Yapsy==1.10.423

0 comments on commit f3e8da4

Please sign in to comment.
You can’t perform that action at this time.