Completed
Push — master ( 0bff3d...b87908 )
by Glenn
46s
created

order_sprint_results()   B

Complexity

Conditions 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
dl 0
loc 22
rs 7.7857
1
from skf.database import db
2
from sqlalchemy import asc, desc
3
from skf.database.groupmembers import groupmembers
4
from skf.database.project_sprints import project_sprints 
5
from skf.database.checklists_results import checklists_results 
6
from skf.api.security import log, val_num, val_alpha_num, val_alpha_num_special
0 ignored issues
show
Unused Code introduced by blabla1337
Unused val_alpha_num imported from skf.api.security
Loading history...
7
8
9
def get_sprint_item(sprint_id, user_id):
10
    log("User requested specific sprint item", "MEDIUM", "PASS")
11
    val_num(sprint_id)
12
    val_num(user_id)
13
    result = project_sprints.query.filter(project_sprints.sprintID == sprint_id).one()
14
    return result
15
16
17
def get_sprint_results(sprint_id, user_id):
18
    log("User requested specific sprint items", "MEDIUM", "PASS")
19
    val_num(sprint_id)
20
    val_num(user_id)
21
    result = checklists_results.query.filter(checklists_results.sprintID == sprint_id).group_by(checklists_results.checklistID).order_by(asc(checklists_results.status)).paginate(1, 500, False)
22
    return order_sprint_results(result)
23
24
25
def get_sprint_results_audit(sprint_id, user_id):
26
    log("User requested specific sprint audit items", "MEDIUM", "PASS")
27
    val_num(sprint_id)
28
    val_num(user_id)
29
    result = checklists_results.query.filter(checklists_results.sprintID == sprint_id).filter(checklists_results.status == 5).group_by(checklists_results.checklistID).paginate(1, 500, False)
30
    return order_sprint_results(result)
31
32
33
def delete_sprint(sprint_id, user_id):
34
    log("User deleted sprint", "MEDIUM", "PASS")
35
    val_num(sprint_id)
36
    val_num(user_id)
37
    result = (project_sprints.query.filter(project_sprints.sprintID == sprint_id).one())
38
    db.session.delete(result)
39
    db.session.commit()
40
    return {'message': 'Sprint successfully deleted'}
41
42
43
def update_sprint(sprint_id, user_id, data):
44
    log("User updated sprint", "MEDIUM", "PASS")
45
    val_num(sprint_id)
46
    val_num(user_id)
47
    sprint = project_sprints.query.filter(project_sprints.sprintID == sprint_id).one()
48
    val_alpha_num_special(data.get('name'))
49
    val_alpha_num_special(data.get('description'))
50
    sprint.sprintName = data.get('name')
51
    sprint.sprintDesc = data.get('description')
52
    db.session.add(sprint) 
53
    db.session.commit()
54
    return {'message': 'Sprint successfully updated'}
55
56
57
def new_sprint(user_id, data):
58
    log("User created new sprint", "MEDIUM", "PASS")
59
    val_alpha_num_special(data.get('name'))
60
    val_alpha_num_special(data.get('description'))
61
    val_num(data.get('projectID'))
62
    sprintName = data.get('name')
63
    sprintDesc = data.get('description')
64
    projectID = data.get('projectID')
65
    groupmember = groupmembers.query.filter(groupmembers.userID == user_id).one()
66
    groupID = groupmember.groupID
67
    sprintAdd = project_sprints(sprintName, sprintDesc, groupID, projectID)
68
    db.session.add(sprintAdd)
69
    db.session.commit()
70
    result = project_sprints.query.filter(project_sprints.groupID == groupID).order_by(desc(project_sprints.sprintID)).first()
71
    return {'sprintID': result.sprintID, 'message': 'Sprint successfully created'}
72
73
74
def stats_sprint(project_id):
75
    log("User requested specific project sprint stats", "MEDIUM", "PASS")
76
    val_num(project_id)
77
    sprint_info = (project_sprints.query.filter(project_sprints.projectID == project_id).all())
78
    sprint = []
79
    for result in sprint_info:
80
        sprint_id = result.sprintID
81
        sprint_desc = result.sprintDesc
82
        sprint_name = result.sprintName
83
        sprint_open = (checklists_results.query.filter(checklists_results.sprintID == sprint_id).filter(checklists_results.status == 1).group_by(checklists_results.checklistID).group_by(checklists_results.checklistID).count())
84
        sprint_closed = (checklists_results.query.filter(checklists_results.sprintID == sprint_id).filter(checklists_results.status == 2).group_by(checklists_results.checklistID).group_by(checklists_results.checklistID).count())
85
        sprint_accepted = (checklists_results.query.filter(checklists_results.sprintID == sprint_id).filter(checklists_results.status == 3).group_by(checklists_results.checklistID).group_by(checklists_results.checklistID).count())
86
        sprint_sec_ack = (checklists_results.query.filter(checklists_results.sprintID == sprint_id).filter(checklists_results.status == 4).group_by(checklists_results.checklistID).group_by(checklists_results.checklistID).count())
87
        sprint_sec_fail = (checklists_results.query.filter(checklists_results.sprintID == sprint_id).filter(checklists_results.status == 5).group_by(checklists_results.checklistID).group_by(checklists_results.checklistID).count())
88
        total = sprint_open + sprint_closed + sprint_accepted + sprint_sec_ack + sprint_sec_fail
89
        sprint.append({'sprint_id': sprint_id, 'sprint_desc': sprint_desc, 'sprint_name': sprint_name, 'sprint_open': sprint_open, 'sprint_closed': sprint_closed, 'sprint_accepted': sprint_accepted, 'sprint_sec_ack': sprint_sec_ack, 'sprint_sec_fail': sprint_sec_fail, 'sprint_items_total': total})
90
    return sprint
91
92
93
def order_sprint_results(sprint_results):
94
    ordered_list = []
95
    ordered_closed = [] # 2
96
    ordered_accepted = [] # 3
97
    ordered_verified = [] # 4
98
    ordered_failed = [] # 5
99
    for item in sprint_results.items:
100
        numbers = item.checklistID.split('.')
101
        category = int(numbers[0])
102
        category_requirement = int(numbers[1])
103
        if (item.status == 1):
104
            ordered_list = insertInOrder(category, category_requirement, item, ordered_list)
105
        elif (item.status == 2):
106
            ordered_closed = insertInOrder(category, category_requirement, item, ordered_closed)
107
        elif (item.status == 3):
108
            ordered_accepted = insertInOrder(category, category_requirement, item, ordered_accepted)
109
        elif (item.status == 4):
110
            ordered_verified = insertInOrder(category, category_requirement, item, ordered_verified)
111
        else:
112
            ordered_failed = insertInOrder(category, category_requirement, item, ordered_failed)
113
    sprint_results.items = ordered_list + ordered_closed + ordered_accepted + ordered_verified + ordered_failed
114
    return sprint_results
115
116
117
def insertInOrder(category, category_requirement, item, status_list):
118
    if (len(status_list) == 0):
119
        status_list.append(item)
120
    else:
121
        y = 0
122
        while y < len(status_list):
123
            numbers_ordered = status_list[y].checklistID.split('.')
124
            category_ordered = int(numbers_ordered[0])
125
            category_requirement_ordered = int(numbers_ordered[1])
126
            if (category < category_ordered):
127
                status_list.insert(y, item)
128
                break
129
            else:
130
                if (category == category_ordered):
131
                    if (category_requirement < category_requirement_ordered):
132
                        status_list.insert(y, item)
133
                        break
134
            y = y + 1
135
        if (y == len(status_list)):
136
            status_list.insert(y, item)
137
    return status_list
138
139