aboutsummaryrefslogtreecommitdiff
path: root/src/web/views/admin.py
blob: 753b864f868c3b12a570f47cbd8b8b662864787a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from flask import (Blueprint, g, render_template, redirect,
                   flash, url_for, request)
from flask.ext.babel import gettext
from flask.ext.login import login_required

from flask.ext.principal import Permission, RoleNeed

from web.lib.utils import redirect_url
from web.models import Role
from web.controllers import UserController, ArticleController, FeedController

from web.forms import InformationMessageForm, UserForm
from web import notifications

admin_bp = Blueprint('admin', __name__, url_prefix='/admin')
admin_permission = Permission(RoleNeed('admin'))


@admin_bp.route('/dashboard', methods=['GET', 'POST'])
@login_required
@admin_permission.require(http_exception=403)
def dashboard():
    """
    Adminstrator's dashboard.
    """
    form = InformationMessageForm()

    if request.method == 'POST':
        if form.validate():
            try:
                notifications.information_message(form.subject.data,
                                                  form.message.data)
            except Exception as error:
                flash(gettext(
                        'Problem while sending email: %(error)s', error=error),
                        'danger')

    users = UserController().read()
    return render_template('admin/dashboard.html',
                           users=users, current_user=g.user, form=form)


@admin_bp.route('/user/create', methods=['GET'])
@admin_bp.route('/user/edit/<int:user_id>', methods=['GET'])
@login_required
@admin_permission.require(http_exception=403)
def user_form(user_id=None):
    if user_id is not None:
        user = UserController().get(id=user_id)
        form = UserForm(obj=user)
        message = gettext('Edit the user <i>%(nick)s</i>', nick=user.nickname)
    else:
        form = UserForm()
        message = gettext('Add a new user')
    return render_template('/admin/create_user.html',
                           form=form, message=message)


@admin_bp.route('/user/create', methods=['POST'])
@admin_bp.route('/user/edit/<int:user_id>', methods=['POST'])
@login_required
@admin_permission.require(http_exception=403)
def process_user_form(user_id=None):
    """
    Create or edit a user.
    """
    form = UserForm()
    user_contr = UserController()

    if not form.validate():
        return render_template('/admin/create_user.html', form=form,
                               message=gettext('Some errors were found'))

    role_user = Role.query.filter(Role.name == "user").first()
    if user_id is not None:
        # Edit a user
        user_contr.update({'id': user_id},
                          {'nickname': form.nickname.data,
                           'email': form.email.data,
                           'password': form.password.data,
                           'refresh_rate': form.refresh_rate.data})
        user = user_contr.get(id=user_id)
        flash(gettext('User %(nick)s successfully updated',
                      nick=user.nickname), 'success')
    else:
        # Create a new user
        user = user_contr.create(nickname=form.nickname.data,
                                 email=form.email.data,
                                 password=form.password.data,
                                 roles=[role_user],
                                 refresh_rate=form.refresh_rate.data,
                                 activation_key="")
        flash(gettext('User %(nick)s successfully created',
                      nick=user.nickname), 'success')
    return redirect(url_for('admin.user_form', user_id=user.id))


@admin_bp.route('/user/<int:user_id>', methods=['GET'])
@login_required
@admin_permission.require(http_exception=403)
def user(user_id=None):
    """
    See information about a user (stations, etc.).
    """
    user = UserController().get(id=user_id)
    if user is not None:
        article_contr = ArticleController(user_id)
        return render_template('/admin/user.html', user=user, feeds=user.feeds,
                article_count=article_contr.count_by_feed(),
                unread_article_count=article_contr.count_by_feed(readed=False))

    else:
        flash(gettext('This user does not exist.'), 'danger')
        return redirect(redirect_url())


@admin_bp.route('/delete_user/<int:user_id>', methods=['GET'])
@login_required
@admin_permission.require(http_exception=403)
def delete_user(user_id=None):
    """
    Delete a user (with all its data).
    """
    try:
        user = UserController().delete(user_id)
        flash(gettext('User %(nick)s successfully deleted',
                      nick=user.nickname), 'success')
    except Exception as error:
        flash(gettext('An error occured while trying to delete a user: '
                      '%(error)', error=error), 'danger')
    return redirect(redirect_url())

@admin_bp.route('/delete/<feed_id>', methods=['GET'])
@login_required
@admin_permission.require(http_exception=403)
def delete(feed_id=None):
    "Deletes the feed of a user."
    try:
        feed = FeedController().delete(feed_id)
        flash(gettext("Feed successfully deleted."), 'success')
    except Exception as error:
        flash('An error occured while trying to delete a feed: '
                      + str(error), 'danger')
    return redirect(url_for('home'))

@admin_bp.route('/toggle_user/<int:user_id>', methods=['GET'])
@login_required
@admin_permission.require()
def toggle_user(user_id=None):
    """
    Enable or disable the account of a user.
    """
    user_contr = UserController()
    user = user_contr.get(id=user_id)

    if user is None:
        flash(gettext('This user does not exist.'), 'danger')
        return redirect(url_for('admin.dashboard'))

    if user.activation_key != "":

        # Send the confirmation email
        try:
            notifications.new_account_activation(user)
            user_contr.unset_activation_key(user.id)
            message = gettext('Account of the user %(nick)s successfully '
                              'activated.', nick=user.nickname)
        except Exception as error:
            flash(gettext('Problem while sending activation email %(error)s:',
                          error=error), 'danger')
            return redirect(url_for('admin.dashboard'))

    else:
        user_contr.set_activation_key(user.id)
        message = gettext('Account of the user %(nick)s successfully disabled',
                          nick=user.nickname)
    flash(message, 'success')
    return redirect(url_for('admin.dashboard'))
bgstack15