pyramid_tut.views   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 35.54 %

Importance

Changes 0
Metric Value
wmc 10
eloc 76
dl 43
loc 121
rs 10
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A dt_110x_advanced_column_search() 0 6 1
A dt_110x_basic_column_search() 0 6 1
A home() 0 13 2
A dt_110x() 0 4 1
A data_advanced() 17 17 1
A data() 0 17 1
A data_yadcf() 21 17 1
A dt_110x_custom_column() 0 6 1
A dt_110x_yadcf() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from pyramid.response import Response
2
from pyramid.view import view_config
3
from sqlalchemy import func
4
from sqlalchemy.exc import DBAPIError
5
6
from datatables import ColumnDT, DataTables
7
8
from .models import Address, DBSession, User
9
10
11
@view_config(route_name="home", renderer="templates/home.html")
12
def home(request):
13
    """Try to connect to database, and list available examples."""
14
    try:
15
        DBSession.query(User).first()
16
    except DBAPIError:
17
        return Response(
18
            conn_err_msg,
19
            content_type="text/plain",
20
            status_int=500,
21
        )
22
23
    return {"project": "pyramid_tut"}
24
25
26
@view_config(route_name="dt_110x", renderer="templates/dt_110x.html")
27
def dt_110x(request):
28
    """List users with DataTables >= 1.10.x."""
29
    return {"project": "dt_110x"}
30
31
32
@view_config(
33
    route_name="dt_110x_custom_column",
34
    renderer="templates/dt_110x_custom_column.html")
35
def dt_110x_custom_column(request):
36
    """Show a CRUD custom column."""
37
    return {"project": "dt_110x_custom_column"}
38
39
40
@view_config(
41
    route_name="dt_110x_basic_column_search",
42
    renderer="templates/dt_110x_basic_column_search.html")
43
def dt_110x_basic_column_search(request):
44
    """Text based per column search."""
45
    return {"project": "dt_110x_basic_column_search"}
46
47
48
@view_config(
49
    route_name="dt_110x_advanced_column_search",
50
    renderer="templates/dt_110x_advanced_column_search.html")
51
def dt_110x_advanced_column_search(request):
52
    """Advanced per column search."""
53
    return {"project": "dt_110x_advanced_column_search"}
54
55
56
@view_config(
57
    route_name="dt_110x_yadcf", renderer="templates/dt_110x_yadcf.html")
58
def dt_110x_yadcf(request):
59
    """Search with yadcf."""
60
    return {"project": "dt_110x_yadcf"}
61
62
63
@view_config(route_name="data", renderer="json")
64
def data(request):
65
    """Return server side data."""
66
    columns = [
67
        ColumnDT(User.id),
68
        ColumnDT(User.name),
69
        ColumnDT(Address.description),
70
        ColumnDT(func.strftime("%d-%m-%Y", User.birthday)),
71
        ColumnDT(User.age)
72
    ]
73
74
    query = DBSession.query().select_from(User).join(Address).filter(
75
        Address.id > 4)
76
77
    rowTable = DataTables(request.GET, query, columns)
78
79
    return rowTable.output_result()
80
81
82 View Code Duplication
@view_config(route_name="data_advanced", renderer="json_with_dates")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
83
def data_advanced(request):
84
    """Return server side data."""
85
    columns = [
86
        ColumnDT(User.id, search_method="numeric"),
87
        ColumnDT(User.name),
88
        ColumnDT(Address.description),
89
        ColumnDT(User.birthday, search_method="date"),
90
        ColumnDT(User.age, search_method="numeric")
91
    ]
92
93
    query = DBSession.query().select_from(User).join(Address).filter(
94
        Address.id > 4)
95
96
    rowTable = DataTables(request.GET, query, columns)
97
98
    return rowTable.output_result()
99
100
101 View Code Duplication
@view_config(route_name="data_yadcf", renderer="json_with_dates")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
102
def data_yadcf(request):
103
    """Return server side data."""
104
    columns = [
105
        ColumnDT(User.id, search_method="yadcf_range_number"),
106
        ColumnDT(User.name, search_method="yadcf_multi_select"),
107
        ColumnDT(Address.description, search_method="yadcf_autocomplete"),
108
        ColumnDT(User.birthday, search_method="yadcf_range_date"),
109
        ColumnDT(User.age, search_method="yadcf_range_number_slider")
110
    ]
111
112
    query = DBSession.query().select_from(User).join(Address).filter(
113
        Address.id > 4)
114
115
    rowTable = DataTables(request.GET, query, columns)
116
117
    return rowTable.output_result()
118
119
120
conn_err_msg = '''\
121
Pyramid is having a problem using your SQL database.  The problem
122
might be caused by one of the following things:
123
124
1.  You may need to run the "initialize_pyramid_tut_db" script
125
    to initialize your database tables.  Check your virtual
126
    environment's "bin" directory for this script and try to run it.
127
128
2.  Your database server may not be running.  Check that the
129
    database server referred to by the "sqlalchemy.url" setting in
130
    your "development.ini" file is running.
131
132
After you fix the problem, please restart the Pyramid application to
133
try it again.
134
'''
135