Merge pull request #147 from expsa/fix_api_web

fix api
This commit is contained in:
AbuzarExp 2024-07-10 15:03:30 +03:00 committed by GitHub
commit 1352fc4ae9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 64 additions and 44 deletions

View File

@ -17,7 +17,7 @@ _logger = logging.getLogger(__name__)
class NotificationController(http.Controller):
@http.route(['/rest_api/v1/notifications/','/rest_api/v1/notifications/page/<int:page>'], type='http', auth='none', csrf=False, methods=['GET'], website=True)
@http.route(['/rest_api/v1/notifications/', '/rest_api/v1/notifications/page/<int:page>'], type='http', auth='none', csrf=False, methods=['GET'], website=True)
def index(self, page=1, **kw):
"""
fetch notifications
@ -40,7 +40,7 @@ class NotificationController(http.Controller):
# partner_ids
# search([('partner_ids','in',user.partner_id.id)])
#
domain = [("sent", "=", True),'&',("is_system", "=", True),('partner_ids','in',user.partner_id.id)]
domain = [("sent", "=", True), '&', ("is_system", "=", True), ('partner_ids', 'in', user.partner_id.id)]
ids = user.env['firebase.notification'].search(domain, order="create_date desc")
notifications = []
pager = request.website.pager(
@ -53,11 +53,10 @@ class NotificationController(http.Controller):
records = user.env['firebase.notification'].search(domain).search(
domain, limit=20, offset=pager['offset'])
for res in records:
notifications.append({
'notification':{"title":res.title,
"body": res.body,},
"body": res.body, },
"id":res.id,
"title":res.title,
"body": res.body,
@ -86,7 +85,6 @@ class NotificationController(http.Controller):
}
return http_helper.response(message="Notifications found", data=values)
@http.route(['/rest_api/v1/notifications/<int:notif>'], type='http', auth='none', csrf=False, methods=['PATCH'], website=True)
def open_view(self, notif=None, **kw):
"""
@ -129,7 +127,6 @@ class NotificationController(http.Controller):
"data": str(res.create_date),
"uuid": res.uid,
})
values = {
'links': None,
@ -140,7 +137,6 @@ class NotificationController(http.Controller):
}
return http_helper.response(message="Notification found", data=values)
@http.route(['/rest_api/v1/notifications/<int:notif>'], type='http', auth='none', csrf=False, methods=['DELETE'], website=True)
def delete_notification(self, notif=None, **kw):
"""
@ -203,7 +199,7 @@ class NotificationController(http.Controller):
# security check done successfully
#
domain = [("sent", "=", True),'&',("is_system", "=", True),('partner_ids','in',user.partner_id.id)]
domain = [("sent", "=", True), '&', ("is_system", "=", True), ('partner_ids', 'in', user.partner_id.id)]
ids = user.env['firebase.notification'].search(domain).unlink()
values = {
@ -213,4 +209,4 @@ class NotificationController(http.Controller):
},
"count": None,
}
return http_helper.response(message="Delete successfully", data=values)
return http_helper.response(message="Delete successfully", data=values)

View File

@ -21,7 +21,7 @@ class LeaveController(http.Controller):
def get_return_data(self,hol,approvel=None):
value = {'id': hol.id, 'type': hol.holiday_status_id.name,
'type_value': hol.holiday_status_id.id,
'reason_msg': hol.reject_reason or "",
'reason_msg': hol.reason or "",
'alternative_chick': hol.holiday_status_id.alternative_chick,
'replacement_id': hol.replace_by.id if hol.replace_by else False,
'replacement_name': hol.replace_by.name if hol.replace_by else False,

View File

@ -38,7 +38,7 @@ class LeaveController(http.Controller):
value = {'id': hol.id,
'type': hol.holiday_status_id.name,
'type_value': hol.holiday_status_id.id,
'reason_msg': hol.reject_reason or "",
'reason_msg': hol.reason or "",
'alternative_chick': hol.holiday_status_id.alternative_chick,
'replacement_id': hol.replace_by.id if hol.replace_by else False,
'replacement_name': hol.replace_by.name if hol.replace_by else False,
@ -59,7 +59,7 @@ class LeaveController(http.Controller):
return value
@http.route(['/rest_api/v2/leaves'], type='http', auth='none', csrf=False, methods=['GET'])
def get_leaves(self, approvel=None, page=None, **kw):
def get_leaves(self, approvel=None, done=None, page=None, **kw):
http_method, body, headers, token = http_helper.parse_request()
result = validator.verify_token(token)
if not result['status']:
@ -89,10 +89,16 @@ class LeaveController(http.Controller):
[('state', '=', 'open'), ('id', '!=', employee.id)], ['name'])
if approvel:
holidays = http.request.env['hr.holidays'].search(
[('state', '!=', 'draft'), ('employee_id', '!=', employee.id), ('type', '=', 'remove')],
[('state', 'in', ['confirm','validate','approved']), ('employee_id', '!=', employee.id), ('type', '=', 'remove')],
offset=offset, limit=limit)
count = http.request.env['hr.holidays'].search_count(
[('state', '!=', 'draft'), ('employee_id', '!=', employee.id), ('type', '=', 'remove')],)
[('state', 'not in', ['confirm','validate','approved']), ('employee_id', '!=', employee.id), ('type', '=', 'remove')],)
if done:
holidays = http.request.env['hr.holidays'].search(
[('state', 'not in', ['validate1','refuse','cancel']), ('employee_id', '!=', employee.id), ('type', '=', 'remove')],
offset=offset, limit=limit)
count = http.request.env['hr.holidays'].search_count(
[('state', 'not in', ['validate1','refuse','cancel']), ('employee_id', '!=', employee.id), ('type', '=', 'remove')],)
else:
holidays = http.request.env['hr.holidays'].search(
[('employee_id', '=', employee.id), ('type', '=', 'remove')], offset=offset, limit=limit)

View File

@ -56,6 +56,7 @@ class FirebaseNotification(models.Model):
#@api.multi
def send(self, **kwargs):
print("**********************************************")
if kwargs is None:
kwargs = {}
for notif in self:

View File

@ -95,6 +95,7 @@ class HrHolidays(models.Model):
})
}
_logger.warning(type(data))
_logger.warning(data)
if users:
partner = users.mapped('partner_id')
for part in partner:

View File

@ -1,45 +1,58 @@
import json
import requests
import logging
from odoo import models, fields, api
import json, requests
_logger = logging.getLogger(__name__)
class Partner(models.Model):
_inherit = 'res.partner'
firebase_registration_ids = fields.One2many(
"firebase.registration", "partner_id", readonly=True
)
def send_notification(self, message_title, message_body,data=None, all_device=True):
def send_notification(self, message_title, message_body, data=None, all_device=True):
notification_data = {
"title": str(message_title),
"body": str(message_body),
"meta": json.dumps(data) if data else None,
"partner_ids": [(4, self.id)],
"is_system": True,
"sent": True,
}
notification = self.env['firebase.notification'].sudo().create(notification_data)
print(notification)
print(notification_data)
if all_device:
import json
record = self.env['firebase.notification'].sudo().create({
"title":str(message_title),
"body":str(message_body),
"meta": str(json.dumps(data)) if data else None,
"partner_ids":[(4,self.id)],
"is_system":True,
"sent":True
})
for reg in self.firebase_registration_ids:
reg.with_context(lang=self.lang).send_message(message_title, message_body,data={
"title":str(message_title),
"body":str(message_body),
"meta": str(json.dumps(data)) if data else None,
"is_system":"true",
'viewed':"false",
"sent":"true",
"data":str(record.create_date),
"id":str(record.id)
})
reg.with_context(lang=self.lang).send_message(
message_title,
message_body,
data={
"title": str(message_title),
"body": str(message_body),
"meta": json.dumps(data) if data else None,
"is_system": "true",
'viewed': "false",
"sent": "true",
"data": str(notification.create_date),
"id": str(notification.id)
}
)
else:
self.firebase_registration_ids[0].with_context(lang=self.lang).send_message(message_title, message_body, data=data)
if self.firebase_registration_ids:
self.firebase_registration_ids[0].with_context(lang=self.lang).send_message(
message_title,
message_body,
data=data
)
def user_push_notification(self, fcm_token):
url = "https://fcm.googleapis.com/fcm/send"
header = {
headers = {
'Content-Type': 'application/json',
'Authorization': 'key=%s' % (self.env.user.company_id.fcm_server_key)
'Authorization': f'key={self.env.user.company_id.fcm_server_key}'
}
body = json.dumps({
"to": fcm_token,
@ -50,7 +63,9 @@ class Partner(models.Model):
}
})
try:
respons = requests.post(url=url, data=body, headers=header)
response = requests.post(url=url, data=body, headers=headers)
response.raise_for_status()
return True
except Exception as e:
except requests.exceptions.RequestException as e:
_logger.error(f"Failed to send push notification: {e}")
return False

View File

@ -246,8 +246,7 @@ class RestApi(Controller):
context = request.env.context.copy()
context.update({"active_model": btn.model})
reject_reason = kw.get('reason_msg')
if reject_reason:
context.update({"reject_reason": reject_reason})
context.update({"active_id": int(active_id)})
request.env.context = context
btn.with_context(context)._run_code(active_id, btn.model, request.env)
@ -265,6 +264,8 @@ class RestApi(Controller):
)
)
users = btn_new.mapped("group_ids").mapped("users") or None
if reject_reason:
obj.write({"reason": reject_reason})
obj.firebase_notification(users)
return http_helper.response(message="Successful", data=res)