Нема описа

models.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #from django.db import models
  2. from django.contrib.gis.db import models
  3. from django_google_maps import fields as map_fields
  4. from colorfield.fields import ColorField
  5. #from smart_selects.db_fields import GroupedForeignKey
  6. from smart_selects.db_fields import (
  7. ChainedForeignKey,
  8. ChainedManyToManyField,
  9. GroupedForeignKey,
  10. )
  11. # Create your models here.
  12. GENDER_CHOICES = (
  13. ('นางสาว','นางสาว'),
  14. ('นาย','นาย'),
  15. ('นาง', 'นาง'),
  16. ("ด.ช.","เด็กชาย"),
  17. ("ด.ญ.","เด็กหญิง"),
  18. )
  19. class Ambulance(models.Model):
  20. code = models.CharField(max_length=100)
  21. license_plate = models.CharField(max_length=100)
  22. brand = models.CharField(max_length=100)
  23. model_name = models.CharField(max_length=100)
  24. comment = models.TextField(blank=True, null=True)
  25. color = ColorField(default='#FF0000')
  26. status = models.CharField(
  27. max_length=30,
  28. choices=(("working", "Working"), ("free", "Free"), ("ma", "MA")),
  29. null=True,
  30. )
  31. created_at = models.DateTimeField(auto_now_add=True, null=True)
  32. updated_at = models.DateTimeField(auto_now=True)
  33. def __str__(self):
  34. return f"{self.license_plate} ({self.get_status_display()}) {self.code} {self.brand} / {self.model_name}"
  35. class Driver(models.Model):
  36. first_name = models.CharField(max_length=100)
  37. last_name = models.CharField(max_length=100)
  38. age = models.IntegerField()
  39. idcard = models.CharField(max_length=20, null=True, blank=False)
  40. prefix = models.CharField(
  41. max_length=30,
  42. choices=GENDER_CHOICES,
  43. null=True,
  44. )
  45. sex = models.CharField(
  46. max_length=30,
  47. choices=(("male", "Male"), ("female", "Female"),),
  48. null=True,
  49. )
  50. photo = models.FileField(upload_to="uploads/%Y/%m/%d/", blank=True, verbose_name="Photo")
  51. address = models.TextField(blank=True, null=True)
  52. #test
  53. status = models.CharField(
  54. max_length=30,
  55. choices=(("working", "Working"), ("free", "Free"), ("block", "Block")),
  56. null=True,
  57. )
  58. created_at = models.DateTimeField(auto_now_add=True, null=True)
  59. updated_at = models.DateTimeField(auto_now=True)
  60. def __str__(self):
  61. return f"{self.first_name} {self.last_name} ({self.get_status_display()})"
  62. class AmbulanceTicket(models.Model):
  63. driver = models.ForeignKey(Driver, on_delete=models.SET_NULL, null=True)
  64. ambulance = models.ForeignKey(Ambulance, on_delete=models.SET_NULL, null=True)
  65. status = models.CharField(
  66. max_length=30,
  67. choices=(("working", "Working"), ("free", "Free"), ("ma", "MA")),
  68. null=True,
  69. )
  70. checkin_at = models.DateTimeField(null=True, blank=True)
  71. checkout_at = models.DateTimeField(null=True, blank=True)
  72. created_at = models.DateTimeField(auto_now_add=True, null=True)
  73. updated_at = models.DateTimeField(auto_now=True)
  74. def save(self, *args, **kwargs):
  75. super(AmbulanceTicket, self).save(*args, **kwargs)
  76. self.ambulance.status = self.status
  77. self.ambulance.save()
  78. self.driver.status = "working"
  79. self.driver.save()
  80. def __str__(self):
  81. return f"{self.driver}@{self.ambulance}"
  82. class Patient(models.Model):
  83. first_name = models.CharField(max_length=100)
  84. last_name = models.CharField(max_length=100)
  85. age = models.IntegerField()
  86. idcard = models.CharField(max_length=20, null=True, blank=False)
  87. prefix = models.CharField(
  88. max_length=30,
  89. choices=GENDER_CHOICES,
  90. null=True,
  91. )
  92. sex = models.CharField(
  93. max_length=30,
  94. choices=(("male", "Male"), ("female", "Female"),),
  95. null=True,
  96. )
  97. photo = models.FileField(upload_to="uploads/%Y/%m/%d/", blank=True, verbose_name="Photo")
  98. address = map_fields.AddressField(max_length=200, null=True)
  99. geolocation = map_fields.GeoLocationField(max_length=100, null=True)
  100. condition_level = models.CharField(
  101. max_length=30,
  102. choices=(("green", "Green"), ("yellow", "Yellow"), ("red", "Red")),
  103. null=True,
  104. )
  105. comment = models.TextField(blank=True, null=True)
  106. #test
  107. def __str__(self):
  108. return f"{self.first_name} {self.last_name}"
  109. class Place(models.Model):
  110. address = map_fields.AddressField(max_length=200)
  111. geolocation = map_fields.GeoLocationField(max_length=100)
  112. created_at = models.DateTimeField(auto_now_add=True, null=True)
  113. updated_at = models.DateTimeField(auto_now=True)
  114. more_info = models.JSONField(null=True, blank=True)
  115. class Hospital(models.Model):
  116. title = models.CharField(max_length=200)
  117. location = models.PointField(blank=True, null=True)
  118. address_text = models.TextField(blank=True, null=True)
  119. #address = models.CharField(max_length=100)
  120. address = map_fields.AddressField(max_length=200)
  121. geolocation = map_fields.GeoLocationField(max_length=100)
  122. created_at = models.DateTimeField(auto_now_add=True, null=True)
  123. updated_at = models.DateTimeField(auto_now=True)
  124. def __str__(self):
  125. return f"{self.title} {self.address_text}"
  126. class Bed(models.Model):
  127. code = models.CharField(max_length=30)
  128. occupy = models.BooleanField(default=False)
  129. patient = models.ForeignKey(Patient, on_delete=models.SET_NULL, null=True, blank=True)
  130. hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE)
  131. created_at = models.DateTimeField(auto_now_add=True, null=True)
  132. updated_at = models.DateTimeField(auto_now=True)
  133. def __str__(self):
  134. return self.code
  135. class PatientLog(models.Model):
  136. patient = models.ForeignKey(Patient, on_delete=models.SET_NULL, null=True)
  137. hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE, null=True)
  138. bed = ChainedForeignKey(
  139. "Bed",
  140. chained_field="hospital",
  141. chained_model_field="hospital",
  142. show_all=False,
  143. auto_choose=True,
  144. null=True
  145. )
  146. notes = models.TextField(blank=True, null=True)
  147. condition_level = models.CharField(
  148. max_length=30,
  149. choices=(("green", "Green"), ("yellow", "Yellow"), ("red", "Red")),
  150. null=True,
  151. )
  152. status = models.CharField(
  153. max_length=30,
  154. choices=(("active", "Active"), ("inactive", "Inactive"), ("transfer", "Transfer")),
  155. null=True,
  156. )
  157. checkin_at = models.DateTimeField(null=True, blank=True)
  158. checkout_at = models.DateTimeField(null=True, blank=True)
  159. created_at = models.DateTimeField(auto_now_add=True, null=True)
  160. updated_at = models.DateTimeField(auto_now=True)