-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
90 lines (62 loc) · 2.48 KB
/
models.py
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
import uuid
from django.db import models
from user.models import User
SCH_DAYS = [
(0, 'Dilluns'),
(1, 'Dimarts'),
(2, 'Dimecres'),
(3, 'Dijous'),
(4, 'Divendres'),
(5, 'Dissabte'),
(6, 'Diumenge'),
]
# sets the path to the file image
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return '{0}/{1}'.format(instance.id, filename)
class Category(models.Model):
name = models.CharField(max_length=50, primary_key=True)
# returns the name for the object
def __str__(self):
return self.name
# Abstract class, this is not in the data base
class Meta:
abstract = True
class PrimaryCategory(Category):
# gets all the secondaries categories from the primary
def get_secondaries(self):
return self.secondary
class SecondaryCategory(Category):
primary = models.ForeignKey(to=PrimaryCategory, related_name='secondary', on_delete=models.DO_NOTHING)
class Service(Category):
pass
class Shop(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
CIF = models.CharField(max_length=10)
name = models.CharField(max_length=30)
meanTime = models.FloatField(null=True, blank=True)
latitude = models.DecimalField(decimal_places=7, max_digits=11, null=True, blank=True)
longitude = models.DecimalField(decimal_places=7, max_digits=11, null=True, blank=True)
owner = models.ForeignKey(to=User, related_name='my_shop', on_delete=models.CASCADE)
admins = models.ManyToManyField(User, blank=True, related_name='shop')
secondaryCategories = models.ManyToManyField(to=SecondaryCategory)
services = models.ManyToManyField(to=Service, blank=True)
photo = models.ImageField(upload_to=user_directory_path, null=True, blank=True)
description = models.CharField(max_length=256)
# name of the object as name + CIF
def __str__(self):
return '%s__%s' % (self.name, self.CIF)
# "Primary key" of Shop
class Meta:
unique_together = (('CIF', 'name'),)
class Schedule(models.Model):
shop = models.ForeignKey(to=Shop, related_name='schedule', on_delete=models.CASCADE)
day = models.IntegerField(choices=SCH_DAYS)
startHour = models.TimeField()
endHour = models.TimeField()
# "Primary key" of Schedule
class Meta:
unique_together = (("day", "startHour", 'shop'),)
# get name of the day
def get_day_name(self):
return SCH_DAYS[self.day][1]