Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.16 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import getopt
  4. import os
  5. import re
  6. import sys
  7. from lib.db_wrapper import DatabaseConnector as Database
  8. import sqlite3
  9. import subprocess
  10.  
  11.  
  12. def main(argv):
  13.     # initialize variables
  14.  
  15.     database_location = "./db/parsed-files.sqlite"
  16.     con = sqlite3.connect(database_location)
  17.     cur = con.cursor()
  18.    
  19.     #Ask user for ID and password
  20.     print('Welcome to RFID parsing utility.')
  21.     userId = raw_input('Please type your internal ID: ')
  22.     passw = raw_input('Please type your password: ')
  23.    
  24.  
  25.     #Choose employee type - then execute module containing privilages
  26.     cur.execute("SELECT * FROM Administrator "
  27.                     " WHERE ID=:userId AND Password=:passw", (userId, passw))
  28.     user = cur.fetchone()
  29.     if user:
  30.         #execute admin module
  31.         print('Admin module')
  32.         admin_module(user)
  33.         sys.exit(0)
  34.  
  35.     cur.execute("SELECT * FROM Manager WHERE ID=:userId AND Password=:passw", (userId, passw)).fetchone()
  36.     user = cur.fetchone()
  37.     if user:
  38.         #execute manager module
  39.         print('Manager module')
  40.         sys.exit(0)
  41.  
  42.     cur.execute("SELECT * FROM 'Lab Employee' WHERE ID=:userId AND Password=:passw", (userId, passw)).fetchone()
  43.     user = cur.fetchone()
  44.     if user:
  45.         #execute labEmp module
  46.         print('Lab employee module')
  47.         sys.exit(0)
  48.  
  49.     cur.execute("SELECT * FROM 'Office Worker' WHERE ID=:userId AND Password=:passw", (userId, passw)).fetchone()
  50.     user = cur.fetchone()
  51.     if user:
  52.         #execute offWorker module
  53.         print('Office Worker module')
  54.         sys.exit(0)
  55.  
  56.     #User was not found in any above table
  57.     print('User with given credentials doesnt exist in database.'
  58.           '\nPlease contact laboratory administrator.')
  59.     sys.exit(0)
  60.  
  61.  
  62. def admin_module(user):
  63.     print('Welcome to RFID parsing utility ') + user[2] + ' ' + user[3]
  64.     # option = 0
  65.     # while(option != 99):
  66.     print('Here are your options:'
  67.         '\n1 - Add file to database'
  68.         '\n2 - Parse all files to database'
  69.         '\n3 - Add new user'
  70.         '\n4 - Update user data'
  71.         '\n5 - Show all items assigned to group'
  72.         '\n6 - Show all items of certain type'
  73.         '\n99 - Exit program')
  74.     option = raw_input('Please choose one option: ')
  75.     # print("You've chosen: ") + option
  76.  
  77.     if option == 1:
  78.         file_location = raw_input('Please type file name \(it should be located in "RFID_files" folder\)')
  79.         subprocess.call("python rfid-parsing-utility-db.py -i ./RFID_files/%s -o ./RFID_files_parsed/%s --regex ./preset-regex.json --database ./db/parsed-files.sqlite".format(file) % file_location, shell=True, universal_newlines=True)
  80.     elif option == 2:
  81.         os.system('python parse_all_files-db.py')
  82.     elif option == 3:
  83.         user_type = raw_input("Becouse of security, it is not allowed to add new administrator from script level. Please work directly on database.\nPlease type user grade. Options: Lab Employee / Office Worker / Manager ")
  84.         try:
  85.             with Database(database_location) as db_handler:
  86.                 if user_type == 'Lab Employee':
  87.                     user_ID = raw_input("Please type user ID: ")
  88.                     manager_ID = raw_input("Now type user Managers ID: ")
  89.                     Name = raw_input("Now type user name: ")
  90.                     Email = raw_input("Now type user email: ")
  91.                     Password = raw_input("And finally user password: ")
  92.                     db_handler.query('INSERT'
  93.                     'INTO "Lab Employee" (ID, "Managers ID", Name, Email, Password) '
  94.                     'VALUES ("{0}", "{1}", "{2}", "{3}", "{4}")'
  95.                     .format(user_ID, manager_ID, Name, Email, Password))
  96.    
  97.                 elif user_type == 'Office Worker':
  98.                     user_ID = raw_input("Please type user ID: ")
  99.                     manager_ID = raw_input("Now type user Managers ID: ")
  100.                     Name = raw_input("Now type user name: ")
  101.                     Email = raw_input("Now type user email: ")
  102.                     Password = raw_input("And finally user password: ")
  103.    
  104.                     db_handler.query('INSERT'
  105.                     'INTO "Office Worker" (ID, "Managers ID", Name, Email, Password) '
  106.                     'VALUES ("{0}", "{1}", "{2}", "{3}", "{4}")'
  107.                     .format(user_ID, manager_ID, Name, Email, Password))
  108.    
  109.                 elif user_type == 'Manager':
  110.                     user_ID = raw_input("Please type user ID: ")
  111.                     Name = raw_input("Now type user name: ")
  112.                     Email = raw_input("Now type user email: ")
  113.                     Password = raw_input("And finally user password: ")
  114.    
  115.                     db_handler.query('INSERT'
  116.                     'INTO "Item" (ID,  Name, Email, Password) '
  117.                     'VALUES ("{0}", "{1}", "{2}", "{3}")'
  118.                     .format(user_ID, Name, Email, Password))
  119.    
  120.                 else:
  121.                     print("Wrong user type! Please try again.")
  122.  
  123.         except sqlite3.Error as e:
  124.             print("Database error " + str(e))
  125.     elif option == 4:
  126.         print("Option not yet implemented. Please be patient or remind author of script.")
  127.     elif option == 5:
  128.         team_name = raw_input("Please type team, which hardware you want to check: ")
  129.         cur.execute("SELECT * FROM 'Item' WHERE 'HW Owner'=:team", (team_name))
  130.         print("Hardware owned by ") + team_name + ": "
  131.         print cur.fetchall()
  132.     elif option == 6:
  133.         hw_name = raw_input("Please type hw name, which you want to check: ")
  134.         cur.execute("SELECT * FROM 'Item' WHERE 'HW Name'=:name", (hw_name))
  135.         print("Hardware named ") + hw_name + ": "
  136.         print cur.fetchall()
  137.     elif option == 99:
  138.         sys.exit(0)
  139.     else:
  140.         print("COS ZJEBALES")
  141.     # else:
  142.     #   print('Wrong option!'
  143.     #         '\nPlease choose correct one')
  144.     #   print ("Option chosen ") + option
  145.  
  146. if __name__ == "__main__":
  147.     main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement