diff --git a/csvimport/admin.py b/csvimport/admin.py index b1b8ef2..daf56c7 100644 --- a/csvimport/admin.py +++ b/csvimport/admin.py @@ -1,3 +1,4 @@ +import locale from datetime import datetime from django import forms from django.db import models @@ -16,6 +17,7 @@ class CSVImportAdmin(ModelAdmin): "field_list", "upload_file", "file_name", + "delimiter", "encoding", "upload_method", "error_log_html", @@ -25,6 +27,11 @@ class CSVImportAdmin(ModelAdmin): models.CharField: {"widget": forms.Textarea(attrs={"rows": "1", "cols": "40"})}, } + def get_changeform_initial_data(self, request): + #This works for my problem, but I don't know if there are side effects. So I won't push it + #locale.setlocale(locale.LC_ALL, '') + return {'delimiter': ";" if locale.localeconv()['decimal_point'] == "," else ","} + def save_model(self, request, obj, form, change): """ Do save and process command - cant commit False since then file wont be found for reopening via right charset @@ -42,6 +49,7 @@ def save_model(self, request, obj, form, change): charset=obj.encoding, uploaded=obj.upload_file, defaults=defaults, + delimiter=obj.delimiter, ) errors = cmd.run(logid=obj.id) if errors: diff --git a/csvimport/migrations/0002_csvimport_model_name_default.py b/csvimport/migrations/0002_csvimport_model_name_default.py new file mode 100644 index 0000000..f8774e3 --- /dev/null +++ b/csvimport/migrations/0002_csvimport_model_name_default.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.5 on 2021-01-25 21:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('csvimport', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='csvimport', + name='model_name', + field=models.CharField(default='app_label.model_name', help_text='Please specify the app_label.model_name', max_length=255), + ), + ] diff --git a/csvimport/migrations/0003_csvimport_delimiter.py b/csvimport/migrations/0003_csvimport_delimiter.py new file mode 100644 index 0000000..b460860 --- /dev/null +++ b/csvimport/migrations/0003_csvimport_delimiter.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.5 on 2021-01-25 21:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('csvimport', '0002_csvimport_model_name_default'), + ] + + operations = [ + migrations.AddField( + model_name='csvimport', + name='delimiter', + field=models.CharField(default=',', max_length=1), + ), + ] diff --git a/csvimport/models.py b/csvimport/models.py index 770c279..39c5b35 100644 --- a/csvimport/models.py +++ b/csvimport/models.py @@ -62,6 +62,7 @@ class CSVImport(models.Model): ) upload_file = models.FileField(upload_to="csv", storage=fs) file_name = models.CharField(max_length=255, blank=True) + delimiter = models.CharField(max_length=1, default=",") encoding = models.CharField(max_length=32, blank=True) upload_method = models.CharField( blank=False, max_length=50, default="manual", choices=CHOICES