Completed
Push — master ( da0207...5a5796 )
by Mathias
01:37
created

cdr_search_admin_form_fun()   F

Complexity

Conditions 15

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 44
rs 2.7451
cc 15

How to fix   Complexity   

Complexity

Complex classes like cdr_search_admin_form_fun() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# Copyright 2013 Mathias WOLFF
2
# This file is part of pyfreebilling.
3
# 
4
# pyfreebilling is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
# 
9
# pyfreebilling is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with pyfreebilling.  If not, see <http://www.gnu.org/licenses/>
16
17
from django.conf import settings
18
from common.common_functions import variable_value, ceil_strdate
19
from datetime import datetime
20
21
22
def cdr_record_common_fun(request):
23
    """Return Form with Initial data or Array (kwargs) for Voipcall_Report
24
Changelist_view"""
25
    start_date = ''
26
    end_date = ''
27
    if request.POST.get('from_date'):
28
        from_date = request.POST.get('from_date')
29
        start_date = ceil_strdate(from_date, 'start')
30
31
    if request.POST.get('to_date'):
32
        to_date = request.POST.get('to_date')
33
        end_date = ceil_strdate(to_date, 'end')
34
35
    # Assign form field value to local variable
36
    disposition = variable_value(request, 'status')
37
    campaign_id = variable_value(request, 'campaign')
38
39
    kwargs = {}
40
    if start_date and end_date:
41
        kwargs['starting_date__range'] = (start_date, end_date)
42
    if start_date and end_date == '':
43
        kwargs['starting_date__gte'] = start_date
44
    if start_date == '' and end_date:
45
        kwargs['starting_date__lte'] = end_date
46
47
    if disposition and disposition != 'all':
48
        kwargs['disposition__exact'] = disposition
49
50
    if campaign_id and campaign_id != '0':
51
        kwargs['callrequest__campaign_id'] = campaign_id
52
53
    if len(kwargs) == 0:
54
        tday = datetime.today()
55
        kwargs['starting_date__gte'] = datetime(tday.year,
56
                                                tday.month,
57
                                                tday.day, 0, 0, 0, 0)
58
        kwargs['starting_date__lte'] = datetime(tday.year,
59
                                                tday.month,
60
                                                tday.day, 23, 59, 59)
61
    return kwargs
62
63
64
def return_query_string(query_string, para):
65
    """
66
Function is used in voipcall_search_admin_form_fun
67
68
>>> return_query_string('key=1', 'key_val=apple')
69
'key=1&key_val=apple'
70
>>> return_query_string(False, 'key_val=apple')
71
'key_val=apple'
72
"""
73
    if query_string:
74
        query_string += '&%s' % (para)
75
    else:
76
        query_string = para
77
    return query_string
78
79
80
def cdr_search_admin_form_fun(request):
81
    """Return query string for Voipcall_Report Changelist_view"""
82
    start_date = ''
83
    end_date = ''
84
    if request.POST.get('from_date'):
85
        start_date = request.POST.get('from_date')
86
87
    if request.POST.get('to_date'):
88
        end_date = request.POST.get('to_date')
89
90
    # Assign form field value to local variable
91
    disposition = variable_value(request, 'status')
92
    campaign_id = variable_value(request, 'campaign')
93
    query_string = ''
94
95
    if start_date and end_date:
96
        date_string = 'starting_date__gte=' + start_date + \
97
            '&starting_date__lte=' + end_date + '+23%3A59%3A59'
98
        query_string = return_query_string(query_string, date_string)
99
100
    if start_date and end_date == '':
101
        date_string = 'starting_date__gte=' + start_date
102
        query_string = return_query_string(query_string, date_string)
103
104
    if start_date == '' and end_date:
105
        date_string = 'starting_date__lte=' + end_date
106
        query_string = return_query_string(query_string, date_string)
107
108
    if disposition and disposition != 'all':
109
        disposition_string = 'disposition__exact=' + disposition
110
        query_string = return_query_string(query_string, disposition_string)
111
112
    if campaign_id and campaign_id != '0':
113
        campaign_string = 'callrequest__campaign_id=' + str(campaign_id)
114
        query_string = return_query_string(query_string, campaign_string)
115
116
    if start_date == '' and end_date == '':
117
        tday = datetime.today()
118
        end_date = start_date = tday.strftime("%Y-%m-%d")
119
        date_string = 'starting_date__gte=' + start_date + \
120
            '&starting_date__lte=' + end_date + '+23%3A59%3A59'
121
        query_string = return_query_string(query_string, date_string)
122
123
    return query_string
124
125
126