def register(): """ basic registration page """ if request.method == 'POST': username = request.form['username'] password = request.form['password'] error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' # ensure the username is not taken else: db = get_mongoDB() result = db.users.find_one({"username": username}) if result is not None: error = 'User {} is already registered.'.format(username) # if error checks pass, insert 'name' and hashed 'password' into database if error is None: addOne = Users(username=username, password=generate_password_hash(password)) addOne.save() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def create_tables(): # creation tables in database try: connection() Users.create_table() except peewee.InternalError as px: config.log.exception('PEEWEE_CREATE_TABLES') print(str(px))
def init_db(): """ populate database with admin user and two posts """ try: version = current_app.config["MONGODB_SETTINGS"]["db"] print('using {} database'.format(version)) except: print( "ERROR: $export APP_SETTINGS=flaskr.config.DevelopmentConfig OR $export APP_SETTINGS=flaskr.config.TestingConfig' to configure database" ) if (version is None): print('raise error: dataBase == None') # if the database version is not 'testDB' and not 'devDB' don't proceed elif ((version != 'testDB') & (version != 'devDB')): print('raise error: not testDB or devDB') else: print('\nrefresh database ...') with current_app.app_context(): db = get_mongoDB() # find all collections in current database collections = db.list_collection_names() for item in collections: db.drop_collection(item) # adding administrative user to setup schema adminUser = Users(username="******", password=generate_password_hash("blackOps")) adminUser.save() # get the admin user objectId admin = db.users.find_one({"username": '******'}) # add two posts to the database post = Posts(post_id=1, username=admin["username"], author_id=ObjectId(str(admin["_id"])), title='first post', body='a first time posting') post2 = Posts(post_id=2, username=admin["username"], author_id=ObjectId(str(admin["_id"])), title='second post', body='second go at it') post.save() post2.save() print('Added admin user and two posts\n')
def change_email(user_id, email=None): if not email: email = input('Введите новый email') query = Users.update({Users.email: email}).where(Users.id == user_id) query.execute() config.log.info(f'id: {user_id} изменил email') return email
def change_number(user_id, number=None): if not number: number = input('Введите новый номер') query = Users.update({Users.number: number}).where(Users.id == user_id) query.execute() config.log.info(f'id: {user_id} изменил номер') return number
def change_name(user_id, name=None): if not name: name = input('Введите новое ФИО') query = Users.update({Users.name: name}).where(Users.id == user_id) query.execute() config.log.info(f'id: {user_id} изменил имя') return name
def get_all_users(): # return list of all users in database query = Users.select() result = [] for i in query: result.append((i.id, i.name, i.number, i.email)) return result
def change_password(user_id, new_pass=None, bot=False): # changing password of user with user_id if not new_pass: new_pass = input('Введите новый пароль: ') hsh = db.operations.check_password(password=new_pass, bot=bot) query = Users.update({Users.password: hsh}).where(Users.id == user_id) query.execute() config.log.info(f'id: {user_id} изменил пароль') return True
def delete_user(user): try: query = Users.delete().where(Users.id == user) query.execute() except: try: query = Users.delete().where(Users.number == user) query.execute() except: try: query = Users.delete().where(Users.email == user) query.execute() except: return False if not query: return False else: config.log.info(f'id: {user} deleted') return 'Пользователь удален'
def find_user(number=None, email=None): # finding user in database with number or email # function return info about found user, or False nick_exist = True query = '' try: if number: query = Users.select().where( Users.number == number.lower().strip()).get() elif email: query = Users.select().where( Users.email == email.lower().strip()).get() except peewee.DoesNotExist: nick_exist = False print('Данного пользователя нет в системе') return False if nick_exist: return (query.id, query.name, query.gender, query.password, query.number, query.email, query.date_creation)
def add_user(name, gender, password, number, email): # adding user in database row = Users( name=name.lower().strip(), gender=gender, password=password, number=number, email=email, ) _save_row(row) config.log.info( f'Регистрация пользователя {name} {gender} {password} {number} {email} прошла успешно' )
def change_gender(user_id=None): while True: gender = input("Если вы мужчина введите 1, если женщина 2: ") if gender == '1': gender = 'Муж' elif gender == '2': gender = 'Жен' else: print('Вы ввели некорректные данные, попробуйте снова') continue break if not user_id: return gender else: query = Users.update({Users.gender: gender}).where(Users.id == user_id) query.execute() config.log.info(f'id: {user_id} изменил пол') return gender
def post(self, request, *args, **kwargs): form = NameForm(request.POST) # Send an email for token value print("fkw") if form.is_valid(): print(form.cleaned_data) # Verifying the token value with the one sent in HA email print("fk") # Verifying that the username is unique if Users.objects.filter(username = form.cleaned_data['username']).count() == 0: user = Users.create(firstname = form.cleaned_data['firstname'], lastname = form.cleaned_data['lastname'], username = form.cleaned_data['username'], password = form.cleaned_data['password']) user.save() print("New Username") else: print("Old Username") # Setting the default email to the HA email return HttpResponseRedirect('/login/')