qt – qtableview fails to display the newly inserted row

When using QtableView + QSqlTableModel combination, QtableView fails to properly display the inserted row after insertion of a new one.

The id field is hidden. The EditStrategy is OnFieldChange. There is a button to insert a new empty row at the end of the model. After the user fills in all fields and hits enter or clicks on other rows, the expected behavior is for the row to be submitted to the database and the view keeping the new row. However, the QSqlTableModel succeeds to insert the data into the database table, but the tableview’s newly inserted row becomes empty and fields uneditable.

The behavior changes to the desired one if the id field is not hidden and is explicitly provided. Yet if the id is not hidden and not populated manually, the behaviour fails again as mentioned above.

Why this happens?

Here is the minimum reproducible code. You will have to change database configuration fileds. And connect to the table with autoincrement id:

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QModelIndex
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5.QtWidgets import QPushButton


class Ui_main(object):
    def setupUi(self, main):
        main.setObjectName("main")
        main.resize(781, 652)
        main.setContextMenuPolicy(QtCore.Qt.NoContextMenu)

        self.verticalLayoutWidget = QtWidgets.QWidget(main)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 761, 631))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")

        # Replace values with your database configurations
        # But the table you refer to must have autoincremented primary key
        database = QSqlDatabase.addDatabase('QPSQL')
        database.setHostName('localhost')
        database.setPort(5432)
        database.setDatabaseName('some_database')
        database.setUserName('some_user')
        database.setPassword('some_password')
        database.open()

        button_add = QPushButton("AddRow")
        button_add.clicked.connect(self.addRow)
        self.verticalLayout.addWidget(button_add)

        self.tableView = QtWidgets.QTableView(self.verticalLayoutWidget)
        self.tableView.setObjectName("tableView")
        self.tableView.verticalHeader().setVisible(False)
        self.verticalLayout.addWidget(self.tableView)

        self.table_model = QSqlTableModel(self.tableView, database)
        table_name = 'some_table'
        self.table_model.setTable(table_name)

        self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange)
        self.table_model.select()

        self.tableView.setModel(self.table_model)
        self.tableView.hideColumn(0)

        self.retranslateUi(main)
        QtCore.QMetaObject.connectSlotsByName(main)

    def retranslateUi(self, main):
        _translate = QtCore.QCoreApplication.translate
        main.setWindowTitle(_translate("main", "main"))

    def addRow(self):
        count = self.table_model.rowCount(QModelIndex())
        self.table_model.insertRow(count)
        self.tableView.scrollToBottom()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main_window = QtWidgets.QWidget()
    window = Ui_main()
    window.setupUi(main_window)
    main_window.show()
    sys.exit(app.exec_())

office 365 – Automatic inclusion of newly created SharePoint site in existing retention policy

You can’t automatically add new sites explicitly to the retention policies out-of-the-box.

One way to automate it is to script this with PowerShell. I’m including all PowerShell commands you’ll need to complete this task.

Get a list of SharePoint Online sites

Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser
Get-SPOSite -Limit ALL

Install the Exchange Online PowerShell Module

Install-Module ExchangeOnlineManagement -Scope CurrentUser

Establish a new session

Connect-PPSSession

Get existing retention policies

Get-RetentionCompliancePolicy -Identity "SharePoint Retention Polocy" -DistributionDetail

Add a new site to the retention policy

Set-RetentionCompliancePolicy -Identity "SharePoint Retention Polocy" -AddSharePointLocation "https://contoso.sharepoint.com/sites/teams/finance"

How can I manage newly created tables permissions in the PostgreSQL?

I want to create default permissions on the schema and automatically assign these permissions to newly created tables. For this purpose, I’ve created a role and assign a user. Afterward, I’ve created default permission with the following statements. But there are no default permissions in the newly created tables. How can I manage users and roles on the newly created tables?

CREATE SCHEMA ${db_schema};

#for create read only role
CREATE ROLE readonly;
GRANT CONNECT ON DATABASE ${db_name} TO readonly;
GRANT USAGE ON SCHEMA  ${db_schema} to readonly;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA ${db_schema} TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA ${db_schema} TO readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA ${db_schema} GRANT SELECT ON TABLES TO readonly;

#for create read write role
CREATE ROLE readwrite;
GRANT CONNECT ON DATABASE ${db_name} TO readwrite;
GRANT USAGE, CREATE ON SCHEMA ${db_schema} TO readwrite;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA ${db_schema} TO readwrite;
ALTER DEFAULT PRIVILEGES IN SCHEMA ${db_schema} GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO readwrite;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA ${db_schema} TO readwrite;
grant readwrite to ${db_user_name}
ALTER DEFAULT PRIVILEGES IN SCHEMA ${db_schema} GRANT USAGE ON SEQUENCES TO readwrite;

# create a user and assign read-write role
create user appuser with password 'secret_password'
grant readwrite to appuser

# create a user and assign read-write role
create user reportuser with password 'secret_password'
grant readonly to reportuser

# create a table with appuser who has the read-write permission
CREATE TABLE usertestscehema.accounts (
username VARCHAR ( 50 ) UNIQUE NOT NULL
);
insert into accounts (username) values ('test');

# select table with reportuser who has the read-only permission
select * from accounts

Finally, I got the below error;

SQL Error (42501): ERROR: permission denied for table accounts

Afterward, I examined the permissions with the following statement and I didn’t see any permission related to reportuser who has my readonly user.

SELECT grantee, privilege_type 
FROM information_schema.role_table_grants 
WHERE table_name='accounts'

Where am I missing about this problem?

ubuntu – Interpreting SMART Logs of Newly Installed NVMe RAID0 Crashing Everyday

An Ubuntu 20.04 system has been stable for a year until a 2nd and 3rd NVMe drive is installed on the motherboard to form a 2x1TB RAID0 array. Ever since then, there is huge amount of IO load on this RAID0 array 24/7 and the system crashes about once a day.

nvme smart-logs /dev/nvme1n1 and nvme smart-logs /dev/nvme1n2 contains a few entries that are non-zero, particularly num_err_log_entries, Thermal Management T1 Trans Count and Thermal Management T1 Total Time.

These 3 entires are all 0 in the existing first NVMe drive in the macine.

What does these 3 entires mean? How can we check the error logs that num_err_log_entries is tracking?

Is this a concern?

$ sudo nvme smart-log /dev/nvme1n1
Smart Log for NVME device:nvme1n1 namespace-id:ffffffff
critical_warning                    : 0
temperature                         : 37 C
available_spare                     : 100%
available_spare_threshold           : 5%
percentage_used                     : 2%
data_units_read                     : 92,212,456
data_units_written                  : 97,579,431
host_read_commands                  : 141,129,409
host_write_commands                 : 164,652,255
controller_busy_time                : 1,266
power_cycles                        : 6
power_on_hours                      : 107
unsafe_shutdowns                    : 5
media_errors                        : 0
num_err_log_entries                 : 18
Warning Temperature Time            : 0
Critical Composite Temperature Time : 0
Thermal Management T1 Trans Count   : 2
Thermal Management T2 Trans Count   : 0
Thermal Management T1 Total Time    : 131395
Thermal Management T2 Total Time    : 0
~$ sudo nvme smart-log /dev/nvme2n1
Smart Log for NVME device:nvme2n1 namespace-id:ffffffff
critical_warning                    : 0
temperature                         : 46 C
available_spare                     : 100%
available_spare_threshold           : 5%
percentage_used                     : 2%
data_units_read                     : 92,212,323
data_units_written                  : 97,557,078
host_read_commands                  : 141,055,382
host_write_commands                 : 163,918,102
controller_busy_time                : 1,774
power_cycles                        : 6
power_on_hours                      : 112
unsafe_shutdowns                    : 5
media_errors                        : 0
num_err_log_entries                 : 18
Warning Temperature Time            : 0
Critical Composite Temperature Time : 0
Thermal Management T1 Trans Count   : 5
Thermal Management T2 Trans Count   : 0
Thermal Management T1 Total Time    : 169552
Thermal Management T2 Total Time    : 0

Leftover files when uninstalling Anti-Virus – do they need to be removed when using newly installed Anti-Virus?

I recently purchased a new laptop. I have two questions.

  1. When I did the original setup, I chose the option "No" when it asked if I wanted to download an anti-virus program. However, even though I chose No, it still downloaded McAfee. Is this typical?

  2. I’ve uninstalled McAfee and downloaded a new Anti-Virus program. However, I am now noticing there are a number of folders with mcafee still saved – under Windows/System32 folder as well as AppData folder. Would this cause issues with the New Antivirus running, given there are still some files/folders from the old one on the PC? Is it safe to delete these?

Thanks very much!

8 – Newly created field not being added to entity

I have created a form that adds a subset of field types to an entity (does not support bundles). (the reason for the custom form is that end users find the Drupal standard ‘Manage fields’ workflow too difficult) The class extends FormBase. When the ‘submitForm’ function is executed, the field is saved but not added to the entity.

This is the code to save a date field (with text values filled in for explanation purposes):

$field_storage_values = (
  'field_name' => 'field_test_date',
  'entity_type' => 'my_entity_id',
  'type' => 'datetime',
  'translatable' => TRUE,
);

$field_values = (
  'field_name' => 'field_test_date',
  'entity_type' => 'my_entity_id',
  'bundle' => 'my_entity_id',
  'label' => 'Test Date',
  'translatable' => FALSE,
);

$this->entityTypeManager->getStorage('field_storage_config')->create($field_storage_values)->save();
$field = $this->entityTypeManager->getStorage('field_config')->create($field_values);
$field->save();

When the form is submitted, the field is created correctly but not added to the entity.

When I export the field configuration to YML, the following appears under dependencies:

dependencies:
  config:
    - field.storage.my_entity_id.field_test_date

Creating an identical Node field results in the following field configuration YML dependencies:

dependencies:
  config:
    - field.storage.node.field_test_date
    - node.type.basic_page

I suspect the field created with my custom form should also add the second configuration dependency like:

my_entity_id.type.my_entity_id

I tried adding the following before the field is saved but the ‘addDependency’ function is protected.

$field->addDependency('config', 'my_entity_id.type.my_entity_id');