Skip to content

Commit

Permalink
Merge pull request #44 from mayurilahane/try
Browse files Browse the repository at this point in the history
 added try catch for allpass
  • Loading branch information
mayurilahane authored Jan 15, 2020
2 parents 82f97f8 + abea51e commit 7b8cb80
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
1 change: 1 addition & 0 deletions genpass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import click
from genpass.password import createpass
from genpass.password import savepass
Expand Down
51 changes: 22 additions & 29 deletions genpass/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,36 @@ class DatabaseConnection:
""" Class of database entries for user's information."""

def __init__(self):
"""Used to create database and then to connect with generated databse file"""
"""Used to create database and then to connect with generated databse file
Checked for table is created? if not then created as per required values """
self.con = sqlite3.connect("generated_password.db")
self.cursor_obj = self.con.cursor()

def create_table(self):
"""Checked for table is created? if not then created as per required values """
self.cursor_obj.execute(
"""CREATE TABLE IF NOT EXISTS passwords
(id integer PRIMARY KEY,portal_name text NOT NULL UNIQUE, password varchar,
creation_date varchar, email varchar, portal_url varchar)
"""
(id integer PRIMARY KEY,portal_name text NOT NULL UNIQUE, password varchar,
creation_date varchar, email varchar, portal_url varchar)
"""
)
self.con.commit()

def insert_data(self, portal_name, password, creation_date, email, portal_url):
"""Adding values into database"""
try:
self.password = password
self.creation_date = creation_date
self.email = email
self.portal_name = portal_name
self.portal_url = portal_url
self.cursor_obj.execute(
"""INSERT INTO passwords
(portal_name, password, creation_date, email, portal_url)
VALUES (?, ?, ?, ?, ?)""",
(self.portal_name, self.password, self.creation_date, self.email, self.portal_url),
)
self.con.commit()
except sqlite3.IntegrityError:
print("Portal name already exists")
self.password = password
self.creation_date = creation_date
self.email = email
self.portal_name = portal_name
self.portal_url = portal_url
self.cursor_obj.execute(
"""INSERT INTO passwords
(portal_name, password, creation_date, email, portal_url)
VALUES (?, ?, ?, ?, ?)""",
(self.portal_name, self.password, self.creation_date, self.email, self.portal_url,),
)
self.con.commit()

def delete_data(self, portal_name):
"""Deleting values from database"""
self.portal_name = portal_name

self.cursor_obj.execute(
"""DELETE from passwords where portal_name = ?""", (self.portal_name,)
)
Expand All @@ -81,20 +75,19 @@ def update_data(self, portal_name, password):
def show_data(self, portal_name):
"""All inserted data will showed"""
self.portal_name = portal_name

self.cursor_obj.execute(
"""SELECT password FROM passwords WHERE portal_name=?""", (self.portal_name,)
"""SELECT password FROM passwords WHERE portal_name=?""", (self.portal_name,),
)
rows = self.cursor_obj.fetchall()

for row in rows:
return row[0]

self.con.commit()

def show_all_data(self):
"""Showing all data saved in database"""
self.cursor_obj.execute(
"""SELECT * FROM passwords"""
)
self.cursor_obj.execute("""SELECT * FROM passwords""")
rows = self.cursor_obj.fetchall()
return (rows)
return rows
self.con.commit()
25 changes: 18 additions & 7 deletions genpass/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def version():
@click.command(help="Show all passwords")
def allpass():
all_pass = db_obj.show_all_data()
if all_pass == []:
print("No records found")
table = BeautifulTable()
table.left_border_char = "|"
table.right_border_char = "|"
Expand All @@ -45,22 +47,30 @@ def allpass():
table.column_headers = ["ID", "PORTAL_NAME", "PASSWORD", "DATE", "EMAIL", "PORTAL_URL"]
for row in all_pass:
table.append_row([row[0], row[1], row[2], row[3], row[4], row[5]])
print(table)
print(table)


@click.command(help="Delete password")
def delpass():
"""used to delete existing password"""
portal_name = click.prompt("Enter portal name", default="None")
db_obj.delete_data(portal_name=portal_name)
value_check = db_obj.show_data(portal_name)
if value_check is None:
print("No records found")
else:
db_obj.delete_data(portal_name=portal_name)


@click.command(help="Update password")
def modpass():
"""Update existing password"""
portal_name = click.prompt("Enter portal name", default="None")
mod = click.prompt("Enter new password", default="None", hide_input=True)
db_obj.update_data(portal_name=portal_name, password=mod)
mod_check = db_obj.show_data(portal_name)
if mod_check is None:
print("No records found")
else:
mod = click.prompt("Enter new password", default="None", hide_input=True)
db_obj.update_data(portal_name=portal_name, password=mod)


@click.command(help="Save existing passwords")
Expand All @@ -71,7 +81,6 @@ def savepass():
creation_date = date.today()
email = click.prompt("Enter email id", default="None")
portal_url = click.prompt("Enter portal url", default="None")
db_obj.create_table()
db_obj.insert_data(
portal_name=portal_name,
password=pwd,
Expand All @@ -89,7 +98,6 @@ def createpass():
creation_date = date.today()
email = click.prompt("Enter email id", default="None")
portal_url = click.prompt("Enter portal url", default="None")
db_obj.create_table()
db_obj.insert_data(
portal_name=portal_name,
password=password,
Expand All @@ -103,4 +111,7 @@ def createpass():
def showpass():
portal_name = click.prompt("Enter portal name", default="None")
spass = db_obj.show_data(portal_name)
print(spass)
if spass is None:
print("No records found")
else:
print(spass)

0 comments on commit 7b8cb80

Please sign in to comment.