diff --git a/.gitignore b/.gitignore index 0ce14757..f06dcd44 100644 --- a/.gitignore +++ b/.gitignore @@ -7,9 +7,11 @@ __pycache__/ # Distribution / packaging .Python env/ +venv/ eggs/ dist/ build/ +vendor/ .eggs/ *.egg-info/ .installed.cfg @@ -19,16 +21,12 @@ build/ # calibre-web *.db *.log -config.ini -cps/static/[0-9]* .idea/ *.bak *.log.* -tags settings.yaml gdrive_credentials - -vendor client_secrets.json + diff --git a/cps/admin.py b/cps/admin.py index 50e0589d..c7f790ae 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -144,7 +144,10 @@ def configuration(): def view_configuration(): readColumn = db.session.query(db.Custom_Columns)\ .filter(and_(db.Custom_Columns.datatype == 'bool',db.Custom_Columns.mark_for_delete == 0)).all() + restrictColumns= db.session.query(db.Custom_Columns)\ + .filter(and_(db.Custom_Columns.datatype == 'text',db.Custom_Columns.mark_for_delete == 0)).all() return render_title_template("config_view_edit.html", conf=config, readColumns=readColumn, + restrictColumns=restrictColumns, title=_(u"UI Configuration"), page="uiconfig") @@ -160,7 +163,7 @@ def update_view_configuration(): _config_string("config_calibre_web_title") _config_string("config_columns_to_ignore") - _config_string("config_mature_content_tags") + # _config_string("config_mature_content_tags") reboot_required |= _config_string("config_title_regex") _config_int("config_read_column") @@ -169,6 +172,10 @@ def update_view_configuration(): _config_int("config_books_per_page") _config_int("config_authors_max") + _config_string("config_restricted_tags") + _config_int("config_restricted_column") + _config_string("config_restricted_column_value") + if config.config_google_drive_watch_changes_response: config.config_google_drive_watch_changes_response = json.dumps(config.config_google_drive_watch_changes_response) @@ -176,8 +183,8 @@ def update_view_configuration(): config.config_default_role &= ~constants.ROLE_ANONYMOUS config.config_default_show = sum(int(k[5:]) for k in to_save if k.startswith('show_')) - if "Show_mature_content" in to_save: - config.config_default_show |= constants.MATURE_CONTENT + '''if "Show_mature_content" in to_save: + config.config_default_show |= constants.MATURE_CONTENT''' config.save() flash(_(u"Calibre-Web configuration updated"), category="success") @@ -448,7 +455,7 @@ def new_user(): 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.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_')) @@ -490,7 +497,10 @@ def new_user(): else: content.role = config.config_default_role content.sidebar_view = config.config_default_show - content.mature_content = bool(config.config_default_show & constants.MATURE_CONTENT) + content.restricted_tags = config.config_restricted_tags + content.restricted_column = config.config_restricted_column + content.restricted_column_value = config.config_restricted_column_value + # content.mature_content = bool(config.config_default_show & constants.MATURE_CONTENT) return render_title_template("user_edit.html", new_user=1, content=content, translations=translations, languages=languages, title=_(u"Add new user"), page="newuser", registered_oauth=oauth_check) @@ -593,7 +603,13 @@ def edit_user(user_id): else: content.sidebar_view &= ~constants.DETAIL_RANDOM - content.mature_content = "Show_mature_content" in to_save + # content.mature_content = "Show_mature_content" in to_save + if "restricted_tags" in to_save: + content.restricted_tags = to_save["restricted_tags"] + if "config_restricted_column" in to_save: + content.restricted_tags = to_save["config_restricted_column"] + if "config_restricted_column_value" in to_save: + content.restricted_tags = to_save["config_restricted_column_value"] if "default_language" in to_save: content.default_language = to_save["default_language"] diff --git a/cps/config_sql.py b/cps/config_sql.py index 8ea8b978..cc1e09ec 100644 --- a/cps/config_sql.py +++ b/cps/config_sql.py @@ -57,7 +57,7 @@ class _Settings(_Base): config_authors_max = Column(Integer, default=0) config_read_column = Column(Integer, default=0) config_title_regex = Column(String, default=u'^(A|The|An|Der|Die|Das|Den|Ein|Eine|Einen|Dem|Des|Einem|Eines)\s+') - config_mature_content_tags = Column(String, default='') + # config_mature_content_tags = Column(String, default='') config_theme = Column(Integer, default=0) config_log_level = Column(SmallInteger, default=logger.DEFAULT_LOG_LEVEL) @@ -70,10 +70,13 @@ class _Settings(_Base): config_public_reg = Column(SmallInteger, default=0) config_remote_login = Column(Boolean, default=False) - config_default_role = Column(SmallInteger, default=0) config_default_show = Column(SmallInteger, default=6143) config_columns_to_ignore = Column(String) + config_restricted_tags = Column(String) + config_restricted_column = Column(SmallInteger, default=0) + config_restricted_column_value = Column(String) + config_allowed_column_value = Column(String) config_use_google_drive = Column(Boolean, default=False) config_google_drive_folder = Column(String) @@ -177,12 +180,12 @@ class _ConfigSQL(object): def show_detail_random(self): return self.show_element_new_user(constants.DETAIL_RANDOM) - def show_mature_content(self): - return self.show_element_new_user(constants.MATURE_CONTENT) + '''def show_mature_content(self): + return self.show_element_new_user(constants.MATURE_CONTENT)''' - def mature_content_tags(self): + '''def mature_content_tags(self): mct = self.config_mature_content_tags.split(",") - return [t.strip() for t in mct] + return [t.strip() for t in mct]''' def get_log_level(self): return logger.get_level_name(self.config_log_level) diff --git a/cps/helper.py b/cps/helper.py index e5e616d5..82322470 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -686,14 +686,36 @@ def common_filters(): lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) else: lang_filter = true() - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) - return and_(lang_filter, ~content_rating_filter) + negtags_list = current_user.list_restricted_tags() + postags_list = current_user.list_allowed_tags() + neg_content_tags_filter = false() if negtags_list == [''] else db.Books.tags.any(db.Tags.name.in_(negtags_list)) + pos_content_tags_filter = true() if postags_list == [''] else db.Books.tags.any(db.Tags.name.in_(postags_list)) + # db.session.query(db.Books).filter(db.Books.custom_column_5.any(db.cc_classes[5].value == 'nikto')).first() + # db.session.query(db.Books).filter( + # getattr(db.Books, 'custom_column_' + str(5)).any(db.cc_classes[5].value == 'nikto').first()) + if config.config_restricted_column: + pos_cc_list = current_user.allowed_column_value.split(',') + pos_content_cc_filter = true() if pos_cc_list == [''] else \ + getattr(db.Books, 'custom_column_' + str(config.config_restricted_column)).\ + any(db.cc_classes[config.config_restricted_column].value.in_(pos_cc_list)) + neg_cc_list = current_user.restricted_column_value.split(',') + neg_content_cc_filter = true() if neg_cc_list == [''] else \ + getattr(db.Books, 'custom_column_' + str(config.config_restricted_column)).\ + any(db.cc_classes[config.config_restricted_column].value.in_(neg_cc_list)) + else: + pos_content_cc_filter = true() + neg_content_cc_filter = false() + return and_(lang_filter, pos_content_tags_filter, ~neg_content_tags_filter, + pos_content_cc_filter, ~neg_content_cc_filter) + def tags_filters(): - return ~(false() if current_user.mature_content else \ - db.Tags.name.in_(config.mature_content_tags())) - # return db.session.query(db.Tags).filter(~content_rating_filter).order_by(db.Tags.name).all() + negtags_list = current_user.list_restricted_tags() + postags_list = current_user.list_allowed_tags() + neg_content_tags_filter = false() if negtags_list == [''] else db.Tags.name.in_(negtags_list) + pos_content_tags_filter = true() if postags_list == [''] else db.Tags.name.in_(postags_list) + return and_(pos_content_tags_filter, ~neg_content_tags_filter) + # return ~(false()) if postags_list == [''] else db.Tags.in_(postags_list) # Creates for all stored languages a translated speaking name in the array for the UI diff --git a/cps/templates/config_edit.html b/cps/templates/config_edit.html index 9556eef4..311d4a16 100644 --- a/cps/templates/config_edit.html +++ b/cps/templates/config_edit.html @@ -71,7 +71,7 @@ - + {% if show_back_button %}

@@ -316,11 +316,14 @@

+ {% endif %}
+ {% if not show_login_button %} + {% endif %} {% if show_back_button %} {{_('Back')}} {% endif %} diff --git a/cps/templates/config_view_edit.html b/cps/templates/config_view_edit.html index bc6defa4..155584a8 100644 --- a/cps/templates/config_view_edit.html +++ b/cps/templates/config_view_edit.html @@ -51,16 +51,19 @@ {% endfor %}
+
+ + +
-
- -
@@ -108,6 +111,14 @@ +
+ + +
+
+ + +
@@ -134,14 +145,9 @@ -
- - -
-
{{_('Back')}} diff --git a/cps/templates/user_edit.html b/cps/templates/user_edit.html index 5ace1eab..54fd683e 100644 --- a/cps/templates/user_edit.html +++ b/cps/templates/user_edit.html @@ -14,9 +14,9 @@
{% if ( g.user and g.user.role_passwd() or g.user.role_admin() ) and not content.role_anonymous() %} - {% if g.user and g.user.role_admin() and g.allow_registration and not new_user and not profile %} -
{{_('Reset user Password')}}
- {% else %} + {% if g.user and g.user.role_admin() and not new_user and not profile and ( mail_configured and content.email if content.email != None ) %} +
{{_('Reset user Password')}}
+ {% endif %}
@@ -39,7 +39,14 @@ {% endfor %}
- +
+ + +
+
+ + +
- {% if registered_oauth.keys()| length > 0 %} {% for id, name in registered_oauth.items() %}
@@ -61,9 +67,7 @@ {% endif %} {% endfor %}
- {% endif %} -
{% for element in sidebar %} {% if element['config_show'] %} @@ -87,10 +91,10 @@
{% endif %} -
+
diff --git a/cps/ub.py b/cps/ub.py index cf5d090c..ec69ca65 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -157,6 +157,27 @@ class UserBase: def show_detail_random(self): return self.check_visibility(constants.DETAIL_RANDOM) + def list_restricted_tags(self): + # return [line in (line.strip("," ) for line in self.restricted_tags.split(",") if not line.startswith('~'))] + # return [line in (line.strip(",") for line in self.restricted_tags.split(",") if not line.startswith('~'))] + #return [p.strip(",") for p in self.restricted_tags.split(",") if not(p.startswith(starter))] + '''for line in self.restricted_tags.split(","): + if not line.startswith('~'): + continue''' + mct = self.restricted_tags.split(",") + return [t.strip() for t in mct] + + def list_allowed_tags(self): + # return [line in (line.strip("," ) for line in self.restricted_tags.split(",") if not line.startswith('~'))] + # return [line in (line.strip(",") for line in self.restricted_tags.split(",") if not line.startswith('~'))] + #return [p.strip(",") for p in self.restricted_tags.split(",") if not(p.startswith(starter))] + '''for line in self.restricted_tags.split(","): + if not line.startswith('~'): + continue''' + mct = self.allowed_tags.split(",") + return [t.strip() for t in mct] + + def __repr__(self): return '' % self.nickname @@ -178,7 +199,11 @@ class User(UserBase, Base): locale = Column(String(2), default="en") sidebar_view = Column(Integer, default=1) default_language = Column(String(3), default="all") - mature_content = Column(Boolean, default=True) + # mature_content = Column(Boolean, default=True) + restricted_tags = Column(String, default="") + allowed_tags = Column(String, default="") + restricted_column_value = Column(String, default="") + allowed_column_value = Column(String, default="") if oauth_support: @@ -212,11 +237,13 @@ class Anonymous(AnonymousUserMixin, UserBase): self.sidebar_view = data.sidebar_view self.default_language = data.default_language self.locale = data.locale - self.mature_content = data.mature_content + # self.mature_content = data.mature_content self.kindle_mail = data.kindle_mail - - # settings = session.query(config).first() - # self.anon_browse = settings.config_anonbrowse + self.restricted_tags = data.restricted_tags + self.allowed_tags = data.allowed_tags + # self.restricted_column = data.restricted_column + self.restricted_column_value = data.restricted_column_value + self.allowed_column_value = data.allowed_column_value def role_admin(self): return False @@ -370,11 +397,19 @@ def migrate_Database(session): 'side_autor': constants.SIDEBAR_AUTHOR, 'detail_random': constants.DETAIL_RANDOM}) session.commit() - try: + '''try: session.query(exists().where(User.mature_content)).scalar() except exc.OperationalError: conn = engine.connect() - conn.execute("ALTER TABLE user ADD column `mature_content` INTEGER DEFAULT 1") + conn.execute("ALTER TABLE user ADD column `mature_content` INTEGER DEFAULT 1")''' + try: + session.query(exists().where(User.restricted_tags)).scalar() + except exc.OperationalError: # Database is not compatible, some columns are missing + conn = engine.connect() + conn.execute("ALTER TABLE user ADD column `restricted_tags` String DEFAULT ''") + conn.execute("ALTER TABLE user ADD column `allowed_tags` String DEFAULT ''") + conn.execute("ALTER TABLE user ADD column `restricted_column_value` DEFAULT ''") + conn.execute("ALTER TABLE user ADD column `allowed_column_value` DEFAULT ''") if session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS).first() is None: create_anonymous_user(session) try: diff --git a/cps/web.py b/cps/web.py index 995ed13c..90bc1c8a 100644 --- a/cps/web.py +++ b/cps/web.py @@ -229,7 +229,6 @@ def edit_required(f): return inner - # ################################### Helper functions ################################################################ @@ -1089,7 +1088,7 @@ def register(): content.password = generate_password_hash(password) content.role = config.config_default_role content.sidebar_view = config.config_default_show - content.mature_content = bool(config.config_default_show & constants.MATURE_CONTENT) + #content.mature_content = bool(config.config_default_show & constants.MATURE_CONTENT) try: ub.session.add(content) ub.session.commit() @@ -1290,8 +1289,8 @@ def profile(): current_user.password = generate_password_hash(to_save["password"]) if "kindle_mail" in to_save and to_save["kindle_mail"] != current_user.kindle_mail: current_user.kindle_mail = to_save["kindle_mail"] - if "kobo_user_key" in to_save and to_save["kobo_user_key"]: - current_user.kobo_user_key_hash = generate_password_hash(to_save["kobo_user_key"]) + if "allowed_tags" in to_save and to_save["allowed_tags"] != current_user.allowed_tags: + current_user.allowed_tags = to_save["allowed_tags"].strip() if to_save["email"] and to_save["email"] != current_user.email: if config.config_public_reg and not check_valid_domain(to_save["email"]): flash(_(u"E-mail is not from valid domain"), category="error") @@ -1329,7 +1328,7 @@ def profile(): if "Show_detail_random" in to_save: current_user.sidebar_view += constants.DETAIL_RANDOM - current_user.mature_content = "Show_mature_content" in to_save + #current_user.mature_content = "Show_mature_content" in to_save try: ub.session.commit()