Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
236 commits
Select commit Hold shift + click to select a range
ddb6759
Add connection.py template as data_handler
kovacsgergo128 Sep 9, 2019
9b028a6
Base server module
Spumi Sep 10, 2019
78492a9
Added: dictionary generator functions for adding questions and answers
Spumi Sep 10, 2019
e60c20f
Added working data directory
Spumi Sep 10, 2019
f5f50f7
Added data files
Spumi Sep 10, 2019
4b4540a
Added csvreader for answers and questions
Spumi Sep 10, 2019
a3b5e2d
Added csv writer and append feature
Spumi Sep 10, 2019
6bea474
Create Home page and List html
mateRajnai Sep 10, 2019
e660fcf
Create list functionality
mateRajnai Sep 10, 2019
975c821
Merge branch 'master' of https://github.com/Spumi/ask-mate-python
mateRajnai Sep 10, 2019
afaba31
Create list functionality
mateRajnai Sep 10, 2019
2af6099
Fixed dictinary value type
Spumi Sep 10, 2019
400b02f
Html layout changes
Spumi Sep 10, 2019
dc08dea
Merge branch 'master' of github.com:Spumi/ask-mate-python
Spumi Sep 10, 2019
fa1b0b1
Fixed merging issues
Spumi Sep 10, 2019
3afcbc9
Add display_question.html template
kovacsgergo128 Sep 10, 2019
c238014
Add get_question and get_answer to data_handler module
kovacsgergo128 Sep 10, 2019
5fc985a
Add /question/<question_id> route to server module
kovacsgergo128 Sep 10, 2019
ae0330c
Refactor question and answer stroring code
Spumi Sep 10, 2019
29d7234
Add link to /list referring to question/guestion id
mateRajnai Sep 10, 2019
8da7bf2
Correct function definition of sorting data
mateRajnai Sep 10, 2019
ce9223a
Merge branch 'master' of https://github.com/Spumi/ask-mate-python
mateRajnai Sep 10, 2019
27337dc
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 10, 2019
0957cfb
Finish question display functionality
kovacsgergo128 Sep 10, 2019
b375ab8
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 10, 2019
db4c22e
Added id generator
Spumi Sep 10, 2019
dd08357
Merge branch 'master' of github.com:Spumi/ask-mate-python
Spumi Sep 10, 2019
5a78ed6
Fix question display funcionality
kovacsgergo128 Sep 10, 2019
332f018
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 10, 2019
412d746
Add general sorting - not complete
mateRajnai Sep 10, 2019
bd87a2b
Create delete_question function
mateRajnai Sep 10, 2019
7cc5119
Add delete question link
mateRajnai Sep 10, 2019
49b2610
Create modify_question_database function
mateRajnai Sep 10, 2019
ff439d0
Add general sorting
mateRajnai Sep 10, 2019
e7ffdb1
Add new question and edit question buttons to display_question template
kovacsgergo128 Sep 10, 2019
138449c
Replace new question button to delete button as new question button …
kovacsgergo128 Sep 10, 2019
b76a48a
Fixed issue causing questions saved as answers
Spumi Sep 11, 2019
eb3e93c
fixed error causing question id for asnwers to be 0
Spumi Sep 11, 2019
b4fdbe1
Fixed: answer id generation
Spumi Sep 11, 2019
c2d59f1
refactor in server module
Spumi Sep 11, 2019
ebe1d6a
Added links to post answers and questions
Spumi Sep 11, 2019
8c08146
Fix URL problem in sort functionality
mateRajnai Sep 11, 2019
4f6ddd0
Fix URL bug in sorting
mateRajnai Sep 11, 2019
3dca545
Added feature: vote up question
Spumi Sep 11, 2019
9935612
Added save questions functinality to data handler
Spumi Sep 11, 2019
6c67802
Add header capitalize to display_question template
kovacsgergo128 Sep 11, 2019
64d494b
fixed list of dict saving in connection module
Spumi Sep 11, 2019
64d59ff
Add edit-question.html template
kovacsgergo128 Sep 11, 2019
fd681f1
Add update question function to data handler module
kovacsgergo128 Sep 11, 2019
3c887bd
Add edit_question route to server
kovacsgergo128 Sep 11, 2019
1487f1c
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 11, 2019
48b45fa
Merge branch 'master' of github.com:Spumi/ask-mate-python
Spumi Sep 11, 2019
c22bc45
Merge branch 'master' of github.com:Spumi/ask-mate-python
Spumi Sep 11, 2019
0c88111
Remove add-question button from index page
mateRajnai Sep 11, 2019
2af0ee5
Correct delete question URL in form tag
mateRajnai Sep 11, 2019
caa6779
Small name modifikation
mateRajnai Sep 11, 2019
0262995
Merge branch 'master' of https://github.com/Spumi/ask-mate-python
mateRajnai Sep 11, 2019
4dcbb07
Fix variable name (id) in edit-question temlate
kovacsgergo128 Sep 11, 2019
5dfe41b
Correct update_question function in data handler module
kovacsgergo128 Sep 11, 2019
c095d98
Correct edit_question route in server
kovacsgergo128 Sep 11, 2019
2f02061
Fixed crash at id generation when the list has 0 elements
Spumi Sep 11, 2019
eac068d
Fix edit_question func in server
kovacsgergo128 Sep 11, 2019
dfbd184
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 11, 2019
0023394
Remove functionality variable
mateRajnai Sep 11, 2019
6951270
Merge branch 'master' of https://github.com/Spumi/ask-mate-python
mateRajnai Sep 11, 2019
8330bf9
Add delete record to data_handler module - currentyl used to delete a…
kovacsgergo128 Sep 11, 2019
ac3825a
Add delete_answer func to server
kovacsgergo128 Sep 11, 2019
603cf9b
Add delete_answer button to display_question template
kovacsgergo128 Sep 11, 2019
f389f12
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 11, 2019
09d7ffc
Create and implement readable date function
mateRajnai Sep 11, 2019
4d59c56
Create and implement readable time function
mateRajnai Sep 11, 2019
ca7b3b6
added vote feature
Spumi Sep 11, 2019
27b0c01
Merge branch 'master' of github.com:Spumi/ask-mate-python
Spumi Sep 11, 2019
d12bb58
Add up and down vote buttons to display_question template
kovacsgergo128 Sep 11, 2019
35e8d68
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 11, 2019
de3de56
Added upload file feature
Spumi Sep 11, 2019
50bdaa3
Fix answer voting buttons
kovacsgergo128 Sep 11, 2019
e3eb79d
Change answer vote function to redirect to particular question site
kovacsgergo128 Sep 11, 2019
bfab5bc
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 11, 2019
49decd2
Add question voting buttons to display_question template
kovacsgergo128 Sep 11, 2019
55854bc
Finish delete question functionality
mateRajnai Sep 11, 2019
85d04bd
Remove redundant data saving function
mateRajnai Sep 11, 2019
5aa9db9
added upload/display image feature
Spumi Sep 11, 2019
df3819d
Merge branch 'master' of github.com:Spumi/ask-mate-python
Spumi Sep 11, 2019
d1e0fe3
Added folder containing user uploaded images
Spumi Sep 11, 2019
9307755
Refactor: Moved functions to util module
Spumi Sep 11, 2019
299eb11
Create %asking if really want to delete% functionality
mateRajnai Sep 11, 2019
7d68832
Add search for keywords functionality
mateRajnai Sep 12, 2019
4e6d82c
Merge add/eedit question pages intoa single html
Spumi Sep 12, 2019
8f766ec
Revert merging of htmls
Spumi Sep 12, 2019
a8a35b1
Fix: issue question upload when not uploading image sometimes causing
Spumi Sep 12, 2019
5294d58
Take out search for question
mateRajnai Sep 12, 2019
b21d1ff
Merge branch 'master' of https://github.com/Spumi/ask-mate-python
mateRajnai Sep 12, 2019
9838fa6
Add search-for-keywords functionality
mateRajnai Sep 12, 2019
8ba2eeb
Page title changes according to context (add/edit)
Spumi Sep 12, 2019
5b1a300
Sorting answers by date on display question page
Spumi Sep 12, 2019
3831ef0
Modify title order on main page
mateRajnai Sep 12, 2019
34efcba
Merge branch 'master' of https://github.com/Spumi/ask-mate-python
mateRajnai Sep 12, 2019
ca96449
Refactor
Spumi Sep 12, 2019
1f0771d
Merge branch 'master' of github.com:Spumi/ask-mate-python
Spumi Sep 12, 2019
e3a319a
Change main page style
mateRajnai Sep 12, 2019
9d3abab
Merge branch 'master' of https://github.com/Spumi/ask-mate-python
mateRajnai Sep 12, 2019
c0cb550
Change main page style
mateRajnai Sep 12, 2019
4023123
Refactor list_question function
mateRajnai Sep 12, 2019
54550ad
Change style
mateRajnai Sep 12, 2019
a62f47c
Fixed issue not showing image on question details
Spumi Sep 12, 2019
f3c3731
Add some design to display question
kovacsgergo128 Sep 12, 2019
355c067
Merge branch 'master' of github.com:Spumi/ask-mate-python
kovacsgergo128 Sep 12, 2019
ee5d4b8
Refactor update question and delete record functions
kovacsgergo128 Sep 12, 2019
42cd9c5
Add 'sure?' question to delete answer feature
kovacsgergo128 Sep 12, 2019
d65bd97
Refactor in server module
Spumi Sep 13, 2019
58a8091
Write docstrings for handle_list_question and convert_to_readable_dat…
mateRajnai Sep 13, 2019
0b57167
Move handle_list_question (util) into list_question (server)
mateRajnai Sep 13, 2019
55c6af3
Move headers form connection to data_handler, delete redundant comments
mateRajnai Sep 13, 2019
9877190
add sql sample data
lterray Aug 28, 2017
f5bf4db
Merge branch 'master' of github.com:Spumi/ask-mate-python
Spumi Sep 23, 2019
ee32710
Add connection and data manager module - connection to database and t…
kovacsgergo128 Sep 23, 2019
e2ecf8a
Add sqlconnection
Spumi Sep 23, 2019
906f2d3
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
Spumi Sep 23, 2019
63b9bb6
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 23, 2019
3fe8205
Implemented: list now questions uses sql
Spumi Sep 23, 2019
eeeb8d8
Delete data_manager.py inserted earlier locally
kovacsgergo128 Sep 23, 2019
7957d0a
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 23, 2019
d45984a
Changed: Added sql support question/answer to sql
Spumi Sep 23, 2019
c19fca1
Rewrite question_display to use database in server module
kovacsgergo128 Sep 23, 2019
5df6d7d
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
Spumi Sep 23, 2019
36cdf1b
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
Spumi Sep 23, 2019
83feb59
delete_question to SQL - refactor needed
mateRajnai Sep 23, 2019
3b211ba
Merge branch '2nd-tw-week' of https://github.com/Spumi/ask-mate-pytho…
mateRajnai Sep 23, 2019
7eabca6
delete_question convert to SQL - refactor needed
mateRajnai Sep 23, 2019
4acabf6
Reworked vote system to use sql instead of csv
Spumi Sep 23, 2019
8f21737
Fixed: htmldoest display missing image when no image is uploaded
Spumi Sep 23, 2019
ff8ca5b
Put search_for_questions to SQL; Search in answer added
mateRajnai Sep 23, 2019
3f8a943
Refactor search_for_questions function
mateRajnai Sep 23, 2019
7388481
Remove sorting_data function from util.py
mateRajnai Sep 23, 2019
c3668bc
Remove convert_to_date function
mateRajnai Sep 23, 2019
cadaa39
Exchange to SQL in delete_question, search_for_keywords
mateRajnai Sep 24, 2019
9ecd92a
Fix bug in delete_question func on server.py
mateRajnai Sep 24, 2019
cc6670a
Change delete_record func in data_handler to use database
kovacsgergo128 Sep 24, 2019
66129c4
Modify delete_answer in server module as database return question_id …
kovacsgergo128 Sep 24, 2019
75f37dd
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 24, 2019
160bfe1
Search keywords in questions put into SQL (search_for_q func)
mateRajnai Sep 24, 2019
db6f21e
Added backend functions for comments
Spumi Sep 24, 2019
961b61b
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
Spumi Sep 24, 2019
93d8368
Added comment display feature to html
Spumi Sep 24, 2019
d8d4eb5
Fixed: User is now retuned to question display after posting a comment
Spumi Sep 24, 2019
e2ac18b
Added display comment feature for answers
Spumi Sep 24, 2019
a80a090
Put search_for_question into SQL with several bugs
mateRajnai Sep 24, 2019
cb0e8a5
Fix bugs in search_for_questions func
mateRajnai Sep 24, 2019
ef45b9f
Simplify search_for_questions; there is a bug to correct
mateRajnai Sep 24, 2019
8c31ec6
Simplify search_for_question; still one bug
mateRajnai Sep 24, 2019
a97e7b5
Add handle_edit_question to util module
kovacsgergo128 Sep 24, 2019
20e5992
Rewrite get_question in data_handler to use database
kovacsgergo128 Sep 24, 2019
b04a218
Rewrite get_question_related_answers in data_handler to use database
kovacsgergo128 Sep 24, 2019
0735772
Add escape_single_quote to data handler
kovacsgergo128 Sep 24, 2019
dc53441
Refactor display_question to use get question and get question relate…
kovacsgergo128 Sep 24, 2019
8d13d0b
Rewrite and rename update_question to update entry which can be used…
kovacsgergo128 Sep 24, 2019
7a59bc7
Rewrite edit question route now it saves the image as well
kovacsgergo128 Sep 24, 2019
6c6a7f7
Cast question_id into string to handle form action properly
kovacsgergo128 Sep 24, 2019
9cbdf8e
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 24, 2019
ca2e960
Correct comment display issues written in email 20190924 20:30
kovacsgergo128 Sep 24, 2019
9d7fe46
Add default ordering by submission time to answer display
kovacsgergo128 Sep 24, 2019
6ad0fe6
Insert escape_single_quote into add new entry function
kovacsgergo128 Sep 24, 2019
8071f9c
Fixed: comment return url
Spumi Sep 25, 2019
fa3eb90
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
Spumi Sep 25, 2019
9d7b039
Fix bug in search_for_question
mateRajnai Sep 25, 2019
74d13c4
Merge branch '2nd-tw-week' of https://github.com/Spumi/ask-mate-pytho…
mateRajnai Sep 25, 2019
5c8bb97
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 25, 2019
99c7a2e
Added Button for comments in ui
Spumi Sep 25, 2019
f0ff7b3
Create adding new_tag_id for tag_question
mateRajnai Sep 25, 2019
2f769f1
Added: edit question comment feature
Spumi Sep 25, 2019
2e9ef48
Added: edit comment for answers
Spumi Sep 25, 2019
06f0095
Add \if new tag\ to tag db - tag_question func
mateRajnai Sep 25, 2019
7dcb15a
Add basic format for tag_question
mateRajnai Sep 25, 2019
94b44f6
Fixed: issue casuing unability to post comments
Spumi Sep 25, 2019
cf4c964
Rewrite get answer to use adatabase - needed for edit answer feature
kovacsgergo128 Sep 25, 2019
1072012
Fix typo in update record function
kovacsgergo128 Sep 25, 2019
822ad61
Change render template to redirect in edit question POST request
kovacsgergo128 Sep 25, 2019
ccca04f
Add edit answer function to server
kovacsgergo128 Sep 25, 2019
642c9b2
Change handle edit question and rename to handle edit entry, so now c…
kovacsgergo128 Sep 25, 2019
3f57181
Change add-answer.html template to handle editing answers as well
kovacsgergo128 Sep 25, 2019
fe67cfe
Added: featrure to delete comments
Spumi Sep 25, 2019
3d6bc12
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 25, 2019
c9aba2e
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 25, 2019
cdc7629
Fixed: The origanalmessage is visible on edit screen
Spumi Sep 25, 2019
085f7da
Add the feature which shows on the '/' route only the last five quest…
kovacsgergo128 Sep 25, 2019
bd59955
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 25, 2019
0fbfc6d
Swap delete and edit buttons on question display template
kovacsgergo128 Sep 25, 2019
8b485ef
Added navlinks and header design
Spumi Sep 25, 2019
63570ae
Added: style for question information section
Spumi Sep 25, 2019
af44f93
Added header title style
Spumi Sep 25, 2019
2c3c663
Create tag_question function - refactor needed
mateRajnai Sep 25, 2019
7f16f40
Added button styles
Spumi Sep 25, 2019
1dfc1c4
Display tags on question/q_id page
mateRajnai Sep 25, 2019
1266a65
Finish tag question - refactor still needed
mateRajnai Sep 25, 2019
004558a
Change list.html for / route: swap sorting buttons to a button point …
kovacsgergo128 Sep 25, 2019
ebbec7d
Change the route of edit answer from /answer/<answer_id> to /answer/<…
kovacsgergo128 Sep 25, 2019
adfc4ff
Create tag_question - refactor is needed
mateRajnai Sep 26, 2019
9b2294a
Add Requirement txt
mateRajnai Sep 26, 2019
6938c70
Refactor util.py: delete not used functions, imports, and change acco…
kovacsgergo128 Sep 26, 2019
0723500
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 26, 2019
90de10d
Update requirements.txt
mateRajnai Sep 26, 2019
1f1f843
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 26, 2019
8f1fd88
Refactor server.py: optimizing imports, PEP8
kovacsgergo128 Sep 26, 2019
e698df3
Fix bug in tag_question
mateRajnai Sep 26, 2019
77f597e
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
kovacsgergo128 Sep 26, 2019
d7c5ca4
Refactor handle add answer in util: delete not used codes
kovacsgergo128 Sep 26, 2019
9410dbc
Css improvements
Spumi Sep 26, 2019
411a2fb
Refactor data_handler: delete not used functions, rows commented out,…
kovacsgergo128 Sep 26, 2019
2d5918d
Refactor server: delete unused ast import
kovacsgergo128 Sep 26, 2019
4a50066
Refactor: delete unused html templates
kovacsgergo128 Sep 26, 2019
90dbc6c
Display question styling
Spumi Sep 26, 2019
6d1da61
Merge branch 'css' into 2nd-tw-week
Spumi Sep 26, 2019
62ed486
Move SQL queries to data_handler except one
mateRajnai Sep 26, 2019
691cae2
Added ccs support for tags
Spumi Sep 26, 2019
361d403
Move SQL to data_handler except one
mateRajnai Sep 26, 2019
29e69f1
Merge branch '2nd-tw-week' of https://github.com/Spumi/ask-mate-pytho…
mateRajnai Sep 26, 2019
5d2de05
Moved answer titles to it's correct location
Spumi Sep 26, 2019
44bd94b
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
Spumi Sep 26, 2019
cd5b481
Added banner to allpages
Spumi Sep 26, 2019
c60278f
Fine-tune question display css
Spumi Sep 26, 2019
b4cea3c
Fixed inabilty to post question comments
Spumi Sep 26, 2019
350f4ea
Move SQL funct to data_handler except list_questions
mateRajnai Sep 26, 2019
1fbd69a
APlly css to main page
Spumi Sep 26, 2019
5b556a6
Merge branch '2nd-tw-week' of github.com:Spumi/ask-mate-python into 2…
Spumi Sep 26, 2019
ddea11f
Added css for edit question
Spumi Sep 26, 2019
b6b10f0
Move SQL to data_hand; refactor list_quest
mateRajnai Sep 26, 2019
2eaa897
Added css for edit question
Spumi Sep 26, 2019
1dfdbdc
Fixed answer comment titles
Spumi Sep 26, 2019
0633535
Added css to search page
Spumi Sep 26, 2019
9f42737
Merge branch '2nd-tw-week' of https://github.com/Spumi/ask-mate-pytho…
mateRajnai Sep 26, 2019
326dc36
Change display image in display question
kovacsgergo128 Sep 27, 2019
9530cbe
Fixed image not sshowing on search screen
Spumi Sep 27, 2019
bc8138f
Refactor display question
Spumi Sep 27, 2019
ce494c3
Fixed missing styles
Spumi Sep 27, 2019
14784a3
Modified db for Sprint 3
Spumi Oct 7, 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
74 changes: 74 additions & 0 deletions connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import csv
import os
import psycopg2
import psycopg2.extras


def csv_to_dict(file_path):
with open(file_path, 'r', newline='') as f:
reader = csv.DictReader(f)
database = [dict(row) for row in reader]
return database


def dict_to_csv(file_path, data, is_answers=False):
with open(file_path, 'w', newline='') as f:
writer = csv.DictWriter(f, ANSWER_DATA_HEADER if is_answers else QUESTION_DATA_HEADER)
writer.writeheader()
writer.writerows(data)


def append_to_csv(file_path, data):
with open(file_path, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerows([data.values()])


# Creates a decorator to handle the database connection/cursor opening/closing.
# Creates the cursor with RealDictCursor, thus it returns real dictionaries, where the column names are the keys.


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
3 changes: 3 additions & 0 deletions data/answer.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,submission_time,vote_number,question_id,message,image
0,1493398154,4,0,"You need to use brackets: my_list = []",
1,1493088154,35,0,"Look it up in the Python docs",
14 changes: 14 additions & 0 deletions data/question.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id,submission_time,view_number,vote_number,title,message,image
0,1493368154,29,7,"How to make lists in Python?","I am totally new to this, any hints?",
1,1493068124,15,9,"Wordpress loading multiple jQuery Versions","I developed a plugin that uses the jquery booklet plugin (http://builtbywill.com/booklet/#/) this plugin binds a function to $ so I cann call $('.myBook').booklet();

I could easy managing the loading order with wp_enqueue_script so first I load jquery then I load booklet so everything is fine.

BUT in my theme i also using jquery via webpack so the loading order is now following:

jquery
booklet
app.js (bundled file with webpack, including jquery)","images/image1.png"
2,1493015432,1364,57,"Drawing canvas with an image picked with Cordova Camera Plugin","I'm getting an image from device and drawing a canvas with filters using Pixi JS. It works all well using computer to get an image. But when I'm on IOS, it throws errors such as cross origin issue, or that I'm trying to use an unknown format.

This is the code I'm using to draw the image (that works on web/desktop but not cordova built ios app)",
228 changes: 228 additions & 0 deletions data_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
from datetime import datetime
from psycopg2 import sql

import connection
from util import string_builder, create_check_keywords_in_database_string, escape_single_quotes
from flask import request


def add_entry(entry, is_answer=False):
table = "answer"
if not is_answer:
table = "question"

query = """INSERT INTO {table}
({columns}) VALUES ({values});
""".format(columns=string_builder(entry.keys()),
values=string_builder(entry.values(), False),
table=table)
execute_query(query)


def get_question(question_id):
question_query = f"""SELECT * FROM question
WHERE id={int(question_id)};"""
question_data = execute_query(question_query)
return question_data


def get_answer(answer_id):
answer_query = f"""SELECT * FROM answer
WHERE id={int(answer_id)}"""
answer_data = execute_query(answer_query)
return answer_data


def get_question_related_answers(question_id):
answers_query = f"""SELECT * FROM answer
WHERE question_id={int(question_id)}
ORDER BY submission_time DESC;"""
answers_of_question = execute_query(answers_query)
return answers_of_question


def update_record(record, is_answer=False):
table = "answer"
record = escape_single_quotes(record)
id_ = record['id']
if not is_answer:
table = "question"
query = f"""UPDATE {table}
SET submission_time={"'" + record['submission_time'] + "'"},
title={"'" + record['title'] + "'"},
message={"'" + record['message'] + "'"},
image={"'" + record['image'] + "'"}
WHERE id={id_};
"""
else:
query = f"""UPDATE {table}
SET submission_time={"'" + record['submission_time'] + "'"},
message={"'" + record['message'] + "'"},
image={"'" + record['image'] + "'"}
WHERE id={id_};
"""

execute_query(query)


def delete_record(id, answer=False, delete=False):
if answer:
question_id_query = f"""SELECT question_id FROM answer
WHERE id={id};"""
delete_answer_query = f"""DELETE FROM answer
WHERE id={id};"""
delete_comment_query = f"""DELETE FROM comment
WHERE answer_id={id};"""
question_id = execute_query(question_id_query)[0]['question_id']

if delete:
execute_query(delete_comment_query)
execute_query(delete_answer_query)

return question_id


@connection.connection_handler
def execute_query(cursor, query):
if query.startswith("SELECT"):
cursor.execute(
sql.SQL(query)
)
result = cursor.fetchall()

else:
result = cursor.execute(query)
return result


def handle_add_comment(req):
req.update(submission_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
query = """INSERT INTO comment ({columns})
VALUES ({value_list})""".format(columns=string_builder(req.keys(), True),
value_list=string_builder(req.values(), False)
)
execute_query(query)


def get_comments(comment_tpe, _id):
comment_tpe += "_id"
query = """SELECT message, submission_time, edited_count, comment.question_id, comment.answer_id, comment.id FROM comment
WHERE {col} = {id} ORDER BY submission_time DESC
""".format(col=comment_tpe, id=_id)
return execute_query(query)


def handle_edit_comment(id, msg):
query = """UPDATE comment
SET message = {msg},
edited_count = COALESCE (edited_count, 0) +1
WHERE id = {id}
""".format(id=id, msg=("'" + msg["message"].replace("'", "''")) + "'")
execute_query(query)


def create_questions_containing_keywords_query(keywords):
questions_containing_keywords_query = """SELECT DISTINCT question.* FROM question
LEFT JOIN answer ON question.id = answer.question_id
WHERE (question.title ILIKE {string_1})
OR (question.message ILIKE {string_2})
OR (answer.message ILIKE {string_3})
""".format(string_1=create_check_keywords_in_database_string(keywords, 'question', 'title'),
string_2=create_check_keywords_in_database_string(keywords, 'question', 'message'),
string_3=create_check_keywords_in_database_string(keywords, 'answer', 'message'))
return questions_containing_keywords_query


def delete_question(question_id):
q = """DELETE FROM comment WHERE question_id = {question_id} OR answer_id = (SELECT id FROM answer WHERE id = {question_id})
""".format(question_id=question_id)
execute_query(q)
q = """DELETE FROM answer WHERE question_id = {question_id}
""".format(question_id=question_id)
execute_query(q)
q = """DELETE FROM question_tag WHERE question_id = {question_id}
""".format(question_id=question_id)
execute_query(q)
q = """DELETE FROM question WHERE id = {question_id}
""".format(question_id=question_id)
execute_query(q)


def get_existing_tags():
existing_tags = [name['name'] for name in
[tag for tag in execute_query("""SELECT id, name FROM tag""")]]
return existing_tags


### REFACTOR IN PROGRESS ###
def tag_question_when_user_choose_from_existing_tags(id):
selected_tag_name = '\'' + request.form.to_dict('selected_tag_name')['selected_tag_name'] + '\''
selected_tag_id = execute_query("""SELECT id FROM tag
LEFT JOIN question_tag ON tag.id = question_tag.tag_id WHERE tag.name = {selected_tag}"""
.format(selected_tag=selected_tag_name))[0]['id']
# Check in question_tag database whether there is a tag to the current question and get the ids...
quest_tag_id_combination = execute_query("""SELECT question_id, tag_id FROM question_tag
WHERE question_id = {q_id} AND tag_id = {t_id}""".format(q_id=id, t_id=selected_tag_id))

# ... if there is not then add new tag id and related question id to question_tag database
if quest_tag_id_combination == []:
execute_query("""INSERT INTO question_tag (question_id, tag_id)
VALUES({q_id}, {t_id})""".format(q_id=id, t_id=selected_tag_id))

### REFACTOR IN PROGRESS ###
def tag_question_when_user_enter_new_tag(id):
new_tag_id = execute_query("""SELECT MAX(id) FROM tag""")[0]['max'] + 1
new_tag_name = '\'' + request.form.get('add_new_tag') + '\'' # ' is needed for the SQL query

quest_tag_id_combination = execute_query("""SELECT question_id, tag_id FROM question_tag
WHERE question_id = {q_id} AND tag_id = (SELECT id FROM tag
WHERE name = {t_name})""".format(q_id=id, t_name=new_tag_name))
if quest_tag_id_combination == []:
execute_query("""INSERT INTO tag (id, name) VALUES({new_tag_id}, {new_tag_name})"""
.format(new_tag_id=new_tag_id, new_tag_name=new_tag_name))

execute_query("""INSERT INTO question_tag (question_id, tag_id)
VALUES({q_id}, {t_id})""".format(q_id=id, t_id=new_tag_id))


def vote_question(_id, vote):
query = """UPDATE question SET vote_number = question.vote_number +{vote}
WHERE id = {id}
""".format(vote=vote,id=_id)
execute_query(query)


def vote_answer(_id, vote):
delta = 1 if vote == "up" else -1
query = """UPDATE answer SET vote_number = vote_number +{vote}
WHERE id = {id}
""".format(vote=delta,id=_id)
execute_query(query)


def get_related_question_id(id):
query = """SELECT answer.question_id FROM answer JOIN comment ON comment.answer_id = answer.id
WHERE answer.id = {id}
""".format(id=id)
result = execute_query(query)
return result.pop()["question_id"]


def get_question_related_tags(question_id):
question_related_tags = execute_query("""SELECT tag.name FROM question_tag LEFT JOIN tag
ON question_tag.tag_id = tag.id WHERE question_tag.question_id = {id}""".format(id=question_id))
return question_related_tags


def delete_comment(comment_id):
query = """DELETE FROM comment WHERE id = {comment_id}
""".format(comment_id=comment_id)
execute_query(query)


def order_questions(order_by, order_direction, is_main):
limit = 'LIMIT 5' if is_main == True else ''
q = """SELECT * FROM question ORDER BY {order_by} {order_direction} {limit}
""".format(order_by=order_by, order_direction=order_direction, limit=limit)
questions = execute_query(q)
return questions
Loading