summaryrefslogtreecommitdiff
path: root/ishtar
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar')
-rw-r--r--ishtar/furnitures/models.py16
-rw-r--r--ishtar/furnitures/views.py5
-rw-r--r--ishtar/scripts/__init__.py0
-rwxr-xr-xishtar/scripts/import_from_csv.py68
-rw-r--r--ishtar/settings.py.example1
5 files changed, 83 insertions, 7 deletions
diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py
index f7cc8288c..1a595f629 100644
--- a/ishtar/furnitures/models.py
+++ b/ishtar/furnitures/models.py
@@ -144,7 +144,7 @@ class LightHistorizedItem(BaseHistorizedItem):
class Departement(models.Model):
label = models.CharField(_(u"Label"), max_length=30)
- number = models.IntegerField(_(u"Number"))
+ number = models.CharField(_(u"Number"), unique=True, max_length=3)
class Meta:
verbose_name = _(u"Departement")
@@ -669,10 +669,14 @@ if settings.COUNTRY == 'fr':
class Town(models.Model):
name = models.CharField(_(u"Name"), max_length=100)
- surface = models.IntegerField(_(u"Surface"))
- center = models.PointField(_(u"Localisation"), srid=settings.SRID)
+ surface = models.IntegerField(_(u"Surface"), blank=True, null=True)
+ center = models.PointField(_(u"Localisation"), srid=settings.SRID,
+ blank=True, null=True)
if settings.COUNTRY == 'fr':
- numero_insee = models.CharField(u"Numéro INSEE", max_length=5)
+ numero_insee = models.CharField(u"Numéro INSEE", max_length=6,
+ unique=True)
+ departement = models.ForeignKey(Departement, verbose_name=u"Département",
+ null=True, blank=True)
canton = models.ForeignKey(Canton, verbose_name=u"Canton", null=True,
blank=True)
objects = models.GeoManager()
@@ -680,8 +684,12 @@ class Town(models.Model):
class Meta:
verbose_name = _(u"Town")
verbose_name_plural = _(u"Towns")
+ if settings.COUNTRY == 'fr':
+ ordering = ['numero_insee']
def __unicode__(self):
+ if settings.COUNTRY == "fr":
+ return " - ".join((self.name, self.numero_insee))
return self.name
class TreatmentType(GeneralType):
diff --git a/ishtar/furnitures/views.py b/ishtar/furnitures/views.py
index 03029cfe4..c9ea7a0fd 100644
--- a/ishtar/furnitures/views.py
+++ b/ishtar/furnitures/views.py
@@ -91,9 +91,8 @@ def autocomplete_town(request):
for q in q.split(' '):
extra = Q(name__icontains=q)
if settings.COUNTRY == 'fr':
- extra = extra | (Q(canton__name__istartswith=q) | \
- Q(canton__arrondissement__name__istartswith=q) | \
- Q(canton__arrondissement__department__label__istartswith=q))
+ extra = (extra | Q(numero_insee__istartswith=q) | \
+ Q(departement__label__istartswith=q))
query = query & extra
limit = 15
towns = models.Town.objects.filter(query)[:limit]
diff --git a/ishtar/scripts/__init__.py b/ishtar/scripts/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/ishtar/scripts/__init__.py
diff --git a/ishtar/scripts/import_from_csv.py b/ishtar/scripts/import_from_csv.py
new file mode 100755
index 000000000..7eb655641
--- /dev/null
+++ b/ishtar/scripts/import_from_csv.py
@@ -0,0 +1,68 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+"""
+Import departements and towns from csv file
+"""
+
+DELIMITER = ","
+QUOTECHAR = '"'
+
+import sys
+import csv
+sys.path.append('.')
+
+from django.core.management import setup_environ
+import settings
+
+setup_environ(settings)
+
+from optparse import OptionParser
+
+from furnitures import models
+
+def insert_department(value):
+ idx, label = value
+ if models.Departement.objects.filter(number=idx).count():
+ return
+ models.Departement(number=idx, label=label).save()
+ print idx, label, u" inserted"
+
+def insert_town(value):
+ idx, label = value
+ if models.Town.objects.filter(numero_insee=idx).count():
+ return
+ try:
+ dpt = models.Departement.objects.get(number=idx[:2])
+ except:
+ return
+ models.Town(numero_insee=idx, name=label, departement=dpt).save()
+ print idx, label, u" inserted"
+
+tables = {u"department":insert_department,
+ u"town":insert_town}
+
+usage = u"usage: %%prog csv_file.csv table_name\n\n"\
+ u"Table name must be in: %s." % u", ".join(tables.keys())
+parser = OptionParser(usage=usage)
+
+(options, args) = parser.parse_args()
+
+try:
+ assert len(args) == 2
+except AssertionError:
+ parser.error(u"You must provide one csv file and the table name.")
+
+try:
+ assert args[1] in tables.keys()
+except AssertionError:
+ parser.error(u"Incorrect table name.")
+
+try:
+ values = csv.reader(open(args[0], 'rb'), delimiter=DELIMITER,
+ quotechar=QUOTECHAR)
+except (IOError):
+ parser.error(u"Incorrect CSV file.")
+
+for value in values:
+ tables[args[1]](value)
diff --git a/ishtar/settings.py.example b/ishtar/settings.py.example
index d38f68cf8..92ce2baa1 100644
--- a/ishtar/settings.py.example
+++ b/ishtar/settings.py.example
@@ -118,5 +118,6 @@ INSTALLED_APPS = (
'django.contrib.messages',
'django.contrib.admin',
'registration',
+ #'ishtar.scripts',
'ishtar.furnitures'
)