def add_new_user(username, db, sql): """Add a user to the Users table.""" strn = """ INSERT INTO users( user, join_date, priority ) VALUES ("{0}", "{1}", "{2}"); """.format(username, utils.gettime(), 1) sql.execute(strn) db.commit()
def test_add_scard_to_submissions(self): """ Try adding an scard to the user information and retrieving it again. """ # Create a test submission to get user submission id uid = update_tables.add_entry_to_submissions(utils.gettime(), self.db, self.sql) update_tables.add_scard_to_submissions(self.scard, uid, self.db, self.sql) self.sql.execute(("SELECT scard FROM submissions WHERE " " user_submission_id = {}").format(uid)) result = self.sql.fetchall()[0][0] self.assertEquals(self.scard, result)
def Batch_Entry(scard_file): timestamp = utils.gettime( ) # Can modify this if need 10ths of seconds or more resolution #Assign a user and a timestamp for a given batch strn = """INSERT INTO Batches(timestamp) VALUES ("{0}");""".format( timestamp) BatchID = utils.sql3_exec(strn) #Write the text contained in scard.txt to a field in the Batches table with open(scard_file, 'r') as file: scard = file.read() strn = """UPDATE Batches SET {0} = '{1}' WHERE BatchID = "{2}";""".format( 'scard', scard, BatchID) utils.sql3_exec(strn) utils.printer( "Batch specifications written to database with BatchID {0}".format( BatchID)) #See if user exists already in database; if not, add them with open(scard_file, 'r') as file: scard_text = file.read() scard_fields = scard_helper.scard_class(scard_text) username = user_validation.user_validation() #Write scard into scard table fields (This will not be needed in the future) print("\nReading in information from {0}".format(scard_file)) utils.printer("Writing SCard to Database") scard_fields.data['group_name'] = scard_fields.data.pop( 'group' ) #'group' is a protected word in SQL so we can't use the field title "group" # For more information on protected words in SQL, see https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=RSQL_reservedwords scard_fields.data['genExecutable'] = file_struct.genExecutable.get( scard_fields.data.get('generator')) scard_fields.data['genOutput'] = file_struct.genOutput.get( scard_fields.data.get('generator')) scard_helper.SCard_Entry(BatchID, timestamp, scard_fields.data) print( '\t Your scard has been read into the database with BatchID = {0} at {1} \n' .format(BatchID, timestamp)) #Write gcards into gcards table utils.printer("Writing GCards to Database") gcard_helper.GCard_Entry(BatchID, timestamp, scard_fields.data['gcards']) print("Successfully added gcards to database") strn = "UPDATE Batches SET {0} = '{1}' WHERE BatchID = {2};".format( 'User', username, BatchID) utils.sql3_exec(strn) return 0
def User_Submission(args): # Get time UserSubmission was submitted timestamp = utils.gettime() # Get user and domain information username = user_validation.user_validation() #Enter UserSubmission timestamp into DB, initializing user submission entry strn = """INSERT INTO UserSubmissions(timestamp) VALUES ("{0}");""".format( timestamp) UserSubmissionID = utils.db_write(strn) #Handle scard information scard_fields = scard_handler.scard_handler(args, UserSubmissionID, timestamp) #Handle gcard information scard_fields = gcard_handler.gcard_handler(args, UserSubmissionID, timestamp, scard_fields) #Update tables with gcard and scard information update_tables.update_tables(args, UserSubmissionID, username, timestamp, scard_fields)
def test_add_entry_to_submissions(self): """ Test the addition of an entry into UserSubmissions table. """ for i in range(10): uid = update_tables.add_timestamp_to_submissions( utils.gettime(), self.db, self.sql) self.assertEquals(uid, i + 1)
def client(args): """ Main client function. This is the driver which validates the scard submitted and populates the database tables. """ logger = utils.configure_logger(args) db_conn, sql = setup_database(args) # Get basic information related to this user submission. # If the username is provided at the CL, that takes # priority over inference of the username. timestamp = utils.gettime() username = args.username or user_validation.get_username() domain_name = user_validation.get_domain_name() logger.debug( 'Found {}@{} at time: {}'.format(username, domain_name, timestamp)) # A simple name based method for retrieval # of the scard type. Open the SCard to validate # this submission before starting the database # population. scard_obj = scard_handler.open_scard(args.scard) scard_type = scard_handler.get_scard_type(args.scard) scard_obj.printer() logger.debug('Type inference for SCard: {}'.format(scard_type)) # Verify that the gcard exists in our container, try to # download online gcards for types 3/4. If any of this # fails we do not go forward with submission. if scard_type in [1, 2]: logger.debug('Adding (type 1/2) gcard: {}'.format( scard_obj.configuration)) elif scard_type in [3, 4]: logger.info('Types 3/4 are not supported yet!') else: print('Could not determine type, exiting') """ ----------------------------------------------- From this point and down, all options have been validated and the databases will be populated. ----------------------------------------------- """ if username not in database.get_users(sql): logger.debug('Adding new user {} to users'.format(username)) update_tables.add_new_user(username, db_conn, sql) # Setup an entry in the UserSubmissions table for the current submission. user_submission_id = update_tables.add_timestamp_to_submissions( timestamp, db_conn, sql) logger.debug('user_submission_id = {}'.format(user_submission_id)) if scard_obj.client_ip: logger.debug('Logging client IP: {}'.format(scard_obj.client_ip)) update_tables.add_client_ip_to_submissions( ip=scard_obj.client_ip, user_submission_id=user_submission_id, db=db_conn, sql=sql ) # Update database tables with scard update_tables.add_scard_to_submissions(scard_obj.raw_text, user_submission_id, db_conn, sql) user_id = database.get_user_id(username, sql) logger.debug('For user = {}, user_id = {}'.format(username, user_id)) # Update User and UserID for this submission with key UserSubmissionID logger.debug('Updating submissions(user,user_id) = ({},{})'.format( username, user_id )) update_tables.update_user_information(username, user_id, user_submission_id, db_conn, sql) update_tables.add_entry_to_submissions( user_submission_id, scard_obj.farm_name, db_conn, sql ) db_conn.close()