Skip to content

Commit 27c9e6c

Browse files
authored
Merge pull request #40 from cdvx/Corrections-PR-Feedback
Corrections pr feedback
2 parents 9fa2564 + 7282587 commit 27c9e6c

File tree

10 files changed

+484
-256
lines changed

10 files changed

+484
-256
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ app/routes/__pycache__
55
.vscode/
66
.vscode/settings.json
77
.pytest_cache/
8-
.coverage
8+
.coverage
9+
.env

.travis.yml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
language: python
22
python:
3-
- "3.6"
4-
3+
- "3.6"
4+
cache: pip
55
install:
6-
- pip install -r requirements.txt
6+
- pip install -r requirements.txt
7+
- pip install python-coveralls
8+
- pip install pytest
9+
- pip install pytest-cov
10+
- pip install coveralls
11+
services:
12+
- postgresql
13+
before_script:
14+
- psql -c 'create database clvx;' -U postgres
15+
716

817
script:
9-
- pytest
10-
11-
after_success:
12-
- coveralls
18+
- pytest --cov=tests/
1319

20+
after_success:
21+
- coveralls

connect.py renamed to app/connect.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
import os
2+
13
import psycopg2
24

5+
# DSN_APP = "dbname='clvx' user='postgres' host='localhost' password='Tesxting' port='5432'"
6+
# DSN_TESTING = "dbname='test_db' user='postgres' host='localhost' password='Tesxting' port='5432'"
7+
38

49
class DatabaseConnection(object):
510
def __init__(self):
11+
if os.getenv('APP_SETTINGS') == "testing":
12+
self.dbname = "test_db"
13+
14+
else:
15+
self.dbname = "clvx"
16+
617
try:
7-
self.connection = psycopg2.connect(
8-
"dbname='clvx' user='postgres' host='localhost' password='Tesxting' port='5432'")
18+
19+
self.connection = psycopg2.connect(dbname=f"{self.dbname}", user='postgres', host='localhost', password='Tesxting', port='5432')
920
self.connection.autocommit = True
1021
self.cursor = self.connection.cursor()
1122
self.last_ten_queries = []
@@ -33,6 +44,7 @@ def create_Questions_table(self):
3344
id serial PRIMARY KEY,
3445
topic varchar(100) NOT NULL,
3546
body varchar(600) NOT NULL,
47+
author varchar(100) NOT NULL,
3648
question_id varchar(150) NOT NULL
3749
);"""
3850
self.cursor.execute(create_table_command)
@@ -47,6 +59,7 @@ def create_Answers_table(self):
4759
Qn_Id varchar(150) NOT NULL,
4860
body varchar(600) NOT NULL,
4961
answer_id varchar(150) NOT NULL,
62+
author varchar(100) NOT NULL,
5063
prefered boolean
5164
);"""
5265
self.cursor.execute(create_table_command)
@@ -78,33 +91,39 @@ def insert_new_record(self, tablename, data):
7891
insert_command = """INSERT INTO questions(
7992
topic,
8093
body,
94+
author,
8195
question_id
8296
) VALUES(
97+
%s,
8398
%s,
8499
%s,
85100
%s
86101
);"""
87102
self.cursor.execute(insert_command, (
88103
data['topic'],
89104
data['body'],
105+
data['author'],
90106
data['questionId'])
91107
)
92108
elif tablename == tables[2]:
93109
insert_command = """INSERT INTO answers(
94110
Qn_Id,
95111
body,
96112
answer_id,
113+
author,
97114
prefered
98115
) VALUES(
99116
%s,
100117
%s,
101118
%s,
119+
%s,
102120
%s
103121
);"""
104122
self.cursor.execute(insert_command, (
105123
data['Qn_Id'],
106124
data['body'],
107125
data['answerId'],
126+
data['author'],
108127
data['prefered'])
109128
)
110129
else:
@@ -173,17 +192,11 @@ def drop_table(self, tablename):
173192
except (Exception, psycopg2.DatabaseError) as error:
174193
print(error)
175194

195+
176196
conn = DatabaseConnection()
177197
#conn.drop_table('answers')
178198
conn.create_Answers_table()
199+
#conn.drop_table('users')
179200
conn.create_Users_table()
201+
#conn.drop_table('questions')
180202
conn.create_Questions_table()
181-
182-
# if not conn.tablename == 'users':
183-
# conn.create_Users_table()
184-
# elif not conn.tablename == 'questions':
185-
# conn.create_Questions_table()
186-
# elif not conn.tablename == 'answers':
187-
# conn.create_Answers_table()
188-
# else:
189-
# pass

app/models.py

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
11
import uuid
22

3-
import bcrypt
43
from werkzeug.security import generate_password_hash
54

5+
from app.connect import conn
6+
67

78
class Question:
8-
def __init__(self, topic, body):
9+
def __init__(self, topic, body, author=None):
910
self.id = uuid.uuid4().int
1011
self.topic = str(topic).strip()
1112
self.body = str(body).strip()
13+
self.author = author
1214

1315
def __repr__(self):
1416
return {
1517
'topic': self.topic,
1618
'body': self.body,
17-
'questionId': self.id
19+
'questionId': self.id,
20+
'author': self.author
1821
}
1922

2023

2124
class Answer:
22-
def __init__(self, body, Qn_Id, pref=False):
25+
def __init__(self, body, Qn_Id, author=None, pref=False):
2326
self.answerId = uuid.uuid4().int
2427
self.body = str(body).strip()
2528
self.Qn_Id = Qn_Id
29+
self.author = author
2630
self.prefered = pref
2731

2832
def __repr__(self):
2933
return {
3034
'answerId': self.answerId,
3135
'Qn_Id': self.Qn_Id,
3236
'body': self.body,
37+
'author': self.author,
3338
'prefered': self.prefered
3439
}
3540

36-
def prefer_answer(self):
37-
self.prefered = True
38-
3941

4042
class User:
4143
def __init__(self, username, email, password):
42-
self.salt = bcrypt.gensalt()
4344
self.id = uuid.uuid4().int
4445
self.username = str(username).strip()
4546
self.email = str(email).strip()
46-
self.password_hash = generate_password_hash(password)
47+
self.password_hash = generate_password_hash(str(password))
4748

4849
def __repr__(self):
4950
return {
@@ -52,3 +53,77 @@ def __repr__(self):
5253
'password': self.password_hash,
5354
'user_id': self.id
5455
}
56+
57+
58+
def valid_username(username):
59+
users = conn.query_all('users')
60+
if len(users) != 0:
61+
for user in users:
62+
existing_user = [user[1]
63+
for user in users if user[1] == username]
64+
if not existing_user:
65+
return True
66+
elif len(users) == 0:
67+
return True
68+
return False
69+
70+
71+
def valid_question(questionObject):
72+
if 'topic' in questionObject.keys() and 'body' in questionObject.keys():
73+
74+
questionsList = conn.query_all('questions')
75+
input_topic = questionObject['topic']
76+
input_body = questionObject['body']
77+
78+
empty_field = len(str(input_topic).strip()) == 0 or len(str(input_body).strip()) == 0
79+
check_type = type(input_topic) == int or type(input_body) == int
80+
if empty_field or check_type:
81+
value = (False, {"hint_1":"Question topic or body should not be empty!",
82+
"hint_2":"body and topic fileds should not consist entirely of integer-type data"})
83+
return value
84+
if questionsList:
85+
topics = [question[1] for question in questionsList if question[1] == input_topic]
86+
if len(topics) != 0:
87+
value = (False, "Question topic already exists!")
88+
return value
89+
else:
90+
if len(topics) == 0:
91+
return (True, )
92+
return (True, )
93+
else:
94+
if 'topic' or 'body' not in questionObject.keys():
95+
return (False, )
96+
97+
98+
def valid_answer(answerObject):
99+
if 'body' in answerObject.keys():
100+
input_body = answerObject['body']
101+
empty_field = len(input_body.strip()) == 0
102+
check_type = type(input_body) == int
103+
if empty_field or check_type:
104+
return (False, {'hint_1': "Answer body should not be empty!",
105+
'hint_2': """body and Qn_Id fileds should not contain
106+
numbers only and string-type data respectively"""}
107+
)
108+
return (True, )
109+
else:
110+
return (False, )
111+
112+
113+
def valid_signup_data(request_data):
114+
keys = request_data.keys()
115+
condition_1 = 'username' in keys and 'email' in keys
116+
condition_2 = 'password' in keys and 'repeat_password' in keys
117+
if condition_1 and condition_2:
118+
return True
119+
else:
120+
return False
121+
122+
123+
def valid_login_data(request_data):
124+
keys = request_data.keys()
125+
condition_1 = 'username' in keys and 'password' in keys
126+
if condition_1:
127+
return True
128+
else:
129+
return False

0 commit comments

Comments
 (0)