From 5f66e6465d3822b150898de2a7fb8df39ed7fdc6 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Sun, 11 Oct 2015 23:34:33 +0200 Subject: removing misplaced stuffs from views, more controllers use --- src/web/controllers/abstract.py | 2 +- src/web/controllers/user.py | 25 +++ src/web/models/user.py | 10 +- src/web/templates/admin/dashboard.html | 18 ++- src/web/templates/home.html | 2 +- src/web/templates/layout.html | 6 +- src/web/utils.py | 2 +- src/web/views/__init__.py | 2 + src/web/views/views.py | 274 +-------------------------------- 9 files changed, 47 insertions(+), 294 deletions(-) (limited to 'src/web') diff --git a/src/web/controllers/abstract.py b/src/web/controllers/abstract.py index f33d241e..99d92ff3 100644 --- a/src/web/controllers/abstract.py +++ b/src/web/controllers/abstract.py @@ -84,7 +84,7 @@ class AbstractController(object): def create(self, **attrs): assert self._user_id_key is None or self._user_id_key in attrs \ - or self.user_id is not None, \ + or self.user_id is None, \ "You must provide user_id one way or another" if self._user_id_key is not None and self._user_id_key not in attrs: diff --git a/src/web/controllers/user.py b/src/web/controllers/user.py index 3f96b185..d8bf1fa1 100644 --- a/src/web/controllers/user.py +++ b/src/web/controllers/user.py @@ -1,3 +1,6 @@ +import random +import hashlib +from werkzeug import generate_password_hash from .abstract import AbstractController from web.models import User @@ -5,3 +8,25 @@ from web.models import User class UserController(AbstractController): _db_cls = User _user_id_key = 'id' + + def unset_activation_key(self, obj_id): + self.update({'id': obj_id}, {'activation_key': ""}) + + def set_activation_key(self, obj_id): + key = str(random.getrandbits(256)).encode("utf-8") + key = hashlib.sha512(key).hexdigest()[:86] + self.update({'id': obj_id}, {'activation_key': key}) + + def _handle_password(self, attrs): + if attrs.get('password'): + attrs['pwdhash'] = generate_password_hash(attrs.pop('password')) + elif 'password' in attrs: + del attrs['password'] + + def create(self, **attrs): + self._handle_password(attrs) + return super().create(**attrs) + + def update(self, filters, attrs): + self._handle_password(attrs) + return super().update(filters, attrs) diff --git a/src/web/models/user.py b/src/web/models/user.py index c5e70036..d1b9c568 100644 --- a/src/web/models/user.py +++ b/src/web/models/user.py @@ -30,7 +30,7 @@ import re import random import hashlib from datetime import datetime -from werkzeug import generate_password_hash, check_password_hash +from werkzeug import check_password_hash from flask.ext.login import UserMixin from bootstrap import db @@ -63,12 +63,6 @@ class User(db.Model, UserMixin): """ return self.id - def set_password(self, password): - """ - Hash the password of the user. - """ - self.pwdhash = generate_password_hash(password) - def check_password(self, password): """ Check the password of the user. @@ -79,7 +73,7 @@ class User(db.Model, UserMixin): """ Return True if the user has administrator rights. """ - return len([role for role in self.roles if role.name == "admin"]) != 0 + return "admin" in [role.name for role in self.roles] def __eq__(self, other): return self.id == other.id diff --git a/src/web/templates/admin/dashboard.html b/src/web/templates/admin/dashboard.html index 25bd3883..2436c955 100644 --- a/src/web/templates/admin/dashboard.html +++ b/src/web/templates/admin/dashboard.html @@ -20,29 +20,31 @@ {{ loop.index }} {% if user.id == current_user.id %} - {{ user.nickname }} (It's you!) + {{ user.nickname }} (It's you!) {% else %} - {{ user.nickname }} + {{ user.nickname }} {% endif %} {{ user.email }} {{ user.last_seen }} - - + + {% if user.id != current_user.id %} + {% if user.activation_key == "" %} - + {% else %} - + {% endif %} - + + {% endif %} {% endfor %} -{{ _('Add a new user') }} +{{ _('Add a new user') }}

{{ _('Send notification messages') }}

{{ form.hidden_tag() }} diff --git a/src/web/templates/home.html b/src/web/templates/home.html index 86d96e94..6b136870 100644 --- a/src/web/templates/home.html +++ b/src/web/templates/home.html @@ -3,7 +3,7 @@ {% if feeds|count == 0 %}

{{ _("You don't have any feeds.") }}

-

{{ _('Add some') }}, {{ _('or') }} {{ _('upload an OPML file.') }}

+

{{ _('Add some') }}, {{ _('or') }} {{ _('upload an OPML file.') }}

{% else %}
diff --git a/src/web/templates/layout.html b/src/web/templates/layout.html index cf2498e2..eb213ca5 100644 --- a/src/web/templates/layout.html +++ b/src/web/templates/layout.html @@ -81,12 +81,12 @@