{"comment":{"url":"https://api.github.com/repos/gltn/stdm/comments/17833956","html_url":"https://github.com/gltn/stdm/commit/63a0c8fbf65025da68d4f4919c5c2ae9471bd0a2#commitcomment-17833956","id":17833956,"user":{"login":"wondie","id":9709624,"avatar_url":"https://avatars.githubusercontent.com/u/9709624?v=3","gravatar_id":"","url":"https://api.github.com/users/wondie","html_url":"https://github.com/wondie","followers_url":"https://api.github.com/users/wondie/followers","following_url":"https://api.github.com/users/wondie/following{/other_user}","gists_url":"https://api.github.com/users/wondie/gists{/gist_id}","starred_url":"https://api.github.com/users/wondie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wondie/subscriptions","organizations_url":"https://api.github.com/users/wondie/orgs","repos_url":"https://api.github.com/users/wondie/repos","events_url":"https://api.github.com/users/wondie/events{/privacy}","received_events_url":"https://api.github.com/users/wondie/received_events","type":"User","site_admin":false},"position":null,"line":null,"path":null,"commit_id":"63a0c8fbf65025da68d4f4919c5c2ae9471bd0a2","created_at":"2016-06-11T23:20:33Z","updated_at":"2016-06-11T23:20:33Z","body":"@gkahiu and @pgathogo , I would like to discuss on how to use source document manager for other entities. Currently, source document manager is used by View STR and New STR wizard. \r\n\r\n**Initialization**\r\nAny file using source document model should call entity_model() to get the entity model and the source document model as shown in the example below.  **False = entity_only, True = with_supporting_doc**\r\n```\r\nThe_entity_model, doc_model = entity_model(\r\n    self.curr_profile.social_tenure, False, True\r\n)\r\n```\r\n\r\nTo initialize the source document, we need to instantiate `SourceDocumentManager `from supportingdocument.py. We need to use the document_model as a parameter with the current widget as a second parameter (This is needed to properly load Document Viewer inside a parent). \r\n\r\nThe source document widget uses tabs categorized based on the document types. \r\nThus, `doc_entity = entity.supporting_doc.document_type_entity` can be used to populate the tabs. \r\nIn addition, a button that loads a `QFileDialog `is needed to load documents from the user directories.\r\nFor visualization only, just populating the tabs is needed.  \r\n\r\nIn addition, we need to register the container that holds the uploaded documents using the following. \r\n```\r\nself.sourceDocManager.registerContainer(\r\n    layout, doc_id\r\n)\r\n```\r\n\r\n**Uploading Source Document**\r\nThe document uploading starts when the user adds a document to the source document tab grouped by document type. As the user adds a file, the sourcedocument.py sets the necessary value for the supporting_document and entity_suporting_document tables. \r\nFor instance, the following method is used for New STR Wizard to get the properties of each documents uploaded for later insertion into the database.  At this point, the file is already saved in the STDM user directory. \r\n```\r\ndef on_upload_document(self):\r\n    '''\r\n    Slot raised when the user clicks\r\n    to upload a title deed\r\n    '''\r\n    document_str = QApplication.translate(\r\n        \"newSTRWiz\",\r\n        \"Specify the Document File Location\"\r\n    )\r\n    documents = self.selectSourceDocumentDialog(document_str)\r\n\r\n    cbo_index = self.cboDocType.currentIndex()\r\n    doc_id = self.cboDocType.itemData(cbo_index)\r\n    # loop through all documents that are added into the widget\r\n    for doc in documents:\r\n        self.sourceDocManager.insertDocumentFromFile(\r\n            doc,\r\n            doc_id,\r\n            self.curr_profile.social_tenure\r\n        )\r\n\r\n```\r\n**Saving to database**\r\nThen, create an object from each models created using entity_model function. And use each object to set value but only use the entity_object to save to the database. This is already well explained by John here. \r\nThe model_objects() method located in sourcedocument.py returns list of lists. [[doc_type_1 document objects],[doc_type_2 document objects]] It loads the model objects for the documents uploaded. \r\nThen to append the documents into the entity model object. Use two loops. \r\nExample:\r\n```\r\n\r\nentity_obj = The_entity_model()\r\nentity_obj.party_id = 4\r\nentity_obj.spatial_unit_id = 1\r\n…..\r\nif len(model_objs) > 0:\r\n       for model_object in model_objects:\r\n\tfor doc_obj in model_objects:\r\n    \t\tentity_obj.documents.append(doc_obj)\r\nentity_obj.save()\r\n\r\n```\r\n**Viewing Source Document**\r\nTo view the source documents that are stored in the database, we need to use `insertDocFromModel() `method located in sourcedocument.py. \r\n`Source_document.insertDocFromModel(source_doc_object, document_type_id)`\r\nThe first parameter is a filtered source_doc_object for a selected entity record. In other words, it means object for all documents uploaded for a given entity for instance, social tenure. \r\n\r\nUnder data/supporting_documents.py, there is a function called document_models that returns list of document_objects for a specific entity id. The parameters are the _entity, entity_souce_document link column_,  and the _entity id_ (int). \r\n\r\nDocument_type_id is used as a container id using which the document type can be loaded into the tabwidget, under a layout. `insertDocFromModel `method then instantiates the document widget so that the user can view by clicking each widget with a file name. \r\n\r\n"}}
{"actor":{"display_login":"wondie"}}