def login_api_route(): request_data = request.get_json() errors_array = [] #Error checking if not ('username' in request_data): errors_array.append( {"message": "You did not provide the necessary fields"}) return jsonify(errors=errors_array), 422 if not ('password' in request_data): errors_array.append( {"message": "You did not provide the necessary fields"}) return jsonify(errors=errors_array), 422 #Get the username and password from the incoming json data user = request_data['username'] pswd = request_data['password'] if user == '' or pswd == '': errors_array.append({"message": "Must submit username and password"}) #Check that Username exists get_user = '******' % (user) check_name = database.read_from_db(get_user) if not check_name: errors_array.append({"message": "Username does not exist"}) return jsonify(errors=errors_array), 404 #Check that the password is correct get_pswd = 'Select password from Users where username = "******";' % (user) check_pswd = database.read_from_db(get_pswd) #A list of dictionaries correct_pass = check_pswd[0][ 'password'] #Get the correct password from the database #Check that the given password will hash to what was stored in the database algorithm = correct_pass[:correct_pass.find('$')] salt = correct_pass[correct_pass.find('$') + 1:correct_pass.rfind('$')] m = hashlib.new(algorithm) m.update(str(salt + pswd).encode('utf-8')) password_hash = m.hexdigest() test = "$".join([algorithm, salt, password_hash]) if test != check_pswd[0]['password']: errors_array.append({"message": "Incorrect Password"}) #If there were errors, reload login page with error messages if len(errors_array) > 0: return jsonify(errors=errors_array), 422 #If we get here, everything is correct and log the user in session['username'] = user return jsonify(username=user)
def index(): name = 'Noname' if request.method == 'POST': name = request.form['name'] add_user(name) _data = read_from_db() return render_template('index.html', data=_data)
def register(): global rank_d global incharge_name_d global station_name_d global state_d global city_d global area_d global postal_code_d select_city_variable = tk.StringVar(root) def submit_cmd(): rank_d = variable_rank.get() incharge_name_d = entry_incharge_name.get() station_name_d = entry_station_name.get() state_d = variable_state.get() city_d = select_city_variable.get() area_d = entry_area.get() postal_code_d = entry_postal_code.get() sql = f"INSERT INTO `police_station` ( `rank`, `incharge_name`, `station_name`, `state`, `city`, `area`, `postalcode`) VALUES (\"{rank_d}\", \"{incharge_name_d}\", \"{station_name_d}\", \"{state_d}\", \"{city_d}\", \"{area_d}\", {int(postal_code_d)})" mycursor.execute(sql) c.execute(sql) mydb.commit() conn.commit() root.destroy() def config_entry(*a): rank_d = variable_rank.get() state_name_dict = dict(database.read_from_db(tablename='state')) state_name_list = list(state_name_dict.values()) city_name_list = [] root.title("Register Police Station") root.geometry("550x300") # root.state('zoomed') f1 = tk.Frame(root) f1.grid(row=0, column=0) f2 = tk.Frame(root) f2.grid(row=1, column=0) rank = [ 'DGP', 'CP', 'SDG', 'ADG', 'IG', 'DIG', 'JOINT CP', 'SSP', 'SDCP', 'SP', 'DCP', 'Dy.SP', 'ACP', 'ASP', 'PI', 'SI', 'ASI' ] def ass(*a): return city_name_list def cities(*a): state_id = int() state = variable_state.get() for key, value in state_name_dict.items(): if state == value: state_id = key city_names = list( database.read_from_db_condition(tablename='cities', state=state_id, column='city')) for x in city_names: city_name_list.append(x[0]) select_city = tk.Label(f2, text="Select City", width=20, font=("bold", 10)) select_city.grid(row=8, column=2) entry_city_w = tk.OptionMenu(f2, select_city_variable, *city_name_list) entry_city_w.grid(row=8, column=6) city_d = select_city_variable.get() registrationForm = tk.Label(f1, text="Register Police Station", width=20, font=("bold", 20)) registrationForm.grid(row=1, column=1) name = tk.Label(f2, text="Station Incharge name", width=20, font=("bold", 10)) name.grid(row=3, column=2) variable_rank = tk.StringVar(root) drop_incharge_rank = tk.OptionMenu(f2, variable_rank, *rank) drop_incharge_rank.grid(row=3, column=4) drop_incharge_rank.config(width=3, font=('Helvetica', 8)) variable_rank.trace('w', config_entry) entry_incharge_name = tk.Entry(f2) entry_incharge_name.grid(row=3, column=6) rank_d = variable_rank.get() incharge_name_d = entry_incharge_name.get() station_name = tk.Label(f2, text="Station Name", width=20, font=("bold", 10)) station_name.grid(row=4, column=2) entry_station_name = tk.Entry(f2) entry_station_name.grid(row=4, column=6) station_name_d = entry_station_name.get() # city = tk.Label(f2, text="Select City", width=20, font=("bold", 10)) # city.grid(row=8, column=2) # variable_city = tk.StringVar(root) # entry_cityw = tk.OptionMenu(root, variable_city , *city_name_list) # entry_cityw.grid(row=8, column=6) # entry_cityw.config(width=10, font=('Helvetica', 12)) states = tk.Label(f2, text="Select State", width=20, font=("bold", 10)) states.grid(row=6, column=2) variable_state = tk.StringVar(f2) variable_state.set('GUJRAT') entry_state = tk.OptionMenu(f2, variable_state, *state_name_list) entry_state.grid(row=6, column=6) entry_state.config(width=10, font=('Helvetica', 12)) variable_state.trace('w', cities) state_d = variable_state.get() area = tk.Label(f2, text="Area Name", width=20, font=("bold", 10)) area.grid(row=10, column=2) entry_area = tk.Entry(f2) entry_area.grid(row=10, column=6) area_d = entry_area.get() postal_code = tk.Label(f2, text="Postal Code", width=20, font=("bold", 10)) postal_code.grid(row=12, column=2) entry_postal_code = tk.Entry(f2) entry_postal_code.grid(row=12, column=6) postal_code_d = entry_area.get() submit_btn = tk.Button(f2, text='Submit', width=20, bg='brown', fg='white', command=submit_cmd) submit_btn.grid(row=16, column=7) root.mainloop()
def all_json(): books = [b.make_clean_dict() for b in database.read_from_db()] return {"book": books}
InputArray = [] Username = "******" InputArray.append(Username) InputArray.append(individualDepressionLevel) InputArray.append(individualAnxietyLevel) InputArray.append(individualStressLevel) for x in range(26, 44): InputArray.append(AnswerArrayCP[x]) InputArray.append(maxIndValueOfDAS) # If this is the first execution, do not compare "Old" values with "new" ones if database.check_existence(Username) is None: print("No previous data found") # Else, compare the values and run the Knowledge Engine Two after Knowledge Engine One, too else: dbOldFactsArray = [] dbOldFactsArray = database.read_from_db(Username) # is this really an array # Start Knowledge-Engine engineTwo = KnowledgeEngineTwo.ComparingOldInputWithNew() engineTwo.reset() # Prepare the engine for the execution. # EngineTwo Declaring New Facts engineTwo.declare(KnowledgeEngineTwo.NewFinancialFact(financialDistress=AnswerArrayCP[26])) engineTwo.declare(KnowledgeEngineTwo.NewFinancialFact(employment=AnswerArrayCP[27])) engineTwo.declare(KnowledgeEngineTwo.NewFamilyFact(isCaretaker=AnswerArrayCP[28])) engineTwo.declare(KnowledgeEngineTwo.NewFamilyFact(getsEnoughSupport=AnswerArrayCP[29])) engineTwo.declare(KnowledgeEngineTwo.NewFamilyFact(caringForAdults=AnswerArrayCP[30])) engineTwo.declare(KnowledgeEngineTwo.NewFamilyFact(caringForU18Children=AnswerArrayCP[31])) engineTwo.declare(KnowledgeEngineTwo.NewFamilyFact(caringForDisabledChildren=AnswerArrayCP[32])) engineTwo.declare(KnowledgeEngineTwo.NewLeisureFact(enoughTimeForOneself=AnswerArrayCP[33])) engineTwo.declare(KnowledgeEngineTwo.NewLeisureFact(leisureTimePlanned=AnswerArrayCP[34]))
def create_user(): print('creating user') request_data = request.get_json() errors_array = [] #Error checking: See if there is a missing field in the json data if not ('username' in request_data and 'firstname' in request_data and 'lastname' in request_data and 'password1' in request_data and 'password2' in request_data and 'email' in request_data): errors_array.append( {"message": "You did not provide the necessary fields"}) return jsonify(errors=errors_array), 422 #Check that all fields contained something user = request_data['username'] first = request_data['firstname'] last = request_data['lastname'] pass1 = request_data['password1'] pass2 = request_data['password2'] email = request_data['email'] rating = request_data['rating'] city = request_data['city'] state = request_data['state'] if user == '' or first == '' or last == '' or pass1 == '' or pass2 == '' or email == '' or rating == '' or city == '' or state == '': errors_array.append( {'message': 'You did not provide the necessary fields'}) return jsonify(errors=errors_array), 422 #Check length requirements if len(user) > 20: errors_array.append( {'message': 'Username must be no longer than 20 characters'}) if len(first) > 20: errors_array.append( {'message': 'First name must be no longer than 20 characters'}) if len(last) > 20: errors_array.append( {'message': 'Last name must be no longer than 20 characters'}) if len(pass1) > 256 or len(pass2) > 256: errors_array.append( {'message': 'Password must be no longer than 256 characters'}) if len(pass1) < 8 or len(pass2) < 8: errors_array.append( {'message': 'Password must be longer than 8 characters'}) if len(email) > 40: errors_array.append( {'message': 'Email must be no longer than 40 characters'}) if len(rating) > 3: errors_array.append( {'message': 'Rating must be no longer than 3 characters'}) if len(city) > 20: errors_array.append( {'message': 'City must be no longer than 20 characters'}) if len(state) > 20: errors_array.append( {'message': 'State must be no longer than 20 characters'}) #Check if userna is taken get_user = '******' % (user) user_taken = database.read_from_db(get_user) if user_taken: errors_array.append({ 'message': 'This username is taken. Please try a different username.' }) #Check for punctuation in usernames and passwords. Usernames can contain letters, numbers, and underscores acceptable = list(string.ascii_letters) acceptable.extend(list(string.digits)) acceptable.extend(['_']) for letter in user: if not letter in acceptable: errors_array.append({ 'message': 'Invalid username. Usernames can only contain letters, numbers, and underscores.' }) break for letter in pass1: if not letter in acceptable: errors_array.append({ 'message': 'Invalid password. Passwords can only contain letters, numbers, and underscores.' }) break #Check for at least one letter and number in acceptable #Check for one letter and number in a password count_letter = 0 count_digit = 0 for char in pass1: if char.isdigit(): count_digit += 1 if char.isalpha(): count_letter += 1 if count_digit == 0 or count_letter == 0: errors_array.append({ "message": "Passwords must contain at least one letter and one number" }) #Check that the passwords are equal if pass1 != pass2: errors_array.append({'message': 'Passwords must match'}) #Check that email is valid if not re.match(r"[^@]+@[^@]+\.[^@]+", email): errors_array.append({"message": "Email address must be valid"}) if len(errors_array) > 0: return jsonify(errors=errors_array), 422 #At this point, all inputs are correctly formatted. #Hash the password and insert these values into the database algorithm = "sha512" salt = uuid.uuid4().hex m = hashlib.new(algorithm) m.update(str(salt + pass1).encode('utf-8')) password_hash = m.hexdigest() real_pass = "******".join([algorithm, salt, password_hash]) #Insert values into database create_new_user = '******' % ( user, first, last, real_pass, email, rating, city, state) print(create_new_user) database.write_to_db(create_new_user) #Start new session for the user session['username'] = user #Returns a json object with the same fields that it came with return jsonify(username=user, firstname=first, lastname=last, password1=pass1, password2=pass2, email=email, rating=rating, city=city, state=state), 201
def dump_db_to_dir(output_dir): for book in database.read_from_db(): write_book_json(output_dir, book)