flask_tut   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 40
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A data() 0 23 1
A home() 0 4 1
A dt_110x() 0 4 1
A initdb() 0 15 2
1
from time import sleep
2
3
import click
4
from datatables import ColumnDT, DataTables
5
from flask import Flask, jsonify, render_template, request
6
from flask_tut.models import Address, User, db
7
8
app = Flask(__name__)
9
app.config.from_pyfile('../app.cfg')
10
db.init_app(app)
11
12
13
@app.cli.command()
14
def initdb():
15
    """Initialize the database."""
16
    click.echo('Init the db...')
17
    db.create_all()
18
19
    for i in range(30):
20
        click.echo("Creating user/address combo #{}...".format(i))
21
        address = Address(description='Address#2' + str(i).rjust(2, "0"))
22
        db.session.add(address)
23
        user = User(name='User#1' + str(i).rjust(2, "0"))
24
        user.address = address
25
        db.session.add(user)
26
        sleep(1)
27
    db.session.commit()
28
29
30
@app.route("/")
31
def home():
32
    """Try to connect to database, and list available examples."""
33
    return render_template('home.html', project='flask_tut')
34
35
36
@app.route("/dt_110x")
37
def dt_110x():
38
    """List users with DataTables <= 1.10.x."""
39
    return render_template('dt_110x.html', project='dt_110x')
40
41
42
@app.route('/data')
43
def data():
44
    """Return server side data."""
45
    # defining columns
46
    columns = [
47
        ColumnDT(User.id),
48
        ColumnDT(User.name),
49
        ColumnDT(Address.description),
50
        ColumnDT(User.created_at)
51
    ]
52
53
    # defining the initial query depending on your purpose
54
    query = db.session.query().select_from(User).join(Address).filter(
55
        Address.id > 14)
56
57
    # GET parameters
58
    params = request.args.to_dict()
59
60
    # instantiating a DataTable for the query and table needed
61
    rowTable = DataTables(params, query, columns)
62
63
    # returns what is needed by DataTable
64
    return jsonify(rowTable.output_result())
65