From a81e6f80e6fcd1211ccd14c4b0ccd3a0d852dd30 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Fri, 10 Jan 2020 17:52:56 -0700 Subject: [PATCH] Adjusted response formatting and added add_note() * Test-Station name *should* be included in all posts * Ticket ID and name included in all posts * Optional osTicket note included in all posts --- scripts/wk/osticket.py | 83 +++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/scripts/wk/osticket.py b/scripts/wk/osticket.py index 6e7d57d6..82c38c7e 100644 --- a/scripts/wk/osticket.py +++ b/scripts/wk/osticket.py @@ -8,16 +8,8 @@ import time import mysql.connector as mariadb +from wk import std from wk.cfg.osticket import SQL, STAFF, TEST_STATIONS -from wk.std import ( - ask, - input_text, - print_colored, - print_error, - print_standard, - print_warning, - sleep, - ) # STATIC_VARIABLES @@ -47,6 +39,7 @@ class osTicket(): # pylint: disable=invalid-name self.db_cursor = None self.disabled = False self.errors = False + self.note = None self.ticket_id = None self.ticket_name = None @@ -72,7 +65,7 @@ class osTicket(): # pylint: disable=invalid-name self.db_cursor = self.db_connection.cursor() except mariadb.errors.InterfaceError: # Network issue? try again - sleep(2) + std.sleep(2) except mariadb.errors.Error: # Bad creds or other SQL error, bail break @@ -116,7 +109,7 @@ class osTicket(): # pylint: disable=invalid-name for s in self.db_cursor: flag_value = s[0] except mariadb.errors.Error as err_msg: - print_error(err_msg) + std.print_error(err_msg) self.errors = True # Done @@ -140,7 +133,7 @@ class osTicket(): # pylint: disable=invalid-name field_data = result[0] except mariadb.errors.Error as err_msg: # Show error and return None - print_error(err_msg) + std.print_error(err_msg) # Done return field_data @@ -161,7 +154,7 @@ class osTicket(): # pylint: disable=invalid-name try: self.db_cursor.execute(sql_cmd) except mariadb.errors.Error as err_msg: - print_error(err_msg) + std.print_error(err_msg) self.errors = True def _verify_ticket_id(self): @@ -170,8 +163,31 @@ class osTicket(): # pylint: disable=invalid-name LOG.error('Ticket ID not set') raise RuntimeError('Ticket ID not set') + def add_note(self): + """Add note to be included in osTicket replies.""" + lines = [] + + # Instructions + std.print_standard('Please enter the additional information for this ticket') + std.print_info(' (End note with an empty line)') + std.print_standard(' ') + + # Get note + while True: + text = std.input_text('> ') + if not text: + break + lines.append(text.strip()) + + # Save note + self.note = lines.pop(0) + for line in lines: + self.note += f'\n...{line}' + def post_response(self, response, color='Normal'): """Post a reply to a ticket in osTicket.""" + lines = [] + test_station = get_test_station_name() self._connect() self._verify_ticket_id() @@ -180,7 +196,14 @@ class osTicket(): # pylint: disable=invalid-name return # Format response - response = str(response).replace("`", "'").replace("'", "\\'") + if test_station: + lines.append(f'[Test-Station: {test_station}]') + lines.append(f'[Report for ticket #{self.ticket_id} {self.ticket_name}]') + if self.note: + lines.append(f'[Note] {self.note}\n') + lines.append(str(response)) + response = '\n'.join(lines) + response = response.replace("`", "'").replace("'", "\\'") # Build SQL cmd sql_cmd = ( @@ -207,16 +230,16 @@ class osTicket(): # pylint: disable=invalid-name def select_ticket(self): """Set ticket number and name from osTicket DB.""" - print_standard('Connecting to osTicket...') + std.print_standard('Connecting to osTicket...') # Connect while True: try: self._connect(silent=False) except (mariadb.errors.Error, RuntimeError): - print_warning('Failed to connect to osTicket') - if not ask('Try again?'): - print_standard('Integration disabled for this session') + std.print_warning('Failed to connect to osTicket') + if not std.ask('Try again?'): + std.print_standard('Integration disabled for this session') self.disabled = True return else: @@ -225,14 +248,16 @@ class osTicket(): # pylint: disable=invalid-name # Main loop while self.ticket_id is None: - print_standard(' ') - _id = input_text('Enter ticket number (or leave blank to disable): ') + std.print_standard(' ') + _id = std.input_text('Enter ticket number (or leave blank to disable): ') _id = _id.strip() # Nothing entered - if not _id and ask('Disable osTicket integration for this session?'): - self.disabled = True - break + if not _id: + print(' ') + if std.ask('Disable osTicket integration for this session?'): + self.disabled = True + break # Invalid ID entered if not _id.isnumeric(): @@ -243,20 +268,20 @@ class osTicket(): # pylint: disable=invalid-name # Invalid ticket selected if _name is None: - print_error(f'Ticket #{_id} not found') + std.print_error(f'Ticket #{_id} not found') continue # Valid ticket selected, lookup subject _subject = self._get_ticket_field(_id, 'subject') # Verify selection - print_colored( + std.print_colored( ['You have selected ticket', f'#{_id}', _name], [None, 'BLUE', None], ) - print_colored(f' {_subject}', 'CYAN') - print_standard(' ') - if ask('Is this correct?'): + std.print_colored(f' {_subject}', 'CYAN') + std.print_standard(' ') + if std.ask('Is this correct?'): self.ticket_id = _id self.ticket_name = _name @@ -308,7 +333,7 @@ class osTicket(): # pylint: disable=invalid-name # Bail if flag checkbox set as FAILED if self._get_flag(real_flag_name) == str(FLAG_CODES['Fail']): - print_warning( + std.print_warning( f'Not replacing osTicket {flag_name} checkbox FAILED value', ) return