Ejemplo n.º 1
0
def edit_user(user_id):
    content = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()  # type: ub.User
    if not content:
        flash(_(u"User not found"), category="error")
        return redirect(url_for('admin.admin'))
    downloads = list()
    languages = calibre_db.speaking_language()
    translations = babel.list_translations() + [LC('en')]
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    for book in content.downloads:
        downloadbook = calibre_db.get_book(book.book_id)
        if downloadbook:
            downloads.append(downloadbook)
        else:
            ub.delete_download(book.book_id)
    if request.method == "POST":
        to_save = request.form.to_dict()
        _handle_edit_user(to_save, content, languages, translations, kobo_support, downloads)
    return render_title_template("user_edit.html",
                                 translations=translations,
                                 languages=languages,
                                 new_user=0,
                                 content=content,
                                 downloads=downloads,
                                 registered_oauth=oauth_check,
                                 mail_configured=config.get_mail_server_configured(),
                                 kobo_support=kobo_support,
                                 title=_(u"Edit User %(nick)s", nick=content.nickname), page="edituser")
Ejemplo n.º 2
0
def new_user():
    content = ub.User()
    languages = speaking_language()
    translations = [LC('en')] + babel.list_translations()
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    if request.method == "POST":
        to_save = request.form.to_dict()
        content.default_language = to_save["default_language"]
        # content.mature_content = "Show_mature_content" in to_save
        content.locale = to_save.get("locale", content.locale)

        content.sidebar_view = sum(int(key[5:]) for key in to_save if key.startswith('show_'))
        if "show_detail_random" in to_save:
            content.sidebar_view |= constants.DETAIL_RANDOM

        content.role = constants.selected_roles(to_save)

        if not to_save["nickname"] or not to_save["email"] or not to_save["password"]:
            flash(_(u"Please fill out all fields!"), category="error")
            return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
                                         registered_oauth=oauth_check, kobo_support=kobo_support,
                                         title=_(u"Add new user"))
        content.password = generate_password_hash(to_save["password"])
        existing_user = ub.session.query(ub.User).filter(func.lower(ub.User.nickname) == to_save["nickname"].lower())\
            .first()
        existing_email = ub.session.query(ub.User).filter(ub.User.email == to_save["email"].lower())\
            .first()
        if not existing_user and not existing_email:
            content.nickname = to_save["nickname"]
            if config.config_public_reg and not check_valid_domain(to_save["email"]):
                flash(_(u"E-mail is not from valid domain"), category="error")
                return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
                                             registered_oauth=oauth_check, kobo_support=kobo_support,
                                             title=_(u"Add new user"))
            else:
                content.email = to_save["email"]
        else:
            flash(_(u"Found an existing account for this e-mail address or nickname."), category="error")
            return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
                                     languages=languages, title=_(u"Add new user"), page="newuser",
                                     kobo_support=kobo_support, registered_oauth=oauth_check)
        try:
            content.allowed_tags = config.config_allowed_tags
            content.denied_tags = config.config_denied_tags
            content.allowed_column_value = config.config_allowed_column_value
            content.denied_column_value = config.config_denied_column_value
            ub.session.add(content)
            ub.session.commit()
            flash(_(u"User '%(user)s' created", user=content.nickname), category="success")
            return redirect(url_for('admin.admin'))
        except IntegrityError:
            ub.session.rollback()
            flash(_(u"Found an existing account for this e-mail address or nickname."), category="error")
    else:
        content.role = config.config_default_role
        content.sidebar_view = config.config_default_show
    return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
                                 languages=languages, title=_(u"Add new user"), page="newuser",
                                 kobo_support=kobo_support, registered_oauth=oauth_check)
Ejemplo n.º 3
0
def new_user():
    content = ub.User()
    languages = calibre_db.speaking_language()
    translations = [LC('en')] + babel.list_translations()
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    if request.method == "POST":
        to_save = request.form.to_dict()
        _handle_new_user(to_save, content, languages, translations, kobo_support)
    else:
        content.role = config.config_default_role
        content.sidebar_view = config.config_default_show
    return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
                                 languages=languages, title=_(u"Add new user"), page="newuser",
                                 kobo_support=kobo_support, registered_oauth=oauth_check)
Ejemplo n.º 4
0
def edit_user(user_id):
    content = ub.session.query(
        ub.User).filter(ub.User.id == int(user_id)).first()  # type: ub.User
    downloads = list()
    languages = speaking_language()
    translations = babel.list_translations() + [LC('en')]
    for book in content.downloads:
        downloadbook = db.session.query(
            db.Books).filter(db.Books.id == book.book_id).first()
        if downloadbook:
            downloads.append(downloadbook)
        else:
            ub.delete_download(book.book_id)
            # ub.session.query(ub.Downloads).filter(book.book_id == ub.Downloads.book_id).delete()
            # ub.session.commit()
    if request.method == "POST":
        to_save = request.form.to_dict()
        if "delete" in to_save:
            if ub.session.query(ub.User).filter(
                    and_(
                        ub.User.role.op('&')(
                            constants.ROLE_ADMIN) == constants.ROLE_ADMIN,
                        ub.User.id != content.id)).count():
                ub.session.query(
                    ub.User).filter(ub.User.id == content.id).delete()
                ub.session.commit()
                flash(_(u"User '%(nick)s' deleted", nick=content.nickname),
                      category="success")
                return redirect(url_for('admin.admin'))
            else:
                flash(_(u"No admin user remaining, can't delete user",
                        nick=content.nickname),
                      category="error")
                return redirect(url_for('admin.admin'))
        else:
            if "password" in to_save and to_save["password"]:
                content.password = generate_password_hash(to_save["password"])
            anonymous = content.is_anonymous
            content.role = constants.selected_roles(to_save)
            if anonymous:
                content.role |= constants.ROLE_ANONYMOUS
            else:
                content.role &= ~constants.ROLE_ANONYMOUS

            val = [int(k[5:]) for k in to_save if k.startswith('show_')]
            sidebar = ub.get_sidebar_config()
            for element in sidebar:
                value = element['visibility']
                if value in val and not content.check_visibility(value):
                    content.sidebar_view |= value
                elif not value in val and content.check_visibility(value):
                    content.sidebar_view &= ~value

            if "Show_detail_random" in to_save:
                content.sidebar_view |= constants.DETAIL_RANDOM
            else:
                content.sidebar_view &= ~constants.DETAIL_RANDOM

            content.mature_content = "Show_mature_content" in to_save

            if "default_language" in to_save:
                content.default_language = to_save["default_language"]
            if "locale" in to_save and to_save["locale"]:
                content.locale = to_save["locale"]
            if to_save["email"] and to_save["email"] != content.email:
                existing_email = ub.session.query(ub.User).filter(ub.User.email == to_save["email"].lower()) \
                    .first()
                if not existing_email:
                    content.email = to_save["email"]
                else:
                    flash(_(
                        u"Found an existing account for this e-mail address."),
                          category="error")
                    return render_title_template(
                        "user_edit.html",
                        translations=translations,
                        languages=languages,
                        mail_configured=config.get_mail_server_configured(),
                        new_user=0,
                        content=content,
                        downloads=downloads,
                        registered_oauth=oauth_check,
                        title=_(u"Edit User %(nick)s", nick=content.nickname),
                        page="edituser")
            if "nickname" in to_save and to_save[
                    "nickname"] != content.nickname:
                # Query User nickname, if not existing, change
                if not ub.session.query(ub.User).filter(
                        ub.User.nickname == to_save["nickname"]).scalar():
                    content.nickname = to_save["nickname"]
                else:
                    flash(_(u"This username is already taken"),
                          category="error")
                    return render_title_template(
                        "user_edit.html",
                        translations=translations,
                        languages=languages,
                        mail_configured=config.get_mail_server_configured(),
                        new_user=0,
                        content=content,
                        downloads=downloads,
                        registered_oauth=oauth_check,
                        title=_(u"Edit User %(nick)s", nick=content.nickname),
                        page="edituser")

            if "kindle_mail" in to_save and to_save[
                    "kindle_mail"] != content.kindle_mail:
                content.kindle_mail = to_save["kindle_mail"]
        try:
            ub.session.commit()
            flash(_(u"User '%(nick)s' updated", nick=content.nickname),
                  category="success")
        except IntegrityError:
            ub.session.rollback()
            flash(_(u"An unknown error occured."), category="error")
    return render_title_template(
        "user_edit.html",
        translations=translations,
        languages=languages,
        new_user=0,
        content=content,
        downloads=downloads,
        registered_oauth=oauth_check,
        mail_configured=config.get_mail_server_configured(),
        title=_(u"Edit User %(nick)s", nick=content.nickname),
        page="edituser")