Completed
Push — master ( 86dfcb...7c8092 )
by -
01:41
created

plain()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.7462
Metric Value
dl 0
loc 8
ccs 3
cts 7
cp 0.4286
rs 9.4285
cc 2
crap 2.7462
1 1
from time import time, localtime, strftime
2
3 1
from flask import current_app, Blueprint, render_template, request, redirect, flash, Response, url_for
4
5 1
from spike.model import db
6 1
from spike.model.naxsi_whitelistsets import NaxsiWhitelistSets
7 1
from spike.model.naxsi_whitelist import NaxsiWhitelist
8
9
10 1
whitelistsets = Blueprint('whitelistsets', __name__)
11
12
13 1
@whitelistsets.route("/")
14
def index():
15
    _wlset = NaxsiWhitelistSets.query.order_by(NaxsiWhitelistSets.name).all()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelistSets does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
16
    return render_template("whitelistsets/index.html", whitelistsets=_wlset)
17
18
19 1
@whitelistsets.route("/plain/")
20 1
@whitelistsets.route("/plain/<int:wid>")
21 1
def plain(wid=0):
22
    if not wid:
23
        out = ''.join(map(__get_whitelist_for_whitelistset, NaxsiWhitelistSets.query.all()))
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelistSets does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
24
    else:
25
        out = __get_whitelist_for_whitelistset(NaxsiWhitelistSets.query.filter(NaxsiWhitelistSets.id == wid).first())
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelistSets does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
26
    return Response(out, mimetype='text/plain')
27
28
29 1
@whitelistsets.route("/new", methods=["POST"])
30
def new():
31
    wname = request.form["wname"].strip().upper()
32
33
    if NaxsiWhitelistSets.query.filter(NaxsiWhitelistSets.name == wname).first():
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelistSets does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
34
        flash("ERROR, ruleset exists: %s " % wname, "error")
35
        return redirect(url_for("whitelistsets.index"))
36
37
    db.session.add(NaxsiWhitelistSets(wname, "naxsi-whitelistset: %s" % wname, int(time())))
38
    db.session.commit()
39
40
    flash("OK created: %s " % wname, "success")
41
    return redirect(url_for("whitelistsets.index"))
42
43
44 1
@whitelistsets.route("/view/<int:wid>")
45
def view(wid):
46
    ruleset = NaxsiWhitelistSets.query.filter(NaxsiWhitelistSets.id == wid).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelistSets does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
47
    return render_template("rulesets/view.html", r=ruleset, rout=__get_whitelist_for_whitelistset(ruleset))
48
49 1
@whitelistsets.route("/select/<string:selector>", methods=["GET"])
50
def select(selector):
51
    _wlset = NaxsiWhitelist.query.filter(NaxsiWhitelist.whitelistset == selector).order_by(NaxsiWhitelist.wid.desc()).all()
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (123/120).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
52
    _selection = "Search wid: %s " % selector
53
    return render_template("whitelistsets/index.html", whitelistsets=_wlset, selection=_selection)
54
55
56 1
@whitelistsets.route("/del/<int:wid>", methods=["POST"])
57
def remove(wid):
58
    _wlset = NaxsiWhitelistSets.query.filter(NaxsiWhitelistSets.id == wid).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelistSets does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
59
    if _wlset is None:
60
        flash("ERROR, ruleset doesn't exists: %s " % wid, "error")
61
        return redirect(url_for("whitelistsets.index"))
62
63
    db.session.delete(_wlset)
64
    db.session.commit()
65
66
    flash("OK deleted: %s " % wid, "success")
67
    return redirect(url_for("whitelistsets.index"))
68
69
70 1
def __get_whitelist_for_whitelistset(whitelistset):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Naming introduced by
The name __get_whitelist_for_whitelistset does not conform to the function naming conventions ([a-z_][a-z0-9_]{1,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
71
    if not whitelistset:
72
        return ''
73
74
    _rules = NaxsiWhitelist.query.filter(
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
75
        NaxsiWhitelist.whitelistset == whitelistset.name,
76
        NaxsiWhitelist.active == 1
77
    ).all()
78
79
    nxruleset = NaxsiWhitelistSets.query.filter(NaxsiWhitelistSets.name == whitelistset.name).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelistSets does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
80
    db.session.add(nxruleset)
81
    db.session.commit()
82
83
    text_rules = ''.join([r.fullstr() for r in _rules])
84
85
    header = current_app.config["RULESET_HEADER"]
86
    header = header.replace("RULESET_DESC", whitelistset.name)
87
    header = header.replace("RULESET_DATE", strftime("%F - %H:%M", localtime(time())))
88
89
    return header + text_rules
90