Skip to content

Commit

Permalink
[MIG] sms_verimor_http: Migration to 16.0 altinkaya-opensource#59
Browse files Browse the repository at this point in the history
  • Loading branch information
milleniumkid committed Dec 21, 2024
1 parent 3510817 commit 7bcea4a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 18 deletions.
4 changes: 4 additions & 0 deletions sms_verimor_http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Copyright 2022 Yiğit Budak (https://github.com/yibudak)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from . import models
7 changes: 5 additions & 2 deletions sms_verimor_http/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# Copyright 2022 Yiğit Budak (https://github.com/yibudak)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

{
"name": "Sms Verimor HTTP",
"summary": "Send sms using Verimor http API",
"version": "12.0.1.0.0",
"version": "16.0.1.0.0",
"category": "SMS",
"website": "https://github.com/odoo-turkey",
"author": "Yiğit Budak, Odoo Turkey Localization Group",
"maintainers": ["yibudak"],
"license": "AGPL-3",
"license": "LGPL-3",
"application": False,
"installable": True,
"external_dependencies": {"python": [], "bin": []},
Expand Down
4 changes: 4 additions & 0 deletions sms_verimor_http/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Copyright 2022 Yiğit Budak (https://github.com/yibudak)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from . import iap_account
from . import sms_api
36 changes: 26 additions & 10 deletions sms_verimor_http/models/iap_account.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
# Copyright 2022 Yiğit Budak (https://github.com/yibudak)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import fields, models, api, _
from odoo.exceptions import Warning


class IapAccount(models.Model):
_inherit = "iap.account"

provider = fields.Selection(selection_add=[("sms_verimor_http", "SMS Verimor API")])
sms_verimor_http_username = fields.Char(string="Username", required=True,
help="Username of your Verimor account. (required)")
sms_verimor_http_password = fields.Char(string="Password", required=True,
help="Password of your Verimor account. (required)")
sms_verimor_http_sms_header = fields.Char(string="SMS Header",
help="Sender ID (Title). If source_addr is empty,"
" your first title registered in the system is used.")
provider = fields.Selection(
selection_add=[("sms_verimor_http", "SMS Verimor API")],
ondelete={"sms_verimor_http": "cascade"},
required=True,
)
sms_verimor_http_username = fields.Char(
string="Username",
required=True,
help="Username of your Verimor account. (required)",
)
sms_verimor_http_password = fields.Char(
string="Password",
required=True,
help="Password of your Verimor account. (required)",
)
sms_verimor_http_sms_header = fields.Char(
string="SMS Header",
help="Sender ID (Title). If source_addr is empty,"
" your first title registered in the system is used.",
)

def _get_service_from_provider(self):
if self.provider == "sms_verimor_http":
return "sms"

@api.one
def get_verimor_sms_balance(self):

if not self or not (self.sms_verimor_http_username and self.sms_verimor_http_password):
if not self or not (
self.sms_verimor_http_username and self.sms_verimor_http_password
):
raise Warning(_("You need to save your Verimor account first."))

SmsAPI = self.env["sms.api"]
Expand Down
41 changes: 36 additions & 5 deletions sms_verimor_http/models/sms_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright 2022 Yiğit Budak (https://github.com/yibudak)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).


import requests

Expand All @@ -19,8 +22,7 @@ def _prepare_verimor_http_params(self, account, number, message):
"username": account.sms_verimor_http_username,
"password": account.sms_verimor_http_password,
"source_addr": account.sms_verimor_http_sms_header,
"messages": [{'msg': message,
'dest': ",".join(n for n in number)}],
"messages": [{"msg": message, "dest": ",".join(n for n in number)}],
}

def _send_sms_with_verimor_http(self, account, number, message):
Expand All @@ -35,24 +37,53 @@ def _send_sms_with_verimor_http(self, account, number, message):

return response


def _get_balance_verimor_sms_api(self, account):
r = requests.get(
VERIMOR_GET_BALANCE_ENDPOINT,
params={
"username": account.sms_verimor_http_username,
"password": account.sms_verimor_http_password,
}
},
)
response = r.text
if r.status_code != 200:
raise ValidationError(response)
return response

@api.model
def _send_sms(self, number, message):
account = self.env["iap.account"].get("sms")
if account.provider == "sms_verimor_http":
self._send_sms_with_verimor_http(account, number, message)
else:
return super()._send_sms(number, message)

def _send_sms_batch(self, messages):
"""Send SMS messages in batch using Verimor HTTP API"""
account = self.env["iap.account"].get("sms")
if account.provider != "sms_verimor_http":
return super()._send_sms_batch(messages)

result = []
for message in messages:
try:
number = message["number"]
content = message["content"]
# Send each SMS individually
response = self._send_sms_with_verimor_http(account, [number], content)
result.append(
{
"res_id": message["res_id"],
"state": "success",
"credit": 1, # Adjust based on the response from the API
}
)
except ValidationError as e:
result.append(
{
"res_id": message["res_id"],
"state": str(e),
"credit": 0,
}
)

return result
2 changes: 1 addition & 1 deletion sms_verimor_http/views/iap_account_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<group>
<field name="sms_verimor_http_username" />
<field name="sms_verimor_http_password" />
<field name="sms_verimor_http_sms_header" />
<field name="sms_verimor_http_sms_header" string="SMS Header" />
</group>

<group>
Expand Down

0 comments on commit 7bcea4a

Please sign in to comment.