Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
a52a989
Printing questions
matolcsim Mar 5, 2019
365bfe9
Started connection and data manager modules
matolcsim Mar 6, 2019
d33374e
Ask a Questionfeature
kolosg Mar 6, 2019
a102d43
post answer feature done.
Vitya94 Mar 7, 2019
10fe484
question redirect fixed
Vitya94 Mar 7, 2019
2bc986d
edit and delete buttons added
matolcsim Mar 7, 2019
b07401d
edit question feature implemented
matolcsim Mar 7, 2019
25b1db8
fix id bugs in csv files
matolcsim Mar 7, 2019
4b1f3b2
print(hack) = True / delete question feature done
kolosg Mar 7, 2019
000495d
delete an answer feature
Vitya94 Mar 7, 2019
8f00049
View number increase and Styling of buttons
matolcsim Mar 8, 2019
579586c
Go back to homepage
matolcsim Mar 8, 2019
474114b
add sql sample data
lterray Aug 28, 2017
bda4f77
database connection.py created
matolcsim Mar 18, 2019
e937787
Listing questions and answers with SQL
matolcsim Mar 18, 2019
e64ead2
add question in process
Vitya94 Mar 18, 2019
f7a1b1a
ask, edit, delete a question feature
Vitya94 Mar 19, 2019
aecf86b
Post answer feature
matolcsim Mar 19, 2019
2ee54ac
delete answer feature
kolosg Mar 19, 2019
b3930f7
delete q&a, increase view number
Vitya94 Mar 19, 2019
9e55a1a
main page and list page created, buttons fixed
kolosg Mar 19, 2019
1dcf94e
Post comment to question done, margins fixed
matolcsim Mar 19, 2019
e7f333e
labels fixed
matolcsim Mar 19, 2019
5ebf5ad
Collapsable comments feature
matolcsim Mar 20, 2019
e83f99c
collapsible tables for comments
matolcsim Mar 20, 2019
51cb346
sorted comments
matolcsim Mar 20, 2019
ed73103
edit question comment feature
kolosg Mar 20, 2019
d8c1a0e
delete question comment
Vitya94 Mar 20, 2019
e23bb45
comment table padding
matolcsim Mar 20, 2019
fb6f040
Answer comments(edit, delete, post)
matolcsim Mar 20, 2019
d121e3c
answer comments
Vitya94 Mar 21, 2019
e40a078
see comments of answers done
Vitya94 Mar 21, 2019
71bfd00
edit answer feature done
kolosg Mar 21, 2019
00d3c34
side border on collapsable table
matolcsim Mar 21, 2019
59ca297
delete comment half done
matolcsim Mar 21, 2019
f823e70
Delete answer comments fully done
matolcsim Mar 21, 2019
fa63bb4
delete answer bug fixed
matolcsim Mar 21, 2019
9eb7c8f
Question comment bug fixed
matolcsim Mar 21, 2019
61d1fcc
search almost done
kolosg Mar 21, 2019
854bc30
search feature fixed - search result not!
kolosg Mar 21, 2019
1c63f28
Search results done
matolcsim Mar 21, 2019
52e144b
search feature done bruh
kolosg Mar 21, 2019
1469486
highlighted search results
Vitya94 Mar 21, 2019
ae559ab
Counting answer comments halfway done
matolcsim Mar 21, 2019
1c05a8c
No comments for answer feature
matolcsim Mar 22, 2019
80f0e2e
Search bar on all question
matolcsim Mar 22, 2019
cfa9a38
bg feature done?
matolcsim Apr 1, 2019
0c73a9a
maybe this one?
matolcsim Apr 1, 2019
e3cc520
Merge pull request #1 from kolosg/feature/bg
Vitya94 Apr 1, 2019
48903ff
login bar and header started
matolcsim Apr 1, 2019
880aca3
fixed conflicts
matolcsim Apr 1, 2019
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
37 changes: 37 additions & 0 deletions connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import csv


def read_data_from_csv(filename, data_list):
with open(filename, 'r') as file:
reader = csv.DictReader(file)
for line in reader:
data_list.append(line)

return data_list


def write_data_to_csv(filename, fields, data_list):
with open(filename, 'w') as file:
writer = csv.DictWriter(file, fieldnames=fields)
writer.writeheader()
for data in data_list:
if filename == "templates/question.csv":
writer.writerow(
{'id': data['id'], 'submission_time': data['submission_time'], 'view_number': data['view_number'],
'vote_number': data['vote_number'], 'title': data['title'], 'message': data['message'],
'image': data['image']})
else:
writer.writerow(
{'id': data['id'], 'submission_time': data['submission_time'], 'vote_number': data['vote_number'],
'question_id': data['question_id'], 'message': data['message'], 'image': data['image']})


def append_data_to_csv(filename, fields, new_id, current_date, title_or_question_id, message):
with open(filename, "a") as file:
writer = csv.DictWriter(file, fieldnames=fields)
if filename == "templates/question.csv":
writer.writerow({'id': new_id, 'submission_time': current_date, 'view_number': 1, 'vote_number': 0,
'title': title_or_question_id, 'message': message, 'image': ""})
else:
writer.writerow({'id': new_id, 'submission_time': current_date, 'vote_number': 0,
'question_id': title_or_question_id, 'message': message, 'image': ""})
266 changes: 266 additions & 0 deletions data_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
import database_connection
from datetime import datetime


@database_connection.connection_handler
def list_all_question(cursor):
cursor.execute("""
SELECT * FROM Question
ORDER BY submission_time DESC;
""")
all_question = cursor.fetchall()
return all_question

@database_connection.connection_handler
def list_answers(cursor):
cursor.execute("""
SELECT * FROM answer
ORDER BY submission_time DESC;
""")
all_answer = cursor.fetchall()
return all_answer


def count_answers(quest_id, func):
table = func
return any(record['question_id'] == int(quest_id) for record in table)


def count_comments(quest_id):
table = select_comments()
return any(record['question_id'] == int(quest_id) for record in table if record['answer_id'] == None)


def count_answer_comments(quest_id):
bools = []
answers = list_answers()
comments = select_comments()
for answer in answers:
if answer['question_id'] == int(quest_id):
bools.append(any(comment['answer_id'] == answer['id'] for comment in comments))

return bools



def last_answer_comment(dict, list_of_bools):
for i in range(len(dict)):
for j in range(len(list_of_bools)):
if i == j:
dict[i]['bool'] = list_of_bools[j]

return dict


@database_connection.connection_handler
def ask_new_question(cursor, title, message):
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

cursor.execute("""
INSERT INTO question (submission_time, view_number, vote_number, title, message)
VALUES (%(dt)s, 0, 0, %(add_title)s, %(add_message)s)
""", dict(dt=dt, add_title=title, add_message=message))



@database_connection.connection_handler
def update_question(cursor, title, message, quest_id):
cursor.execute("""
UPDATE question
SET title = %(title)s, message = %(message)s
WHERE id = %(quest_id)s
""", dict(title=title, message=message, quest_id=quest_id))


@database_connection.connection_handler
def delete_question(cursor, quest_id):
cursor.execute("""
DELETE FROM comment
WHERE question_id = %(quest_id)s
""", dict(quest_id=quest_id))
cursor.execute("""
DELETE FROM answer
WHERE question_id = %(quest_id)s
""", dict(quest_id=quest_id))
cursor.execute("""
DELETE FROM question
WHERE id = %(quest_id)s
""", dict(quest_id=quest_id))




@database_connection.connection_handler
def get_latest_id(cursor):
cursor.execute("""
SELECT id FROM question
ORDER BY id DESC
LIMIT 1
""")
return cursor.fetchone()


@database_connection.connection_handler
def post_answer(cursor, quest_id, message):
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute("""
INSERT INTO answer(submission_time, vote_number, question_id, message)
VALUES(%(dt)s, 0, %(quest_id)s, %(message)s)
""", dict(dt=dt, quest_id=quest_id, message=message))

@database_connection.connection_handler
def delete_answer(cursor, answer_id):
cursor.execute("""
DELETE FROM comment
WHERE answer_id = %(answer_id)s
""", dict(answer_id=answer_id))
cursor.execute("""
DELETE FROM answer
WHERE id = %(answer_id)s
""", dict(answer_id=answer_id))


@database_connection.connection_handler
def get_question_id_to_delete(cursor, answer_id):
cursor.execute("""
SELECT question_id FROM answer
WHERE id = %(answer_id)s
""", dict(answer_id=answer_id))
return cursor.fetchone()


@database_connection.connection_handler
def increase_view_number(cursor, quest_id):
cursor.execute("""
UPDATE question
SET view_number = view_number + 1
WHERE id = %(quest_id)s
""", dict(quest_id=quest_id))


@database_connection.connection_handler
def list_latest_questions(cursor):
cursor.execute("""
SELECT * FROM Question
ORDER BY submission_time DESC
LIMIT 5
""")
latest_questions = cursor.fetchall()
return latest_questions


@database_connection.connection_handler
def post_comment_to_question(cursor, quest_id, message):
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute("""
INSERT INTO comment(question_id, message, submission_time)
VALUES(%(quest_id)s, %(message)s, %(dt)s)
""", dict(quest_id=quest_id, message=message, dt=dt))


@database_connection.connection_handler
def select_comments(cursor):
cursor.execute("""
SELECT submission_time, message, question_id, id, answer_id FROM comment
ORDER BY submission_time DESC
""")
comments = cursor.fetchall()
return comments


@database_connection.connection_handler
def get_comment_ids(cursor):
cursor.execute("""
SELECT id FROM comment
""")
ids = cursor.fetchall()
return str(ids['id'])


@database_connection.connection_handler
def update_comment(cursor, message, comment_id):
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute("""
UPDATE comment
SET message = %(message)s, submission_time = %(dt)s
WHERE id = %(comment_id)s
""", dict(message=message, comment_id=comment_id, dt=dt))

@database_connection.connection_handler
def get_question_id(cursor, comment_id):
cursor.execute("""
SELECT question_id FROM comment
WHERE id = %(comment_id)s
""", dict(comment_id=comment_id))
return cursor.fetchone()


@database_connection.connection_handler
def delete_comment(cursor, comment_id):
cursor.execute("""
DELETE FROM comment
WHERE id = %(comment_id)s
""", dict(comment_id=comment_id))


@database_connection.connection_handler
def get_question_id_from_answers(cursor, answer_id):
cursor.execute("""
SELECT answer.id, answer.question_id, comment.answer_id
FROM answer
INNER JOIN comment ON comment.answer_id = answer_id
WHERE answer.id = %(answer_id)s
""", dict(answer_id=answer_id))
return cursor.fetchone()


@database_connection.connection_handler
def post_comment_to_answer(cursor, quest_id, answer_id, message):
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute("""
INSERT INTO comment(question_id, answer_id, message, submission_time)
VALUES(%(quest_id)s, %(answer_id)s, %(message)s, %(dt)s)
""", dict(quest_id=int(quest_id), answer_id=answer_id, message=message, dt=dt))

@database_connection.connection_handler
def update_answer(cursor, message, answer_id):
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute("""
UPDATE answer
SET message = %(message)s, submission_time = %(dt)s
WHERE id = %(answer_id)s
""", dict(message=message, answer_id=answer_id, dt=dt))


@database_connection.connection_handler
def question_results(cursor, search):
cursor.execute("""
SELECT * FROM question
WHERE title ILIKE %(search)s OR message ILIKE %(search)s
ORDER BY submission_time DESC
""", dict(search=search))
result = cursor.fetchall()
return result

@database_connection.connection_handler
def answer_results(cursor, search):
cursor.execute("""
SELECT * FROM answer
WHERE message ILIKE %(search)s
ORDER BY submission_time DESC
""", dict(search=search))
result = cursor.fetchall()
return result

def add_selector_to_search_result(search_phrase, dicts):
for dict in dicts:
for key, value in dict.items():
if key == 'message' or key == 'title':
if search_phrase.lower() in value:
dict[key] = dict[key].replace(search_phrase.lower(), '<span id="highlight">' + search_phrase.lower() + '</span>')
elif search_phrase.upper() in value:
dict[key] = dict[key].replace(search_phrase.upper(), '<span id="highlight">' + search_phrase.upper() + '</span>')
elif search_phrase.capitalize() in value:
dict[key] = dict[key].replace(search_phrase.capitalize(), '<span id="highlight">' + search_phrase.capitalize() + '</span>')

return dicts
49 changes: 49 additions & 0 deletions database_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import psycopg2
import psycopg2.extras


def get_connection_string():
# setup connection string
# to do this, please define these environment variables first
user_name = os.environ.get('PSQL_USER_NAME')
password = os.environ.get('PSQL_PASSWORD')
host = os.environ.get('PSQL_HOST')
database_name = os.environ.get('PSQL_DB_NAME')

env_variables_defined = user_name and password and host and database_name

if env_variables_defined:
# this string describes all info for psycopg2 to connect to the database
return 'postgresql://{user_name}:{password}@{host}/{database_name}'.format(
user_name=user_name,
password=password,
host=host,
database_name=database_name
)
else:
raise KeyError('Some necessary environment variable(s) are not defined')


def open_database():
try:
connection_string = get_connection_string()
connection = psycopg2.connect(connection_string)
connection.autocommit = True
except psycopg2.DatabaseError as exception:
print('Database connection problem')
raise exception
return connection


def connection_handler(function):
def wrapper(*args, **kwargs):
connection = open_database()
# we set the cursor_factory parameter to return with a RealDictCursor cursor (cursor which provide dictionaries)
dict_cur = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
ret_value = function(dict_cur, *args, **kwargs)
dict_cur.close()
connection.close()
return ret_value

return wrapper
Loading