Passed
Branch master (e737b9)
by jvo
01:27
created

view()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.1852
Metric Value
cc 2
dl 0
loc 6
ccs 2
cts 6
cp 0.3333
crap 3.1852
rs 9.4285
1 1
from time import time, localtime, strftime
2
3 1
from flask import current_app, Blueprint, render_template, request, redirect, flash, Response
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (93/90).

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

Loading history...
Configuration introduced by
The import flask could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
5 1
from spike.model import db
6 1
from spike.model.naxsi_rules import NaxsiRules
7 1
from spike.model.naxsi_rulesets import NaxsiRuleSets
8
9 1
from rules import __get_textual_representation_rule
0 ignored issues
show
Configuration introduced by
The import rules could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
10
11 1
rulesets = Blueprint('rulesets', __name__)
0 ignored issues
show
Coding Style Naming introduced by
The name rulesets does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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...
12
13
14 1
@rulesets.route("/")
15
def index():
16 1
    _rulesets = NaxsiRuleSets.query.order_by(NaxsiRuleSets.name).all()
17 1
    return render_template("/rulesets/index.html", rulesets=_rulesets)
18
19
20 1
@rulesets.route("/plain/")
21 1
@rulesets.route("/plain/<int:rid>")
22 1
def plain(rid=0):
23
    """
24
    Show the rule `rid` in plain text
25
    :param int rid: Rule id
26
    """
27 1
    if not rid:
28 1
        out = ''.join(map(__get_rules_for_ruleset, NaxsiRuleSets.query.all()))
29
    else:
30 1
        out = __get_rules_for_ruleset(NaxsiRuleSets.query.filter(NaxsiRuleSets.id == rid).first())
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (98/90).

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

Loading history...
31 1
    return Response(out, mimetype='text/plain')
32
33
34 1
@rulesets.route("/view/<int:rid>")
35 1
def view(rid=0):
36
    if not rid:
37
        return redirect("/rulesets/")
38
    ruleset = NaxsiRuleSets.query.filter(NaxsiRuleSets.id == rid).first()
39
    return render_template("/rulesets/view.html", r=ruleset, rout=__get_rules_for_ruleset(ruleset))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (99/90).

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

Loading history...
40
41
42 1
@rulesets.route("/new", methods=["POST"])
43
def new():  # TODO filter parameter
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
44 1
    rname = request.form["rname"].strip().upper()
45
46 1
    if NaxsiRuleSets.query.filter(NaxsiRuleSets.name == rname).first():
47 1
        flash("ERROR, ruleset exists: %s " % rname, "error")
48 1
        return redirect("/rulesets/")
49
50
    db.session.add(NaxsiRuleSets(rname, "naxsi-ruleset: %s" % rname, int(time())))
51
    db.session.commit()
52
53
    flash("OK created: %s " % rname, "success")
54
    return redirect("/rulesets/")
55
56
57 1
def __get_rules_for_ruleset(ruleset, with_header=True):
58 1
    _rules = NaxsiRules.query.filter(
59
        NaxsiRules.ruleset == ruleset.name,
60
        NaxsiRules.active == 1
61
    ).all()
62
63 1
    nxruleset = NaxsiRuleSets.query.filter(NaxsiRuleSets.name == ruleset.name).first()
64 1
    db.session.add(nxruleset)
65 1
    db.session.commit()
66 1
    text_rules = ''.join(map(__get_textual_representation_rule, _rules))
67
68 1
    if with_header is False:
69
        return text_rules
70
71 1
    header = current_app.config["RULESET_HEADER"]
72 1
    header = header.replace("RULESET_DESC", ruleset.name)
73 1
    header = header.replace("RULESET_DATE", strftime("%F - %H:%M", localtime(time())))
74
75
    return header + text_rules
76