-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
205 lines (165 loc) · 6.39 KB
/
models.py
File metadata and controls
205 lines (165 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models
from django.conf import settings
import bcrypt
from django.utils import timezone
class Role(models.Model):
ROLE_CHOICES = [
("Admin", "Admin"),
("Moderator", "Moderator"),
("User", "User"),
("Guest", "Guest"),
]
name = models.CharField(max_length=50, unique=True, choices=ROLE_CHOICES)
def __str__(self):
return self.name
class UserManager(BaseUserManager):
def create_user(self, email, full_name, password=None, role_name="User"):
if not email:
raise ValueError("Users must have an email address")
# get role
try:
role = Role.objects.get(name=role_name)
except Role.DoesNotExist:
raise ValueError(f"Role '{role_name}' does not exist")
email = self.normalize_email(email)
password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
user = self.model(
email=email,
full_name=full_name,
password_hash=password_hash,
role=role,
is_active=True
)
user.save(using=self._db)
return user
def create_superuser(self, email, full_name, password):
user = self.create_user(email, full_name, password, role_name="Admin")
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
email = models.EmailField(unique=True)
full_name = models.CharField(max_length=255)
password_hash = models.CharField(max_length=128)
role = models.ForeignKey(Role, on_delete=models.CASCADE)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False) # Admins only
is_superuser = models.BooleanField(default=False) # Admins only
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["full_name"]
objects = UserManager()
def __str__(self):
return f"{self.full_name} ({self.email}) - {self.role.name}"
def check_password(self, raw_password):
return bcrypt.checkpw(raw_password.encode(), self.password_hash.encode())
def has_perm(self, perm, obj=None):
return self.is_superuser
def has_module_perms(self, app_label):
return self.is_superuser
class BusinessElement(models.Model):
name = models.CharField(max_length=100, unique=True)
def __str__(self):
return self.name
class AccessRoleRule(models.Model):
role = models.ForeignKey("Role", on_delete=models.CASCADE, related_name="access_rules")
element = models.ForeignKey(BusinessElement, on_delete=models.CASCADE, related_name="access_rules")
read_permission = models.BooleanField(default=False)
read_all_permission = models.BooleanField(default=False)
create_permission = models.BooleanField(default=False)
update_permission = models.BooleanField(default=False)
update_all_permission = models.BooleanField(default=False)
delete_permission = models.BooleanField(default=False)
delete_all_permission = models.BooleanField(default=False)
class Meta:
unique_together = ('role', 'element')
def __str__(self):
return f"{self.role.name} → {self.element.name}"
class Store(models.Model):
"""
Represents a store where products are sold.
"""
name = models.CharField(max_length=255, unique=True)
address = models.TextField(blank=True, null=True)
is_active = models.BooleanField(default=True)
owner = models.ForeignKey( # Store owner (User)
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name='owned_stores',
null=True,
blank=True
)
def __str__(self):
return self.name
class Product(models.Model):
"""
Represents a product belonging to a store.
Includes ownership so we can check object-level permissions.
"""
name = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
store = models.ForeignKey(Store, on_delete=models.CASCADE, related_name='products')
is_active = models.BooleanField(default=True)
owner = models.ForeignKey( # Product creator/owner
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name='owned_products',
null=True,
blank=True
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-created_at']
def __str__(self):
return f"{self.name} ({self.store.name})"
class Order(models.Model):
STATUS_CHOICES = [
("pending", "Pending"),
("paid", "Paid"),
("shipped", "Shipped"),
("completed", "Completed"),
("cancelled", "Cancelled"),
]
product = models.ForeignKey(
"Product",
on_delete=models.CASCADE,
related_name="orders"
)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="orders"
)
quantity = models.PositiveIntegerField(default=1)
total_price = models.DecimalField(max_digits=10, decimal_places=2)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="pending")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="owned_orders",
null=True,
blank=True
)
class Meta:
ordering = ['-created_at']
def save(self, *args, **kwargs):
# Calculate total price automatically if not provided
if not self.total_price:
self.total_price = self.product.price * self.quantity
# Set owner to the user if not explicitly set
if not self.owner:
self.owner = self.user
super().save(*args, **kwargs)
def __str__(self):
return f"Order #{self.id} - {self.product.name} x{self.quantity} by {self.user.full_name}"
class RevokedToken(models.Model):
token = models.CharField(max_length=512, unique=True)
revoked_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"RevokedToken({self.token[:20]}...)"