lass="lines-code">
+ 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
|
+
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+{% extends "base.html" %}
|
|
|
2
|
+
|
|
|
3
|
+{% block title %}My amazing blog{% endblock %}
|
|
|
4
|
+
|
|
|
5
|
+{% block content %}
|
|
|
6
|
+Import File
|
|
|
7
|
+{% endblock %}
|
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+{% extends "base.html" %}
|
|
|
2
|
+
|
|
|
3
|
+{% block title %}My amazing blog{% endblock %}
|
|
|
4
|
+
|
|
|
5
|
+{% block content %}
|
|
|
6
|
+Hello world
|
|
|
7
|
+<a href="{% url "import_file" %}">Import</a>
|
|
|
8
|
+{% endblock %}
|
|
|
@@ -4,4 +4,5 @@ from . import views
|
|
4
|
4
|
|
|
5
|
5
|
urlpatterns = [
|
|
6
|
6
|
path('', views.index, name='index'),
|
|
|
7
|
+ path('import_file', views.import_file, name='import_file'),
|
|
7
|
8
|
]
|
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+from django.shortcuts import render
|
|
|
2
|
+
|
|
|
3
|
+# Create your views here.
|
|
|
4
|
+from django.http import HttpResponse
|
|
|
5
|
+
|
|
|
6
|
+
|
|
|
7
|
+def index(request):
|
|
|
8
|
+ return render(request, 'backend/index.html')
|
|
|
9
|
+
|
|
|
10
|
+def import_file(request):
|
|
|
11
|
+ return render(request, 'backend/import_file.html')
|
|
|
@@ -5,3 +5,4 @@ django-smart-selects
|
|
5
|
5
|
django-colorfield
|
|
6
|
6
|
googlemaps
|
|
7
|
7
|
django-json-widget
|
|
|
8
|
+django-import-export
|
|
|
@@ -33,12 +33,13 @@ ALLOWED_HOSTS = []
|
|
33
|
33
|
# Application definition
|
|
34
|
34
|
|
|
35
|
35
|
INSTALLED_APPS = [
|
|
|
36
|
+ 'django.contrib.staticfiles',
|
|
36
|
37
|
'django.contrib.admin',
|
|
37
|
38
|
'django.contrib.auth',
|
|
38
|
39
|
'django.contrib.contenttypes',
|
|
39
|
40
|
'django.contrib.sessions',
|
|
40
|
41
|
'django.contrib.messages',
|
|
41
|
|
- 'django.contrib.staticfiles',
|
|
|
42
|
+ 'import_export',
|
|
42
|
43
|
'django_google_maps',
|
|
43
|
44
|
'django.contrib.gis',
|
|
44
|
45
|
'smart_selects',
|
|
|
@@ -62,7 +63,7 @@ ROOT_URLCONF = 'shaqfindbed.urls'
|
|
62
|
63
|
TEMPLATES = [
|
|
63
|
64
|
{
|
|
64
|
65
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
65
|
|
- 'DIRS': [],
|
|
|
66
|
+ 'DIRS': [os.path.join(BASE_DIR, 'templates')],
|
|
66
|
67
|
'APP_DIRS': True,
|
|
67
|
68
|
'OPTIONS': {
|
|
68
|
69
|
'context_processors': [
|
|
|
@@ -23,7 +23,7 @@ urlpatterns = [
|
|
23
|
23
|
path('backend/', include('backend.urls')),
|
|
24
|
24
|
path('admin/', admin.site.urls),
|
|
25
|
25
|
url(r'^chaining/', include('smart_selects.urls')),
|
|
26
|
|
-]
|
|
|
26
|
+] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
|
27
|
27
|
|
|
28
|
28
|
if settings.DEBUG:
|
|
29
|
29
|
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|