models.py
12.1 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
##from __future__ import unicode_literals
# (EP 21/9/22) To allow autoreferencing (ex: AgentCmd.create() returns a AgentCmd)
from __future__ import annotations
# Stdlib imports
from src.device_controller.abstract_component.device_controller import DeviceCmd
from datetime import datetime, timedelta, date
from dateutil.relativedelta import relativedelta
import os
import sys
from typing import Any, List, Tuple, Optional
import re
# Django imports
from django.core.validators import MaxValueValidator, MinValueValidator
# DJANGO imports
from django.contrib.auth.models import AbstractUser, UserManager
from django.db import models
from django.db.models import Q, Max
from django.core.validators import MaxValueValidator, MinValueValidator
#from django.db.models.deletion import DO_NOTHING
from django.db.models.expressions import F
from django.db.models.query import QuerySet
from model_utils import Choices
from django.utils import timezone
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
from django.db.models.signals import post_save
from django.dispatch import receiver
# Project imports
from user_mgmt.models import PyrosUser
# DeviceCommand is used by class Command
sys.path.append("../../..")
"""
STYLE RULES
===========
https://simpleisbetterthancomplex.com/tips/2018/02/10/django-tip-22-designing-better-models.html
https://steelkiwi.com/blog/best-practices-working-django-models-python/
- Model name => singular
Call it Company instead of Companies.
A model definition is the representation of a single object (the object in this example is a company),
and not a collection of companies
The model definition is a class, so always use CapWords convention (no underscores)
E.g. User, Permission, ContentType, etc.
- For the model’s attributes use snake_case.
E.g. first_name, last_name, etc
- Blank and Null Fields (https://simpleisbetterthancomplex.com/tips/2016/07/25/django-tip-8-blank-or-null.html)
- Null: It is database-related. Defines if a given database column will accept null values or not.
- Blank: It is validation-related. It will be used during forms validation, when calling form.is_valid().
Do not use null=True for text-based fields that are optional.
Otherwise, you will end up having two possible values for “no data”, that is: None and an empty string.
Having two possible values for “no data” is redundant.
The Django convention is to use the empty string, not NULL.
Example:
# The default values of `null` and `blank` are `False`.
class Person(models.Model):
name = models.CharField(max_length=255) # Mandatory
bio = models.TextField(max_length=500, blank=True) # Optional (don't put null=True)
birth_date = models.DateField(null=True, blank=True) # Optional (here you may add null=True)
The default values of null and blank are False.
Special case, when you need to accept NULL values for a BooleanField, use NullBooleanField instead.
- Choices : you can use Choices from the model_utils library. Take model Article, for instance:
from model_utils import Choices
class Article(models.Model):
STATUSES = Choices(
(0, 'draft', _('draft')),
(1, 'published', _('published')) )
status = models.IntegerField(choices=STATUSES, default=STATUSES.draft)
- Reverse Relationships
- related_name :
Rule of thumb: if you are not sure what would be the related_name,
use the plural of the model holding the ForeignKey.
ex:
class Company:
name = models.CharField(max_length=30)
class Employee:
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='employees')
usage:
google = Company.objects.get(name='Google')
google.employees.all()
You can also use the reverse relationship to modify the company field on the Employee instances:
vitor = Employee.objects.get(first_name='Vitor')
google = Company.objects.get(name='Google')
google.employees.add(vitor)
- related_query_name :
This kind of relationship also applies to query filters.
For example, if I wanted to list all companies that employs people named ‘Vitor’, I could do the following:
companies = Company.objects.filter(employee__first_name='Vitor')
If you want to customize the name of this relationship, here is how we do it:
class Employee:
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
company = models.ForeignKey(
Company,
on_delete=models.CASCADE,
related_name='employees',
related_query_name='person'
)
Then the usage would be:
companies = Company.objects.filter(person__first_name='Vitor')
To use it consistently, related_name goes as plural and related_query_name goes as singular.
GENERAL EXAMPLE
=======
from django.db import models
from django.urls import reverse
class Company(models.Model):
# CHOICES
PUBLIC_LIMITED_COMPANY = 'PLC'
PRIVATE_COMPANY_LIMITED = 'LTD'
LIMITED_LIABILITY_PARTNERSHIP = 'LLP'
COMPANY_TYPE_CHOICES = (
(PUBLIC_LIMITED_COMPANY, 'Public limited company'),
(PRIVATE_COMPANY_LIMITED, 'Private company limited by shares'),
(LIMITED_LIABILITY_PARTNERSHIP, 'Limited liability partnership'),
)
# DATABASE FIELDS
name = models.CharField('name', max_length=30)
vat_identification_number = models.CharField('VAT', max_length=20)
company_type = models.CharField('type', max_length=3, choices=COMPANY_TYPE_CHOICES)
# MANAGERS
objects = models.Manager()
limited_companies = LimitedCompanyManager()
# META CLASS
class Meta:
verbose_name = 'company'
verbose_name_plural = 'companies'
# TO STRING METHOD
def __str__(self):
return self.name
# SAVE METHOD
def save(self, *args, **kwargs):
do_something()
super().save(*args, **kwargs) # Call the "real" save() method.
do_something_else()
# ABSOLUTE URL METHOD
def get_absolute_url(self):
return reverse('company_details', kwargs={'pk': self.id})
# OTHER METHODS
def process_invoices(self):
do_something()
"""
# ---
# --- Utility functions
# ---
def printd(*args, **kwargs):
if os.environ.get('PYROS_DEBUG', '0') == '1':
print('(MODEL)', *args, **kwargs)
'''
def get_or_create_unique_row_from_model(model: models.Model):
# return model.objects.get(id=1) if model.objects.exists() else model.objects.create(id=1)
return model.objects.first() if model.objects.exists() else model.objects.create(id=1)
'''
"""
------------------------
BASE MODEL CLASSES
------------------------
"""
"""
------------------------
OTHER MODEL CLASSES
------------------------
"""
# TODO: OLD Config : à virer (mais utilisé dans dashboard/templatetags/tags.py)
class Config(models.Model):
PYROS_STATE = ["Starting", "Passive", "Standby",
"Remote", "Startup", "Scheduler", "Closing"]
id = models.IntegerField(default='1', primary_key=True)
#latitude = models.FloatField(default=1)
latitude = models.DecimalField(
max_digits=4, decimal_places=2,
default=1,
validators=[
MaxValueValidator(90),
MinValueValidator(-90)
]
)
local_time_zone = models.FloatField(default=1)
#longitude = models.FloatField(default=1)
longitude = models.DecimalField(
max_digits=5, decimal_places=2,
default=1,
validators=[
MaxValueValidator(360),
MinValueValidator(-360)
]
)
altitude = models.FloatField(default=1)
horizon_line = models.FloatField(default=1)
row_data_save_frequency = models.IntegerField(default='300')
request_frequency = models.IntegerField(default='300')
analysed_data_save = models.IntegerField(default='300')
telescope_ip_address = models.CharField(max_length=45, default="127.0.0.1")
camera_ip_address = models.CharField(max_length=45, default="127.0.0.1")
plc_ip_address = models.CharField(max_length=45, default="127.0.0.1")
# TODO: changer ça, c'est pas clair du tout...
# True = mode Scheduler-standby, False = mode Remote !!!!
global_mode = models.BooleanField(default='True')
ack = models.BooleanField(default='False')
bypass = models.BooleanField(default='True')
lock = models.BooleanField(default='False')
pyros_state = models.CharField(max_length=25, default=PYROS_STATE[0])
force_passive_mode = models.BooleanField(default='False')
plc_timeout_seconds = models.PositiveIntegerField(default=60)
majordome_state = models.CharField(max_length=25, default="")
ntc = models.BooleanField(default='False')
majordome_restarted = models.BooleanField(default='False')
class Meta:
managed = True
db_table = 'config'
verbose_name_plural = "Config"
def __str__(self):
return (str(self.__dict__))
class Log(models.Model):
agent = models.CharField(max_length=45, blank=True, null=True)
created = models.DateTimeField(blank=True, null=True, auto_now_add=True)
message = models.TextField(blank=True, null=True)
class Meta:
managed = True
db_table = 'log'
def __str__(self):
return (str(self.agent))
# TODO: à virer car utilisé pour Celery (ou bien à utiliser pour les agents)
'''
class TaskId(models.Model):
task = models.CharField(max_length=45, blank=True, null=True)
created = models.DateTimeField(blank=True, null=True, auto_now_add=True)
task_id = models.CharField(max_length=45, blank=True, null=True)
class Meta:
managed = True
db_table = 'task_id'
def __str__(self):
return (str(self.task) + " - " + str(self.task_id))
'''
class Version(models.Model):
module_name = models.CharField(max_length=45, blank=True, null=True)
version = models.CharField(max_length=15, blank=True, null=True)
created = models.DateTimeField(blank=True, null=True, auto_now_add=True)
updated = models.DateTimeField(blank=True, null=True, auto_now=True)
class Meta:
managed = True
db_table = 'version'
def __str__(self):
return (str(self.module_name) + " - " + str(self.version))
class Tickets(models.Model):
created = models.DateTimeField(blank=True, null=True, auto_now_add=True)
updated = models.DateTimeField(blank=True, null=True, auto_now=True)
end = models.DateTimeField(blank=True, null=True)
title = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
resolution = models.TextField(blank=True, null=True)
pyros_user = models.ForeignKey(PyrosUser, on_delete=models.DO_NOTHING, related_name="tickets", blank=True, null=True)
last_modified_by = models.ForeignKey(PyrosUser, on_delete=models.DO_NOTHING, related_name="tickets_modified_by", blank=True, null=True)
LEVEL_ONE = "ONE"
LEVEL_TWO = "TWO"
LEVEL_THREE = "THREE"
LEVEL_FOUR = "FOUR"
LEVEL_FIVE = "FIVE"
SECURITY_LEVEL_CHOICES = (
(LEVEL_ONE,"Warning non compromising for the operation of the system"),
(LEVEL_TWO,"Known issue which can be solved by operating the software remotely"),
(LEVEL_THREE,"Known issue which can be solved by an human remotely"),
(LEVEL_FOUR,"Known issue without immediate solution"),
(LEVEL_FIVE,"Issue not categorized until it happened")
)
security_level = models.TextField(choices=SECURITY_LEVEL_CHOICES, default=LEVEL_ONE)
class Meta:
managed = True
db_table = 'tickets'
verbose_name_plural = "tickets"