|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+from django.db import models
|
|
|
2
|
+
|
|
|
3
|
+from django_google_maps import fields as map_fields
|
|
|
4
|
+from colorfield.fields import ColorField
|
|
|
5
|
+from smart_selects.db_fields import (
|
|
|
6
|
+ ChainedForeignKey,
|
|
|
7
|
+ ChainedManyToManyField,
|
|
|
8
|
+ GroupedForeignKey,
|
|
|
9
|
+)
|
|
|
10
|
+from django.db.models import Q
|
|
|
11
|
+import googlemaps
|
|
|
12
|
+from django.contrib.gis.geos import fromstr
|
|
|
13
|
+
|
|
|
14
|
+from django.conf import settings
|
|
|
15
|
+import csv
|
|
|
16
|
+import haversine as hs
|
|
|
17
|
+from mptt.models import MPTTModel, TreeForeignKey
|
|
|
18
|
+
|
|
|
19
|
+from django.contrib.auth.models import User
|
|
|
20
|
+from django.db.models.signals import post_save
|
|
|
21
|
+from django.dispatch import receiver
|
|
|
22
|
+from shaqfindbed.utils import get_current_user
|
|
|
23
|
+
|
|
|
24
|
+
|
|
|
25
|
+# Create your models here.
|
|
|
26
|
+
|
|
|
27
|
+class GenericModel(models.Model):
|
|
|
28
|
+ created_at = models.DateTimeField(auto_now_add=True, null=True)
|
|
|
29
|
+ updated_at = models.DateTimeField(auto_now=True)
|
|
|
30
|
+
|
|
|
31
|
+ created_by = models.ForeignKey(User, null=True, editable=False, related_name='%(class)s_created', on_delete=models.SET_NULL)
|
|
|
32
|
+ modified_by = models.ForeignKey(User, null=True, editable=False, related_name='%(class)s_modified', on_delete=models.SET_NULL)
|
|
|
33
|
+
|
|
|
34
|
+ '''
|
|
|
35
|
+ def save(self, *args, **kwargs):
|
|
|
36
|
+ user = get_current_user()
|
|
|
37
|
+ print("user "+user.is_authenticated())
|
|
|
38
|
+ if user and user.is_authenticated():
|
|
|
39
|
+ self.modified_by = user
|
|
|
40
|
+ if not self.id:
|
|
|
41
|
+ self.created_by = user
|
|
|
42
|
+ super(GenericModel, self).save(*args, **kwargs)
|
|
|
43
|
+ '''
|
|
|
44
|
+
|
|
|
45
|
+ class Meta:
|
|
|
46
|
+ abstract = True
|
|
|
47
|
+
|
|
|
48
|
+class Store(models.Model):
|
|
|
49
|
+ name = models.CharField(max_length=200)
|
|
|
50
|
+ address_text = models.TextField(blank=True, null=True)
|
|
|
51
|
+ #address = models.CharField(max_length=100)
|
|
|
52
|
+ address = map_fields.AddressField(max_length=200)
|
|
|
53
|
+ geolocation = map_fields.GeoLocationField(max_length=100)
|
|
|
54
|
+
|
|
|
55
|
+
|
|
|
56
|
+ tel = models.CharField(max_length=100, null=True, blank=True)
|
|
|
57
|
+ line_id = models.CharField(max_length=100, null=True, blank=True)
|
|
|
58
|
+ email = models.EmailField(null=True, blank=True)
|
|
|
59
|
+
|
|
|
60
|
+
|
|
|
61
|
+ created_at = models.DateTimeField(auto_now_add=True, null=True)
|
|
|
62
|
+ updated_at = models.DateTimeField(auto_now=True)
|
|
|
63
|
+
|
|
|
64
|
+ def __str__(self):
|
|
|
65
|
+ return f"{self.name}"
|
|
|
66
|
+
|
|
|
67
|
+
|
|
|
68
|
+
|
|
|
69
|
+class ProductType(MPTTModel):
|
|
|
70
|
+ name = models.CharField(max_length=50, unique=True)
|
|
|
71
|
+ parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
|
|
|
72
|
+
|
|
|
73
|
+ class MPTTMeta:
|
|
|
74
|
+ order_insertion_by = ['name']
|
|
|
75
|
+
|
|
|
76
|
+
|
|
|
77
|
+ def __str__(self):
|
|
|
78
|
+ return f"{self.name}"
|
|
|
79
|
+
|
|
|
80
|
+
|
|
|
81
|
+class Product(GenericModel, models.Model ):
|
|
|
82
|
+ name = models.CharField(max_length=200)
|
|
|
83
|
+ code = models.CharField(max_length=200)
|
|
|
84
|
+ product_type = models.ForeignKey('ProductType', on_delete=models.SET_NULL, null=True)
|
|
|
85
|
+ description = models.TextField(blank=True, null=True)
|
|
|
86
|
+ store = models.ForeignKey('Store', on_delete=models.CASCADE, null=True, blank=False)
|
|
|
87
|
+ price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
|
|
|
88
|
+
|
|
|
89
|
+ details = models.JSONField(null=True, blank=True)
|
|
|
90
|
+
|
|
|
91
|
+ n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
|
|
|
92
|
+ unit_name = models.CharField(max_length=200, null=True)
|
|
|
93
|
+
|
|
|
94
|
+ def __str__(self):
|
|
|
95
|
+ return f"{self.name} {self.code}"
|
|
|
96
|
+
|
|
|
97
|
+class ProductSKU(GenericModel, models.Model ):
|
|
|
98
|
+ sku = models.CharField(max_length=200)
|
|
|
99
|
+ product = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)
|
|
|
100
|
+ description = models.TextField(blank=True, null=True)
|
|
|
101
|
+ price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
|
|
|
102
|
+
|
|
|
103
|
+ details = models.JSONField(null=True, blank=True)
|
|
|
104
|
+
|
|
|
105
|
+ n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
|
|
|
106
|
+ unit_name = models.CharField(max_length=200, null=True)
|
|
|
107
|
+
|
|
|
108
|
+class Photo(GenericModel, models.Model):
|
|
|
109
|
+ name = models.CharField(max_length=200, unique=True)
|
|
|
110
|
+ photo = models.ImageField(upload_to="uploads/%Y/%m/%d/", blank=True, verbose_name="Photo")
|
|
|
111
|
+ product = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)
|
|
|
112
|
+
|
|
|
113
|
+
|
|
|
114
|
+
|
|
|
115
|
+
|
|
|
116
|
+
|
|
|
117
|
+class Profile(models.Model):
|
|
|
118
|
+ user = models.OneToOneField(User, on_delete=models.CASCADE)
|
|
|
119
|
+ bio = models.TextField(max_length=500, blank=True)
|
|
|
120
|
+ location = models.CharField(max_length=30, blank=True)
|
|
|
121
|
+ birth_date = models.DateField(null=True, blank=True)
|
|
|
122
|
+
|
|
|
123
|
+@receiver(post_save, sender=User)
|
|
|
124
|
+def create_user_profile(sender, instance, created, **kwargs):
|
|
|
125
|
+ if created:
|
|
|
126
|
+ Profile.objects.create(user=instance)
|
|
|
127
|
+
|
|
|
128
|
+@receiver(post_save, sender=User)
|
|
|
129
|
+def save_user_profile(sender, instance, **kwargs):
|
|
|
130
|
+ try:
|
|
|
131
|
+ instance.profile.save()
|
|
|
132
|
+ except:
|
|
|
133
|
+ Profile.objects.create(user=instance)
|