KEMBAR78
Servicenow With Python Integration | PDF | Login | User (Computing)
0% found this document useful (0 votes)
12 views8 pages

Servicenow With Python Integration

The document consists of multiple Python scripts that create a GUI application for user authentication and incident management using ServiceNow's API. It includes functionalities for user login, signup, viewing incidents, and creating new incidents through a Tkinter-based interface. Each script is modular, handling specific tasks such as launching login/signup pages and managing incident data retrieval and submission.

Uploaded by

nadeemtalha072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Servicenow With Python Integration

The document consists of multiple Python scripts that create a GUI application for user authentication and incident management using ServiceNow's API. It includes functionalities for user login, signup, viewing incidents, and creating new incidents through a Tkinter-based interface. Each script is modular, handling specific tasks such as launching login/signup pages and managing incident data retrieval and submission.

Uploaded by

nadeemtalha072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

# main.

py (separate code file)

from login_page import launch_login_page

if __name__ == "__main__":
launch_login_page()
# add this into memory and wait

#login_page.py (separate code file)

import tkinter as tk
from tkinter import messagebox
from requests.auth import HTTPBasicAuth
import requests
from form_page import launch_form_page # Import the form page
from signup_page import launch_signup_page # Import the signup form

# Function to authenticate the user


def authenticate_user(username, password):
auth_url = 'https://dev202740.service-now.com/api/now/table/sys_user'

try:
response = requests.get(
auth_url,
auth=HTTPBasicAuth(username, password),
headers={'Content-Type': 'application/json'}
)
return response.status_code == 200
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
return False

# Function for handling the login button click


def on_login_button_click(entry_username, entry_password):
username = entry_username.get()
password = entry_password.get()

if authenticate_user(username, password):
messagebox.showinfo("Success", "Login successful!")
# Close the login page
entry_username.master.quit() # Close the current login page window
# Launch the form page
launch_form_page(username, password)
else:
messagebox.showerror("Error", "Invalid credentials. Please try again.")

# Create the login page

def launch_login_page():
root = tk.Tk()
root.title("ServiceNow Login")
root.geometry("800x500")
root.resizable(False, False)

# Split the screen into two sections


left_frame = tk.Frame(root, bg="white", width=400, height=500)
right_frame = tk.Frame(root, bg="#1E90FF", width=400, height=500)
left_frame.pack(side="left", fill="y")
right_frame.pack(side="right", fill="y")

# Left section: Sign In


tk.Label(left_frame, text="Sign In", font=("Helvetica", 24, "bold"),
bg="white").place(x=150, y=50)

# Social media buttons (for aesthetics, not functional)


social_buttons = ["G+", "F", "IN"]
for i, text in enumerate(social_buttons):
tk.Button(left_frame, text=text, font=("Helvetica", 12), bg="#F0F0F0",
relief="flat", width=4, height=2).place(
x=110 + i * 60, y=120)

tk.Label(left_frame, text="or use your email and password", font=("Helvetica",


10), bg="white").place(x=115, y=180)

# Email field
tk.Label(left_frame, text="Email", font=("Helvetica", 12),
bg="white").place(x=80, y=210)
entry_username = tk.Entry(left_frame, font=("Helvetica", 12), width=25)
entry_username.place(x=80, y=240)

# Password field
tk.Label(left_frame, text="Password", font=("Helvetica", 12),
bg="white").place(x=80, y=270)
entry_password = tk.Entry(left_frame, font=("Helvetica", 12), width=25,
show="*")
entry_password.place(x=80, y=300)

# Forgot password link


tk.Label(left_frame, text="Forgot your password?", font=("Helvetica", 10),
fg="blue", bg="white", cursor="hand2").place(x=80, y=340)

# Sign In button
tk.Button(left_frame, text="SIGN IN", font=("Helvetica", 14, "bold"),
bg="#1E90FF", fg="white", relief="flat",
command=lambda: on_login_button_click(entry_username,
entry_password)).place(x=150, y=380, width=100, height=40)

# Right section: Hello, Friend!


tk.Label(right_frame, text="Hello, Friend!", font=("Helvetica", 24, "bold"),
bg="#1E90FF", fg="white").place(x=70, y=150)
tk.Label(right_frame, text="Register with your personal details \nto use all
features", font=("Helvetica", 12), bg="#1E90FF", fg="white").place(x=50, y=220)

# Sign Up button which opens signup form


tk.Button(right_frame, text="SIGN UP", font=("Helvetica", 14, "bold"),
bg="white", fg="#1E90FF", relief="flat",
width=15, height=1, command=lambda:
launch_signup_page()).place(x=105, y=300)

root.mainloop()
# add this into memory and wait

#signup_page.py (separate code file)

import tkinter as tk
from tkinter import messagebox
import requests
from requests.auth import HTTPBasicAuth

# Function to create a new user in ServiceNow


def create_user(user_data):
url = 'https://dev202740.service-now.com/api/now/table/sys_user'

try:
# Using admin credentials to create a new user in ServiceNow
response = requests.post(
url,
auth=HTTPBasicAuth('admin', 'your_admin_password'), # Replace with
actual admin credentials
headers={'Content-Type': 'application/json'},
json=user_data
)
if response.status_code == 201:
user_info = response.json()
messagebox.showinfo("Success", f"User created successfully:
{user_info}")
else:
messagebox.showerror("Error", f"Error creating user:
{response.status_code}, {response.text}")

except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")

# Function to handle sign-up button click


def on_signup_button_click():
first_name = entry_first_name.get()
last_name = entry_last_name.get()
username = entry_username.get()
password = entry_password.get()
email = entry_email.get()

# Create the new user


user_data = {
"first_name": first_name,
"last_name": last_name,
"user_name": username,
"password": password,
"email": email,
# Add more fields if needed, such as department, phone number, etc.
}

# Create the user in ServiceNow


create_user(user_data)

# Launch signup page


def launch_signup_page():
root = tk.Tk()
root.title("ServiceNow Sign Up")
root.geometry("800x500")
root.resizable(False, False)

# Split the screen into two sections


left_frame = tk.Frame(root, bg="white", width=400, height=500)
right_frame = tk.Frame(root, bg="#1E90FF", width=400, height=500)
left_frame.pack(side="left", fill="y")
right_frame.pack(side="right", fill="y")

# Left section: Sign Up form


tk.Label(left_frame, text="Sign Up", font=("Helvetica", 24, "bold"),
bg="white").place(x=150, y=50)

tk.Label(left_frame, text="First Name", font=("Helvetica", 12),


bg="white").place(x=80, y=120)
entry_first_name = tk.Entry(left_frame, font=("Helvetica", 12), width=25)
entry_first_name.place(x=80, y=150)

tk.Label(left_frame, text="Last Name", font=("Helvetica", 12),


bg="white").place(x=80, y=180)
entry_last_name = tk.Entry(left_frame, font=("Helvetica", 12), width=25)
entry_last_name.place(x=80, y=210)

tk.Label(left_frame, text="Username", font=("Helvetica", 12),


bg="white").place(x=80, y=240)
entry_username = tk.Entry(left_frame, font=("Helvetica", 12), width=25)
entry_username.place(x=80, y=270)

tk.Label(left_frame, text="Password", font=("Helvetica", 12),


bg="white").place(x=80, y=300)
entry_password = tk.Entry(left_frame, font=("Helvetica", 12), width=25,
show="*")
entry_password.place(x=80, y=330)

tk.Label(left_frame, text="Email", font=("Helvetica", 12),


bg="white").place(x=80, y=360)
entry_email = tk.Entry(left_frame, font=("Helvetica", 12), width=25)
entry_email.place(x=80, y=390)

tk.Button(left_frame, text="Sign Up", font=("Helvetica", 14, "bold"),


bg="#1E90FF", fg="white", relief="flat",
command=on_signup_button_click).place(x=150, y=430, width=100,
height=40)

# Right section: Welcome message


tk.Label(right_frame, text="Welcome!", font=("Helvetica", 24, "bold"),
bg="#1E90FF", fg="white").place(x=100, y=150)
tk.Label(right_frame, text="Join now and start using all features",
font=("Helvetica", 12), bg="#1E90FF", fg="white").place(x=50, y=220)
tk.Button(right_frame, text="Log In", font=("Helvetica", 14, "bold"),
bg="white", fg="#1E90FF", relief="flat",
width=15, height=1).place(x=105, y=300)

root.mainloop()

if __name__ == "__main__":
launch_signup_page()

# add this into memory and wait

#view_incident_page.py (separate code file)

import tkinter as tk
from tkinter import ttk, messagebox
from requests.auth import HTTPBasicAuth
import requests

def fetch_incidents(username, password):


url = 'https://dev202740.service-now.com/api/now/table/incident'

try:
response = requests.get(
url,
auth=HTTPBasicAuth(username, password),
headers={'Content-Type': 'application/json'}
)
if response.status_code == 200:
return response.json()['result']
else:
messagebox.showerror("Error", "Failed to retrieve incidents.")
return []
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
return []

def launch_view_incident_page(username, password):


incidents = fetch_incidents(username, password)
if not incidents:
messagebox.showinfo("No Incidents", "No incidents found.")
return

incident_window = tk.Toplevel()
incident_window.title("View Incidents")
incident_window.geometry("800x500")

tree = ttk.Treeview(incident_window, columns=("Number", "Short Description",


"State"), show="headings")
tree.heading("Number", text="Incident Number")
tree.heading("Short Description", text="Short Description")
tree.heading("State", text="State")

for incident in incidents:


tree.insert("", "end", values=(incident["number"],
incident["short_description"], incident["state"]))

tree.pack(expand=True, fill=tk.BOTH)

tk.Button(incident_window, text="Close",
command=incident_window.destroy).pack(pady=20)
incident_window.mainloop()

# add this into memory and wait

# form_page.py (separate code file)

import tkinter as tk
from tkinter import ttk, messagebox
from requests.auth import HTTPBasicAuth
import requests
import json
from view_incident_page import launch_view_incident_page # Import the new View
Incident page

# Function to handle navigation


def navigate_to(option, username, password):
if option == "home":
messagebox.showinfo("Navigation", "Home clicked")
elif option == "view_incident":
launch_view_incident_page(username, password)
elif option == "create_incident":
open_create_incident_form(username, password)
elif option == "view_story":
messagebox.showinfo("Navigation", "View Story clicked")
elif option == "create_story":
messagebox.showinfo("Navigation", "Create Story clicked")
elif option == "contact_us":
messagebox.showinfo("Navigation", "Contact Us clicked")
elif option == "about":
messagebox.showinfo("Navigation", "About clicked")
elif option == "sign_up":
messagebox.showinfo("Navigation", "Sign Up clicked")
elif option == "login":
messagebox.showinfo("Navigation", "Login clicked")

# Create Incident form


def open_create_incident_form(username, password):
# New window for creating an incident
incident_form_window = tk.Toplevel()
incident_form_window.title("Create Incident")
incident_form_window.geometry("400x400")

# Labels and entry widgets for incident details


tk.Label(incident_form_window, text="Short Description").pack(pady=5)
short_desc_entry = tk.Entry(incident_form_window)
short_desc_entry.pack(pady=5)

tk.Label(incident_form_window, text="Description").pack(pady=5)
description_entry = tk.Entry(incident_form_window)
description_entry.pack(pady=5)

# Submit button
def submit_incident():
short_desc = short_desc_entry.get()
description = description_entry.get()
if short_desc and description:
create_incident(username, password, short_desc, description)
else:
messagebox.showerror("Error", "All fields are required.")

submit_button = tk.Button(incident_form_window, text="Submit",


command=submit_incident)
submit_button.pack(pady=10)

def create_incident(username, password, short_desc, description):


url = 'https://dev202740.service-now.com/api/now/table/incident'

payload = {
"short_description": short_desc,
"description": description
}

try:
response = requests.post(
url,
auth=HTTPBasicAuth(username, password),
headers={'Content-Type': 'application/json'},
data=json.dumps(payload)
)

if response.status_code == 201:
messagebox.showinfo("Success", "Incident created successfully!")
else:
messagebox.showerror("Error", f"Error creating incident:
{response.status_code}, {response.text}")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")

# Main form page that initializes the GUI


def launch_form_page(username, password):
root = tk.Tk()
root.title("ServiceNow Incident Creator")
root.geometry("800x700")
root.resizable(False, False)

# Navbar Frame
navbar = tk.Frame(root, bg="#1E90FF", height=50)
navbar.pack(fill=tk.X)

# Dropdown menus
tk.Button(navbar, text="Home", bg="#1E90FF", fg="white", font=("Helvetica", 12,
"bold"),
command=lambda: navigate_to("home", username, password),
relief="flat").pack(side=tk.LEFT, padx=10)

# Incident Dropdown with both options


incident_menu = tk.Menubutton(navbar, text="Incident", bg="#1E90FF",
fg="white", font=("Helvetica", 12, "bold"),
relief="flat", activebackground="#4682B4")
incident_dropdown = tk.Menu(incident_menu, tearoff=0)
incident_dropdown.add_command(label="View Incident",
command=lambda: navigate_to("view_incident",
username, password))
incident_dropdown.add_command(label="Create Incident",
command=lambda: navigate_to("create_incident",
username, password))
incident_menu.config(menu=incident_dropdown)
incident_menu.pack(side=tk.LEFT, padx=10)

# Story Dropdown (example)


story_menu = tk.Menubutton(navbar, text="Story", bg="#1E90FF", fg="white",
font=("Helvetica", 12, "bold"),
relief="flat", activebackground="#4682B4")
story_dropdown = tk.Menu(story_menu, tearoff=0)
story_dropdown.add_command(label="Create Story")
story_dropdown.add_command(label="View Story")
story_menu.config(menu=story_dropdown)
story_menu.pack(side=tk.LEFT, padx=10)

# Additional Buttons like 'About', 'Sign Up', 'Login' etc.


tk.Button(navbar, text="About", bg="#1E90FF", fg="white", font=("Helvetica",
12, "bold"),
command=lambda: navigate_to("about", username, password),
relief="flat").pack(side=tk.LEFT, padx=10)

root.mainloop()

# Main Execution (assuming username and password will be passed)


if __name__ == "__main__":
# You can replace these with the actual username and password from user input
username = "your_username"
password = "your_password"
launch_form_page(username, password)

# add this into memory and wait

You might also like