i have odoo17 enterprise and fastapi project and we need to add location to sessions list and detail and we get it from ip adress of user that we get from request, heres my files add location and ip adress of user when creating session and write it to session data and give it back in session list and session detail routers:
redis_session.py: import redis
from datetime import datetime, timezone
import json
import uuid
import asyncio
from ..schemas.user_session_schema import UserStatus
class IntegratedAuthSessionManager:
def init(self, redis_url):
self.redis = redis.Redis(host=redis_url, port=6379, decode_responses=True)
self.cleanup_interval = 60
self.activity_timeout = 300 # 5 minutes for idle status
self.websocket_connections: Dict[str, Set[str]] = {}
code
Assistant
async def start_cleanup_task(self):
while True:
await self.cleanup_expired_sessions()
await self.update_idle_status()
await asyncio.sleep(self.cleanup_interval)
async def update_idle_status(self):
current_time = datetime.now(timezone.utc)
for key in self.redis.scan_iter("session:*"):
session_data = self.redis.get(key)
if session_data:
session = json.loads(session_data)
last_activity = datetime.fromisoformat(session['last_activity'])
if (current_time - last_activity).total_seconds() > self.activity_timeout:
session['status'] = UserStatus.IDLE
self.redis.set(key, json.dumps(session))
async def cleanup_expired_sessions(self):
current_time = datetime.now(timezone.utc).isoformat()
for key in self.redis.scan_iter("session:*"):
session_data = self.redis.get(key)
if session_data:
session = json.loads(session_data)
if session['expires_at'] <= current_time:
self.end_session(key.decode('utf-8').split(':')[1])
def create_session(self, user_data):
user_id = user_data['user_id']
session_id = str(uuid.uuid4())
session_data = {
'user_id': user_id,
'device_name': user_data['device_name'],
'device_id': user_data['device_id'],
'expires_at': user_data['expires_at'].isoformat(),
'app_version': user_data.get('app_version'),
'location': user_data.get('location'),
'last_activity': datetime.now(timezone.utc).isoformat(),
'device_unique_name': user_data.get('device_unique_name'),
'is_ios': user_data.get('is_ios'),
'status': UserStatus.ONLINE,
'lock_timestamp': datetime.now(timezone.utc).isoformat()
}
self.redis.setex(
f"session:{session_id}",
int(user_data['expires_at'].timestamp()) - int(datetime.now(timezone.utc).timestamp()),
json.dumps(session_data)
)
self.redis.sadd(f"user_sessions:{user_id}", session_id)
return session_id
# Store the session data with expiration
self.redis.setex(f"session:{session_id}", int(user_data['expires_at'].timestamp()) - int(datetime.now(timezone.utc).timestamp()), json.dumps(session_data))
# Add this session to the user's set of active sessions
self.redis.sadd(f"user_sessions:{user_id}", session_id)
return session_id
def get_session(self, session_id):
session_data = self.redis.get(f"session:{session_id}")
if not session_data:
return None
session = json.loads(session_data)
# Check if the session has expired
current_time = datetime.now(timezone.utc).isoformat()
if current_time > session['expires_at']:
self.end_session(session_id)
return None
return session
def end_session(self, session_id):
session_data = self.redis.get(f"session:{session_id}")
if session_data:
session = json.loads(session_data)
user_id = session['user_id']
self.redis.delete(f"session:{session_id}")
self.redis.srem(f"user_sessions:{user_id}", session_id)
def update_last_activity(self, session_id):
session_data = self.get_session(session_id)
if session_data:
session_data['last_activity'] = datetime.now(timezone.utc).isoformat()
self.redis.set(f"session:{session_id}", json.dumps(session_data))
def update_session(self, session_id, user_data):
session_data = self.get_session(session_id)
if session_data:
session_data.update(user_data)
self.redis.set(f"session:{session_id}", json.dumps(session_data))
def get_sessions_by_user_and_device(self, user_id, device_id):
"""
Retrieve all active sessions for a specific user and device.
"""
# Get all session IDs associated with the user
session_ids = self.redis.smembers(f"user_sessions:{user_id}")
active_sessions = []
# Iterate over all session IDs and filter by device_id
for session_id in session_ids:
session_data = self.get_session(session_id)
if session_data:
# Check if the session belongs to the given device_id
if session_data.get('device_id') == device_id:
active_sessions.append(session_id)
return active_sessions
def update_session_status(self, session_id: str, status: UserStatus):
session_data = self.get_session(session_id)
if session_data:
session_data['status'] = status
session_data['last_activity'] = datetime.now(timezone.utc).isoformat()
self.redis.set(f"session:{session_id}", json.dumps(session_data))
def register_websocket(self, session_id: str, websocket_id: str):
if session_id not in self.websocket_connections:
self.websocket_connections[session_id] = set()
self.websocket_connections[session_id].add(websocket_id)
self.update_session_status(session_id, UserStatus.ONLINE)
def unregister_websocket(self, session_id: str, websocket_id: str):
if session_id in self.websocket_connections:
self.websocket_connections[session_id].discard(websocket_id)
if not self.websocket_connections[session_id]:
self.update_session_status(session_id, UserStatus.OFFLINE)
del self.websocket_connections[session_id]
user_id = partner.id
user_sessions = redis_session_manager.redis.smembers(f"user_sessions:{user_id}")
if session_id not in user_sessions:
raise HTTPException(status_code=404, detail=_("Session not found"))
session_data = redis_session_manager.get_session(session_id)
if not session_data:
raise HTTPException(status_code=404, detail=_("Session data not found"))
session_detail = SessionSchema(
session_id=session_id,
app_version=session_data.get('app_version'),
device_name=session_data['device_name'],
location=session_data.get('location'),
last_activity=session_data.get('last_activity'),
is_ios=session_data.get('is_ios') or False,
status=session_data.get('status', UserStatus.OFFLINE),
lock_timestamp=session_data.get('lock_timestamp') if 'lock_timestamp' in session_data else None,
)
return session_detail
session_schema.py:
from pydantic import BaseModel
from typing import List, Optional
from datetime import datetime
from enum import Enum
class SessionResponse(BaseModel):
current_session: SessionSchema
other_sessions: List[SessionSchema]
ip_geolocation.py:
ip_geolocation.py
import requests
from typing import Optional, Dict
import asyncio
def get_location_from_ip_sync(ip_address: str) -> Optional[Dict[str, str]]:
"""
Synchronously fetches location data for a given IP address using ip-api.com.
code
Args:
ip_address (str): The IP address of the user.
Returns:
Optional[Dict[str, str]]: A dictionary containing location details if successful, else None.
"""
url = f"http://ip-api.com/json/{ip_address}"
try:
response = requests.get(url, timeout=5)
data = response.json()
if data.get("status") == "success":
location = {
"country": data.get("country"),
"regionName": data.get("regionName"),
"city": data.get("city"),
"zip": data.get("zip"),
"lat": data.get("lat"),
"lon": data.get("lon"),
"timezone": data.get("timezone"),
"isp": data.get("isp"),
"org": data.get("org"),
"as": data.get("as"),
}
return location
else:
return None
except Exception as e:
return None
async def get_location_from_ip(ip_address: str) -> Optional[Dict[str, str]]:
"""
Asynchronously fetches location data for a given IP address using ip-api.com.
code
Args:
ip_address (str): The IP address of the user.
Returns:
Optional[Dict[str, str]]: A dictionary containing location details if successful, else None.
"""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, get_location_from_ip_sync, ip_address)
user.py: auth method
code
@api.model
def commeta_authenticate(self, token_data, device_data):
if not token_data:
return {'error': 'No token data provided'}
user_info = self.commeta_get_user_info(token_data['access_token'])
if not user_info:
return {'error': 'Failed to get user info'}
user = self.sudo().search([('phone_number', '=', user_info['phone'])], limit=1)
profile = user_info['profile']
avatar_data = None
if 'avatar' in profile and profile['avatar'] and (not user or not user.avatar):
try:
response = requests.get(profile['avatar'])
if response.status_code == 200:
avatar_data = base64.b64encode(BytesIO(response.content).read()).decode('utf-8')
except requests.RequestException as e:
_logger.error(f"Failed to download avatar: {e}")
if not user:
# Create a new user if none exists
user_data = {
'first_name': profile['first_name'],
'name': profile['first_name'],
'last_name': profile['last_name'],
'middle_name': profile['middle_name'],
'phone_number': user_info['phone'],
'birth_date': profile['birth_date'],
# 'avatar': profile['avatar'], # TODO avatar for user
'gender': 'male' if profile['gender'] == 'M' else 'female',
'last_login': datetime.now(),
'pnfl': profile['pinfl'],
}
if avatar_data:
user_data['avatar'] = avatar_data
user = self.sudo().create(user_data)
else:
# Update only missing or outdated fields
profile = user_info['profile']
update_fields = {}
try:
if not user.first_name:
update_fields['first_name'] = profile['first_name']
update_fields['name'] = profile['first_name']
if not user.last_name:
update_fields['last_name'] = profile['last_name']
if not user.middle_name:
update_fields['middle_name'] = profile['middle_name']
if not user.birth_date:
update_fields['birth_date'] = profile['birth_date']
if not user.avatar and avatar_data:
update_fields['avatar'] = avatar_data # TODO Avatar For user
if not user.gender:
update_fields['gender'] = 'male' if profile['gender'] == 'M' else 'female'
if not user.pnfl:
update_fields['pnfl'] = profile['pinfl']
except KeyError as e:
return {'error': f"Missing required field in profile: {e}"}
if update_fields:
update_fields['last_login'] = datetime.now() # Update the last login time
user.write(update_fields)
device_user = self.env['user.device'].sudo().search([
('partner_id', '=', user.id),
('device_name', '=', device_data.get('device_name')),
('device_unique_name', '=', device_data.get('device_unique_name')),
])
if not device_user:
device_user = self.env['user.device'].sudo().create({
'partner_id': user.id,
'device_name': device_data.get('device_name'),
'device_unique_name': device_data.get('device_unique_name'),
'is_ios': device_data.get('is_ios'),
})
existing_sessions = redis_session_manager.get_sessions_by_user_and_device(user.id, device_user.id)
if existing_sessions:
session_id = existing_sessions[0] # Assuming sessions are stored as a list
session_data = redis_session_manager.get_session(session_id)
else:
# Create a new session if no existing session is found
user_data = {
'user_id': user.id,
'expires_at': datetime.now(timezone.utc) + timedelta(days=365),
'device_name': device_data.get('device_name'),
'device_id': device_user.id,
'device_unique_name': device_data.get('device_unique_name'),
'is_ios': device_data.get('is_ios'),
}
session_id = redis_session_manager.create_session(user_data=user_data)
project_token_data = {
'user_id': user.id,
'phone': user.phone_number,
'device_user_id': device_user.id,
'session_id': session_id
}
access_token, refresh_token = self.create_tokens(project_token_data)
access_token_expire = datetime.now(timezone.utc) + timedelta(days=365)
return {
'user_id': user.id,
"username": user.name,
'access_token': access_token,
'refresh_token': refresh_token,
'expires_at': access_token_expire.isoformat(),
'is_verified': bool(user.pnfl),
}
To add the user's IP address and location to session data in your Odoo 17 Enterprise and FastAPI project, follow these steps:
Extract the User's IP Address from the Request
Fetch the Location Using the IP Address
Update the Session Creation Process to Include IP and Location
Modify Session Schemas to Include IP Address
Ensure IP and Location are Returned in Session List and Details
Below are detailed steps with code modifications for each part of your project.
1. Extract the User's IP Address from the Request
To obtain the user's IP address, you need to extract it from the incoming HTTP request. In FastAPI, you can achieve this by accessing the Request object.
Modify the Authentication Endpoint in FastAPI
Assuming you have an authentication endpoint in FastAPI that calls commeta_authenticate, modify it to extract the IP address and pass it to the authentication method.
Add IP Extraction in FastAPI Router (e.g., auth_router.py):
python
from fastapi import APIRouter, Request, HTTPException
from ..dependencies import get_odoo_instance # Assuming you have a dependency to get Odoo instance
from ..schemas.user_session_schema import TokenData, DeviceData # Adjust imports as necessary
router = APIRouter(tags=['Authentication'])
@router.post("/authenticate", response_model=AuthenticationResponse)
async def authenticate(request: Request, token_data: TokenData, device_data: DeviceData, odoo=Depends(get_odoo_instance)):
client_ip = request.client.host
# Fetch location asynchronously
location_data = await get_location_from_ip(client_ip)
if location_data:
location = f"{location_data.get('city')}, {location_data.get('regionName')}, {location_data.get('country')}"
else:
location = None
# Call Odoo's commeta_authenticate with IP and location
auth_result = odoo.commeta_authenticate(token_data, device_data, ip_address=client_ip, location=location)
if 'error' in auth_result:
raise HTTPException(status_code=400, detail=auth_result['error'])
return auth_result
Notes:
Replace AuthenticationResponse, TokenData, and DeviceData with your actual Pydantic models.
Ensure get_location_from_ip is properly imported from your ip_geolocation.py.
2. Fetch the Location Using the IP Address
Use the provided ip_geolocation.py to fetch the location data asynchronously.
Ensure ip_geolocation.py is Available:
You've already provided ip_geolocation.py with synchronous and asynchronous functions. Ensure it's correctly placed and accessible.
3. Update the Session Creation Process to Include IP and Location
Modify the commeta_authenticate method in your Odoo model to accept ip_address and location, then store them in the session.
Modify commeta_authenticate in user.py
Updated commeta_authenticate Method:
python
Key Changes:
Parameters Added:ip_address and location.
Session Data Updated: Includes 'ip_address': ip_address and 'location': location.
Notes:
Ensure that commeta_authenticate is called with ip_address and location from your FastAPI authentication endpoint.
Adjust error handling and logging as needed.
4. Modify Session Schemas to Include IP Address
Update your Pydantic schemas to include the ip_address field.
Update session_schema.py
Modified SessionSchema and SessionResponse:
python
from pydantic import BaseModel
from typing import List, Optional
from datetime import datetime
from enum import Enum
class UserStatus(str, Enum):
ONLINE = "online"
OFFLINE = "offline"
IDLE = "idle"
class SessionSchema(BaseModel):
session_id: str
app_version: Optional[str]
device_name: Optional[str]
location: Optional[str]
ip_address: Optional[str] # New field added
last_activity: Optional[str]
is_ios: Optional[bool] = False
status: UserStatus = UserStatus.OFFLINE
lock_timestamp: Optional[datetime] = None
class SessionResponse(BaseModel):
current_session: SessionSchema
other_sessions: List[SessionSchema]
Key Changes:
Added ip_address: Optional[str] to SessionSchema.
5. Update the Session Manager to Store IP and Location
Ensure the session manager stores ip_address and location when creating a session.
Included 'ip_address': user_data.get('ip_address').
6. Ensure IP and Location are Returned in Session List and Details
Modify your FastAPI routers to include ip_address in the session responses.
Update user_session_router.py
Modified Endpoints to Include ip_address:
python
Key Changes:
Added ip_address=current_session_data.get('ip_address') to SessionSchema instances in both /sessions and /session/{session_id} endpoints.
7. Ensure Session Cleanup Handles IP and Location Appropriately
While not strictly necessary for adding IP and location, ensure that your session cleanup tasks in redis_session.py handle the updated session data correctly.
No Changes Needed Here Unless Specific Cleanup Logic Relies on IP or Location
8. Testing the Implementation
After making the above changes, perform the following tests to ensure everything works as expected:
Authenticate a User:
Send a request to the /authenticate endpoint.
Ensure that the response contains the access and refresh tokens.
Check Session Creation:
Verify in Redis that a new session is created with ip_address and location fields.
Retrieve Sessions List:
Call the /sessions endpoint.
Ensure that both current_session and other_sessions include ip_address and location.
Retrieve Session Details:
Call the /session/{session_id} endpoint with a valid session_id.
Ensure the response includes ip_address and location.
Session Cleanup:
Let a session expire and ensure it is removed from Redis and no longer appears in session lists.
Complete Code Snippets
For clarity, here's how the key parts should look after modifications.
session_schema.py
python
from pydantic import BaseModel
from typing import List, Optional
from datetime import datetime
from enum import Enum
class UserStatus(str, Enum):
ONLINE = "online"
OFFLINE = "offline"
IDLE = "idle"
class SessionSchema(BaseModel):
session_id: str
app_version: Optional[str]
device_name: Optional[str]
location: Optional[str]
ip_address: Optional[str] # New field
last_activity: Optional[str]
is_ios: Optional[bool] = False
status: UserStatus = UserStatus.OFFLINE
lock_timestamp: Optional[datetime] = None
class SessionResponse(BaseModel):
current_session: SessionSchema
other_sessions: List[SessionSchema]
By following the above steps, you will successfully record the user's IP address and location when creating a session and include this information in both session lists and session details. Ensure that all parts of the application are correctly synchronized with these changes and thoroughly test each functionality after implementation.
Additional Recommendations:
Security Considerations:
Ensure that storing IP addresses complies with your privacy policy and relevant regulations.
Consider anonymizing or encrypting IP addresses if necessary.
Error Handling:
Enhance error handling in commeta_authenticate to manage failures in fetching location data gracefully.
Performance:
Be mindful of the additional latency introduced by geolocation lookups. Optimize or cache results if needed.
Logging:
Implement logging for session creations, especially when adding IP and location data, to aid in monitoring and debugging.
Not yet included in a published release.
from odoo import models, fields, api, _
from datetime import datetime, timedelta, timezone
import base64
from io import BytesIO
from ..utils.redis_session import redis_session_manager
from ..utils.ip_geolocation import get_location_from_ip_sync # Ensure correct import
class ResUsers(models.Model):
_inherit = 'res.users'
@api.model
def commeta_authenticate(self, token_data, device_data, ip_address, location):
if not token_data:
return {'error': 'No token data provided'}
user_info = self.commeta_get_user_info(token_data['access_token'])
if not user_info:
return {'error': 'Failed to get user info'}
user = self.sudo().search([('phone_number', '=', user_info['phone'])], limit=1)
profile = user_info.get('profile', {})
avatar_data = None
if profile.get('avatar') and (not user or not user.avatar):
try:
response = requests.get(profile['avatar'], timeout=5)
if response.status_code == 200:
avatar_data = base64.b64encode(BytesIO(response.content).read()).decode('utf-8')
except requests.RequestException as e:
_logger.error(f"Failed to download avatar: {e}")
if not user:
# Create a new user if none exists
user_data = {
'first_name': profile.get('first_name'),
'name': profile.get('first_name'),
'last_name': profile.get('last_name'),
'middle_name': profile.get('middle_name'),
'phone_number': user_info.get('phone'),
'birth_date': profile.get('birth_date'),
'gender': 'male' if profile.get('gender') == 'M' else 'female',
'last_login': datetime.now(timezone.utc),
'pnfl': profile.get('pinfl'),
}
if avatar_data:
user_data['avatar'] = avatar_data
user = self.sudo().create(user_data)
else:
# Update only missing or outdated fields
update_fields = {}
if not user.first_name:
update_fields['first_name'] = profile.get('first_name')
update_fields['name'] = profile.get('first_name')
if not user.last_name:
update_fields['last_name'] = profile.get('last_name')
if not user.middle_name:
update_fields['middle_name'] = profile.get('middle_name')
if not user.birth_date:
update_fields['birth_date'] = profile.get('birth_date')
if not user.avatar and avatar_data:
update_fields['avatar'] = avatar_data
if not user.gender:
update_fields['gender'] = 'male' if profile.get('gender') == 'M' else 'female'
if not user.pnfl:
update_fields['pnfl'] = profile.get('pinfl')
if update_fields:
update_fields['last_login'] = datetime.now(timezone.utc)
user.write(update_fields)
device_user = self.env['user.device'].sudo().search([
('partner_id', '=', user.id),
('device_name', '=', device_data.get('device_name')),
('device_unique_name', '=', device_data.get('device_unique_name')),
])
if not device_user:
device_user = self.env['user.device'].sudo().create({
'partner_id': user.id,
'device_name': device_data.get('device_name'),
'device_unique_name': device_data.get('device_unique_name'),
'is_ios': device_data.get('is_ios'),
})
existing_sessions = redis_session_manager.get_sessions_by_user_and_device(user.id, device_user.id)
if existing_sessions:
session_id = existing_sessions[0]
session_data = redis_session_manager.get_session(session_id)
else:
# Create a new session with IP and location
user_data = {
'user_id': user.id,
'expires_at': datetime.now(timezone.utc) + timedelta(days=365),
'device_name': device_data.get('device_name'),
'device_id': device_user.id,
'device_unique_name': device_data.get('device_unique_name'),
'is_ios': device_data.get('is_ios'),
'ip_address': ip_address,
'location': location,
}
session_id = redis_session_manager.create_session(user_data=user_data)
project_token_data = {
'user_id': user.id,
'phone': user.phone_number,
'device_user_id': device_user.id,
'session_id': session_id
}
access_token, refresh_token = self.create_tokens(project_token_data)
access_token_expire = datetime.now(timezone.utc) + timedelta(days=365)
return {
'user_id': user.id,
"username": user.name,
'access_token': access_token,
'refresh_token': refresh_token,
'expires_at': access_token_expire.isoformat(),
'is_verified': bool(user.pnfl),
}
from fastapi import APIRouter, HTTPException, Depends
from typing import Annotated
from odoo.api import Environment
from odoo import _
from ..dependencies import odoo_env, get_partner_with_session, get_partner_with_session_id
from ..schemas.user_session_schema import UserStatus, SessionSchema, SessionResponse
from odoo.addons.base.models.res_partner import Partner
from odoo.addons.fastapi_addon.utils.redis_session import redis_session_manager
from datetime import datetime
router = APIRouter(tags=['User Session'])
@router.get("/sessions", response_model=SessionResponse)
async def get_sessions(
env: Annotated[Environment, Depends(odoo_env)],
partner: Annotated[Partner, Depends(get_partner_with_session)],
current_session_id: str = Depends(get_partner_with_session_id)
):
user_id = partner.id
session_ids = redis_session_manager.redis.smembers(f"user_sessions:{user_id}")
current_session_data = redis_session_manager.get_session(current_session_id)
if not current_session_data:
raise HTTPException(status_code=404, detail=_("Current session not found"))
response = SessionResponse(
current_session=SessionSchema(
session_id=current_session_id,
app_version=current_session_data.get('app_version'),
device_name=current_session_data.get('device_name'),
location=current_session_data.get('location'),
ip_address=current_session_data.get('ip_address'), # Added IP address
last_activity=current_session_data.get('last_activity'),
is_ios=current_session_data.get('is_ios') or False,
status=current_session_data.get('status', UserStatus.OFFLINE),
lock_timestamp=current_session_data.get('lock_timestamp') or datetime.now(timezone.utc)
),
other_sessions=[]
)
for session_id in session_ids:
if session_id != current_session_id:
session_data = redis_session_manager.get_session(session_id)
if session_data:
session_info = SessionSchema(
session_id=session_id,
app_version=session_data.get('app_version'),
device_name=session_data.get('device_name'),
location=session_data.get('location'),
ip_address=session_data.get('ip_address'), # Added IP address
last_activity=session_data.get('last_activity'),
is_ios=session_data.get('is_ios') or False,
status=session_data.get('status', UserStatus.OFFLINE),
lock_timestamp=session_data.get('lock_timestamp') or datetime.now(timezone.utc)
)
response.other_sessions.append(session_info)
return response
@router.get("/session/{session_id}", response_model=SessionSchema)
async def get_session_detail(
session_id: str,
env: Annotated[Environment, Depends(odoo_env)],
partner: Annotated[Partner, Depends(get_partner_with_session)]
):
user_id = partner.id
user_sessions = redis_session_manager.redis.smembers(f"user_sessions:{user_id}")
if session_id not in user_sessions:
raise HTTPException(status_code=404, detail=_("Session not found"))
session_data = redis_session_manager.get_session(session_id)
if not session_data:
raise HTTPException(status_code=404, detail=_("Session data not found"))
session_detail = SessionSchema(
session_id=session_id,
app_version=session_data.get('app_version'),
device_name=session_data.get('device_name'),
location=session_data.get('location'),
ip_address=session_data.get('ip_address'), # Added IP address
last_activity=session_data.get('last_activity'),
is_ios=session_data.get('is_ios') or False,
status=session_data.get('status', UserStatus.OFFLINE),
lock_timestamp=session_data.get('lock_timestamp') if 'lock_timestamp' in session_data else None,
)
return session_detail
from odoo import models, fields, api, _
from datetime import datetime, timedelta, timezone
import base64
from io import BytesIO
from ..utils.redis_session import redis_session_manager
from ..utils.ip_geolocation import get_location_from_ip_sync # Ensure correct import
class ResUsers(models.Model):
_inherit = 'res.users'
@api.model
def commeta_authenticate(self, token_data, device_data, ip_address, location):
if not token_data:
return {'error': 'No token data provided'}
user_info = self.commeta_get_user_info(token_data['access_token'])
if not user_info:
return {'error': 'Failed to get user info'}
user = self.sudo().search([('phone_number', '=', user_info['phone'])], limit=1)
profile = user_info.get('profile', {})
avatar_data = None
if profile.get('avatar') and (not user or not user.avatar):
try:
response = requests.get(profile['avatar'], timeout=5)
if response.status_code == 200:
avatar_data = base64.b64encode(BytesIO(response.content).read()).decode('utf-8')
except requests.RequestException as e:
_logger.error(f"Failed to download avatar: {e}")
if not user:
# Create a new user if none exists
user_data = {
'first_name': profile.get('first_name'),
'name': profile.get('first_name'),
'last_name': profile.get('last_name'),
'middle_name': profile.get('middle_name'),
'phone_number': user_info.get('phone'),
'birth_date': profile.get('birth_date'),
'gender': 'male' if profile.get('gender') == 'M' else 'female',
'last_login': datetime.now(timezone.utc),
'pnfl': profile.get('pinfl'),
}
if avatar_data:
user_data['avatar'] = avatar_data
user = self.sudo().create(user_data)
else:
# Update only missing or outdated fields
update_fields = {}
if not user.first_name:
update_fields['first_name'] = profile.get('first_name')
update_fields['name'] = profile.get('first_name')
if not user.last_name:
update_fields['last_name'] = profile.get('last_name')
if not user.middle_name:
update_fields['middle_name'] = profile.get('middle_name')
if not user.birth_date:
update_fields['birth_date'] = profile.get('birth_date')
if not user.avatar and avatar_data:
update_fields['avatar'] = avatar_data
if not user.gender:
update_fields['gender'] = 'male' if profile.get('gender') == 'M' else 'female'
if not user.pnfl:
update_fields['pnfl'] = profile.get('pinfl')
if update_fields:
update_fields['last_login'] = datetime.now(timezone.utc)
user.write(update_fields)
device_user = self.env['user.device'].sudo().search([
('partner_id', '=', user.id),
('device_name', '=', device_data.get('device_name')),
('device_unique_name', '=', device_data.get('device_unique_name')),
])
if not device_user:
device_user = self.env['user.device'].sudo().create({
'partner_id': user.id,
'device_name': device_data.get('device_name'),
'device_unique_name': device_data.get('device_unique_name'),
'is_ios': device_data.get('is_ios'),
})
existing_sessions = redis_session_manager.get_sessions_by_user_and_device(user.id, device_user.id)
if existing_sessions:
session_id = existing_sessions[0]
session_data = redis_session_manager.get_session(session_id)
else:
# Create a new session with IP and location
user_data = {
'user_id': user.id,
'expires_at': datetime.now(timezone.utc) + timedelta(days=365),
'device_name': device_data.get('device_name'),
'device_id': device_user.id,
'device_unique_name': device_data.get('device_unique_name'),
'is_ios': device_data.get('is_ios'),
'ip_address': ip_address, # Added IP address
'location': location, # Added location
}
session_id = redis_session_manager.create_session(user_data=user_data)
project_token_data = {
'user_id': user.id,
'phone': user.phone_number,
'device_user_id': device_user.id,
'session_id': session_id
}
access_token, refresh_token = self.create_tokens(project_token_data)
access_token_expire = datetime.now(timezone.utc) + timedelta(days=365)
return {
'user_id': user.id,
"username": user.name,
'access_token': access_token,
'refresh_token': refresh_token,
'expires_at': access_token_expire.isoformat(),
'is_verified': bool(user.pnfl),
}
from fastapi import APIRouter, HTTPException, Depends
from typing import Annotated
from odoo.api import Environment
from odoo import _
from ..dependencies import odoo_env, get_partner_with_session, get_partner_with_session_id
from ..schemas.user_session_schema import UserStatus, SessionSchema, SessionResponse
from odoo.addons.base.models.res_partner import Partner
from odoo.addons.fastapi_addon.utils.redis_session import redis_session_manager
from datetime import datetime
router = APIRouter(tags=['User Session'])
@router.get("/sessions", response_model=SessionResponse)
async def get_sessions(
env: Annotated[Environment, Depends(odoo_env)],
partner: Annotated[Partner, Depends(get_partner_with_session)],
current_session_id: str = Depends(get_partner_with_session_id)
):
user_id = partner.id
session_ids = redis_session_manager.redis.smembers(f"user_sessions:{user_id}")
current_session_data = redis_session_manager.get_session(current_session_id)
if not current_session_data:
raise HTTPException(status_code=404, detail=_("Current session not found"))
response = SessionResponse(
current_session=SessionSchema(
session_id=current_session_id,
app_version=current_session_data.get('app_version'),
device_name=current_session_data.get('device_name'),
location=current_session_data.get('location'),
ip_address=current_session_data.get('ip_address'), # Added IP address
last_activity=current_session_data.get('last_activity'),
is_ios=current_session_data.get('is_ios') or False,
status=current_session_data.get('status', UserStatus.OFFLINE),
lock_timestamp=current_session_data.get('lock_timestamp') or datetime.now(timezone.utc)
),
other_sessions=[]
)
for session_id in session_ids:
if session_id != current_session_id:
session_data = redis_session_manager.get_session(session_id)
if session_data:
session_info = SessionSchema(
session_id=session_id,
app_version=session_data.get('app_version'),
device_name=session_data.get('device_name'),
location=session_data.get('location'),
ip_address=session_data.get('ip_address'), # Added IP address
last_activity=session_data.get('last_activity'),
is_ios=session_data.get('is_ios') or False,
status=session_data.get('status', UserStatus.OFFLINE),
lock_timestamp=session_data.get('lock_timestamp') or datetime.now(timezone.utc)
)
response.other_sessions.append(session_info)
return response
@router.get("/session/{session_id}", response_model=SessionSchema)
async def get_session_detail(
session_id: str,
env: Annotated[Environment, Depends(odoo_env)],
partner: Annotated[Partner, Depends(get_partner_with_session)]
):
user_id = partner.id
user_sessions = redis_session_manager.redis.smembers(f"user_sessions:{user_id}")
if session_id not in user_sessions:
raise HTTPException(status_code=404, detail=_("Session not found"))
session_data = redis_session_manager.get_session(session_id)
if not session_data:
raise HTTPException(status_code=404, detail=_("Session data not found"))
session_detail = SessionSchema(
session_id=session_id,
app_version=session_data.get('app_version'),
device_name=session_data.get('device_name'),
location=session_data.get('location'),
ip_address=session_data.get('ip_address'), # Added IP address
last_activity=session_data.get('last_activity'),
is_ios=session_data.get('is_ios') or False,
status=session_data.get('status', UserStatus.OFFLINE),
lock_timestamp=session_data.get('lock_timestamp') if 'lock_timestamp' in session_data else None,
)
return session_detail