Commit c56f949c authored by Shashank Kumar's avatar Shashank Kumar

refactored code

parent 616240ed
# For Python
*.pyc
# For Tests
.pytest_cache
.coverage
......
v0.0.1
## June 11 2018 ( Shashank Kumar )
## June 11 2018
- Adding Dashboard feature
- Adding blog, cli, communication, encryption, how_to_use, vcs and way_ahead modules for courseware
- Adding application, profile and theme modules for settings
- Adding Dashboard feature (Shashank Kumar)
- Adding blog, cli, communication, encryption, how_to_use, vcs and way_ahead modules for courseware (Shashank Kumar)
- Adding application, profile and theme modules for settings (Shashank Kumar)
## June 9 2018 ( Shashank Kumar )
## June 9 2018
- Added SignIn feature.
- Added SignIn feature. (Shashank Kumar)
## June 7 2018 ( Shashank Kumar )
## June 7 2018
- Added SignUp feature.
- Added SignUp feature. (Shashank Kumar)
## June 3 2018 ( Shashank Kumar )
## June 3 2018
- Reporting coverage while running Gitlab CI.
- Reporting coverage while running Gitlab CI. (Shashank Kumar)
## June 1 2018 ( Shashank Kumar )
## June 1 2018
- Initial app setup using Kivy.
- Integrate pylint, pytest and support for Gitlab CI.
- Initial app setup using Kivy. (Shashank Kumar)
- Integrate pylint, pytest and support for Gitlab CI. (Shashank Kumar)
......@@ -5,7 +5,7 @@ from kivy.app import App
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager
from settings import initializing_database
from settings import get_db_connection
from modules.signup import signup
from modules.signin import signin
from modules.dashboard import dashboard
......@@ -30,7 +30,7 @@ if __name__ == '__main__':
'''
Setting up things
'''
initializing_database()
get_db_connection()
'''
Setting window width to 720px and height to 480px
......
'''
This modules contain classes to query sqlite3 database
'''
from settings import initializing_database, USER_INFOMATION_TABLE
from settings import get_db_connection, USER_INFOMATION_TABLE
from modules.signin.exceptions import SignInError
from modules.signin.utils import (
clean_email,
......@@ -9,21 +9,21 @@ from modules.signin.utils import (
)
def sign_in_user(**user_info):
def sign_in_user(email, password):
'''
sign_in_user would try to check for user's email and hashed
password in the database
Would result in a UserError if email doesn't exist
Would result in a PasswordError if password doesn't match
'''
connection = initializing_database()
connection = get_db_connection()
db_cursor = connection.cursor()
user_info['email'] = clean_email(user_info['email'])
hashed_pass = hash_password(user_info['password'])
cleaned_email = clean_email(email)
hashed_pass = hash_password(password)
sign_in_query = 'SELECT * FROM {} WHERE email=?'.format(USER_INFOMATION_TABLE)
user_info = db_cursor.execute(sign_in_query, (user_info['email'], )).fetchone()
user_info = db_cursor.execute(sign_in_query, (cleaned_email, )).fetchone()
if not user_info:
raise SignInError('Email does not exist!')
......
......@@ -47,20 +47,21 @@ class SignIn(BoxLayout, Screen):
'''
original_text = self.ids[label].text
self.ids[label].text = error_text
self.ids[label].color = (255, 0, 0, 1)
self.ids[label].color = [1, 0, 0, 1]
def replace_label(*args):
'''
Replacing original text in label
delay time is defined by args[0]
'''
logging.info(
'\'%s\' changed to \'%s\' after %s seconds',
'SignIn: \'%s\' changed to \'%s\' after %s seconds',
error_text,
original_text,
args[0]
)
self.ids[label].text = original_text
self.ids[label].color = (255, 255, 255, 1)
self.ids[label].color = [1, 1, 1, 1]
Clock.schedule_once(replace_label, 2)
def validate(self):
......@@ -68,44 +69,43 @@ class SignIn(BoxLayout, Screen):
Validating Email and Password provided by user
'''
email_validation = True
pass_validation = True
password_validation = True
user_email = self.ids["user_email"].text
user_email = self.ids['user_email'].text
try:
validate_email(user_email)
except SignInError as error:
email_validation = False
self.prompt_error_message(
'email_label',
error.message,
)
email_validation = False
password = self.ids['password'].text
try:
validate_password(password)
except SignInError as error:
pass_validation = False
self.prompt_error_message(
'password_label',
error.message,
)
password_validation = False
return email_validation and pass_validation
return email_validation and password_validation
def sign_up(self):
def sign_in(self):
'''
Signin user in case of successful validation
Prompting error message to the user otherwise
'''
if self.validate():
user_email = self.ids["user_email"].text
user_pass = self.ids["password"].text
user_info = {
'email': user_email,
'password': user_pass,
}
user_email = self.ids['user_email'].text
user_pass = self.ids['password'].text
try:
user_info = sign_in_user(**user_info)
sign_in_user(
email=user_email,
password=user_pass
)
self.manager.transition.direction = 'left'
self.manager.current = 'dashboard'
except SignInError as error:
......
......@@ -15,5 +15,4 @@ def hash_password(password):
'''
hash_password converts plain text password into sha256 hash
'''
hased_pass = hashlib.sha256(password.encode()).hexdigest()
return hased_pass
return hashlib.sha256(password.encode()).hexdigest()
......@@ -3,7 +3,7 @@ This module contains services to be utilized by the application
'''
import sqlite3
from settings import initializing_database, USER_INFOMATION_TABLE
from settings import get_db_connection, USER_INFOMATION_TABLE
from modules.signup.exceptions import SignUpError
from modules.signup.utils import (
generate_uuid,
......@@ -13,7 +13,7 @@ from modules.signup.utils import (
)
def sign_up_user(**user_info):
def sign_up_user(email, password, full_name, language, timezone):
'''
sign_up_user creates connection with the sqlite3 database,
calls methods to clean up full_name, convert password into
......@@ -21,26 +21,30 @@ def sign_up_user(**user_info):
Would result in a False statement if the Email is already
present.
'''
connection = initializing_database()
connection = get_db_connection()
db_cursor = connection.cursor()
user_info['table_name'] = USER_INFOMATION_TABLE
user_info['user_id'] = generate_uuid()
user_info['email'] = clean_email(user_info['email'])
user_info['password'] = hash_password(user_info['password'])
user_info['full_name'] = clean_full_name(user_info['full_name'])
user_info = {
'table_name': USER_INFOMATION_TABLE,
'user_id': generate_uuid(),
'email': clean_email(email),
'password': hash_password(password),
'full_name': clean_full_name(full_name),
'language': language,
'timezone': timezone,
}
try:
sign_up_query = '''
INSERT INTO {table_name} VALUES
("{user_id}",
"{email}",
"{password}",
"{full_name}",
"{language}",
"{timezone}")
'''
db_cursor.execute(sign_up_query.format(**user_info))
('{user_id}',
'{email}',
'{password}',
'{full_name}',
'{language}',
'{timezone}')
'''.format(**user_info)
db_cursor.execute(sign_up_query)
connection.commit()
except sqlite3.IntegrityError:
raise SignUpError('Email already exists')
......
......@@ -49,20 +49,21 @@ class SignUp(BoxLayout, Screen):
'''
original_text = self.ids[label].text
self.ids[label].text = error_text
self.ids[label].color = (255, 0, 0, 1)
self.ids[label].color = [1, 0, 0, 1]
def replace_label(*args):
'''
Replacing original text in label
delay time is defined by args[0]
'''
logging.info(
'\'%s\' changed to \'%s\' after %s seconds',
'SignUp: \'%s\' changed to \'%s\' after %s seconds',
error_text,
original_text,
args[0]
)
self.ids[label].text = original_text
self.ids[label].color = (255, 255, 255, 1)
self.ids[label].color = [1, 1, 1, 1]
Clock.schedule_once(replace_label, 2)
def validate(self):
......@@ -70,50 +71,50 @@ class SignUp(BoxLayout, Screen):
Validating Email, Password and Full Name provided by user
'''
email_validation = True
pass_validation = True
full_name_validation = True
name_validation = True
password_validation = True
user_email = self.ids['user_email'].text
try:
validate_email(user_email)
except SignUpError as error:
email_validation = False
self.prompt_error_message(
'email_label',
error.message,
)
email_validation = False
first_pass = self.ids['first_pass'].text
try:
validate_first_pass(first_pass)
except SignUpError as error:
pass_validation = False
self.prompt_error_message(
'first_pass_label',
error.message,
)
password_validation = False
confirm_pass = self.ids['confirm_pass'].text
try:
validate_confirm_pass(first_pass, confirm_pass)
except SignUpError as error:
pass_validation = False
self.prompt_error_message(
'confirm_pass_label',
error.message,
)
password_validation = False
full_name = self.ids['user_full_name'].text
try:
validate_full_name(full_name)
except SignUpError as error:
full_name_validation = False
self.prompt_error_message(
'full_name_label',
error.message,
)
name_validation = False
return email_validation and pass_validation and full_name_validation
return email_validation and name_validation and password_validation
def sign_up(self):
'''
......@@ -121,20 +122,19 @@ class SignUp(BoxLayout, Screen):
Prompting error message to the user otherwise
'''
if self.validate():
user_email = self.ids['user_email'].text
user_pass = self.ids['first_pass'].text
user_full_name = self.ids['user_full_name'].text
user_language = self.ids['user_language'].text
user_timezone = self.ids['user_timezone'].text
user_info = {
'email': user_email,
'password': user_pass,
'full_name': user_full_name,
'language': user_language,
'timezone': user_timezone
}
email = self.ids['user_email'].text
password = self.ids['first_pass'].text
full_name = self.ids['user_full_name'].text
language = self.ids['user_language'].text
timezone = self.ids['user_timezone'].text
try:
sign_up_user(**user_info)
sign_up_user(
email=email,
password=password,
full_name=full_name,
language=language,
timezone=timezone
)
self.manager.transition.direction = 'left'
self.manager.current = 'dashboard'
except SignUpError as error:
......
......@@ -31,5 +31,4 @@ def hash_password(password):
'''
hash_password converts plain text password into sha256 hash
'''
hased_pass = hashlib.sha256(password.encode()).hexdigest()
return hased_pass
return hashlib.sha256(password.encode()).hexdigest()
......@@ -9,9 +9,9 @@ DATABASE_FILE = 'new_contributor_wizard.db'
USER_INFOMATION_TABLE = 'USERS'
def initializing_database():
def get_db_connection():
'''
Creating database file is not present
Creating connection to the database and schema if required
'''
connection = sqlite3.connect(DATABASE_FILE)
db_cursor = connection.cursor()
......
......@@ -6,17 +6,24 @@ from modules.signin.exceptions import SignInError
from settings import (
DATABASE_FILE,
USER_INFOMATION_TABLE,
initializing_database
get_db_connection
)
from modules.signin.utils import (
clean_email,
hash_password
)
def setup():
# inserting test values to the database
connection = initializing_database()
connection = get_db_connection()
db_cursor = connection.cursor()
cleaned_email = clean_email('shanky@shanky.xyz')
hashed_password = hash_password('mynewpass')
db_cursor.execute('''
INSERT INTO USERS (email, password) VALUES ('shanky@shanky.xyz', '605fcc4bcef3ce81c818304b6e9e9074977a84be7493acae33ac1ab2e56d99dc')
''')
INSERT INTO USERS (email, password) VALUES ('{}', '{}')
'''.format(cleaned_email, hashed_password)
)
connection.commit()
......@@ -57,10 +64,10 @@ def test_sign_in():
def teardown():
# deleting test values from the database
connection = initializing_database()
connection = get_db_connection()
db_cursor = connection.cursor()
db_cursor.execute('''
DELETE FROM USERS WHERE USERS.email="shanky@shanky.xyz"
DELETE FROM USERS WHERE USERS.email='shanky@shanky.xyz'
''')
connection.commit()
connection.close()
......@@ -7,13 +7,13 @@ from modules.signup.exceptions import SignUpError
from settings import (
DATABASE_FILE,
USER_INFOMATION_TABLE,
initializing_database
get_db_connection
)
def setup():
#setting up database schema
initializing_database()
get_db_connection()
def testing_setting_constants():
......@@ -45,7 +45,7 @@ def teardown():
connection = sqlite3.connect(DATABASE_FILE)
db_cursor = connection.cursor()
db_cursor.execute('''
DELETE FROM USERS WHERE USERS.email="abc@shanky.xyz"
DELETE FROM USERS WHERE USERS.email='abc@shanky.xyz'
''')
connection.commit()
connection.close()
This diff is collapsed.
......@@ -106,7 +106,7 @@
padding: 0, 0
halign: 'center'
valign: 'middle'
on_press: root.sign_up()
on_press: root.sign_in()
FloatLayout:
BoxLayout:
size_hint: 1, 0.16
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment