Skip to content

Commit cf6fbef

Browse files
committedMay 29, 2015
Python 3 fixes
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
1 parent 43ae4ff commit cf6fbef

File tree

4 files changed

+48
-37
lines changed

4 files changed

+48
-37
lines changed
 

‎coil/data/templates/mako/coil_users.tmpl

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ $('#deleteModal').on('show.bs.modal', function (event) {
8787
% endfor
8888
<tr><form action="${url_for('acp_users_edit')}" method="POST">${editform.csrf_token}
8989
<td class="uid"><strong>Create:</strong></td>
90-
<td colspan="4"><input name="username" placeholder="New user" class="form-control input-sm"></td>
90+
<td colspan="4"><input name="username" placeholder="New user"
91+
class="form-control input-sm"><input type="hidden" name="action" value="new"></td>
9192
<td><button type="submit" class="btn btn-sm btn-primary"><i class="fa fa-plus-square fa-fw"></i> Create</button></td>
9293
</form></tr>
9394
<tr><form action="${url_for('acp_users_import')}" method="POST" enctype="multipart/form-data">${importform.csrf_token}

‎coil/tasks.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ def build_single(mode):
111111
p = subprocess.Popen([_executable, '-m', 'nikola', 'build'] + amode,
112112
stderr=subprocess.PIPE)
113113
p.wait()
114-
out = ''.join(p.stderr.readlines())
114+
rl = p.stderr.readlines()
115+
try:
116+
out = ''.join(rl)
117+
except TypeError:
118+
out = ''.join(l.decode('utf-8') for l in rl)
115119
return (p.returncode == 0), out
116120

117121

@@ -125,7 +129,7 @@ def orphans_single(default_exec=False):
125129
p = subprocess.Popen([_executable, '-m', 'nikola', 'orphans'],
126130
stdout=subprocess.PIPE)
127131
p.wait()
128-
files = [l.strip() for l in p.stdout.readlines()]
132+
files = [l.strip().decode('utf-8') for l in p.stdout.readlines()]
129133
for f in files:
130134
if f:
131135
os.unlink(f)

‎coil/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def ask(query, default=None):
5656
else:
5757
default_q = ''
5858
if sys.version_info[0] == 3:
59-
inp = raw_input("{query}{default_q}: ".format(
59+
inp = input("{query}{default_q}: ".format(
6060
query=query, default_q=default_q)).strip()
6161
else:
6262
inp = raw_input("{query}{default_q}: ".format(
@@ -116,7 +116,7 @@ def reload_site(self):
116116
timeline = self.db.lrange('site:timeline', 0, -1)
117117
self._timeline = []
118118
for data in timeline:
119-
data = json.loads(data)
119+
data = json.loads(data.decode('utf-8'))
120120
self._timeline.append(Post(data[0], self.config, data[1],
121121
data[2], data[3], self.messages,
122122
self._site.compilers[data[4]]))

‎coil/web.py

+38-32
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import logbook
3636
import redis
3737
import rq
38+
import operator
3839
import coil.tasks
3940
from nikola.utils import (unicode_str, get_logger, ColorfulStderrHandler,
4041
write_metadata, TranslatableSetting)
@@ -207,7 +208,10 @@ def password_hash(password):
207208
:return: password hash
208209
:rtype: str
209210
"""
210-
return bcrypt_sha256.encrypt(password)
211+
try:
212+
return bcrypt_sha256.encrypt(password)
213+
except TypeError:
214+
return bcrypt_sha256.encrypt(password.decode('utf-8'))
211215

212216

213217
def check_password(pwdhash, password):
@@ -341,6 +345,14 @@ def render(template_name, context=None, code=200, headers=None):
341345
headers['Pragma'] = 'no-cache'
342346
headers['Cache-Control'] = 'private, max-age=0, no-cache'
343347

348+
try:
349+
mcp = current_user.must_change_password in (True, '1')
350+
except AttributeError:
351+
mcp = False
352+
353+
if mcp and not context.get('pwdchange_skip', False):
354+
return redirect(url_for('acp_account') + '?status=pwdchange')
355+
344356
return _site.render_template(template_name, None, context), code, headers
345357

346358

@@ -457,11 +469,25 @@ def get_user(uid):
457469
:raises KeyError: if user does not exist
458470
"""
459471
if db is not None:
472+
try:
473+
uid = uid.decode('utf-8')
474+
except AttributeError:
475+
pass
460476
d = db.hgetall('user:{0}'.format(uid))
461477
if d:
478+
nd = {}
479+
# strings everywhere
480+
for k in d:
481+
try:
482+
nd[k.decode('utf-8')] = d[k].decode('utf-8')
483+
except AttributeError:
484+
try:
485+
nd[k.decode('utf-8')] = d[k]
486+
except AttributeError:
487+
nd[k] = d[k]
462488
for p in PERMISSIONS:
463-
d[p] = d.get(p) == '1'
464-
return User(uid=uid, **d)
489+
nd[p] = nd.get(p) == '1'
490+
return User(uid=uid, **nd)
465491
else:
466492
return None
467493
else:
@@ -557,7 +583,8 @@ def login():
557583
alert = 'Logged out successfully.'
558584
alert_status = 'success'
559585
return render('coil_login.tmpl', {'title': 'Login', 'alert': alert, 'form':
560-
form, 'alert_status': alert_status},
586+
form, 'alert_status': alert_status,
587+
'pwdchange_skip': True},
561588
code)
562589

563590

@@ -576,9 +603,6 @@ def index():
576603
577604
:param int all: Whether or not should show all posts
578605
"""
579-
if current_user.must_change_password:
580-
return redirect(url_for('acp_account') + '?status=pwdchange')
581-
582606
context = {'postform': NewPostForm(),
583607
'pageform': NewPageForm(),
584608
'delform': DeleteForm()}
@@ -627,9 +651,6 @@ def edit(path):
627651
628652
:param path: Path to post to edit.
629653
"""
630-
if current_user.must_change_password:
631-
return redirect(url_for('acp_account') + '?status=pwdchange')
632-
633654
context = {'path': path, 'site': site}
634655
post = find_post(path)
635656
if post is None:
@@ -709,9 +730,6 @@ def edit(path):
709730
@login_required
710731
def delete():
711732
"""Delete a post."""
712-
if current_user.must_change_password:
713-
return redirect(url_for('acp_account') + '?status=pwdchange')
714-
715733
form = DeleteForm()
716734
path = request.form['path']
717735
post = find_post(path)
@@ -776,9 +794,6 @@ def api_rebuild():
776794
@login_required
777795
def rebuild(mode=''):
778796
"""Rebuild the site with a nice UI."""
779-
if current_user.must_change_password:
780-
return redirect(url_for('acp_account') + '?status=pwdchange')
781-
782797
scan_site() # for good measure
783798
if not current_user.can_rebuild_site:
784799
return error('You are not permitted to rebuild the site.</p>'
@@ -812,9 +827,6 @@ def new(obj):
812827
813828
:param str obj: Object to create (post or page)
814829
"""
815-
if current_user.must_change_password:
816-
return redirect(url_for('acp_account') + '?status=pwdchange')
817-
818830
title = request.form['title']
819831
_site.config['ADDITIONAL_METADATA']['author.uid'] = current_user.uid
820832
try:
@@ -905,9 +917,11 @@ def acp_account():
905917
if request.args.get('status') == 'pwdchange':
906918
alert = 'You must change your password before proceeding.'
907919
alert_status = 'danger'
920+
pwdchange_skip = True
908921
else:
909922
alert = ''
910923
alert_status = ''
924+
pwdchange_skip = False
911925

912926
if db is None:
913927
form = PwdHashForm()
@@ -936,6 +950,7 @@ def acp_account():
936950
if data['newpwd1'] == data['newpwd2'] and pwd_ok:
937951
current_user.password = password_hash(data['newpwd1'])
938952
current_user.must_change_password = False
953+
pwdchange_skip = True
939954
else:
940955
alert = 'Passwords don’t match.'
941956
alert_status = 'danger'
@@ -950,7 +965,8 @@ def acp_account():
950965
'action': action,
951966
'alert': alert,
952967
'alert_status': alert_status,
953-
'form': form})
968+
'form': form,
969+
'pwdchange_skip': pwdchange_skip})
954970

955971

956972
@app.route('/account/pwdhash', methods=['POST'])
@@ -974,8 +990,6 @@ def acp_pwdhash():
974990
@login_required
975991
def acp_users():
976992
"""List all users."""
977-
if current_user.must_change_password:
978-
return redirect(url_for('acp_account') + '?status=pwdchange')
979993
if not current_user.is_admin:
980994
return error("Not authorized to edit users.", 401)
981995
if not db:
@@ -991,7 +1005,7 @@ def acp_users():
9911005
alert_status = 'success'
9921006
else:
9931007
uids = db.hgetall('users').values()
994-
USERS = sorted([(i, get_user(i)) for i in uids])
1008+
USERS = sorted([(int(i), get_user(i)) for i in uids], key=operator.itemgetter(0))
9951009
return render('coil_users.tmpl',
9961010
context={'title': 'Users',
9971011
'USERS': USERS,
@@ -1007,8 +1021,6 @@ def acp_users():
10071021
def acp_users_edit():
10081022
"""Edit an user account."""
10091023
global current_user
1010-
if current_user.must_change_password:
1011-
return redirect(url_for('acp_account') + '?status=pwdchange')
10121024
if not current_user.is_admin:
10131025
return error("Not authorized to edit users.", 401)
10141026
if not db:
@@ -1023,7 +1035,7 @@ def acp_users_edit():
10231035
if action == 'new':
10241036
if not data['username']:
10251037
return error("No username to create specified.", 400)
1026-
uid = max(db.hgetall('users').values()) + 1
1038+
uid = max(int(i) for i in db.hgetall('users').values()) + 1
10271039
pf = [False for p in PERMISSIONS]
10281040
pf[0] = True # active
10291041
pf[7] = True # must_change_password
@@ -1083,8 +1095,6 @@ def acp_users_edit():
10831095
@login_required
10841096
def acp_users_import():
10851097
"""Import users from a TSV file."""
1086-
if current_user.must_change_password:
1087-
return redirect(url_for('acp_account') + '?status=pwdchange')
10881098
if not current_user.is_admin:
10891099
return error("Not authorized to edit users.", 401)
10901100
if not db:
@@ -1103,8 +1113,6 @@ def acp_users_import():
11031113
@login_required
11041114
def acp_users_delete():
11051115
"""Delete or undelete an user account."""
1106-
if current_user.must_change_password:
1107-
return redirect(url_for('acp_account') + '?status=pwdchange')
11081116
if not current_user.is_admin:
11091117
return error("Not authorized to edit users.", 401)
11101118
if not db:
@@ -1130,8 +1138,6 @@ def acp_users_delete():
11301138
@login_required
11311139
def acp_users_permissions():
11321140
"""Change user permissions."""
1133-
if current_user.must_change_password:
1134-
return redirect(url_for('acp_account') + '?status=pwdchange')
11351141
if not current_user.is_admin:
11361142
return error("Not authorized to edit users.", 401)
11371143
if not db:

0 commit comments

Comments
 (0)
Please sign in to comment.