Skip to content

Commit

Permalink
Added a new field
Browse files Browse the repository at this point in the history
  • Loading branch information
MoustafaShaaban committed Mar 25, 2023
1 parent 6bf0c29 commit 383024d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
22 changes: 22 additions & 0 deletions backend/geoapp/migrations/0002_feature_owner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.0.10 on 2023-03-25 01:39

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('geoapp', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='feature',
name='owner',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
]
8 changes: 6 additions & 2 deletions backend/geoapp/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from django.db import models

from django.contrib.auth import get_user_model
# Create your models here.


User = get_user_model()

class Feature(models.Model):
name = models.CharField(max_length=250, help_text='Feature Name')
description = models.TextField(help_text='Feature Description')
owner = models.ForeignKey(User, on_delete=models.CASCADE)
type = models.CharField(max_length=100, help_text='Feature Type (Hotel, Hospital, School, Park, ...)')
latitude = models.FloatField(help_text='Latitude')
longitude = models.FloatField(help_text='Longitude')
Expand All @@ -15,4 +19,4 @@ class Meta:
verbose_name_plural = ("features")

def __str__(self):
return f'{self.name}, {self.type}'
return f'{self.name}, {self.type}'
6 changes: 4 additions & 2 deletions backend/geoapp/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class FeatureResource(resources.ModelResource):

class Meta:
model = Feature
exclude = ('pk',)
exclude = ('pk', 'owner')
skip_unchanged = True
report_skipped = False


def after_import_instance(self, instance, new, **kwargs):
instance.owner = kwargs['user']
6 changes: 6 additions & 0 deletions backend/geoapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class CreateFeature(LoginRequiredMixin, generic.CreateView):
template_name = 'geoapp/create_feature.html'
success_url = reverse_lazy('geoapp:index')

def form_valid(self, form):
form.instance.owner = self.request.user
return super().form_valid(form)

def get_context_data(self, **kwargs):
map = folium.Map(
tiles='cartodbdark_matter',
Expand Down Expand Up @@ -64,6 +68,8 @@ def import_data(request):
# Import now
feature_resource.import_data(imported_data, dry_run=False)
messages.success(request, 'Data Imported Successfully.')
else:
messages.warning(request, 'Could not import data')

return render(request, 'geoapp/import_data.html')

Expand Down
8 changes: 4 additions & 4 deletions test.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
,name,description,type,latitude,longitude
,school 1,great school,school,30.123449999999998,31.123449999999998
,hotel 1,great hotel,hotel ,-7.7110000000000003,-47.109400000000001
,hospital 1,great hospital,hospital,43.580399999999997,63.632800000000003
,name,description,,type,latitude,longitude
,school 1,great school,,school,30.123449999999998,31.123449999999998
,hotel 1,great hotel,,hotel ,-7.7110000000000003,-47.109400000000001
,hospital 1,great hospital,,hospital,43.580399999999997,63.632800000000003

0 comments on commit 383024d

Please sign in to comment.