Comprehensive user profile system for the Open Source Site Tracking platform, including profile management, customization, and user preferences.
The user profile system provides comprehensive tools for managing user profiles, customizing user experiences, and tracking user activity. It supports profile customization, preference management, and activity tracking.
# User profile model
class UserProfile:
user_id: str
username: str
email: str
first_name: Optional[str]
last_name: Optional[str]
display_name: Optional[str]
avatar_url: Optional[str]
bio: Optional[str]
location: Optional[str]
website: Optional[str]
timezone: str
language: str
visibility: str
theme_preference: str
created_at: datetime
updated_at: datetime
last_login: Optional[datetime]
is_active: bool
is_verified: bool
preferences: Optional[Dict]
social_links: Optional[Dict]
class ProfileVisibility:
PUBLIC = "public"
PRIVATE = "private"
TEAM_ONLY = "team_only"
ORGANIZATION_ONLY = "organization_only"
class ThemePreference:
LIGHT = "light"
DARK = "dark"
SYSTEM = "system"
// Theme customization implementation
class ThemeCustomizer:
def __init__(self, user_id):
self.user_id = user_id
self.default_themes = {
'light': {
'primary_color': '#0066cc',
'background_color': '#ffffff',
'text_color': '#333333',
'accent_color': '#ff6600'
},
'dark': {
'primary_color': '#00aaff',
'background_color': '#1a1a1a',
'text_color': '#ffffff',
'accent_color': '#ff6600'
},
'system': {
'primary_color': '#0066cc',
'background_color': 'auto',
'text_color': 'auto',
'accent_color': '#ff6600'
}
}
async def get_user_theme(self):
"""Get user's current theme"""
profile = await self.get_user_profile()
theme_preference = profile.theme_preference
if theme_preference == 'system':
return self.detect_system_theme()
return self.default_themes.get(theme_preference, self.default_themes['light'])
async def apply_theme(self, theme_name, custom_colors=None):
"""Apply theme to user profile"""
profile = await self.get_user_profile()
if custom_colors:
theme_data = self.default_themes.get(theme_name, {})
theme_data.update(custom_colors)
profile.theme_preference = 'custom'
profile.custom_theme = theme_data
else:
profile.theme_preference = theme_name
profile.custom_theme = None
await self.save_profile(profile)
return profile
# Notification preferences implementation
class NotificationPreferences:
def __init__(self, user_id):
self.user_id = user_id
self.default_preferences = {
'email_notifications': {
'weekly_reports': True,
'security_alerts': True,
'system_updates': False,
'marketing_emails': False
},
'push_notifications': {
'real_time_alerts': True,
'project_updates': True,
'team_mentions': True,
'system_notifications': False
},
'in_app_notifications': {
'new_messages': True,
'task_assignments': True,
'deadline_reminders': True,
'achievement_badges': True
}
}
async def update_preferences(self, category, preferences):
"""Update user notification preferences"""
profile = await self.get_user_profile()
if not profile.preferences:
profile.preferences = {}
if category not in profile.preferences:
profile.preferences[category] = {}
profile.preferences[category].update(preferences)
await self.save_profile(profile)
# Apply preference changes
await self.apply_preference_changes(category, preferences)
return profile
# User activity tracking
class UserActivityTracker:
def __init__(self, user_id):
self.user_id = user_id
self.activity_types = {
'login': 'User logged in',
'logout': 'User logged out',
'page_view': 'User viewed page',
'project_access': 'User accessed project',
'settings_change': 'User changed settings',
'profile_update': 'User updated profile'
}
async def track_activity(self, activity_type, details=None):
"""Track user activity"""
activity = UserActivity(
user_id=self.user_id,
activity_type=activity_type,
details=details or {},
timestamp=datetime.utcnow(),
ip_address=self.get_client_ip(),
user_agent=self.get_user_agent()
)
await self.save_activity(activity)
# Update user last activity
await self.update_last_activity(activity.timestamp)
# Check for suspicious activity
await self.check_suspicious_activity(activity)
return activity
async def get_activity_summary(self, start_date, end_date):
"""Get user activity summary"""
activities = await self.get_activities_by_date_range(start_date, end_date)
summary = {
'total_activities': len(activities),
'activity_by_type': {},
'daily_activity': {},
'most_active_day': None,
'login_count': 0,
'page_views': 0,
'project_accesses': 0
}
for activity in activities:
# Count by type
if activity.activity_type not in summary['activity_by_type']:
summary['activity_by_type'][activity.activity_type] = 0
summary['activity_by_type'][activity.activity_type] += 1
# Count by day
day_key = activity.timestamp.strftime('%Y-%m-%d')
if day_key not in summary['daily_activity']:
summary['daily_activity'][day_key] = 0
summary['daily_activity'][day_key] += 1
# Count specific activities
if activity.activity_type == 'login':
summary['login_count'] += 1
elif activity.activity_type == 'page_view':
summary['page_views'] += 1
elif activity.activity_type == 'project_access':
summary['project_accesses'] += 1
return summary
# Get user profile
GET /api/v1/users/{user_id}/profile
Response:
{
"user_id": "user-123",
"username": "johndoe",
"email": "john@example.com",
"first_name": "John",
"last_name": "Doe",
"display_name": "John Doe",
"avatar_url": "https://example.com/avatar.jpg",
"bio": "Analytics enthusiast",
"location": "New York",
"website": "https://johndoe.com",
"timezone": "America/New_York",
"language": "en",
"visibility": "public",
"theme_preference": "dark",
"preferences": {
"email_notifications": {
"weekly_reports": true,
"security_alerts": true
}
},
"social_links": {
"twitter": "@johndoe",
"linkedin": "johndoe"
},
"created_at": "2024-01-15T10:30:00Z",
"last_login": "2024-01-20T14:30:00Z"
}
# Update user profile
PUT /api/v1/users/{user_id}/profile
{
"first_name": "John",
"last_name": "Doe",
"bio": "Updated bio",
"location": "San Francisco",
"website": "https://johndoe.com",
"timezone": "America/Los_Angeles",
"visibility": "public"
}
Response:
{
"user_id": "user-123",
"updated_at": "2024-01-20T15:30:00Z",
"changes": [
"first_name",
"last_name",
"bio",
"location",
"timezone"
]
}
# Update user preferences
PUT /api/v1/users/{user_id}/preferences
{
"email_notifications": {
"weekly_reports": false,
"security_alerts": true,
"system_updates": true
},
"theme_preference": "light",
"language": "es"
}
Response:
{
"preferences": {
"email_notifications": {
"weekly_reports": false,
"security_alerts": true,
"system_updates": true
},
"theme_preference": "light",
"language": "es"
},
"updated_at": "2024-01-20T15:30:00Z"
}
# Get user activity
GET /api/v1/users/{user_id}/activity?start_date=2024-01-01&end_date=2024-01-31
Response:
{
"activities": [
{
"id": "activity-123",
"activity_type": "login",
"timestamp": "2024-01-20T14:30:00Z",
"details": {
"ip_address": "192.168.1.1",
"user_agent": "Mozilla/5.0..."
}
}
],
"summary": {
"total_activities": 45,
"login_count": 15,
"page_views": 20,
"project_accesses": 10
}
}