netcop-ai/users/models.py
thecyberlearn 060c3a9c78 Initial commit: Django web app with user auth, Stripe payments, and n8n workflow integration
Features:
- Email-based user authentication with token auth
- Stripe Checkout integration for wallet top-ups
- n8n workflow triggering with automatic fee deduction ($0.10)
- Comprehensive transaction and usage logging
- Django Admin interface for monitoring
- Rate limiting and security middleware
- Production-ready deployment configuration
- Modular settings (dev/prod environments)
- UUID primary keys for enhanced security
- Comprehensive test coverage

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-30 13:34:19 +05:30

52 lines
1.8 KiB
Python

import uuid
from django.contrib.auth.models import AbstractUser, UserManager as BaseUserManager
from django.db import models
from decimal import Decimal
class UserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError('The Email field must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self.create_user(email, password, **extra_fields)
class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
email = models.EmailField(unique=True)
username = models.CharField(max_length=150, unique=True, null=True, blank=True)
wallet_balance = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0.00'))
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
def has_sufficient_balance(self, amount):
return self.wallet_balance >= Decimal(str(amount))
def deduct_balance(self, amount):
if self.has_sufficient_balance(amount):
self.wallet_balance -= Decimal(str(amount))
self.save()
return True
return False
def add_balance(self, amount):
self.wallet_balance += Decimal(str(amount))
self.save()