def change_info():
    if "userid" not in session:
        return redirect('/login')
    form = ChangeInfoForm()
    if form.password_hash.data:
        password_hash = generate_password_hash(form.password_hash.data)
    else:
        password_hash = ''
    keys = {'user_name': form.user_name.data, 'password_hash': password_hash}
    users = UsersModel(db.get_connection())
    if form.validate_on_submit():
        if not users.get_by_name(form.user_name.data):
            if form.main_photo.data != '':
                f = request.files['main_photo']
                save_filename, thmb_filename = save_file(f)
                users.update_user_info(session['userid'], 'main_photo',
                                       thmb_filename)
            for key in keys:
                if keys[key] != '':
                    users.update_user_info(session['userid'], key, keys[key])
            return redirect('/')
        else:
            form.username.errors.append(
                'Пользователь с таким именем уже существует')
    user_data = users.get(session['userid'])
    user_info = {'username': user_data[1], 'main_photo': user_data[4]}
    return render_template('project_change_info.html',
                           form=form,
                           user_info=user_info,
                           sess_info=user_info)
Exemple #2
0
 def save(self):
     users = UsersModel(self.db.get_connection())
     if self.new_name.text() != '':
         user = UsersModel(self.db.get_connection()).get_by_name(
             self.new_name.text())
     else:
         user = None
     if not user and self.new_name.text() != '':
         users.update_user_info(self.current_user[0], 'user_name',
                                self.new_name.text())
         self.current_user = UsersModel(
             self.db.get_connection()).get_by_name(self.new_name.text())
         self.current_page = self.current_user
         self.warning.setText('')
     elif user and self.new_name.text() != '':
         if user[1] != self.current_user[1]:
             self.warning.setText('Это имя пользователя уже занято.')
         elif user[1] == self.current_user[1]:
             self.warning.setText('')
     if not user or user[1] == self.current_user[1]:
         if self.new_password.text() != '' and self.new_password.text(
         ) == self.new_password_2.text():
             users.update_user_info(self.current_user[0], 'password_hash',
                                    self.new_password.text())
             self.current_user = UsersModel(
                 self.db.get_connection()).get_by_name(self.current_user[1])
             self.current_page = self.current_user
             self.warning.setText('')
         elif self.new_password.text() != self.new_password_2.text():
             self.warning.setText('Пароли не совпадают.')
     elif user[1] != self.current_user[1]:
         self.warning.setText('Это имя пользователя уже занято.')
     if self.filename.text() != '':
         save_filename, thmb_filename = save_file(self.filename.text(),
                                                  self.current_user[0])
         users.update_user_info(self.current_user[0], 'main_photo',
                                thmb_filename)
         self.current_page = self.current_user
     if self.warning.text() == '':
         self.show_page()