Skip to content

Commit

Permalink
finalize user accounts in Redis.
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jan 11, 2015
1 parent f3e8da4 commit f970ac6
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 65 deletions.
2 changes: 2 additions & 0 deletions comet/__init__.py
Expand Up @@ -27,4 +27,6 @@

from __future__ import unicode_literals

__all__ = ['__version__']

__version__ = '0.6.0'
4 changes: 3 additions & 1 deletion comet/__main__.py
Expand Up @@ -43,6 +43,7 @@
"""

import comet
import comet.utils
import sys
import docopt
import webbrowser
Expand All @@ -69,7 +70,8 @@ def init(arguments):

def write_users(arguments):
import comet.init
return comet.init.write_users()
u = comet.utils.ask("Redis URL", "redis://")
return comet.init.write_users(u)


def devserver(arguments):
Expand Down
2 changes: 1 addition & 1 deletion comet/data/templates/jinja/comet_index.tmpl
Expand Up @@ -86,7 +86,7 @@ Show posts of:
<h4 class="modal-title" id="deleteModalLabel">Confirm deletion</h4>
</div>
<div class="modal-body">
Really delete <span class="del-title">post</span>?
Really delete <strong class="del-title">post</strong>?
</div>
<div class="modal-footer">
<form method="POST" action="/delete" class="delete-button">
Expand Down
2 changes: 1 addition & 1 deletion comet/data/templates/jinja/comet_post_edit.tmpl
Expand Up @@ -19,7 +19,7 @@
disabled
{% endif %}
>
{% for auid, aname in USERS %}
{% for auid, aname in users %}
<option
{% if auid == current_auid %}
selected
Expand Down
6 changes: 4 additions & 2 deletions comet/data/templates/jinja/comet_users.tmpl
Expand Up @@ -19,10 +19,12 @@ $('#deleteModal').on('show.bs.modal', function (event) {
modal.find('.del-noun').text('deletion');
modal.find('.del-verb').text('delete');
modal.find('.del-verb-capital').text('Delete');
modal.find('.del-verb-capital').addClass('btn-danger');
} else {
modal.find('.del-noun').text('undeletion');
modal.find('.del-verb').text('undelete');
modal.find('.del-verb-capital').text('Undelete');
modal.find('.del-verb-capital').addClass('btn-success');
}
});
</script>
Expand Down Expand Up @@ -97,14 +99,14 @@ $('#deleteModal').on('show.bs.modal', function (event) {
<h4 class="modal-title" id="deleteModalLabel">Confirm <span class="del-noun">something</span></h4>
</div>
<div class="modal-body">
Really <span class="del-verb">do something to</span> <span class="del-username">user</span>?
Really <span class="del-verb">do something to</span> <strong class="del-username">user</strong>?
</div>
<div class="modal-footer">
<form method="POST" action="/users/delete" class="delete-button">
<input type="hidden" name="uid" class="del-uid">
<input type="hidden" name="direction" class="del-direction">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger del-verb-capital">Delete</button>
<button type="submit" class="btn del-verb-capital">Delete</button>
</div>
</div>
</div>
Expand Down
14 changes: 12 additions & 2 deletions comet/data/templates/jinja/comet_users_permissions.tmpl
Expand Up @@ -55,6 +55,10 @@ $(document).ready(function() {
<thead><tr>
<th class="uid">#</th>
<th class="username">Username</th>
<th class="perm is_active">Active<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="is_active"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="is_active"><i class="fa fa-square-o fa-fw"></i></button>
</th>
<th class="perm is_admin">Admin<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="is_admin"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="is_admin"><i class="fa fa-square-o fa-fw"></i></button>
Expand Down Expand Up @@ -82,12 +86,18 @@ $(document).ready(function() {
<th class="select_all">Select all</th>
</tr></thead>
{% for uid, user in USERS.items() %}
{% if user.active %}
{% if user.is_active %}
<tr class="u{{ uid }}">
<td class="uid">{{ uid }}</td>
<td class="username">{{ user.username }}</td>
{% for p in PERMISSIONS %}
<td class="perm {{ p }}">{{ display_permission(user, p) }}</td>
<td class="perm
{% if p == 'active' %}
is_active
{% else %}
{{ p }}
{% endif %}
">{{ display_permission(user, p) }}</td>
{% endfor %}
<td class="select_all"><button type="button" class="btn btn-info select_all-user" data-uid="{{ uid }}"><i class="fa fa-check-square-o fa-fw"></i></button> <button type="button" class="btn btn-info select_none-user" data-uid="{{ uid }}"><i class="fa fa-square-o fa-fw"></i></button></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion comet/data/templates/mako/comet_index.tmpl
Expand Up @@ -86,7 +86,7 @@ Show posts of:
<h4 class="modal-title" id="deleteModalLabel">Confirm deletion</h4>
</div>
<div class="modal-body">
Really delete <span class="del-title">post</span>?
Really delete <strong class="del-title">post</strong>?
</div>
<div class="modal-footer">
<form method="POST" action="/delete" class="delete-button">
Expand Down
2 changes: 1 addition & 1 deletion comet/data/templates/mako/comet_post_edit.tmpl
Expand Up @@ -19,7 +19,7 @@
disabled
% endif
>
% for auid, aname in USERS:
% for auid, aname in users:
<option
%if auid == current_auid:
selected
Expand Down
6 changes: 4 additions & 2 deletions comet/data/templates/mako/comet_users.tmpl
Expand Up @@ -19,10 +19,12 @@ $('#deleteModal').on('show.bs.modal', function (event) {
modal.find('.del-noun').text('deletion');
modal.find('.del-verb').text('delete');
modal.find('.del-verb-capital').text('Delete');
modal.find('.del-verb-capital').addClass('btn-danger');
} else {
modal.find('.del-noun').text('undeletion');
modal.find('.del-verb').text('undelete');
modal.find('.del-verb-capital').text('Undelete');
modal.find('.del-verb-capital').addClass('btn-success');
}
});
</script>
Expand Down Expand Up @@ -97,14 +99,14 @@ $('#deleteModal').on('show.bs.modal', function (event) {
<h4 class="modal-title" id="deleteModalLabel">Confirm <span class="del-noun">something</span></h4>
</div>
<div class="modal-body">
Really <span class="del-verb">do something to</span> <span class="del-username">user</span>?
Really <span class="del-verb">do something to</span> <strong class="del-username">user</strong>?
</div>
<div class="modal-footer">
<form method="POST" action="/users/delete" class="delete-button">
<input type="hidden" name="uid" class="del-uid">
<input type="hidden" name="direction" class="del-direction">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger del-verb-capital">Delete</button>
<button type="submit" class="btn del-verb-capital">Delete</button>
</div>
</div>
</div>
Expand Down
14 changes: 12 additions & 2 deletions comet/data/templates/mako/comet_users_permissions.tmpl
Expand Up @@ -55,6 +55,10 @@ $(document).ready(function() {
<thead><tr>
<th class="uid">#</th>
<th class="username">Username</th>
<th class="perm is_active">Active<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="is_active"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="is_active"><i class="fa fa-square-o fa-fw"></i></button>
</th>
<th class="perm is_admin">Admin<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="is_admin"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="is_admin"><i class="fa fa-square-o fa-fw"></i></button>
Expand Down Expand Up @@ -82,12 +86,18 @@ $(document).ready(function() {
<th class="select_all">Select all</th>
</tr></thead>
% for uid, user in USERS.items():
% if user.active:
% if user.is_active:
<tr class="u${uid}">
<td class="uid">${uid}</td>
<td class="username">${user.username}</td>
% for p in PERMISSIONS:
<td class="perm ${p}">${display_permission(user, p)}</td>
<td class="perm
% if p == 'active':
is_active
% else:
${p}
%endif
">${display_permission(user, p)}</td>
% endfor
<td class="select_all"><button type="button" class="btn btn-info select_all-user" data-uid="${uid}"><i class="fa fa-check-square-o fa-fw"></i></button> <button type="button" class="btn btn-info select_none-user" data-uid="${uid}"><i class="fa fa-square-o fa-fw"></i></button></td>
</tr>
Expand Down
25 changes: 18 additions & 7 deletions comet/init.py
Expand Up @@ -26,21 +26,32 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import print_function, unicode_literals
import pkg_resources
import os
from comet.utils import PERMISSIONS, parse_redis
import redis

__all__ = ['init', 'write_users']

def init():
print("ERROR: Not implemented.")
return 255


def write_users():
with open('comet_users.json', 'wb') as fh:
fh.write(pkg_resources.resource_string(
'comet', os.path.join('data', 'comet_users.json')))
def write_users(dburl):
data = {
'username': 'admin',
'realname': 'Website Administrator',
'password': '$2a$12$.qMCcA2uOo0BKkDtEF/bueYtHjcdPBmfEdpxtktRwRTgsR7ZVTWmW',
}

for p in PERMISSIONS:
data[p] = '1'

db = redis.StrictRedis(**parse_redis(dburl))
db.hmset('user:1', data)
db.hset('users', 'admin', '1')
if not db.exists('last_uid'):
db.incr('last_uid')

print("Wrote comet_users.json.\n")
print("Username: admin")
print("Password: admin")
return 0
97 changes: 97 additions & 0 deletions comet/utils.py
@@ -0,0 +1,97 @@
# -*- coding: utf-8 -*-

# Comet CMS v0.6.0
# Copyright © 2014-2015 Chris Warrick, Roberto Alsina, Henry Hirsch et al.

# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import unicode_literals
import kombu
import sys


__all__ = ['PERMISSIONS', 'USER_FIELDS', 'USER_ALL', 'parse_redis', 'ask', 'ask_yesno']

PERMISSIONS = ['active', 'is_admin', 'can_edit_all_posts', 'wants_all_posts',
'can_upload_attachments', 'can_rebuild_site',
'can_transfer_post_authorship']
USER_FIELDS = ['username', 'realname', 'password',]
USER_ALL = USER_FIELDS + PERMISSIONS


def parse_redis(url):
"""Parse Redis URL.
:param str url: Redis URL
:return: data for connection
:rtype: dict
:raises ValueError: invalid URL
"""
redis_raw = kombu.parse_url(url)
if redis_raw['transport'] == 'redis':
return {'host': redis_raw['hostname'] or 'localhost',
'port': redis_raw['port'] or 6379,
'db': int(redis_raw['virtual_host'] or 0),
'password': redis_raw['password']}
elif redis_raw['transport'] == 'redis+socket':
return {'unix_socket_path`': redis_raw['virtual_host']}
else:
raise ValueError("invalid Redis URL")


# The following two functions come from Nikola.
def ask(query, default=None):
"""Ask a question."""
if default:
default_q = ' [{0}]'.format(default)
else:
default_q = ''
if sys.version_info[0] == 3:
inp = raw_input("{query}{default_q}: ".format(query=query, default_q=default_q)).strip()
else:
inp = raw_input("{query}{default_q}: ".format(query=query, default_q=default_q).encode('utf-8')).strip()
if inp or default is None:
return inp
else:
return default


def ask_yesno(query, default=None):
"""Ask a yes/no question."""
if default is None:
default_q = ' [y/n]'
elif default is True:
default_q = ' [Y/n]'
elif default is False:
default_q = ' [y/N]'
if sys.version_info[0] == 3:
inp = raw_input("{query}{default_q} ".format(query=query, default_q=default_q)).strip()
else:
inp = raw_input("{query}{default_q} ".format(query=query, default_q=default_q).encode('utf-8')).strip()
if inp:
return inp.lower().startswith('y')
elif default is not None:
return default
else:
# Loop if no answer and no default.
return ask_yesno(query, default)

0 comments on commit f970ac6

Please sign in to comment.