Skip to content

Commit

Permalink
Merge pull request #25 from dibik96/17.0-develop
Browse files Browse the repository at this point in the history
SR-123: ODK app user listed in registration portal user view
  • Loading branch information
shibu-narayanan authored Jul 30, 2024
2 parents da52f62 + e6ec130 commit c584880
Show file tree
Hide file tree
Showing 18 changed files with 702 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ jobs:
ports:
- 5432:5432
steps:
# TODO: Think of caching dependency install.
- run: DEBIAN_FRONTEND=noninteractive apt-get install -qq --no-install-recommends build-essential autoconf autotools-dev automake libtool
- uses: actions/checkout@v3
with:
persist-credentials: false
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Available addons
addon | version | maintainers | summary
--- | --- | --- | ---
[g2p_lock_unlock](g2p_lock_unlock/) | 17.0.1.0.0 | | OpenG2P Lock Unlock
[g2p_registration_portal_user](g2p_registration_portal_user/) | 17.0.1.0.0 | | Registration Portal User
[g2p_registry_g2p_connect_rest_api](g2p_registry_g2p_connect_rest_api/) | 17.0.1.0.0 | | OpenG2P Registry: G2P Connect REST API
[g2p_registry_id_deduplication](g2p_registry_id_deduplication/) | 17.0.1.0.0 | | OpenG2P Registry ID Deduplication
[g2p_social_registry](g2p_social_registry/) | 17.0.1.0.0 | | OpenG2P Social Registry
Expand Down
57 changes: 57 additions & 0 deletions g2p_registration_portal_user/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
========================
Registration Portal User
========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:925a4bc0a62b70aa19178851792a6a4900f707189dd64fbe37d9323cf52de1d7
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/github-OpenG2P%2Fopeng2p--social--registry-lightgray.png?logo=github
:target: https://github.com/OpenG2P/openg2p-social-registry/tree/17.0-develop/g2p_registration_portal_user
:alt: OpenG2P/openg2p-social-registry

|badge1| |badge2|

G2P Registration Portal User

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OpenG2P/openg2p-social-registry/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OpenG2P/openg2p-social-registry/issues/new?body=module:%20g2p_registration_portal_user%0Aversion:%2017.0-develop%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* OpenG2P

Contributors
~~~~~~~~~~~~

`OpenG2P <https://openg2p.org>`__ donated the original code to the project.

Maintainers
~~~~~~~~~~~

This module is part of the `OpenG2P/openg2p-social-registry <https://github.com/OpenG2P/openg2p-social-registry/tree/17.0-develop/g2p_registration_portal_user>`_ project on GitHub.

You are welcome to contribute.
2 changes: 2 additions & 0 deletions g2p_registration_portal_user/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
23 changes: 23 additions & 0 deletions g2p_registration_portal_user/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Registration Portal User",
"category": "G2P",
"version": "17.0.1.0.0",
"sequence": 1,
"author": "OpenG2P",
"website": "https://openg2p.org",
"license": "Other OSI approved licence",
"depends": ["g2p_service_provider_portal_base", "g2p_odk_importer"],
"data": [
"security/ir.model.access.csv",
"views/registration_user_view.xml",
"views/odk_app_user.xml",
],
"assets": {
"web.assets_frontend": [],
"web.assets_common": [],
"website.assets_wysiwyg": [],
},
"application": True,
"installable": True,
"auto_install": False,
}
Empty file.
2 changes: 2 additions & 0 deletions g2p_registration_portal_user/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import res_partner
from . import odk_app_user
10 changes: 10 additions & 0 deletions g2p_registration_portal_user/models/odk_app_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from odoo import fields, models


class ODKAppUser(models.Model):
_name = "odk.app.user"
_description = "ODK App User"

name = fields.Char(string="ODK App User Name")
odk_user_id = fields.Integer(string="ODK App User ID")
partner_id = fields.Many2one("res.partner", string="Partner")
75 changes: 75 additions & 0 deletions g2p_registration_portal_user/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import json
import logging

import requests

from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError

_logger = logging.getLogger(__name__)


class ResPartner(models.Model):
_inherit = "res.partner"

odk_config_id = fields.Many2one("odk.config", string="ODK Config")
odk_app_user = fields.Many2one("odk.app.user", string="ODK App User")
session = fields.Char(string="Session Token", readonly=True)

@api.onchange("odk_config_id")
def _onchange_odk_config_id(self):
self.odk_app_user = [
5,
]
if self.odk_config_id:
base_url = self.odk_config_id.base_url
project_id = self.odk_config_id.project
username = self.odk_config_id.username
password = self.odk_config_id.password
self._login(base_url, username, password)
if self.session:
app_users = self._fetch_app_users(base_url, project_id)
_logger.info(
"LOG----->ODK APP USER:%s"
% [f"{user['id']} -{user['displayName']}" for user in app_users]
)
return {"domain": {"odk_app_user": [("id", "in", [user["id"] for user in app_users])]}}
else:
return {"domain": {"odk_app_user": []}}

def _login(self, base_url, username, password):
login_url = f"{base_url}/v1/sessions"
headers = {"Content-Type": "application/json"}
data = json.dumps({"email": username, "password": password})
try:
response = requests.post(login_url, headers=headers, data=data, timeout=10)
response.raise_for_status()
if response.status_code == 200:
self.session = response.json()["token"]
except Exception as e:
_logger.exception("Login failed: %s", e)
raise ValidationError(f"Login failed: {e}") from e

def _fetch_app_users(self, base_url, project_id):
url = f"{base_url}/v1/projects/{project_id}/app-users"
headers = {
"Content-Type": "application/json",
"X-Extended-Metadata": "true",
"Authorization": f"Bearer {self.session}",
}
response = requests.get(url, headers=headers, timeout=10)

if response.status_code == 200:
app_users_data = response.json()
query = """
DELETE FROM odk_app_user WHERE partner_id = %s
"""
self.env.cr.execute(query, (self.id.origin or self.id,))

for user in app_users_data:
self.env["odk.app.user"].create(
{"name": user["displayName"], "odk_user_id": user["id"], "partner_id": self.id.origin}
)
return app_users_data
else:
raise UserError(_("Failed to fetch app users"))
3 changes: 3 additions & 0 deletions g2p_registration_portal_user/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
1 change: 1 addition & 0 deletions g2p_registration_portal_user/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`OpenG2P <https://openg2p.org>`__ donated the original code to the project.
1 change: 1 addition & 0 deletions g2p_registration_portal_user/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
G2P Registration Portal User
2 changes: 2 additions & 0 deletions g2p_registration_portal_user/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_odk_app_user,odk_app_user_name,model_odk_app_user,,1,1,1,1
Loading

0 comments on commit c584880

Please sign in to comment.