From f6818c9ff3372d2ddea337892f4d0e297e0558e5 Mon Sep 17 00:00:00 2001 From: Anna Date: Tue, 12 Sep 2017 16:50:45 +0300 Subject: [PATCH] password generator --- users.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/users.py b/users.py index 5da1c80..4c1dfab 100755 --- a/users.py +++ b/users.py @@ -2,6 +2,7 @@ import argparse import bcrypt +import random import sqlite3 @@ -45,12 +46,16 @@ class User: def add(self): if self.password is None: self.password = self._passgen() + print "Password has been generated: %s", self.password hashed_password = bcrypt.hashpw(self.password.encode('utf-8'), bcrypt.gensalt()) - self.connection["cursor"].execute( - "INSERT INTO users (name, password) VALUES (?, ?)", - (self.user, hashed_password) - ) - self._commit() + try: + self.connection["cursor"].execute( + "INSERT INTO users (name, password) VALUES (?, ?)", + (self.user, hashed_password) + ) + self._commit() + except sqlite3.IntegrityError: + print 'user already exists' self._close_connection() def delete(self): @@ -62,7 +67,8 @@ class User: self._close_connection() def _passgen(self): - pass + chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' + return ''.join(random.choice(chars) for i in range(10)) if __name__ == "__main__":