<commit_before>import yaml
import json
import sql
import psycopg2
import time


# # connect to database makerspace
# conn = psycopg2.connect("host=localhost dbname=makerspace user=postgres")
# # open a cursor to perform database operations
# cur = conn.cursor()

# sdb = StudentDatabase( "host", "db_name", "user", "pass" )
# sdb.add_user( "cardID", "ID", "netID", "Name", "Lastname", "oldid" )


class StudentDatabase:
    def __init__(self, host, db_name, user, password):
        # TODO: add password field to connection after successful database connection
        self.host = host
        self.db_name = db_name
        self.password = password
        self.user = user

        try:
            # connect to database makerspace
            self.conn = psycopg2.connect("host={} dbname={} user={}".format(self.host, self.db_name, self.user))
            # open a cursor to perform database operations
        except:
            print("I am unable to connect to the database. \n Please supply parameters in this format: StudentDatabase.StudentDatabase(self, host, db_name, user, password)")

    def __dbReq(self, stringReq):
        """
        Handles creating a cursor, making a request, and closing said cursor.
        """
        # TODO: Add exception handling, do some loggingtry:
        try:
            cur = self.conn.cursor()
            print("Request made!")
            cur.execute(stringReq)
            cur.close()
        except Exception as e:
            print(e)

    def get_init(self):
        return "{} {} {} {}".format(self.host, self.db_name, self.password, self.user)

    def add_user(self, card_id, uw_id, uw_netid, first_name, last_name):
        # searches table for existing users with any matching unique inputs, i.e. duplicates
        request = "SELECT * FROM users WHERE card_id={} OR uw_id={} OR uw_netid={}".format(str(card_id), str(uw_id), str(uw_netid))
        print(request)
        self.__dbReq(request)
        # add user to table if no duplicates are found
        cur = self.conn.cursor()
        if len(cur.fetchall()) == 0:
            cur.close()
            self.__dbReq("INSERT INTO users (card_id, uw_id, uw_netid, first_name, last_name) VALUES({}, {}, {}, {}, {}".format(str(card_id), str(uw_id), str(uw_netid), str(first_name), str(last_name)))
        # error, duplicates found
        else:
            pass

    # removing user from form input
    def remove_user(self, card_id, uw_id, uw_netid):
        # searches for user with a matching input
        self.__dbReq("SELECT * FROM users WHERE card_id=%(card_id)s OR uw_id=%(uw_id)s OR uw_netid=%(uw_netid)s") % {'card_id': str(card_id), 'uw_id': str(uw_id), 'uw_netid': str(uw_netid)}
        # if a user is found, remove from table
        if len(cur.fetchall()) == 1:
            self.__dbReq("DELETE FROM users")
            # move deleted user to new table?
        # error, no user found matching inputs
        else:
            pass

    # editing user entry by form input
    def edit_user(self, old_uw_id, card_id, uw_id, uw_netid, first_name, last_name):
        # gets id of user entry matching
        self.__dbReq("SELECT id FROM users WHERE uw_id=%(uw_id)s") % {'uw_id': str(old_uw_id)}
        # if id is found update user entry
        if len(cur.fetchall()) == 1:
            id = cur.fetchall()[0]
            self.__dbReq("UPDATE users SET card_id=%(card_id)s, uw_id=%(uw_id)s, uw_netid=%(uw_netid)s, first_name=%(first_name)s, last_name=%(last_name)s WHERE id=%(id)s") % {'card_id': str(card_id), 'uw_id': str(uw_id), 'uw_netid': str(uw_netid), 'first_name': str(firstname), 'last_name': str(last_name), 'id': str(id)}
        # error, no id found so no update
        else:
            pass

    # display all users
    def display_users():
        table = "<table>"
        self.__dbReq("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='users'")
        header = cur.fetchall()
        for column in header:
            table += "<th>" + str(column[3]) + "</th>"
        self.__dbReq("SELECT * FROM users")
        data = cur.fetchall()
        for row in data:
            table += "<tr>"
            for column in row:
                table += "<td>" + str(column) + "</td>"
            table += "</tr>"
        return table + "</table>"

    # add membership to uw_id given card_id and type of membership
    # expiration_date is only required if it is a main_door membership
    def add_membership(card_id, type, expiration_date):
        pass

    # display all memberships and allow removing one by selecting one
    def remove_membership(card_id, type, expiration_date):
        pass

    # edit details of a membership
    def edit_membership(card_id, type, expiration_date):
        pass

    # ban membership of uw_id given card_id and type of membership
    # start_date is from time of form submission and end_date set by submitter
    def ban_card(card_id, type, start_date, end_date):
        pass

    # display list of all bans and allow unbanning by selecting one
    def unban_card(card_id, type, start_date, end_date):
        pass

    def add_card_reader():
        pass<commit_msg>added display_all_users()<commit_after>import yaml
import json
import sql
import psycopg2
import time
import pprint


# # connect to database makerspace
# conn = psycopg2.connect("host=localhost dbname=makerspace user=postgres")
# # open a cursor to perform database operations
# cur = conn.cursor()

# sdb = StudentDatabase( "host", "db_name", "user", "pass" )
# sdb.add_user( "cardID", "ID", "netID", "Name", "Lastname", "oldid" )


class StudentDatabase:
    def __init__(self, host, db_name, user, password):
        # TODO: add password field to connection after successful database connection
        self.host = host
        self.db_name = db_name
        self.password = password
        self.user = user

        try:
            # connect to database makerspace
            self.conn = psycopg2.connect("host={} dbname={} user={}".format(self.host, self.db_name, self.user))
            # open a cursor to perform database operations
        except:
            print("I am unable to connect to the database. \n Please supply parameters in this format: StudentDatabase.StudentDatabase(self, host, db_name, user, password)")

    def __dbReq(self, stringReq):
        """
        Handles creating a cursor, making a request, and closing said cursor.
        """
        # TODO: Add exception handling, do some loggingtry:
        try:
            cur = self.conn.cursor()
            print("Request made!")
            cur.execute(stringReq)
            self.conn.commit()
			# this is probably going to need to be changed
            rows = cur.fetchall()
            pprint.pprint(rows)
            cur.close()
        except Exception as e:
            print(e)

    def get_init(self):
        return "{} {} {} {}".format(self.host, self.db_name, self.password, self.user)

    def add_user(self, card_id, uw_id, uw_netid, first_name, last_name):
        # searches table for existing users with any matching unique inputs, i.e. duplicates
        request = "SELECT * FROM users WHERE card_id={} OR uw_id={} OR uw_netid={}".format(str(card_id), str(uw_id), str(uw_netid))
        print(request)
        self.__dbReq(request)
        # add user to table if no duplicates are found
        cur = self.conn.cursor()
        if len(cur.fetchall()) == 0:
            cur.close()
            self.__dbReq("INSERT INTO users (card_id, uw_id, uw_netid, first_name, last_name) VALUES({}, {}, {}, {}, {}".format(str(card_id), str(uw_id), str(uw_netid), str(first_name), str(last_name)))
        # error, duplicates found
        else:
            pass

    # removing user from form input
    def remove_user(self, card_id, uw_id, uw_netid):
        # searches for user with a matching input
        self.__dbReq("SELECT * FROM users WHERE card_id=%(card_id)s OR uw_id=%(uw_id)s OR uw_netid=%(uw_netid)s") % {'card_id': str(card_id), 'uw_id': str(uw_id), 'uw_netid': str(uw_netid)}
        # if a user is found, remove from table
        if len(cur.fetchall()) == 1:
            self.__dbReq("DELETE FROM users")
            # move deleted user to new table?
        # error, no user found matching inputs
        else:
            pass

    # editing user entry by form input
    def edit_user(self, old_uw_id, card_id, uw_id, uw_netid, first_name, last_name):
        # gets id of user entry matching
        self.__dbReq("SELECT id FROM users WHERE uw_id=%(uw_id)s") % {'uw_id': str(old_uw_id)}
        # if id is found update user entry
        if len(cur.fetchall()) == 1:
            id = cur.fetchall()[0]
            self.__dbReq("UPDATE users SET card_id=%(card_id)s, uw_id=%(uw_id)s, uw_netid=%(uw_netid)s, first_name=%(first_name)s, last_name=%(last_name)s WHERE id=%(id)s") % {'card_id': str(card_id), 'uw_id': str(uw_id), 'uw_netid': str(uw_netid), 'first_name': str(firstname), 'last_name': str(last_name), 'id': str(id)}
        # error, no id found so no update
        else:
            pass

    # display all users
    def display_users():
        table = "<table>"
        self.__dbReq("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='users'")
        header = cur.fetchall()
        for column in header:
            table += "<th>" + str(column[3]) + "</th>"
        self.__dbReq("SELECT * FROM users")
        data = cur.fetchall()
        for row in data:
            table += "<tr>"
            for column in row:
                table += "<td>" + str(column) + "</td>"
            table += "</tr>"
        return table + "</table>"

    def display_all_users(self):
        self.__dbReq("SELECT * FROM users")

    # add membership to uw_id given card_id and type of membership
    # expiration_date is only required if it is a main_door membership
    def add_membership(card_id, type, expiration_date):
        pass

    # display all memberships and allow removing one by selecting one
    def remove_membership(card_id, type, expiration_date):
        pass

    # edit details of a membership
    def edit_membership(card_id, type, expiration_date):
        pass

    # ban membership of uw_id given card_id and type of membership
    # start_date is from time of form submission and end_date set by submitter
    def ban_card(card_id, type, start_date, end_date):
        pass

    # display list of all bans and allow unbanning by selecting one
    def unban_card(card_id, type, start_date, end_date):
        pass

    def add_card_reader():
        pass
{'fraction_non_alphanumeric': 0.11382807316842006, 'fraction_numerical': 0.0015164439389631316, 'mean_word_length': 2.6715379262352124, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4408264', 'n_tokens_mistral': 3239, 'n_tokens_neox': 3194, 'n_words': 994}