Delegates are used for FK display. NOTE: CORE DUMP ON EXIT
authorStas Shtin <antisvin@gmail.com>
Tue, 13 Apr 2010 16:53:18 +0000 (20:53 +0400)
committerStas Shtin <antisvin@gmail.com>
Tue, 13 Apr 2010 16:53:18 +0000 (20:53 +0400)
src/ipypbx/controllers.py
src/ipypbx/create.sql
src/ipypbx/main.py
src/ipypbxweb/models.py

index 132b7db..5ae2c30 100644 (file)
@@ -95,8 +95,6 @@ class BaseController(QtCore.QObject):
         else:
             self.view_display = QtGui.QDataWidgetMapper(parent)
             self.view_display.setModel(self.model)
-            if self.delegate:
-                self.view_display.setItemDelegate(QtSql.QSqlRelationalDelegate(self))
 
             display_fields = self.getDisplayFields()
             
@@ -112,18 +110,21 @@ class BaseController(QtCore.QObject):
 
             for data in self.relations:
                 column, name, table, display = data                
-                column_id = self.model.fieldIndex(column)
+                column_index = self.model.fieldIndex(column)
 
                 # SetRelation screws table data filtering?
-#                self.model.setRelation(
-#                    column_id,
-#                    QtSql.QSqlRelation('ipypbxweb_%s' % table, 'id', display))
+                self.model.setRelation(
+                    column_index,
+                    QtSql.QSqlRelation('ipypbxweb_%s' % table, 'id', display))
+                #self.model.select()
 
-#                rel = self.model.relationModel(column_id)
+                rel = self.model.relationModel(column_index)
 
                 widget = self.getFieldWidget(name)
                 widget.setModel(self.parent().controllers[table].model)
-                widget.setItemDelegate(self.delegate)
+                widget.setModelColumn(rel.fieldIndex(display))
+                #widget.setItemDelegate(self.delegate)
+
 
         # Select first row in the view list.
         self.view_display.toFirst()
@@ -186,8 +187,6 @@ class BaseController(QtCore.QObject):
         # Focust to the first displayed field.
         self.getFieldWidget(self.getDisplayFields()[0]).setFocus()
 
-        # TODO: set default values?
-
     def save(self):
         """
         Save to database.
@@ -241,8 +240,10 @@ class ConnectionChangeListenerController(BaseController):
             connection_id, ok = index.model().data(
                 index.sibling(index.row(), 0)).toInt()
             self.connection_id = connection_id
-            self.model.setFilter('connection_id = %i' % connection_id)
-            self.model.index(0, 0)
+            self.model.setFilter('ipypbxweb_%s.connection_id = %i' % (self.basename, connection_id))
+            self.view_list.selectRow(0)
+            if not self.model.rowCount():
+                self.add()
 
     def setConnectionId(self, row, record):
         record.setValue('connection_id', self.connection_id)
@@ -278,7 +279,7 @@ class DomainController(ConnectionChangeListenerController):
         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Host Name'),
         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
     view_list_fields = 'SIP Profile ID', 'Host Name'
-    relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'Name'),)
+    relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'name'),)
     
 
 class GatewayController(ConnectionChangeListenerController):
@@ -299,7 +300,7 @@ class GatewayController(ConnectionChangeListenerController):
         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Caller ID In From Field'),
         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
     view_list_fields = 'SIP Profile ID', 'Name'
-    relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'Name'),)
+    relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'name'),)
     
 
 class EndpointController(ConnectionChangeListenerController):
@@ -314,7 +315,7 @@ class EndpointController(ConnectionChangeListenerController):
         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
     view_list_fields = 'User ID', 'Domain ID'
-    relations = (('domain_id', 'Domain ID', 'domain', 'Host Name'),)
+    relations = (('domain_id', 'Domain ID', 'domain', 'host_name'),)
     
 
 class ExtensionController(ConnectionChangeListenerController):
@@ -331,5 +332,7 @@ class ExtensionController(ConnectionChangeListenerController):
         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
     view_list_fields = 'Destination Match',
-    relations = (('domain_id', 'Domain ID', 'domain', 'Host Name'),)
+    relations = (
+        ('domain_id', 'Domain ID', 'domain', 'host_name'),
+        ('endpoint_id', 'Endpoint ID', 'endpoint', 'user_id'))
         
index a2a8e3e..c587abf 100644 (file)
@@ -58,8 +58,8 @@ CREATE TABLE "ipypbxweb_extension" (
     "connection_id" integer NOT NULL REFERENCES "ipypbxweb_connection" ("id"),
     "destination_match" varchar(100) NOT NULL,
     "xml_dialplan" text NOT NULL,
-    "domain_id" integer NOT NULL REFERENCES "ipypbxweb_domain" ("id"),
-    "endpoint_id" integer NOT NULL REFERENCES "ipypbxweb_endpoint" ("id"),
+    "domain_id" integer REFERENCES "ipypbxweb_domain" ("id"),
+    "endpoint_id" integer REFERENCES "ipypbxweb_endpoint" ("id"),
     "authenticate_calls" bool NOT NULL,
     "is_active" bool NOT NULL
 )
index 7d25b49..9c8ebfe 100644 (file)
@@ -57,6 +57,8 @@ def setupDb(prefix=PREFIX, dbname=DB_NAME):
             query = QtSql.QSqlQuery()
             for query_string in sql_queries[:-1]:
                 query.exec_(query_string)
+
+        return created
     else:
         # Something went horribly wrong.
         QtGui.QMessageBox.warning(
@@ -75,7 +77,7 @@ if __name__ == '__main__':
         QtGui.QApplication.installTranslator(translator)
 
     # Initialize main window.
-    setupDb()
+    created = setupDb()
     main = QtGui.QMainWindow()
     views = ui.Ui_MainWindow()
     views.setupUi(main)
@@ -96,6 +98,7 @@ if __name__ == '__main__':
     # Get first row index.
     first_row_id, ok = connection_model.data(
         connection_index.sibling(0, 0)).toInt()
+    
     if ok:
         # We have to explicitly emit row selection signal since previous state
         # was unselected.
@@ -104,5 +107,7 @@ if __name__ == '__main__':
             QtCore.SIGNAL('currentRowChanged(QModelIndex, QModelIndex)'),
             connection_index, connection_index)
 
+    if created:
+        main.controllers['connection'].add()
     sys.exit(app.exec_())
 
index 75266a5..57a3194 100644 (file)
@@ -75,7 +75,7 @@ class Extension(models.Model):
     connection = models.ForeignKey(Connection)
     destination_match = models.CharField(max_length=100)
     xml_dialplan = models.TextField()
-    domain = models.ForeignKey(Domain)
-    endpoint = models.ForeignKey(Endpoint)
+    domain = models.ForeignKey(Domain, null=True, blank=True)
+    endpoint = models.ForeignKey(Endpoint, null=True, blank=True)
     authenticate_calls = models.BooleanField()
     is_active = models.BooleanField()