diff options
Diffstat (limited to 'src/web/views')
-rw-r--r-- | src/web/views/__init__.py | 4 | ||||
-rw-r--r-- | src/web/views/admin.py | 166 | ||||
-rw-r--r-- | src/web/views/api/category.py | 31 | ||||
-rw-r--r-- | src/web/views/category.py | 86 | ||||
-rw-r--r-- | src/web/views/feed.py | 5 | ||||
-rw-r--r-- | src/web/views/icon.py | 1 | ||||
-rw-r--r-- | src/web/views/user.py | 151 | ||||
-rw-r--r-- | src/web/views/views.py | 49 |
8 files changed, 483 insertions, 10 deletions
diff --git a/src/web/views/__init__.py b/src/web/views/__init__.py index f02e86e6..27370fc3 100644 --- a/src/web/views/__init__.py +++ b/src/web/views/__init__.py @@ -7,3 +7,7 @@ from .category import category_bp, categories_bp from .icon import icon_bp from .admin import admin_bp from .user import user_bp, users_bp + + +__all__ = ['article_bp', 'articles_bp', 'feed_bp', 'feeds_bp', 'category_bp', + 'categories_bp', 'icon_bp', 'admin_bp', 'user_bp', 'users_bp'] diff --git a/src/web/views/admin.py b/src/web/views/admin.py new file mode 100644 index 00000000..ec79262d --- /dev/null +++ b/src/web/views/admin.py @@ -0,0 +1,166 @@ +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 + +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('/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')) diff --git a/src/web/views/api/category.py b/src/web/views/api/category.py new file mode 100644 index 00000000..7923279a --- /dev/null +++ b/src/web/views/api/category.py @@ -0,0 +1,31 @@ +from flask import g + +from web.controllers.category import CategoryController +from web.views.api.common import (PyAggResourceNew, + PyAggResourceExisting, + PyAggResourceMulti) + + +CAT_ATTRS = {'name': {'type': str}, + 'user_id': {'type': int}} + + +class CategoryNewAPI(PyAggResourceNew): + controller_cls = CategoryController + attrs = CAT_ATTRS + + +class CategoryAPI(PyAggResourceExisting): + controller_cls = CategoryController + attrs = CAT_ATTRS + + +class CategoriesAPI(PyAggResourceMulti): + controller_cls = CategoryController + attrs = CAT_ATTRS + + +g.api.add_resource(CategoryNewAPI, '/category', endpoint='category_new.json') +g.api.add_resource(CategoryAPI, '/category/<int:obj_id>', + endpoint='category.json') +g.api.add_resource(CategoriesAPI, '/categories', endpoint='categories.json') diff --git a/src/web/views/category.py b/src/web/views/category.py new file mode 100644 index 00000000..20b90caa --- /dev/null +++ b/src/web/views/category.py @@ -0,0 +1,86 @@ +from flask import g, Blueprint, render_template, flash, redirect, url_for +from flask.ext.babel import gettext +from flask.ext.login import login_required + +from web.forms import CategoryForm +from web.lib.utils import redirect_url +from web.lib.view_utils import etag_match +from web.controllers import ArticleController, FeedController, \ + CategoryController + +categories_bp = Blueprint('categories', __name__, url_prefix='/categories') +category_bp = Blueprint('category', __name__, url_prefix='/category') + + +@categories_bp.route('/', methods=['GET']) +@login_required +@etag_match +def list_(): + "Lists the subscribed feeds in a table." + art_contr = ArticleController(g.user.id) + return render_template('categories.html', + categories=list(CategoryController(g.user.id).read()), + feeds_count=FeedController(g.user.id).count_by_category(), + unread_article_count=art_contr.count_by_category(readed=False), + article_count=art_contr.count_by_category()) + + +@category_bp.route('/create', methods=['GET']) +@category_bp.route('/edit/<int:category_id>', methods=['GET']) +@login_required +@etag_match +def form(category_id=None): + action = gettext("Add a category") + head_titles = [action] + if category_id is None: + return render_template('edit_category.html', action=action, + head_titles=head_titles, form=CategoryForm()) + category = CategoryController(g.user.id).get(id=category_id) + action = gettext('Edit category') + head_titles = [action] + if category.name: + head_titles.append(category.name) + return render_template('edit_category.html', action=action, + head_titles=head_titles, category=category, + form=CategoryForm(obj=category)) + + +@category_bp.route('/delete/<int:category_id>', methods=['GET']) +@login_required +def delete(category_id=None): + category = CategoryController(g.user.id).delete(category_id) + flash(gettext("Category %(category_name)s successfully deleted.", + category_name=category.name), 'success') + return redirect(redirect_url()) + + +@category_bp.route('/create', methods=['POST']) +@category_bp.route('/edit/<int:category_id>', methods=['POST']) +@login_required +def process_form(category_id=None): + form = CategoryForm() + cat_contr = CategoryController(g.user.id) + + if not form.validate(): + return render_template('edit_category.html', form=form) + existing_cats = list(cat_contr.read(name=form.name.data)) + if existing_cats and category_id is None: + flash(gettext("Couldn't add category: already exists."), "warning") + return redirect(url_for('category.form', + category_id=existing_cats[0].id)) + # Edit an existing category + category_attr = {'name': form.name.data} + + if category_id is not None: + cat_contr.update({'id': category_id}, category_attr) + flash(gettext('Category %(cat_name)r successfully updated.', + cat_name=category_attr['name']), 'success') + return redirect(url_for('category.form', category_id=category_id)) + + # Create a new category + new_category = cat_contr.create(**category_attr) + + flash(gettext('Category %(category_name)r successfully created.', + category_name=new_category.name), 'success') + + return redirect(url_for('category.form', category_id=new_category.id)) diff --git a/src/web/views/feed.py b/src/web/views/feed.py index 187fd4c2..69972958 100644 --- a/src/web/views/feed.py +++ b/src/web/views/feed.py @@ -1,20 +1,17 @@ #! /usr/bin/env python # -*- coding: utf-8 - -import base64 import requests.exceptions -from hashlib import md5 from datetime import datetime, timedelta from sqlalchemy import desc from werkzeug.exceptions import BadRequest from flask import Blueprint, g, render_template, flash, \ - redirect, request, url_for, Response + redirect, request, url_for from flask.ext.babel import gettext from flask.ext.login import login_required import conf from web import utils -from web.lib.utils import redirect_url from web.lib.view_utils import etag_match from web.lib.feed_utils import construct_feed_from from web.forms import AddFeedForm diff --git a/src/web/views/icon.py b/src/web/views/icon.py index 895b4740..64e54cab 100644 --- a/src/web/views/icon.py +++ b/src/web/views/icon.py @@ -5,6 +5,7 @@ from web.lib.view_utils import etag_match icon_bp = Blueprint('icon', __name__, url_prefix='/icon') + @icon_bp.route('/', methods=['GET']) @etag_match def icon(): diff --git a/src/web/views/user.py b/src/web/views/user.py new file mode 100644 index 00000000..754d3b9a --- /dev/null +++ b/src/web/views/user.py @@ -0,0 +1,151 @@ +import string +import random +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 + +import conf +from web import utils, notifications +from web.controllers import (UserController, FeedController, ArticleController) + +from web.forms import ProfileForm, RecoverPasswordForm + +users_bp = Blueprint('users', __name__, url_prefix='/users') +user_bp = Blueprint('user', __name__, url_prefix='/user') + + +@user_bp.route('/management', methods=['GET', 'POST']) +@login_required +def management(): + """ + Display the management page. + """ + if request.method == 'POST': + if None != request.files.get('opmlfile', None): + # Import an OPML file + data = request.files.get('opmlfile', None) + if not utils.allowed_file(data.filename): + flash(gettext('File not allowed.'), 'danger') + else: + try: + nb = utils.import_opml(g.user.email, data.read()) + if conf.CRAWLING_METHOD == "classic": + utils.fetch(g.user.email, None) + flash(str(nb) + ' ' + gettext('feeds imported.'), + "success") + flash(gettext("Downloading articles..."), 'info') + except: + flash(gettext("Impossible to import the new feeds."), + "danger") + elif None != request.files.get('jsonfile', None): + # Import an account + data = request.files.get('jsonfile', None) + if not utils.allowed_file(data.filename): + flash(gettext('File not allowed.'), 'danger') + else: + try: + nb = utils.import_json(g.user.email, data.read()) + flash(gettext('Account imported.'), "success") + except: + flash(gettext("Impossible to import the account."), + "danger") + else: + flash(gettext('File not allowed.'), 'danger') + + nb_feeds = FeedController(g.user.id).read().count() + art_contr = ArticleController(g.user.id) + nb_articles = art_contr.read().count() + nb_unread_articles = art_contr.read(readed=False).count() + return render_template('management.html', user=g.user, + nb_feeds=nb_feeds, nb_articles=nb_articles, + nb_unread_articles=nb_unread_articles) + + +@user_bp.route('/profile', methods=['GET', 'POST']) +@login_required +def profile(): + """ + Edit the profile of the currently logged user. + """ + user_contr = UserController(g.user.id) + user = user_contr.get(id=g.user.id) + form = ProfileForm() + + if request.method == 'POST': + if form.validate(): + user_contr.update({'id': g.user.id}, + {'nickname': form.nickname.data, + 'email': form.email.data, + 'password': form.password.data, + 'refresh_rate': form.refresh_rate.data}) + + flash(gettext('User %(nick)s successfully updated', + nick=user.nickname), 'success') + return redirect(url_for('user.profile')) + else: + return render_template('profile.html', user=user, form=form) + + if request.method == 'GET': + form = ProfileForm(obj=user) + return render_template('profile.html', user=user, form=form) + + +@user_bp.route('/delete_account', methods=['GET']) +@login_required +def delete_account(): + """ + Delete the account of the user (with all its data). + """ + UserController(g.user.id).delete(g.user.id) + flash(gettext('Your account has been deleted.'), 'success') + return redirect(url_for('login')) + + +@user_bp.route('/confirm_account/<string:activation_key>', methods=['GET']) +def confirm_account(activation_key=None): + """ + Confirm the account of a user. + """ + user_contr = UserController() + if activation_key != "": + user = user_contr.read(activation_key=activation_key).first() + if user is not None: + user_contr.update({'id': user.id}, {'activation_key': ''}) + flash(gettext('Your account has been confirmed.'), 'success') + else: + flash(gettext('Impossible to confirm this account.'), 'danger') + return redirect(url_for('login')) + + +@user_bp.route('/recover', methods=['GET', 'POST']) +def recover(): + """ + Enables the user to recover its account when he has forgotten + its password. + """ + form = RecoverPasswordForm() + user_contr = UserController() + + if request.method == 'POST': + if form.validate(): + user = user_contr.get(email=form.email.data) + characters = string.ascii_letters + string.digits + password = "".join(random.choice(characters) + for x in range(random.randint(8, 16))) + user.set_password(password) + user_contr.update({'id': user.id}, {'password': password}) + + # Send the confirmation email + try: + notifications.new_password_notification(user, password) + flash(gettext('New password sent to your address.'), 'success') + except Exception as error: + flash(gettext('Problem while sending your new password: ' + '%(error)s', error=error), 'danger') + + return redirect(url_for('login')) + return render_template('recover.html', form=form) + + if request.method == 'GET': + return render_template('recover.html', form=form) diff --git a/src/web/views/views.py b/src/web/views/views.py index 17db8906..b29ef645 100644 --- a/src/web/views/views.py +++ b/src/web/views/views.py @@ -27,9 +27,6 @@ __copyright__ = "Copyright (c) Cedric Bonhomme" __license__ = "AGPLv3" import os -import string -import random -import hashlib import logging import datetime from collections import OrderedDict @@ -43,7 +40,6 @@ from flask.ext.principal import Principal, Identity, AnonymousIdentity, \ identity_changed, identity_loaded, Permission,\ RoleNeed, UserNeed from flask.ext.babel import gettext -from sqlalchemy import or_, and_ from sqlalchemy.exc import IntegrityError from werkzeug import generate_password_hash @@ -52,11 +48,10 @@ from web.lib.utils import redirect_url from web import utils, notifications, export from web.lib.view_utils import etag_match from web.models import User, Feed, Article, Role -from web.decorators import feed_access_required from web.forms import SignupForm, SigninForm from web.controllers import UserController, FeedController, \ - ArticleController + ArticleController, CategoryController Principal(app) @@ -235,6 +230,48 @@ def signup(): return render_template('signup.html', form=form) +from flask import jsonify + + +@app.route('/home2') +def new_home(): + return render_template('home2.html') + + +@app.route('/menu') +@login_required +def get_menu(): + categories = {c.id: c.dump() for c in CategoryController(g.user.id).read()} + categories[0] = {'name': 'No category', 'id': 0} + unread = ArticleController(g.user.id).count_by_feed(readed=False) + for cat_id in categories: + categories[cat_id]['unread'] = 0 + categories[cat_id]['feeds'] = [] + for feed in FeedController(g.user.id).read(): + feed = feed.dump() + feed['category_id'] = feed['category_id'] or 0 + feed['unread'] = unread.get(feed['id'], 0) + if feed.get('icon_url'): + feed['icon_url'] = url_for('icon.icon', url=feed['icon_url']) + categories[feed['category_id']]['unread'] += feed['unread'] + categories[feed['category_id']]['feeds'].append(feed) + return jsonify(**{'categories': list(categories.values()), + 'all_unread_count': sum(unread.values())}) + + +@app.route('/middle_panel') +@login_required +def get_middle_panel(): + fd_hash = {fd.id: fd for fd in FeedController(g.user.id).read()} + articles = ArticleController(g.user.id).read(readed=False) + return jsonify(**{'articles': [{'title': art.title, 'liked': art.like, + 'read': art.readed, 'article_id': art.id, + 'feed_title': fd_hash[art.feed_id].title, + 'icon_url': url_for('icon.icon', url=fd_hash[art.feed_id].icon_url) + if fd_hash[art.feed_id].icon_url else None, + 'date': art.date} for art in articles]}) + + @etag_match def render_home(filters=None, head_titles=None, page_to_render='home', **kwargs): |