Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ app/routes/__pycache__
.vscode/
.vscode/settings.json
.pytest_cache/
.coverage
.coverage
.env
22 changes: 15 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
language: python
python:
- "3.6"

- "3.6"
cache: pip
install:
- pip install -r requirements.txt
- pip install -r requirements.txt
- pip install python-coveralls
- pip install pytest
- pip install pytest-cov
- pip install coveralls
services:
- postgresql
before_script:
- psql -c 'create database clvx;' -U postgres


script:
- pytest

after_success:
- coveralls
- pytest --cov=tests/

after_success:
- coveralls
35 changes: 24 additions & 11 deletions connect.py → app/connect.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import os

import psycopg2

# DSN_APP = "dbname='clvx' user='postgres' host='localhost' password='Tesxting' port='5432'"
# DSN_TESTING = "dbname='test_db' user='postgres' host='localhost' password='Tesxting' port='5432'"


class DatabaseConnection(object):
def __init__(self):
if os.getenv('APP_SETTINGS') == "testing":
self.dbname = "test_db"

else:
self.dbname = "clvx"

try:
self.connection = psycopg2.connect(
"dbname='clvx' user='postgres' host='localhost' password='Tesxting' port='5432'")

self.connection = psycopg2.connect(dbname=f"{self.dbname}", user='postgres', host='localhost', password='Tesxting', port='5432')
self.connection.autocommit = True
self.cursor = self.connection.cursor()
self.last_ten_queries = []
Expand Down Expand Up @@ -33,6 +44,7 @@ def create_Questions_table(self):
id serial PRIMARY KEY,
topic varchar(100) NOT NULL,
body varchar(600) NOT NULL,
author varchar(100) NOT NULL,
question_id varchar(150) NOT NULL
);"""
self.cursor.execute(create_table_command)
Expand All @@ -47,6 +59,7 @@ def create_Answers_table(self):
Qn_Id varchar(150) NOT NULL,
body varchar(600) NOT NULL,
answer_id varchar(150) NOT NULL,
author varchar(100) NOT NULL,
prefered boolean
);"""
self.cursor.execute(create_table_command)
Expand Down Expand Up @@ -78,33 +91,39 @@ def insert_new_record(self, tablename, data):
insert_command = """INSERT INTO questions(
topic,
body,
author,
question_id
) VALUES(
%s,
%s,
%s,
%s
);"""
self.cursor.execute(insert_command, (
data['topic'],
data['body'],
data['author'],
data['questionId'])
)
elif tablename == tables[2]:
insert_command = """INSERT INTO answers(
Qn_Id,
body,
answer_id,
author,
prefered
) VALUES(
%s,
%s,
%s,
%s,
%s
);"""
self.cursor.execute(insert_command, (
data['Qn_Id'],
data['body'],
data['answerId'],
data['author'],
data['prefered'])
)
else:
Expand Down Expand Up @@ -173,17 +192,11 @@ def drop_table(self, tablename):
except (Exception, psycopg2.DatabaseError) as error:
print(error)


conn = DatabaseConnection()
#conn.drop_table('answers')
conn.create_Answers_table()
#conn.drop_table('users')
conn.create_Users_table()
#conn.drop_table('questions')
conn.create_Questions_table()

# if not conn.tablename == 'users':
# conn.create_Users_table()
# elif not conn.tablename == 'questions':
# conn.create_Questions_table()
# elif not conn.tablename == 'answers':
# conn.create_Answers_table()
# else:
# pass
93 changes: 84 additions & 9 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
import uuid

import bcrypt
from werkzeug.security import generate_password_hash

from app.connect import conn


class Question:
def __init__(self, topic, body):
def __init__(self, topic, body, author=None):
self.id = uuid.uuid4().int
self.topic = str(topic).strip()
self.body = str(body).strip()
self.author = author

def __repr__(self):
return {
'topic': self.topic,
'body': self.body,
'questionId': self.id
'questionId': self.id,
'author': self.author
}


class Answer:
def __init__(self, body, Qn_Id, pref=False):
def __init__(self, body, Qn_Id, author=None, pref=False):
self.answerId = uuid.uuid4().int
self.body = str(body).strip()
self.Qn_Id = Qn_Id
self.author = author
self.prefered = pref

def __repr__(self):
return {
'answerId': self.answerId,
'Qn_Id': self.Qn_Id,
'body': self.body,
'author': self.author,
'prefered': self.prefered
}

def prefer_answer(self):
self.prefered = True


class User:
def __init__(self, username, email, password):
self.salt = bcrypt.gensalt()
self.id = uuid.uuid4().int
self.username = str(username).strip()
self.email = str(email).strip()
self.password_hash = generate_password_hash(password)
self.password_hash = generate_password_hash(str(password))

def __repr__(self):
return {
Expand All @@ -52,3 +53,77 @@ def __repr__(self):
'password': self.password_hash,
'user_id': self.id
}


def valid_username(username):
users = conn.query_all('users')
if len(users) != 0:
for user in users:
existing_user = [user[1]
for user in users if user[1] == username]
if not existing_user:
return True
elif len(users) == 0:
return True
return False


def valid_question(questionObject):
if 'topic' in questionObject.keys() and 'body' in questionObject.keys():

questionsList = conn.query_all('questions')
input_topic = questionObject['topic']
input_body = questionObject['body']

empty_field = len(str(input_topic).strip()) == 0 or len(str(input_body).strip()) == 0
check_type = type(input_topic) == int or type(input_body) == int
if empty_field or check_type:
value = (False, {"hint_1":"Question topic or body should not be empty!",
"hint_2":"body and topic fileds should not consist entirely of integer-type data"})
return value
if questionsList:
topics = [question[1] for question in questionsList if question[1] == input_topic]
if len(topics) != 0:
value = (False, "Question topic already exists!")
return value
else:
if len(topics) == 0:
return (True, )
return (True, )
else:
if 'topic' or 'body' not in questionObject.keys():
return (False, )


def valid_answer(answerObject):
if 'body' in answerObject.keys():
input_body = answerObject['body']
empty_field = len(input_body.strip()) == 0
check_type = type(input_body) == int
if empty_field or check_type:
return (False, {'hint_1': "Answer body should not be empty!",
'hint_2': """body and Qn_Id fileds should not contain
numbers only and string-type data respectively"""}
)
return (True, )
else:
return (False, )


def valid_signup_data(request_data):
keys = request_data.keys()
condition_1 = 'username' in keys and 'email' in keys
condition_2 = 'password' in keys and 'repeat_password' in keys
if condition_1 and condition_2:
return True
else:
return False


def valid_login_data(request_data):
keys = request_data.keys()
condition_1 = 'username' in keys and 'password' in keys
if condition_1:
return True
else:
return False
Loading