|
|
@@ -8,6 +8,13 @@ from smart_selects.db_fields import (
|
|
8
|
8
|
ChainedManyToManyField,
|
|
9
|
9
|
GroupedForeignKey,
|
|
10
|
10
|
)
|
|
|
11
|
+import googlemaps
|
|
|
12
|
+from django.contrib.gis.geos import fromstr
|
|
|
13
|
+
|
|
|
14
|
+from django.conf import settings
|
|
|
15
|
+import csv
|
|
|
16
|
+
|
|
|
17
|
+gmaps = googlemaps.Client(key=settings.GOOGLE_MAPS_API_KEY)
|
|
11
|
18
|
# Create your models here.
|
|
12
|
19
|
GENDER_CHOICES = (
|
|
13
|
20
|
('นางสาว','นางสาว'),
|
|
|
@@ -122,9 +129,66 @@ class Patient(models.Model):
|
|
122
|
129
|
comment = models.TextField(blank=True, null=True)
|
|
123
|
130
|
#test
|
|
124
|
131
|
def __str__(self):
|
|
|
132
|
+ #self.nearby()
|
|
125
|
133
|
return f"{self.first_name} {self.last_name}"
|
|
126
|
134
|
|
|
|
135
|
+
|
|
|
136
|
+ def nearby(self):
|
|
|
137
|
+ r = gmaps.places_nearby(location=(self.geolocation.lat, self.geolocation.lon), type="hospital", radius=10000)
|
|
|
138
|
+ bd = ""
|
|
|
139
|
+ for r0 in r['results']:
|
|
|
140
|
+ openh = "-"
|
|
|
141
|
+ if 'opening_hours' in r0:
|
|
|
142
|
+ openh = r0['opening_hours']['open_now']
|
|
|
143
|
+ else:
|
|
|
144
|
+ openh = "-"
|
|
|
145
|
+
|
|
|
146
|
+ bd += f"<tr><td>{r0['name']}</td><td>{openh}</td><td>{r0['vicinity']}</td></tr>"
|
|
|
147
|
+
|
|
|
148
|
+ rt = f'''
|
|
|
149
|
+<br>
|
|
|
150
|
+ <table><thead><tr><th>Name</th><th>Opening Hours</th><th>Vicinity</th></tr></thead>
|
|
|
151
|
+ <tbody>
|
|
|
152
|
+ {bd}
|
|
|
153
|
+ </tbody>
|
|
|
154
|
+ </table>
|
|
|
155
|
+ '''
|
|
|
156
|
+ return rt
|
|
|
157
|
+
|
|
|
158
|
+
|
|
|
159
|
+
|
|
|
160
|
+class ImportFile(models.Model):
|
|
|
161
|
+ hospital_file = models.FileField(upload_to="uploads/%Y/%m/%d/", blank=True, verbose_name="Hospital (csv)")
|
|
|
162
|
+
|
|
|
163
|
+ def save(self, *args, **kwargs):
|
|
|
164
|
+ super(ImportFile, self).save(*args, **kwargs)
|
|
|
165
|
+
|
|
|
166
|
+ with self.hospital_file.open('r') as csv_file:
|
|
|
167
|
+ csv_reader = csv.reader(csv_file, delimiter=',')
|
|
|
168
|
+ Hospital.objects.all().delete()
|
|
|
169
|
+ line_count = 0
|
|
|
170
|
+
|
|
|
171
|
+ for r in csv_reader:
|
|
|
172
|
+ if line_count > 0:
|
|
|
173
|
+ print(r)
|
|
|
174
|
+ print(f"{r[7]},{r[6]}")
|
|
|
175
|
+ try:
|
|
|
176
|
+ gp = map_fields.GeoPt(lat=float(r[6]), lon=float(r[7]))
|
|
|
177
|
+
|
|
|
178
|
+ location = fromstr(f'POINT({r[7]} {r[6]})', srid=4326)
|
|
|
179
|
+ print(location)
|
|
|
180
|
+ h = Hospital(title=r[3], address_text=r[5], geolocation=gp, address=r[3])
|
|
|
181
|
+ h.save()
|
|
|
182
|
+ except Exception as e:
|
|
|
183
|
+ print(e)
|
|
|
184
|
+
|
|
|
185
|
+ line_count += 1
|
|
|
186
|
+
|
|
|
187
|
+
|
|
|
188
|
+
|
|
127
|
189
|
class Place(models.Model):
|
|
|
190
|
+ #title = models.Char
|
|
|
191
|
+ title = models.CharField(max_length=200, blank=True, null=True)
|
|
128
|
192
|
address = map_fields.AddressField(max_length=200)
|
|
129
|
193
|
geolocation = map_fields.GeoLocationField(max_length=100)
|
|
130
|
194
|
|
|
|
@@ -133,6 +197,36 @@ class Place(models.Model):
|
|
133
|
197
|
|
|
134
|
198
|
more_info = models.JSONField(null=True, blank=True)
|
|
135
|
199
|
|
|
|
200
|
+ def __str__(self):
|
|
|
201
|
+ return f"{self.address} ({self.geolocation})"
|
|
|
202
|
+class Points(models.Model):
|
|
|
203
|
+ #src = models.ForeignKey(Place, on_delete=models.SET_NULL, null=True, blank=False, related_name='src')
|
|
|
204
|
+ dest = models.ForeignKey(Place, on_delete=models.SET_NULL, null=True, blank=False, related_name='dest')
|
|
|
205
|
+ address = map_fields.AddressField(max_length=200, null=True)
|
|
|
206
|
+ geolocation = map_fields.GeoLocationField(max_length=100, null=True)
|
|
|
207
|
+
|
|
|
208
|
+ distance = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7, verbose_name="Distance (km)")
|
|
|
209
|
+ duration = models.CharField(max_length=200, null=True, blank=True)
|
|
|
210
|
+
|
|
|
211
|
+ directions = models.JSONField(null=True, blank=True)
|
|
|
212
|
+
|
|
|
213
|
+ def save(self, *args, **kwargs):
|
|
|
214
|
+ geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
|
|
|
215
|
+ print(geocode_result)
|
|
|
216
|
+ print(self.geolocation)
|
|
|
217
|
+ origin = [(self.geolocation.lat, self.geolocation.lon)]
|
|
|
218
|
+ dest = [(self.dest.geolocation.lat, self.dest.geolocation.lon)]
|
|
|
219
|
+ dst = gmaps.distance_matrix(origin, dest)
|
|
|
220
|
+ dirs = gmaps.directions(origin[0], dest[0])
|
|
|
221
|
+ self.directions = dirs
|
|
|
222
|
+ print(dirs)
|
|
|
223
|
+ self.distance = dst['rows'][0]['elements'][0]['distance']['value'] / 1000
|
|
|
224
|
+ self.duration = dst['rows'][0]['elements'][0]['duration']['text']
|
|
|
225
|
+ print(dst)
|
|
|
226
|
+ super(Points, self).save(*args, **kwargs)
|
|
|
227
|
+
|
|
|
228
|
+
|
|
|
229
|
+
|
|
136
|
230
|
class Hospital(models.Model):
|
|
137
|
231
|
title = models.CharField(max_length=200)
|
|
138
|
232
|
location = models.PointField(blank=True, null=True)
|
|
|
@@ -190,3 +284,5 @@ class PatientLog(models.Model):
|
|
190
|
284
|
updated_at = models.DateTimeField(auto_now=True)
|
|
191
|
285
|
|
|
192
|
286
|
|
|
|
287
|
+
|
|
|
288
|
+
|