diff --git a/backend/geoapp/migrations/0002_feature_owner.py b/backend/geoapp/migrations/0002_feature_owner.py new file mode 100644 index 0000000..647c636 --- /dev/null +++ b/backend/geoapp/migrations/0002_feature_owner.py @@ -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, + ), + ] diff --git a/backend/geoapp/models.py b/backend/geoapp/models.py index 04a4986..c4284bf 100644 --- a/backend/geoapp/models.py +++ b/backend/geoapp/models.py @@ -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') @@ -15,4 +19,4 @@ class Meta: verbose_name_plural = ("features") def __str__(self): - return f'{self.name}, {self.type}' \ No newline at end of file + return f'{self.name}, {self.type}' diff --git a/backend/geoapp/resources.py b/backend/geoapp/resources.py index 1262057..4d2814d 100644 --- a/backend/geoapp/resources.py +++ b/backend/geoapp/resources.py @@ -7,7 +7,9 @@ class FeatureResource(resources.ModelResource): class Meta: model = Feature - exclude = ('pk',) + exclude = ('pk', 'owner') skip_unchanged = True report_skipped = False - \ No newline at end of file + + def after_import_instance(self, instance, new, **kwargs): + instance.owner = kwargs['user'] \ No newline at end of file diff --git a/backend/geoapp/views.py b/backend/geoapp/views.py index 7d10fa8..a04042a 100644 --- a/backend/geoapp/views.py +++ b/backend/geoapp/views.py @@ -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', @@ -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') diff --git a/test.csv b/test.csv index a9f743c..258d886 100644 --- a/test.csv +++ b/test.csv @@ -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