From d9adb4fc94f80bed5e6d6d7ecf9173ac524b738b Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Wed, 1 Jan 2020 17:26:47 +0100 Subject: [PATCH] Improvements for limit tags --- cps/admin.py | 115 ++++++++++++++++++++++++++- cps/config_sql.py | 20 +++-- cps/server.py | 2 +- cps/static/js/table.js | 76 ++++++++++++++++++ cps/templates/book_edit.html | 2 - cps/templates/config_view_edit.html | 22 ++++- cps/templates/layout.html | 2 +- cps/templates/modal_restriction.html | 36 +++++++++ cps/templates/user_edit.html | 9 +++ cps/ub.py | 19 ++--- 10 files changed, 276 insertions(+), 27 deletions(-) create mode 100644 cps/templates/modal_restriction.html diff --git a/cps/admin.py b/cps/admin.py index 1caee104..85d5eb7d 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -207,7 +207,6 @@ def edit_domain(allow): # value: 'superuser!' //new value vals = request.form.to_dict() answer = ub.session.query(ub.Registration).filter(ub.Registration.id == vals['pk']).first() - # domain_name = request.args.get('domain') answer.domain = vals['value'].replace('*', '%').replace('?', '_').lower() ub.session.commit() return "" @@ -252,6 +251,118 @@ def list_domain(allow): response.headers["Content-Type"] = "application/json; charset=utf-8" return response +@admi.route("/ajax/editrestriction/", methods=['POST']) +@login_required +@admin_required +def edit_restriction(type): + element = request.form.to_dict() + if element['id'].startswith('a'): + if type == 0: # Tags as template + elementlist = config.list_restricted_tags() + elementlist[id[1:]]=element['Element'] + config.config_restricted_tags = ','.join(elementlist) + if type == 1: # CustomC + pass + if type == 2: # Tags per user + pass + if element['type'].startswith('d'): + if type == 0: # Tags as template + elementlist = config.list_allowed_tags() + elementlist[id[1:]]=element['Element'] + config.config_restricted_tags = ','.join(elementlist) + if type == 1: # CustomC + pass + if type == 2: # Tags per user + pass + config.save() + return "" + + +@admi.route("/ajax/addrestriction/", methods=['POST']) +@login_required +@admin_required +def add_restriction(type): + log.info("Hit: " + str(type)) + element = request.form.to_dict() + if type == 0: # Tags as template + if 'submit_allow' in element: + elementlist = config.list_allowed_tags() + if elementlist == ['']: + elementlist= [] + if not element['add_element'] in elementlist: + elementlist += [element['add_element']] + config.config_allowed_tags = ','.join(elementlist) + elif 'submit_deny' in element: + elementlist = config.list_restricted_tags() + if elementlist == ['']: + elementlist= [] + if not element['add_element'] in elementlist: + elementlist+=[element['add_element']] + config.config_restricted_tags = ','.join(elementlist) + config.save() + if type == 1: # CustomC + pass + if type == 2: # Tags per user + pass + return "" + + +@admi.route("/ajax/deleterestriction/", methods=['POST']) +@login_required +@admin_required +def delete_restriction(type): + element = request.form.to_dict() + if int(element['type']) == 1: + if type == 0: # Tags as template + if element['id'].startswith('a'): + elementlist = config.list_allowed_tags() + if element['Element'] in elementlist: + elementlist.remove(element['Element']) + config.config_allowed_tags = ','.join(elementlist) + elif element['id'].startswith('d'): + elementlist = config.list_restricted_tags() + if element['Element'] in elementlist: + elementlist.remove(element['Element']) + config.config_restricted_tags = ','.join(elementlist) + config.save() + if type == 1: # CustomC + pass + if type == 2: # Tags per user + pass + if int(element['type'])== 2: + if type == 0: # Tags as template + elementlist = config.list_allowed_tags() + if not element['Element'] in elementlist: + elementlist+=element['Element'] + config.config_restricted_tags = ','.join(elementlist) + if type == 1: # CustomC + pass + if type == 2: # Tags per user + pass + return "" + + +@admi.route("/ajax/listrestriction/") +@login_required +@admin_required +def list_restriction(type): + if type == 0: # Tags as template + #for x, i in enumerate(config.list_restricted_tags()): + # if x != '': + # {'Element': x, 'type': '1', 'id': 'a' + str(i)} + restrict = [{'Element': x, 'type':'1', 'id': 'd'+str(i) } for i,x in enumerate(config.list_restricted_tags()) if x != '' ] + allow = [{'Element': x, 'type':'1', 'id': 'a'+str(i) } for i,x in enumerate(config.list_allowed_tags()) if x != ''] + json_dumps = restrict + allow + elif type == 1: # CustomC + json_dumps = "" + elif type == 2: # Tags per user + json_dumps = "" + else: + json_dumps = "" + js = json.dumps(json_dumps) + response = make_response(js.replace("'", '"')) + response.headers["Content-Type"] = "application/json; charset=utf-8" + return response @admi.route("/config", methods=["GET", "POST"]) @unconfigured @@ -306,7 +417,7 @@ def _configuration_update_helper(): reboot_required |= _config_string("config_certfile") if config.config_certfile and not os.path.isfile(config.config_certfile): return _configuration_result('Certfile location is not valid, please enter correct path', gdriveError) - + _config_string("config_server_url") _config_checkbox_int("config_uploading") diff --git a/cps/config_sql.py b/cps/config_sql.py index 5b837b28..404cea02 100644 --- a/cps/config_sql.py +++ b/cps/config_sql.py @@ -74,6 +74,7 @@ class _Settings(_Base): config_default_show = Column(SmallInteger, default=6143) config_columns_to_ignore = Column(String) config_restricted_tags = Column(String) + config_allowed_tags = Column(String) config_restricted_column = Column(SmallInteger, default=0) config_restricted_column_value = Column(String) config_allowed_column_value = Column(String) @@ -180,12 +181,21 @@ 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 list_restricted_tags(self): + mct = self.config_restricted_tags.split(",") + return [t.strip() for t in mct] - '''def mature_content_tags(self): - mct = self.config_mature_content_tags.split(",") - return [t.strip() for t in mct]''' + def list_allowed_tags(self): + mct = self.config_allowed_tags.split(",") + return [t.strip() for t in mct] + + def list_restricted_column_values(self): + mct = self.config_restricted_column_values().split(",") + return [t.strip() for t in mct] + + def list_allowed_column_values(self): + mct = self.config_allowed_column_values().split(",") + 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/server.py b/cps/server.py index d736c4c9..43792ecd 100755 --- a/cps/server.py +++ b/cps/server.py @@ -24,7 +24,7 @@ import signal import socket try: - from gevent.pyswsgi import WSGIServer + from gevent.pywsgi import WSGIServer from gevent.pool import Pool from gevent import __version__ as _version VERSION = 'Gevent ' + _version diff --git a/cps/static/js/table.js b/cps/static/js/table.js index 12c07102..7c94d072 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -93,6 +93,73 @@ $(function() { var domainId = $(e.relatedTarget).data("domain-id"); $(e.currentTarget).find("#btndeletedomain").data("domainId", domainId); }); + + $('#restrictModal').on('hidden.bs.modal', function () { + $('#restrict-elements-table').bootstrapTable('destroy'); + }); + // $('#table').bootstrapTable('destroy'); + function startTable(type){ + $("#restrict-elements-table").bootstrapTable({ + formatNoMatches: function () { + return ""; + }, + url:window.location.pathname + "/../../ajax/listrestriction/" + type, + onClickCell: function (field, value, row, $element) { + // ... + if(field == 3){ + $.ajax ({ + type: 'Post', + data: 'id=' + row.id + '&type=' + row.type + "&Element=" + row.Element, + url: window.location.pathname + "/../../ajax/deleterestriction/" + type, + async: true, + timeout: 900, + success:function(data) { + $.ajax({ + method:"get", + url: window.location.pathname + "/../../ajax/listrestriction/"+type, + async: true, + timeout: 900, + success:function(data) { + $("#restrict-elements-table").bootstrapTable("load", data); + } + }); + } + }); + } + }, + striped: false + }); + $("[id^=submit_]").click(function(event) { + event.preventDefault(); + console.log($(this)[0].name) + $.ajax({ + url: window.location.pathname + "/../../ajax/addrestriction/"+type, + type: 'Post', + data: $(this).closest("form").serialize() + "&" + $(this)[0].name + "=", + success: function () { + $.ajax ({ + method:"get", + url: window.location.pathname + "/../../ajax/listrestriction/"+type, + async: true, + timeout: 900, + success:function(data) { + $("#restrict-elements-table").bootstrapTable("load", data); + } + }); + } + }); + return; + }); + } + $('#get_column_values').on('click',function() + { + startTable(1); + }); + + $('#get_tags').on('click',function() + { + startTable(0); + }); }); /* Function for deleting domain restrictions */ @@ -104,3 +171,12 @@ function TableActions (value, row, index) { "" ].join(""); } + +/* Function for deleting domain restrictions */ +function RestrictionActions (value, row, index) { + return [ + "
", + "", + "
" + ].join(""); +} diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index e734698a..88369f05 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -1,8 +1,6 @@ {% extends "layout.html" %} {% block body %} {% if book %} - -
{{ book.title }} diff --git a/cps/templates/config_view_edit.html b/cps/templates/config_view_edit.html index 155584a8..141842fe 100644 --- a/cps/templates/config_view_edit.html +++ b/cps/templates/config_view_edit.html @@ -1,4 +1,8 @@ {% extends "layout.html" %} +{% block header %} + + +{% endblock %} {% block body %}

{{title}}

@@ -111,14 +115,17 @@
-
@@ -155,6 +162,9 @@ {% endblock %} +{% block modal %} +{{ restrict_modal() }} +{% endblock %} {% block js %} + + + + {% endblock %} diff --git a/cps/templates/layout.html b/cps/templates/layout.html index 9ffb04f8..f5fdd79c 100644 --- a/cps/templates/layout.html +++ b/cps/templates/layout.html @@ -1,3 +1,4 @@ +{% from 'modal_restriction.html' import restrict_modal %} @@ -229,7 +230,6 @@ }); }); - {% block js %}{% endblock %} diff --git a/cps/templates/modal_restriction.html b/cps/templates/modal_restriction.html new file mode 100644 index 00000000..cb352f7c --- /dev/null +++ b/cps/templates/modal_restriction.html @@ -0,0 +1,36 @@ +{% macro restrict_modal() %} + +{% endmacro %} diff --git a/cps/templates/user_edit.html b/cps/templates/user_edit.html index 2b0c5b36..77c78783 100644 --- a/cps/templates/user_edit.html +++ b/cps/templates/user_edit.html @@ -155,3 +155,12 @@ {% endif %} {% endblock %} +{% block modal %} +{{ restrict_modal() }} +{% endblock %} +{% block js %} + + + + +{% endblock %} diff --git a/cps/ub.py b/cps/ub.py index 19bfff22..a2fcd554 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -158,25 +158,20 @@ class UserBase: 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 list_restricted_column_values(self): + mct = self.restricted_column_values().split(",") + return [t.strip() for t in mct] + + def list_allowed_column_values(self): + mct = self.allowed_column_values().split(",") + return [t.strip() for t in mct] def __repr__(self): return '' % self.nickname