Upgrade from Wt3 to Wt4
This commit is contained in:
@@ -17,13 +17,13 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <Wt/WString>
|
||||
#include <Wt/WPushButton>
|
||||
#include <Wt/WComboBox>
|
||||
#include <Wt/WLineEdit>
|
||||
#include <Wt/WString.h>
|
||||
#include <Wt/WPushButton.h>
|
||||
#include <Wt/WComboBox.h>
|
||||
#include <Wt/WLineEdit.h>
|
||||
|
||||
#include <Wt/WFormModel>
|
||||
#include <Wt/WStringListModel>
|
||||
#include <Wt/WFormModel.h>
|
||||
#include <Wt/WStringListModel.h>
|
||||
|
||||
#include "common/Validators.hpp"
|
||||
#include "database/MediaDirectory.hpp"
|
||||
@@ -46,8 +46,8 @@ class DatabaseSettingsModel : public Wt::WFormModel
|
||||
static const Field UpdatePeriodField;
|
||||
static const Field UpdateStartTimeField;
|
||||
|
||||
DatabaseSettingsModel(Wt::WObject *parent = 0)
|
||||
: Wt::WFormModel(parent)
|
||||
DatabaseSettingsModel()
|
||||
: Wt::WFormModel()
|
||||
{
|
||||
initializeModels();
|
||||
|
||||
@@ -55,7 +55,7 @@ class DatabaseSettingsModel : public Wt::WFormModel
|
||||
addField(UpdatePeriodField);
|
||||
addField(UpdateStartTimeField);
|
||||
|
||||
DirectoryValidator* dirValidator = new DirectoryValidator();
|
||||
auto dirValidator = std::make_shared<DirectoryValidator>();
|
||||
dirValidator->setMandatory(true);
|
||||
setValidator(MediaDirectoryField, dirValidator);
|
||||
|
||||
@@ -66,42 +66,42 @@ class DatabaseSettingsModel : public Wt::WFormModel
|
||||
loadData();
|
||||
}
|
||||
|
||||
Wt::WAbstractItemModel *updatePeriodModel() { return _updatePeriodModel; }
|
||||
Wt::WAbstractItemModel *updateStartTimeModel() { return _updateStartTimeModel; }
|
||||
std::shared_ptr<Wt::WAbstractItemModel> updatePeriodModel() { return _updatePeriodModel; }
|
||||
std::shared_ptr<Wt::WAbstractItemModel> updateStartTimeModel() { return _updateStartTimeModel; }
|
||||
|
||||
void loadData()
|
||||
{
|
||||
using namespace Database;
|
||||
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
Wt::Dbo::Transaction transaction(LmsApp->getDboSession());
|
||||
|
||||
std::vector<MediaDirectory::pointer> mediaDirectories = MediaDirectory::getAll(DboSession());
|
||||
std::vector<MediaDirectory::pointer> mediaDirectories = MediaDirectory::getAll(LmsApp->getDboSession());
|
||||
if (!mediaDirectories.empty())
|
||||
setValue(MediaDirectoryField, mediaDirectories.front()->getPath().string());
|
||||
|
||||
auto periodRow = getUpdatePeriodModelRow( Scanner::getUpdatePeriod(DboSession()) );
|
||||
auto periodRow = getUpdatePeriodModelRow( Scanner::getUpdatePeriod(LmsApp->getDboSession()) );
|
||||
if (periodRow)
|
||||
setValue(UpdatePeriodField, updatePeriodString(*periodRow));
|
||||
|
||||
auto startTimeRow = getUpdateStartTimeModelRow( Scanner::getUpdateStartTime(DboSession()) );
|
||||
auto startTimeRow = getUpdateStartTimeModelRow( Scanner::getUpdateStartTime(LmsApp->getDboSession()) );
|
||||
if (startTimeRow)
|
||||
setValue(UpdateStartTimeField, updateStartTimeString(*startTimeRow) );
|
||||
}
|
||||
|
||||
void saveData()
|
||||
{
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
Wt::Dbo::Transaction transaction(LmsApp->getDboSession());
|
||||
|
||||
MediaDirectory::eraseAll(DboSession());
|
||||
MediaDirectory::create(DboSession(), boost::any_cast<Wt::WString>(value(MediaDirectoryField)).toUTF8());
|
||||
MediaDirectory::eraseAll(LmsApp->getDboSession());
|
||||
MediaDirectory::create(LmsApp->getDboSession(), Wt::cpp17::any_cast<Wt::WString>(value(MediaDirectoryField)).toUTF8());
|
||||
|
||||
auto updatePeriodRow = getUpdatePeriodModelRow( boost::any_cast<Wt::WString>(value(UpdatePeriodField)));
|
||||
auto updatePeriodRow = getUpdatePeriodModelRow( Wt::cpp17::any_cast<Wt::WString>(value(UpdatePeriodField)));
|
||||
assert(updatePeriodRow);
|
||||
Scanner::setUpdatePeriod(DboSession(), updatePeriod(*updatePeriodRow));
|
||||
Scanner::setUpdatePeriod(LmsApp->getDboSession(), updatePeriod(*updatePeriodRow));
|
||||
|
||||
auto startTimeRow = getUpdateStartTimeModelRow( boost::any_cast<Wt::WString>(value(UpdateStartTimeField)));
|
||||
auto startTimeRow = getUpdateStartTimeModelRow( Wt::cpp17::any_cast<Wt::WString>(value(UpdateStartTimeField)));
|
||||
assert(startTimeRow);
|
||||
Scanner::setUpdateStartTime(DboSession(), updateStartTime(*startTimeRow));
|
||||
Scanner::setUpdateStartTime(LmsApp->getDboSession(), updateStartTime(*startTimeRow));
|
||||
}
|
||||
|
||||
boost::optional<int> getUpdatePeriodModelRow(Wt::WString value)
|
||||
@@ -128,14 +128,14 @@ class DatabaseSettingsModel : public Wt::WFormModel
|
||||
|
||||
Scanner::UpdatePeriod updatePeriod(int row)
|
||||
{
|
||||
return boost::any_cast<Scanner::UpdatePeriod>
|
||||
(_updatePeriodModel->data(_updatePeriodModel->index(row, 0), Wt::UserRole));
|
||||
return Wt::cpp17::any_cast<Scanner::UpdatePeriod>
|
||||
(_updatePeriodModel->data(_updatePeriodModel->index(row, 0), Wt::ItemDataRole::User));
|
||||
}
|
||||
|
||||
Wt::WString updatePeriodString(int row)
|
||||
{
|
||||
return boost::any_cast<Wt::WString>
|
||||
(_updatePeriodModel->data(_updatePeriodModel->index(row, 0), Wt::DisplayRole));
|
||||
return Wt::cpp17::any_cast<Wt::WString>
|
||||
(_updatePeriodModel->data(_updatePeriodModel->index(row, 0), Wt::ItemDataRole::Display));
|
||||
}
|
||||
|
||||
|
||||
@@ -150,7 +150,7 @@ class DatabaseSettingsModel : public Wt::WFormModel
|
||||
return boost::none;
|
||||
}
|
||||
|
||||
boost::optional<int> getUpdateStartTimeModelRow(boost::posix_time::time_duration startTime)
|
||||
boost::optional<int> getUpdateStartTimeModelRow(Wt::WTime startTime)
|
||||
{
|
||||
for (int i = 0; i < _updateStartTimeModel->rowCount(); ++i)
|
||||
{
|
||||
@@ -161,16 +161,16 @@ class DatabaseSettingsModel : public Wt::WFormModel
|
||||
return boost::none;
|
||||
}
|
||||
|
||||
boost::posix_time::time_duration updateStartTime(int row)
|
||||
Wt::WTime updateStartTime(int row)
|
||||
{
|
||||
return boost::any_cast<boost::posix_time::time_duration>
|
||||
(_updateStartTimeModel->data(_updateStartTimeModel->index(row, 0), Wt::UserRole));
|
||||
return Wt::cpp17::any_cast<Wt::WTime>
|
||||
(_updateStartTimeModel->data(_updateStartTimeModel->index(row, 0), Wt::ItemDataRole::User));
|
||||
}
|
||||
|
||||
Wt::WString updateStartTimeString(int row)
|
||||
{
|
||||
return boost::any_cast<Wt::WString>
|
||||
(_updateStartTimeModel->data(_updateStartTimeModel->index(row, 0), Wt::DisplayRole));
|
||||
return Wt::cpp17::any_cast<Wt::WString>
|
||||
(_updateStartTimeModel->data(_updateStartTimeModel->index(row, 0), Wt::ItemDataRole::Display));
|
||||
}
|
||||
|
||||
|
||||
@@ -179,43 +179,35 @@ class DatabaseSettingsModel : public Wt::WFormModel
|
||||
void initializeModels()
|
||||
{
|
||||
|
||||
_updatePeriodModel = new Wt::WStringListModel(this);
|
||||
_updatePeriodModel = std::make_shared<Wt::WStringListModel>();
|
||||
|
||||
_updatePeriodModel->addString(Wt::WString::tr("Lms.Admin.Database.never"));
|
||||
_updatePeriodModel->setData(0, 0, Scanner::UpdatePeriod::Never, Wt::UserRole);
|
||||
_updatePeriodModel->setData(0, 0, Scanner::UpdatePeriod::Never, Wt::ItemDataRole::User);
|
||||
|
||||
_updatePeriodModel->addString(Wt::WString::tr("Lms.Admin.Database.daily"));
|
||||
_updatePeriodModel->setData(1, 0, Scanner::UpdatePeriod::Daily, Wt::UserRole);
|
||||
_updatePeriodModel->setData(1, 0, Scanner::UpdatePeriod::Daily, Wt::ItemDataRole::User);
|
||||
|
||||
_updatePeriodModel->addString(Wt::WString::tr("Lms.Admin.Database.weekly"));
|
||||
_updatePeriodModel->setData(2, 0, Scanner::UpdatePeriod::Weekly, Wt::UserRole);
|
||||
_updatePeriodModel->setData(2, 0, Scanner::UpdatePeriod::Weekly, Wt::ItemDataRole::User);
|
||||
|
||||
_updatePeriodModel->addString(Wt::WString::tr("Lms.Admin.Database.monthly"));
|
||||
_updatePeriodModel->setData(3, 0, Scanner::UpdatePeriod::Monthly, Wt::UserRole);
|
||||
_updatePeriodModel->setData(3, 0, Scanner::UpdatePeriod::Monthly, Wt::ItemDataRole::User);
|
||||
|
||||
_updateStartTimeModel = new Wt::WStringListModel(this);
|
||||
_updateStartTimeModel = std::make_shared<Wt::WStringListModel>();
|
||||
|
||||
for (std::size_t i = 0; i < 24; ++i)
|
||||
{
|
||||
boost::posix_time::time_duration dur = boost::posix_time::hours(i);
|
||||
Wt::WTime time(i, 0);
|
||||
|
||||
boost::posix_time::time_facet* facet = new boost::posix_time::time_facet("%H:%M");
|
||||
facet->time_duration_format("%H:%M");
|
||||
|
||||
std::ostringstream oss;
|
||||
oss.imbue(std::locale(oss.getloc(), facet));
|
||||
|
||||
oss << dur;
|
||||
|
||||
_updateStartTimeModel->addString( oss.str() );
|
||||
_updateStartTimeModel->setData(i, 0, dur, Wt::UserRole);
|
||||
_updateStartTimeModel->addString( time.toString() );
|
||||
_updateStartTimeModel->setData(i, 0, time, Wt::ItemDataRole::User);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Wt::WStringListModel* _updatePeriodModel;
|
||||
Wt::WStringListModel* _updateStartTimeModel;
|
||||
std::shared_ptr<Wt::WStringListModel> _updatePeriodModel;
|
||||
std::shared_ptr<Wt::WStringListModel> _updateStartTimeModel;
|
||||
|
||||
};
|
||||
|
||||
@@ -224,43 +216,33 @@ const Wt::WFormModel::Field DatabaseSettingsModel::UpdatePeriodField = "update-
|
||||
const Wt::WFormModel::Field DatabaseSettingsModel::UpdateStartTimeField = "update-start-time";
|
||||
|
||||
|
||||
DatabaseSettingsView::DatabaseSettingsView(Wt::WContainerWidget *parent)
|
||||
: Wt::WTemplateFormView(parent)
|
||||
DatabaseSettingsView::DatabaseSettingsView()
|
||||
: Wt::WTemplateFormView(Wt::WString::tr("Lms.Admin.Database.template"))
|
||||
{
|
||||
auto model = new DatabaseSettingsModel(this);
|
||||
|
||||
setTemplateText(tr("Lms.Admin.Database.template"));
|
||||
addFunction("tr", &WTemplate::Functions::tr);
|
||||
addFunction("id", &WTemplate::Functions::id);
|
||||
auto model = std::make_shared<DatabaseSettingsModel>();
|
||||
|
||||
// Media Directory
|
||||
Wt::WLineEdit *mediaDirectoryEdit = new Wt::WLineEdit();
|
||||
setFormWidget(DatabaseSettingsModel::MediaDirectoryField, mediaDirectoryEdit);
|
||||
setFormWidget(DatabaseSettingsModel::MediaDirectoryField, std::make_unique<Wt::WLineEdit>());
|
||||
|
||||
// Update Period
|
||||
Wt::WComboBox *updatePeriodCB = new Wt::WComboBox();
|
||||
setFormWidget(DatabaseSettingsModel::UpdatePeriodField, updatePeriodCB);
|
||||
updatePeriodCB->setModel(model->updatePeriodModel());
|
||||
auto updatePeriod = std::make_unique<Wt::WComboBox>();
|
||||
updatePeriod->setModel(model->updatePeriodModel());
|
||||
setFormWidget(DatabaseSettingsModel::UpdatePeriodField, std::move(updatePeriod));
|
||||
|
||||
// Update Start Time
|
||||
Wt::WComboBox *updateStartTimeCB = new Wt::WComboBox();
|
||||
setFormWidget(DatabaseSettingsModel::UpdateStartTimeField, updateStartTimeCB);
|
||||
updateStartTimeCB->setModel(model->updateStartTimeModel());
|
||||
auto updateStartTime = std::make_unique<Wt::WComboBox>();
|
||||
updateStartTime->setModel(model->updateStartTimeModel());
|
||||
setFormWidget(DatabaseSettingsModel::UpdateStartTimeField, std::move(updateStartTime));
|
||||
|
||||
// Buttons
|
||||
|
||||
Wt::WPushButton *saveBtn = new Wt::WPushButton(Wt::WString::tr("Lms.apply"));
|
||||
bindWidget("apply-btn", saveBtn);
|
||||
|
||||
Wt::WPushButton *discardBtn = new Wt::WPushButton(Wt::WString::tr("Lms.discard"));
|
||||
bindWidget("discard-btn", discardBtn);
|
||||
|
||||
Wt::WPushButton *immScanBtn = new Wt::WPushButton(Wt::WString::tr("Lms.Admin.Database.immediate-scan"));
|
||||
bindWidget("immediate-scan-btn", immScanBtn);
|
||||
Wt::WPushButton *saveBtn = bindWidget("apply-btn", std::make_unique<Wt::WPushButton>(Wt::WString::tr("Lms.apply")));
|
||||
Wt::WPushButton *discardBtn = bindWidget("discard-btn", std::make_unique<Wt::WPushButton>(Wt::WString::tr("Lms.discard")));
|
||||
Wt::WPushButton *immScanBtn = bindWidget("immediate-scan-btn", std::make_unique<Wt::WPushButton>(Wt::WString::tr("Lms.Admin.Database.immediate-scan")));
|
||||
|
||||
saveBtn->clicked().connect(std::bind([=] ()
|
||||
{
|
||||
updateModel(model);
|
||||
updateModel(model.get());
|
||||
|
||||
if (model->validate())
|
||||
{
|
||||
@@ -271,14 +253,14 @@ DatabaseSettingsView::DatabaseSettingsView(Wt::WContainerWidget *parent)
|
||||
}
|
||||
|
||||
// Udate the view: Delete any validation message in the view, etc.
|
||||
updateView(model);
|
||||
updateView(model.get());
|
||||
}));
|
||||
|
||||
discardBtn->clicked().connect(std::bind([=] ()
|
||||
{
|
||||
model->loadData();
|
||||
model->validate();
|
||||
updateView(model);
|
||||
updateView(model.get());
|
||||
}));
|
||||
|
||||
immScanBtn->clicked().connect(std::bind([=] ()
|
||||
@@ -287,7 +269,7 @@ DatabaseSettingsView::DatabaseSettingsView(Wt::WContainerWidget *parent)
|
||||
LmsApp->notifyMsg(Wt::WString::tr("Lms.Admin.Database.scan-launched"));
|
||||
}));
|
||||
|
||||
updateView(model);
|
||||
updateView(model.get());
|
||||
}
|
||||
|
||||
} // namespace UserInterface
|
||||
|
||||
@@ -19,15 +19,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Wt/WContainerWidget>
|
||||
#include <Wt/WTemplateFormView>
|
||||
#include <Wt/WTemplateFormView.h>
|
||||
|
||||
namespace UserInterface {
|
||||
|
||||
class DatabaseSettingsView : public Wt::WTemplateFormView
|
||||
{
|
||||
public:
|
||||
DatabaseSettingsView(Wt::WContainerWidget *parent = 0);
|
||||
DatabaseSettingsView();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -17,10 +17,10 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <Wt/WFormModel>
|
||||
#include <Wt/WLineEdit>
|
||||
#include <Wt/WPushButton>
|
||||
#include <Wt/Auth/Identity>
|
||||
#include <Wt/WFormModel.h>
|
||||
#include <Wt/WLineEdit.h>
|
||||
#include <Wt/WPushButton.h>
|
||||
#include <Wt/Auth/Identity.h>
|
||||
|
||||
#include "utils/Logger.hpp"
|
||||
|
||||
@@ -40,8 +40,7 @@ class InitWizardModel : public Wt::WFormModel
|
||||
static const Field PasswordField;
|
||||
static const Field PasswordConfirmField;
|
||||
|
||||
InitWizardModel(Wt::WObject *parent = 0)
|
||||
: Wt::WFormModel(parent)
|
||||
InitWizardModel() : Wt::WFormModel()
|
||||
{
|
||||
addField(AdminLoginField);
|
||||
addField(PasswordField);
|
||||
@@ -54,16 +53,16 @@ class InitWizardModel : public Wt::WFormModel
|
||||
|
||||
void saveData()
|
||||
{
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
Wt::Dbo::Transaction transaction(LmsApp->getDboSession());
|
||||
|
||||
// Check if a user already exist
|
||||
// If it's the case, just do nothing
|
||||
if (!Database::User::getAll(DboSession()).empty())
|
||||
if (!Database::User::getAll(LmsApp->getDboSession()).empty())
|
||||
throw std::runtime_error("Admin user already created");
|
||||
|
||||
// Create user
|
||||
Wt::Auth::User authUser = DbHandler().getUserDatabase().registerNew();
|
||||
Database::User::pointer user = DbHandler().getUser(authUser);
|
||||
Wt::Auth::User authUser = LmsApp->getDb().getUserDatabase().registerNew();
|
||||
Database::User::pointer user = LmsApp->getDb().createUser(authUser);
|
||||
|
||||
// Account
|
||||
authUser.setIdentity(Wt::Auth::Identity::LoginName, valueText(AdminLoginField));
|
||||
@@ -94,7 +93,7 @@ class InitWizardModel : public Wt::WFormModel
|
||||
}
|
||||
else if (field == PasswordConfirmField)
|
||||
{
|
||||
if (validation(PasswordField).state() == Wt::WValidator::Valid)
|
||||
if (validation(PasswordField).state() == Wt::ValidationState::Valid)
|
||||
{
|
||||
if (valueText(PasswordField) != valueText(PasswordConfirmField))
|
||||
error = Wt::WString::tr("Lms.passwords-dont-match");
|
||||
@@ -105,9 +104,9 @@ class InitWizardModel : public Wt::WFormModel
|
||||
return Wt::WFormModel::validateField(field);
|
||||
}
|
||||
|
||||
setValidation(field, Wt::WValidator::Result( error.empty() ? Wt::WValidator::Valid : Wt::WValidator::Invalid, error));
|
||||
setValidation(field, Wt::WValidator::Result( error.empty() ? Wt::ValidationState::Valid : Wt::ValidationState::Invalid, error));
|
||||
|
||||
return (validation(field).state() == Wt::WValidator::Valid);
|
||||
return (validation(field).state() == Wt::ValidationState::Valid);
|
||||
}
|
||||
|
||||
};
|
||||
@@ -116,34 +115,28 @@ const Wt::WFormModel::Field InitWizardModel::AdminLoginField = "admin-login";
|
||||
const Wt::WFormModel::Field InitWizardModel::PasswordField = "password";
|
||||
const Wt::WFormModel::Field InitWizardModel::PasswordConfirmField = "password-confirm";
|
||||
|
||||
InitWizardView::InitWizardView(Wt::WContainerWidget *parent)
|
||||
: Wt::WTemplateFormView(parent)
|
||||
InitWizardView::InitWizardView()
|
||||
: Wt::WTemplateFormView(Wt::WString::tr("Lms.Admin.InitWizard.template"))
|
||||
{
|
||||
auto model = new InitWizardModel(this);
|
||||
|
||||
setTemplateText(Wt::WString::tr("Lms.Admin.InitWizard.template"));
|
||||
addFunction("tr", &WTemplate::Functions::tr);
|
||||
addFunction("id", &WTemplate::Functions::id);
|
||||
auto model = std::make_shared<InitWizardModel>();
|
||||
|
||||
// AdminLogin
|
||||
Wt::WLineEdit* accountEdit = new Wt::WLineEdit();
|
||||
setFormWidget(InitWizardModel::AdminLoginField, accountEdit);
|
||||
setFormWidget(InitWizardModel::AdminLoginField, std::make_unique<Wt::WLineEdit>());
|
||||
|
||||
// Password
|
||||
Wt::WLineEdit* passwordEdit = new Wt::WLineEdit();
|
||||
setFormWidget(InitWizardModel::PasswordField, passwordEdit );
|
||||
passwordEdit->setEchoMode(Wt::WLineEdit::Password);
|
||||
auto passwordEdit = std::make_unique<Wt::WLineEdit>();
|
||||
passwordEdit->setEchoMode(Wt::EchoMode::Password);
|
||||
setFormWidget(InitWizardModel::PasswordField, std::move(passwordEdit) );
|
||||
|
||||
// Password confirmation
|
||||
Wt::WLineEdit* passwordConfirmEdit = new Wt::WLineEdit();
|
||||
setFormWidget(InitWizardModel::PasswordConfirmField, passwordConfirmEdit);
|
||||
passwordConfirmEdit->setEchoMode(Wt::WLineEdit::Password);
|
||||
auto passwordConfirmEdit = std::make_unique<Wt::WLineEdit>();
|
||||
passwordConfirmEdit->setEchoMode(Wt::EchoMode::Password);
|
||||
setFormWidget(InitWizardModel::PasswordConfirmField, std::move(passwordConfirmEdit));
|
||||
|
||||
auto saveButton = new Wt::WPushButton(Wt::WString::tr("Lms.create"));
|
||||
bindWidget("create-btn", saveButton);
|
||||
Wt::WPushButton* saveButton = bindNew<Wt::WPushButton>("create-btn", Wt::WString::tr("Lms.create"));
|
||||
saveButton->clicked().connect(std::bind([=]
|
||||
{
|
||||
updateModel(model);
|
||||
updateModel(model.get());
|
||||
|
||||
if (model->validate())
|
||||
{
|
||||
@@ -152,10 +145,10 @@ InitWizardView::InitWizardView(Wt::WContainerWidget *parent)
|
||||
saveButton->setEnabled(false);
|
||||
}
|
||||
|
||||
updateView(model);
|
||||
updateView(model.get());
|
||||
}));
|
||||
|
||||
updateView(model);
|
||||
updateView(model.get());
|
||||
}
|
||||
|
||||
} // namespace UserInterface
|
||||
|
||||
@@ -19,15 +19,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Wt/WContainerWidget>
|
||||
#include <Wt/WTemplateFormView>
|
||||
#include <Wt/WTemplateFormView.h>
|
||||
|
||||
namespace UserInterface {
|
||||
|
||||
class InitWizardView : public Wt::WTemplateFormView
|
||||
{
|
||||
public:
|
||||
InitWizardView(Wt::WContainerWidget *parent = 0);
|
||||
InitWizardView();
|
||||
|
||||
};
|
||||
|
||||
|
||||
+47
-52
@@ -17,15 +17,15 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <Wt/WApplication>
|
||||
#include <Wt/WCheckBox>
|
||||
#include <Wt/WComboBox>
|
||||
#include <Wt/WLineEdit>
|
||||
#include <Wt/WPushButton>
|
||||
#include <Wt/WTemplateFormView>
|
||||
#include <Wt/WApplication.h>
|
||||
#include <Wt/WCheckBox.h>
|
||||
#include <Wt/WComboBox.h>
|
||||
#include <Wt/WLineEdit.h>
|
||||
#include <Wt/WPushButton.h>
|
||||
#include <Wt/WTemplateFormView.h>
|
||||
|
||||
#include <Wt/WFormModel>
|
||||
#include <Wt/WStringListModel>
|
||||
#include <Wt/WFormModel.h>
|
||||
#include <Wt/WStringListModel.h>
|
||||
|
||||
#include "common/Validators.hpp"
|
||||
#include "utils/Utils.hpp"
|
||||
@@ -44,8 +44,8 @@ class UserModel : public Wt::WFormModel
|
||||
static const Field PasswordField;
|
||||
static const Field BitrateLimitField;
|
||||
|
||||
UserModel(boost::optional<Database::User::id_type> userId, Wt::WObject *parent = 0)
|
||||
: Wt::WFormModel(parent),
|
||||
UserModel(boost::optional<Database::User::id_type> userId)
|
||||
: Wt::WFormModel(),
|
||||
_userId(userId)
|
||||
{
|
||||
if (!_userId)
|
||||
@@ -72,12 +72,12 @@ class UserModel : public Wt::WFormModel
|
||||
if (!_userId)
|
||||
return;
|
||||
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
Wt::Dbo::Transaction transaction(LmsApp->getDboSession());
|
||||
|
||||
Wt::Auth::User authUser = DbHandler().getUserDatabase().findWithId( std::to_string(*_userId) );
|
||||
Database::User::pointer user = DbHandler().getUser(authUser);
|
||||
Wt::Auth::User authUser = LmsApp->getDb().getUserDatabase().findWithId( std::to_string(*_userId) );
|
||||
Database::User::pointer user = LmsApp->getDb().getUser(authUser);
|
||||
|
||||
if (user == CurrentUser())
|
||||
if (user == LmsApp->getCurrentUser())
|
||||
throw std::runtime_error("Cannot edit ourselves");
|
||||
|
||||
auto bitrate = getBitrateLimitRow(user->getMaxAudioBitrate());
|
||||
@@ -87,13 +87,13 @@ class UserModel : public Wt::WFormModel
|
||||
|
||||
void saveData()
|
||||
{
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
Wt::Dbo::Transaction transaction(LmsApp->getDboSession());
|
||||
|
||||
if (_userId)
|
||||
{
|
||||
// Update user
|
||||
Wt::Auth::User authUser = DbHandler().getUserDatabase().findWithId( std::to_string(*_userId) );
|
||||
Database::User::pointer user = DbHandler().getUser( authUser );
|
||||
Wt::Auth::User authUser = LmsApp->getDb().getUserDatabase().findWithId( std::to_string(*_userId) );
|
||||
Database::User::pointer user = LmsApp->getDb().getUser( authUser );
|
||||
|
||||
// Account
|
||||
if (!valueText(PasswordField).empty())
|
||||
@@ -106,8 +106,8 @@ class UserModel : public Wt::WFormModel
|
||||
else
|
||||
{
|
||||
// Create user
|
||||
Wt::Auth::User authUser = DbHandler().getUserDatabase().registerNew();
|
||||
Database::User::pointer user = DbHandler().createUser(authUser);
|
||||
Wt::Auth::User authUser = LmsApp->getDb().getUserDatabase().registerNew();
|
||||
Database::User::pointer user = LmsApp->getDb().createUser(authUser);
|
||||
|
||||
// Account
|
||||
authUser.setIdentity(Wt::Auth::Identity::LoginName, valueText(LoginField));
|
||||
@@ -142,35 +142,35 @@ class UserModel : public Wt::WFormModel
|
||||
|
||||
std::size_t bitrateLimit(int row)
|
||||
{
|
||||
return boost::any_cast<std::size_t>
|
||||
(_bitrateModel->data(_bitrateModel->index(row, 0), Wt::UserRole));
|
||||
return Wt::cpp17::any_cast<std::size_t>
|
||||
(_bitrateModel->data(_bitrateModel->index(row, 0), Wt::ItemDataRole::User));
|
||||
}
|
||||
|
||||
Wt::WString bitrateLimitString(int row)
|
||||
{
|
||||
return boost::any_cast<Wt::WString>
|
||||
(_bitrateModel->data(_bitrateModel->index(row, 0), Wt::DisplayRole));
|
||||
return Wt::cpp17::any_cast<Wt::WString>
|
||||
(_bitrateModel->data(_bitrateModel->index(row, 0), Wt::ItemDataRole::Display));
|
||||
}
|
||||
|
||||
Wt::WAbstractItemModel *bitrateModel() { return _bitrateModel; }
|
||||
std::shared_ptr<Wt::WAbstractItemModel> bitrateModel() { return _bitrateModel; }
|
||||
|
||||
private:
|
||||
|
||||
void initializeModels()
|
||||
{
|
||||
_bitrateModel = new Wt::WStringListModel(this);
|
||||
_bitrateModel = std::make_shared<Wt::WStringListModel>();
|
||||
|
||||
std::size_t id = 0;
|
||||
for (auto bitrate : Database::User::audioBitrates)
|
||||
{
|
||||
_bitrateModel->addString( Wt::WString::fromUTF8(std::to_string(bitrate / 1000)) );
|
||||
_bitrateModel->setData( id++, 0, bitrate, Wt::UserRole);
|
||||
_bitrateModel->setData( id++, 0, bitrate, Wt::ItemDataRole::User);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Wt::WStringListModel* _bitrateModel;
|
||||
std::shared_ptr<Wt::WStringListModel> _bitrateModel;
|
||||
boost::optional<Database::User::id_type> _userId;
|
||||
};
|
||||
|
||||
@@ -178,8 +178,7 @@ const Wt::WFormModel::Field UserModel::LoginField = "login";
|
||||
const Wt::WFormModel::Field UserModel::PasswordField = "password";
|
||||
const Wt::WFormModel::Field UserModel::BitrateLimitField = "audio-bitrate-limit";
|
||||
|
||||
UserView::UserView(Wt::WContainerWidget* parent)
|
||||
: Wt::WContainerWidget(parent)
|
||||
UserView::UserView()
|
||||
{
|
||||
wApp->internalPathChanged().connect(std::bind([=]
|
||||
{
|
||||
@@ -195,47 +194,44 @@ UserView::refreshView()
|
||||
if (!wApp->internalPathMatches("/admin/user"))
|
||||
return;
|
||||
|
||||
auto userId = readLong(wApp->internalPathNextPart("/admin/user/"));
|
||||
auto userId = readAs<Database::User::id_type>(wApp->internalPathNextPart("/admin/user/"));
|
||||
|
||||
LMS_LOG(UI, DEBUG) << "userId = " << (userId ? std::to_string(*userId) : "none");
|
||||
|
||||
clear();
|
||||
|
||||
auto t = new Wt::WTemplateFormView(Wt::WString::tr("Lms.Admin.User.template"), this);
|
||||
Wt::WTemplateFormView* t = addNew<Wt::WTemplateFormView>(Wt::WString::tr("Lms.Admin.User.template"));
|
||||
|
||||
t->addFunction("tr", &Wt::WTemplate::Functions::tr);
|
||||
t->addFunction("id", &Wt::WTemplate::Functions::id);
|
||||
|
||||
auto model = new UserModel(userId ? boost::make_optional<Database::User::id_type>(*userId) : boost::none, this);
|
||||
auto model = std::make_shared<UserModel>(userId);
|
||||
|
||||
if (userId)
|
||||
{
|
||||
auto authUser = DbHandler().getUserDatabase().findWithId( std::to_string(*userId) );
|
||||
auto authUser = LmsApp->getDb().getUserDatabase().findWithId( std::to_string(*userId) );
|
||||
auto name = authUser.identity(Wt::Auth::Identity::LoginName);
|
||||
t->bindString("title", Wt::WString::tr("Lms.Admin.User.user-edit").arg(name), Wt::PlainText);
|
||||
t->bindString("title", Wt::WString::tr("Lms.Admin.User.user-edit").arg(name), Wt::TextFormat::Plain);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Login
|
||||
t->setCondition("if-has-login", true);
|
||||
t->setFormWidget(UserModel::LoginField, new Wt::WLineEdit());
|
||||
t->setFormWidget(UserModel::LoginField, std::make_unique<Wt::WLineEdit>());
|
||||
t->bindString("title", Wt::WString::tr("Lms.Admin.User.user-create"));
|
||||
}
|
||||
|
||||
|
||||
// Password
|
||||
Wt::WLineEdit* passwordEdit = new Wt::WLineEdit();
|
||||
t->setFormWidget(UserModel::PasswordField, passwordEdit );
|
||||
passwordEdit->setEchoMode(Wt::WLineEdit::Password);
|
||||
auto passwordEdit = std::make_unique<Wt::WLineEdit>();
|
||||
passwordEdit->setEchoMode(Wt::EchoMode::Password);
|
||||
t->setFormWidget(UserModel::PasswordField, std::move(passwordEdit));
|
||||
|
||||
// AudioBitrate
|
||||
Wt::WComboBox *bitrateCB = new Wt::WComboBox();
|
||||
t->setFormWidget(UserModel::BitrateLimitField, bitrateCB);
|
||||
bitrateCB->setModel(model->bitrateModel());
|
||||
// Bitrate
|
||||
auto bitrate = std::make_unique<Wt::WComboBox>();
|
||||
bitrate->setModel(model->bitrateModel());
|
||||
t->setFormWidget(UserModel::BitrateLimitField, std::move(bitrate));
|
||||
|
||||
auto saveBtn = new Wt::WPushButton(Wt::WString::tr(userId ? "Lms.save" : "Lms.create"));
|
||||
t->bindWidget("save-btn", saveBtn);
|
||||
Wt::WPushButton* saveBtn = t->bindNew<Wt::WPushButton>("save-btn", Wt::WString::tr(userId ? "Lms.save" : "Lms.create"));
|
||||
saveBtn->clicked().connect(std::bind([=]
|
||||
{
|
||||
t->updateModel(model);
|
||||
t->updateModel(model.get());
|
||||
|
||||
if (model->validate())
|
||||
{
|
||||
@@ -245,12 +241,11 @@ UserView::refreshView()
|
||||
}
|
||||
else
|
||||
{
|
||||
t->updateView(model);
|
||||
t->updateView(model.get());
|
||||
}
|
||||
}));
|
||||
|
||||
t->updateView(model);
|
||||
|
||||
t->updateView(model.get());
|
||||
}
|
||||
|
||||
} // namespace UserInterface
|
||||
|
||||
@@ -19,14 +19,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Wt/WContainerWidget>
|
||||
#include <Wt/WContainerWidget.h>
|
||||
|
||||
namespace UserInterface {
|
||||
|
||||
class UserView : public Wt::WContainerWidget
|
||||
{
|
||||
public:
|
||||
UserView(Wt::WContainerWidget *parent = 0);
|
||||
UserView();
|
||||
|
||||
private:
|
||||
void refreshView();
|
||||
|
||||
+19
-26
@@ -17,8 +17,8 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <Wt/WTemplate>
|
||||
#include <Wt/WPushButton>
|
||||
#include <Wt/WTemplate.h>
|
||||
#include <Wt/WPushButton.h>
|
||||
|
||||
#include "database/Types.hpp"
|
||||
#include "utils/Logger.hpp"
|
||||
@@ -28,18 +28,14 @@
|
||||
|
||||
namespace UserInterface {
|
||||
|
||||
UsersView::UsersView(Wt::WContainerWidget *parent)
|
||||
: Wt::WContainerWidget(parent)
|
||||
UsersView::UsersView()
|
||||
: Wt::WTemplate(Wt::WString::tr("Lms.Admin.Users.template"))
|
||||
{
|
||||
auto t = new Wt::WTemplate(Wt::WString::tr("Lms.Admin.Users.template"), this);
|
||||
t->addFunction("tr", &Wt::WTemplate::Functions::tr);
|
||||
addFunction("tr", &Wt::WTemplate::Functions::tr);
|
||||
|
||||
_container = new Wt::WContainerWidget();
|
||||
t->bindWidget("users", _container);
|
||||
|
||||
auto addBtn = new Wt::WPushButton(Wt::WString::tr("Lms.Admin.Users.add"));
|
||||
t->bindWidget("add-btn", addBtn);
|
||||
_container = bindNew<Wt::WContainerWidget>("users");
|
||||
|
||||
Wt::WPushButton* addBtn = bindNew<Wt::WPushButton>("add-btn", Wt::WString::tr("Lms.Admin.Users.add"));
|
||||
addBtn->clicked().connect(std::bind([=]
|
||||
{
|
||||
LmsApp->setInternalPath("/admin/user", true);
|
||||
@@ -61,44 +57,41 @@ UsersView::refreshView()
|
||||
|
||||
_container->clear();
|
||||
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
Wt::Dbo::Transaction transaction(LmsApp->getDboSession());
|
||||
|
||||
auto users = Database::User::getAll(DboSession());
|
||||
auto users = Database::User::getAll(LmsApp->getDboSession());
|
||||
for (auto user : users)
|
||||
{
|
||||
auto userId = std::to_string(user.id());
|
||||
auto entry = new Wt::WTemplate(Wt::WString::tr("Lms.Admin.Users.template.entry"), _container);
|
||||
|
||||
Wt::Auth::User authUser = DbHandler().getUserDatabase().findWithId(userId);
|
||||
Wt::WTemplate* entry = _container->addNew<Wt::WTemplate>(Wt::WString::tr("Lms.Admin.Users.template.entry"));
|
||||
|
||||
Wt::Auth::User authUser = LmsApp->getDb().getUserDatabase().findWithId(userId);
|
||||
if (!authUser.isValid()) {
|
||||
LMS_LOG(UI, ERROR) << "Skipping invalid userId = " << user.id();
|
||||
continue;
|
||||
}
|
||||
|
||||
entry->bindString("name", authUser.identity(Wt::Auth::Identity::LoginName), Wt::PlainText);
|
||||
entry->bindString("name", authUser.identity(Wt::Auth::Identity::LoginName), Wt::TextFormat::Plain);
|
||||
|
||||
// Don't edit ourself this way
|
||||
if (CurrentUser() == user)
|
||||
if (LmsApp->getCurrentUser() == user)
|
||||
continue;
|
||||
|
||||
entry->setCondition("if-edit", true);
|
||||
auto editBtn = new Wt::WPushButton(Wt::WString::tr("Lms.Admin.Users.edit"));
|
||||
entry->bindWidget("edit-btn", editBtn);
|
||||
Wt::WPushButton* editBtn = entry->bindNew<Wt::WPushButton>("edit-btn", Wt::WString::tr("Lms.Admin.Users.edit"));
|
||||
editBtn->clicked().connect(std::bind([=]
|
||||
{
|
||||
LmsApp->setInternalPath("/admin/user/" + std::to_string(user.id()), true);
|
||||
}));
|
||||
|
||||
auto delBtn = new Wt::WPushButton(Wt::WString::tr("Lms.Admin.Users.del"));
|
||||
entry->bindWidget("del-btn", delBtn);
|
||||
Wt::WPushButton* delBtn = entry->bindNew<Wt::WPushButton>("del-btn", Wt::WString::tr("Lms.Admin.Users.del"));
|
||||
delBtn->clicked().connect(std::bind([=]
|
||||
{
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
Wt::Dbo::Transaction transaction(LmsApp->getDboSession());
|
||||
|
||||
auto authUser = DbHandler().getUserDatabase().findWithId(userId);
|
||||
auto user = DbHandler().getUser(authUser);
|
||||
DbHandler().getUserDatabase().deleteUser( authUser );
|
||||
auto authUser = LmsApp->getDb().getUserDatabase().findWithId(userId);
|
||||
auto user = LmsApp->getDb().getUser(authUser);
|
||||
LmsApp->getDb().getUserDatabase().deleteUser( authUser );
|
||||
user.remove();
|
||||
_container->removeWidget(entry);
|
||||
}));
|
||||
|
||||
@@ -19,14 +19,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Wt/WContainerWidget>
|
||||
#include <Wt/WContainerWidget.h>
|
||||
|
||||
namespace UserInterface {
|
||||
|
||||
class UsersView : public Wt::WContainerWidget
|
||||
class UsersView : public Wt::WTemplate
|
||||
{
|
||||
public:
|
||||
UsersView(Wt::WContainerWidget *parent = 0);
|
||||
UsersView();
|
||||
|
||||
private:
|
||||
void refreshView();
|
||||
|
||||
Reference in New Issue
Block a user