11import uuid
22
3- import bcrypt
43from werkzeug .security import generate_password_hash
54
5+ from app .connect import conn
6+
67
78class 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
2124class 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
4042class 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