| import logging |
| from datetime import datetime |
| from bson import ObjectId |
| from django.conf import settings |
| from django.contrib.auth.hashers import check_password, make_password |
| from expense_tracker.utils import MongoDBClient |
|
|
| class MongoUser: |
| """ |
| A custom User class that mimics Django's User model but stores data in MongoDB. |
| """ |
| def __init__(self, data): |
| self._id = data.get('_id') |
| self.pk = str(self._id) if self._id else None |
| self.id = self.pk |
| |
| self.username = data.get('username') |
| self.email = data.get('email') |
| self.password = data.get('password') |
| self.is_active = data.get('is_active', True) |
| self.is_staff = data.get('is_staff', False) |
| self.is_superuser = data.get('is_superuser', False) |
| self.last_login = data.get('last_login') |
| self.date_joined = data.get('date_joined') |
|
|
| @property |
| def is_authenticated(self): |
| return True |
|
|
| def save(self): |
| |
| pass |
|
|
| def __str__(self): |
| return self.username |
|
|
| def __eq__(self, other): |
| return isinstance(other, MongoUser) and self.pk == other.pk |
|
|
| class MongoBackend: |
| """ |
| Authentication Backend to authenticate against MongoDB users collection. |
| """ |
| def authenticate(self, request, username=None, password=None, **kwargs): |
| db = MongoDBClient.get_client() |
| if username is None: |
| username = kwargs.get('email') |
| |
| |
| user_data = db.users.find_one({'$or': [{'username': username}, {'email': username}]}) |
| |
| if user_data: |
| if check_password(password, user_data.get('password')): |
| |
| db.users.update_one({'_id': user_data['_id']}, {'$set': {'last_login': datetime.now()}}) |
| return MongoUser(user_data) |
| return None |
|
|
| def get_user(self, user_id): |
| try: |
| db = MongoDBClient.get_client() |
| user_data = db.users.find_one({'_id': ObjectId(user_id)}) |
| if user_data: |
| return MongoUser(user_data) |
| except: |
| pass |
| return None |
|
|