comparison collect_issues.py @ 27:cdab667c6abb

Delete Code Duplication and Clean Up. * The search for the status does not require the "-1". * If the parameter you are looking for is not available in the tracker, an error message is issued and the program terminates, to avoid incorrect entries in the database
author Magnus Schieder <mschieder@intevation.de>
date Tue, 13 Nov 2018 21:04:22 +0100
parents 761ee2351f58
children e2864dabdb8c
comparison
equal deleted inserted replaced
26:761ee2351f58 27:cdab667c6abb
19 import urllib.request 19 import urllib.request
20 import csv 20 import csv
21 import io 21 import io
22 import sqlite3 as db 22 import sqlite3 as db
23 import os 23 import os
24 import sys
24 25
25 26
26 # Getting all priority in their order. 27 # Getting all priority in their order.
27 CHECK_ROUNDUP_ORDER_PRIO = "priority?@action=export_csv&@columns=id,order" 28 CHECK_ROUNDUP_ORDER_PRIO = "priority?@action=export_csv&@columns=id,order"
28 # Getting all statuses in their order. 29 # Getting all statuses in their order.
29 CHECK_ROUNDUP_ORDER_STATUS = "status?@action=export_csv&@columns=id,order" 30 CHECK_ROUNDUP_ORDER_STATUS = "status?@action=export_csv&@columns=id,order"
31
30 # Getting keywords and their ids. 32 # Getting keywords and their ids.
31 CHECK_KEYWORD_ORDER = "keyword?@action=export_csv&@columns=id,name" 33 CHECK_KEYWORD_VALUES = "keyword?@action=export_csv&@columns=id,name"
32 # Getting states and their ids. 34 # Getting states and their ids.
33 CHECK_ROUNDUP_SEARCH_VALUES = "status?@action=export_csv&@columns=id,name&@filter=open&open=1" 35 CHECK_STATUS_VALUES = "status?@action=export_csv&@columns=id,name"
36
34 # Getting the priority of each issue with the filter status and keywords 37 # Getting the priority of each issue with the filter status and keywords
35 SEARCH_ROUNDUP_PRIO = "issue?@action=export_csv&@columns=priority&@filter=status,keyword&@pagesize=500&@startwith=0&status={search_values}&keyword={keyword_values}" 38 SEARCH_ROUNDUP_PRIO = "issue?@action=export_csv&@columns=priority&@filter=status,keyword&@pagesize=500&@startwith=0&status={search_values}&keyword={keyword_values}"
36 # Getting the status of each issue with the filter keywords 39 # Getting the status of each issue with the filter keywords
37 SEARCH_ROUNDUP_STATUS = "issue?@action=export_csv&@columns=status&@filter=keyword&@pagesize=500&@startwith=0&keyword={keyword_values}" 40 SEARCH_ROUNDUP_STATUS = "issue?@action=export_csv&@columns=status&@filter=keyword&@pagesize=500&@startwith=0&keyword={keyword_values}"
38 41
71 if cur: 74 if cur:
72 cur.close() 75 cur.close()
73 if con: 76 if con:
74 con.close() 77 con.close()
75 78
76 def get_keyword_ids(opener, baseurl, keywords): 79 def get_ids(opener, baseurl, parameter, url):
77 if keywords == [""]: 80 if parameter == [""]:
78 return "" 81 return ""
79 82
80 keywords_csv = get_csv_from_server(opener, baseurl, CHECK_KEYWORD_ORDER) 83 parameter_csv = get_csv_from_server(opener, baseurl, url)
81 keywords_dict = {} 84 parameter_dict = {}
82 for x in keywords_csv: 85 for x in parameter_csv:
83 keywords_dict[x["name"]] = x["id"] 86 parameter_dict[x["name"]] = x["id"]
84 87
85 keywords_ids = [] 88 parameter_ids = []
86 for x in keywords: 89 for x in parameter:
87 keywords_ids.append(keywords_dict[x]) 90 if x not in parameter_dict:
91 print('The parameter "%s" does not exist in the tracker.' % x)
92 sys.exit(0)
88 93
89 return ",".join(keywords_ids) 94 parameter_ids.append(parameter_dict[x])
90 95
91 def get_status_ids(opener, baseurl, status): 96 return ",".join(parameter_ids)
92
93 if status == [""]:
94 return ""
95
96 status_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_SEARCH_VALUES)
97
98 status_dict = {}
99 for x in status_csv:
100 status_dict[x["name"]] = x["id"]
101
102 staus_ids = ["-1"]
103 for x in status:
104 staus_ids.append(status_dict[x])
105
106 return ",".join(staus_ids)
107 97
108 98
109 def issues_to_quantities(issue_csv, columns, orders_csv): 99 def issues_to_quantities(issue_csv, columns, orders_csv):
110 """Count issues per priority. 100 """Count issues per priority.
111 101
158 def save_stats_in_db(search, login_parmeters, baseurl, db_file, columns, sql_create_db, sql_insert_in_db, keywords, status, include_no_prio=False): 148 def save_stats_in_db(search, login_parmeters, baseurl, db_file, columns, sql_create_db, sql_insert_in_db, keywords, status, include_no_prio=False):
159 try: 149 try:
160 150
161 opener = connect_to_server(login_parmeters, baseurl) 151 opener = connect_to_server(login_parmeters, baseurl)
162 152
163 keywords_ids_url = get_keyword_ids(opener, baseurl, keywords) 153 keywords_ids_url = get_ids(opener, baseurl, keywords, CHECK_KEYWORD_VALUES)
164 154
165 if search == "prio": 155 if search == "prio":
166 order_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_ORDER_PRIO) 156 order_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_ORDER_PRIO)
167 status_ids_url = get_status_ids(opener, baseurl, status) 157 status_ids_url = get_ids(opener, baseurl, status, CHECK_STATUS_VALUES)
168 formated_search_url = SEARCH_ROUNDUP_PRIO.format(search_values=status_ids_url, keyword_values=keywords_ids_url) 158 formated_search_url = SEARCH_ROUNDUP_PRIO.format(search_values=status_ids_url, keyword_values=keywords_ids_url)
169 elif search == "status": 159 elif search == "status":
170 order_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_ORDER_STATUS) 160 order_csv = get_csv_from_server(opener, baseurl, CHECK_ROUNDUP_ORDER_STATUS)
171 formated_search_url = SEARCH_ROUNDUP_STATUS.format(keyword_values=keywords_ids_url) 161 formated_search_url = SEARCH_ROUNDUP_STATUS.format(keyword_values=keywords_ids_url)
172 162
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)