Skip to content

Commit 9be0398

Browse files
committed
fix: permisos para el board del usuario
Incluye comando para arreglar los permisos de los usuarios que ya tiene el sistema.
1 parent 835b459 commit 9be0398

4 files changed

Lines changed: 27 additions & 11 deletions

File tree

newswriter/admin_commands.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from newswriter.models.security import Role, User, create_user as CreateUser
2+
from newswriter.models.content import Board
3+
from newswriter.models.permissions import BOARD_ALL_PERMS
24
from newswriter.schemas import UserSchema, RoleSchema
35
from newswriter import db
46
from flask import Blueprint
@@ -95,3 +97,13 @@ def listar_roles():
9597
click.echo("Listado de roles")
9698
s = RoleSchema()
9799
pprint(s.dump(Role.query.all(), many=True), indent=2)
100+
101+
102+
@users_cmds.cli.command('fixboards')
103+
def fix_userboards():
104+
"""Ensure all users has a board"""
105+
for u in User.query.all():
106+
for p in BOARD_ALL_PERMS:
107+
u.getUserRole().addPermission(
108+
p, Board.createUserBoard(u).name, 'board')
109+
db.session.commit()

newswriter/models/security.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def addPermission(self, name, record_id, model_name):
4343
self.permissions.append(p)
4444
self.query.session.add(self)
4545

46+
return p
47+
4648
@classmethod
4749
def getUserEspecialRole(cls, user: 'User') -> 'Role':
4850
return cls.query.filter_by(
@@ -136,12 +138,7 @@ def create_user(
136138
user_board = content.Board.createUserBoard(user)
137139
# assing add user permissions on the board
138140
for p in BOARD_ALL_PERMS:
139-
db.session.add(Permission(
140-
name=p,
141-
model_name='board',
142-
record_id=user_board.id,
143-
role_id=user_rol.id
144-
))
141+
user_rol.addPermission(p, user_board.name, 'board')
145142
# --
146143

147144
return user

newswriter/templates/default/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
{% block content %}
66
<div class="works-list">
77

8-
{% if results.items: %}
8+
{% if results.total > 0 %}
99

10-
{% for item in results.items: %}
10+
{% for item in results.items %}
1111
<div class="card">
1212
<div class="card-content">
1313
<span class="card-title"><a href="{{ url_for('.preview', pkid=item.id) }}">{{ item.headline }}</a></span>

newswriter/views/default.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,16 @@ def index():
4141
page = request.args.get('page', 1, type=int)
4242
ub = Board.getUserBoard(current_user)
4343

44-
articulos = Article.query.filter(
45-
Article.board_id == ub.id).order_by(
46-
Article.created_on.desc()).paginate(page, per_page=4)
44+
if ub:
45+
articulos = Article.query.filter(
46+
Article.board_id == ub.name).order_by(
47+
Article.created_on.desc()).paginate(page, per_page=4)
48+
else:
49+
# empty result set
50+
articulos = {
51+
"total": 0,
52+
"items": []
53+
}
4754

4855
return render_template(
4956
'default/index.html', results=articulos)

0 commit comments

Comments
 (0)