Add the picture importer, and add several tests data for testing import.

This commit is contained in:
Fred 2012-04-15 12:47:39 +02:00
parent e4effb626f
commit 3a0c6c6024
40 changed files with 455 additions and 41 deletions

1
.gitignore vendored
View File

@ -2,5 +2,6 @@
db/*
migrations/*
media/*
pictures/*
Thumbs.db
*.*~

View File

@ -1,47 +1,27 @@
#coding=utf-8
from django.core.management.base import BaseCommand, CommandError
from django.core.files import File
from xcards.cards.models import *
import os
from lxml import etree
class Command(BaseCommand):
args = '<filepath to open>'
help = 'Import data from an xml file generated by gc-star'
def handle(self, *args, **options):
if args is None:
if args is None or len(args) == 0:
raise Exception("Aucun fichier n'a été fourni en argument.")
l = self.fetch_gclist(args[0])
for item in l:
self.insert(item)
self.stdout.write('Successfully import information from data file')
def insert(item):
"""
Insère un item provenant de GCStar dans la base de données.
"""
self.stdout.write(item.gcsid + ' ' + item.country)
i = Item()
country, created = Country.objects.get_or_create(name=item.country)
i.country = country
category, created = Category.objects.get_or_create(label=item.category)
i.category = category
subcategory, created = SubCategory.objects.get_or_create(label=item.subcategory)
i.subcategory = subcategory
i.units = item.units
i.label = item.label
i.emissionDate = item.emissionDate
i.expirationDate = item.expirationDate
i.numberOfCopies = item.numberOfCopies
i.save()
def fetch_gclist(filepath):
self.stdout.write('Successfully import information from data file\n')
def fetch_gclist(self, filepath):
"""
Ouvre un fichier GCStar structuré de la manière suivante:
@ -63,7 +43,7 @@ class Command(BaseCommand):
</collection>
"""
if not os.path.file.exists(filepath):
if not os.path.exists(filepath):
raise IOError("Le fichier fourni en paramètre n'existe pas.")
element = etree.parse(filepath)
@ -77,7 +57,7 @@ class Command(BaseCommand):
i = GCItem()
i.country = attrs.get('gcsfield1')
i.with_chip = attrs.get('gcsfield2')
i.image_path = self._get_absfilepath(attrs.get('gcsfield3'))
i.image_path = self._get_absfilepath(os.path.dirname(filepath), attrs.get('gcsfield3'))
i.subcategory = attrs.get('gcsfield4')
i.units = attrs.get('gcsfield5')
i.label = attrs.get('gcsfield6')
@ -92,7 +72,7 @@ class Command(BaseCommand):
return my_list
def _get_absfilepath(folderpath, filepath):
def _get_absfilepath(self, folderpath, filepath):
"""
En paramètres:
- le dossier dans lequel se trouve le fichier xml
@ -100,19 +80,53 @@ class Command(BaseCommand):
Si le chemin référence déjà un chemin absolu, on le retourne simplement,
sinon, on combine les deux (le dossier et le chemin vers le fichier) et on retourne le résultat
s'il existe.
s'il existe. Note: dans certains cas, GCStar stocke ses fichiers dans un dossier 'collection_images'.
Sinon, jete une exception de type IOError.
Si le fichier n'a pas été trouvé, on jette une exception de type IOError.
"""
if os.path.exists(filepath):
return filepath
combinedPath = os.path.join(folderpath, filepath)
if filepath.startswith('./'):
filepath = filepath[2:]
if os.path.exists(combinedPath):
return combinedPath
possiblePaths = (filepath, os.path.join(folderpath, 'collection_images', filepath),
os.path.join(folderpath, filepath),)
raise IOError("Le fichier référencé (%s) n'existe pas." % combinedPath)
for file in possiblePaths:
if os.path.exists(file):
return os.path.abspath(file)
#raise IOError("Le fichier référencé n'existe pas. Chemins essayés: %s" % (', '.join(possiblePaths)))
self.stdout.write("Le fichier référencé n'existe pas. Chemins essayés: %s" % (', '.join(possiblePaths)))
return None
def insert(self, item):
"""
Insère un item provenant de GCStar dans la base de données.
"""
i = Item()
country, created = Country.objects.get_or_create(name=item.country)
i.country = country
category, created = Category.objects.get_or_create(label=item.category)
i.category = category
subcategory, created = SubCategory.objects.get_or_create(label=item.subcategory)
i.subcategory = subcategory
i.units = item.units
i.label = item.label
i.emissionDate = item.emissionDate
i.expirationDate = item.expirationDate
i.numberOfCopies = item.numberOfCopies
if item.image_path is not None:
i.image.save(i.label, File(open(item.image_path)))
i.save()
class GCItem():
"""

View File

@ -0,0 +1,14 @@
#coding=utf-8
from django.utils import unittest
from cards.management.commands import gcstar_import
class ImportTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_get_absfilepath(self):
self.assertEqual(1, 2)

325
data/tests/collection.gcs Normal file
View File

@ -0,0 +1,325 @@
<?xml version="1.0" encoding="UTF-8"?>
<collection type="Cartes_telephoniques" items="408" version="1.5.0">
<information>
<email>user_mail@...</email>
<images>collection_images</images>
<lang>FR (Français)</lang>
<maxId>422</maxId>
<owner>owner informations</owner>
</information>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="Vitell_1_0.jpg"
gcsfield4="Boissons"
gcsfield5="50"
gcsfield6="Vitell 1"
gcsfield7="03/1993"
gcsfield8=""
gcsfield9="B320C0061"
gcsfield10="1500000"
gcsfield11="Alimentation "
gcsautoid="4"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="france/Alimentation/Boissons/boissonivittel2.JPG"
gcsfield4="Boissons"
gcsfield5="50"
gcsfield6="Vitell 2"
gcsfield7="03/1997"
gcsfield8=""
gcsfield9="B72089104 732157506"
gcsfield10="1000000"
gcsfield11="Alimentation "
gcsautoid="5"
>
</item>
<item
gcsfield1="Royaume-Unis"
gcsfield2=""
gcsfield3="royaume unis/P1010073.JPG"
gcsfield4=""
gcsfield5="£5"
gcsfield6="OnLine"
gcsfield7=""
gcsfield8="06/1999"
gcsfield9="284-830722"
gcsfield10=""
gcsfield11="Appels internationnaux"
gcsautoid="187"
>
</item>
<item
gcsfield1="Royaume-Unis"
gcsfield2="1"
gcsfield3="royaume unis/P1010074.JPG"
gcsfield4="Musique"
gcsfield5="£3"
gcsfield6="Glastonbury Festival 26-27-28 June 1998"
gcsfield7=""
gcsfield8="31/03/2000"
gcsfield9="02/3/002039/0845"
gcsfield10=""
gcsfield11="Spectacles / Festivals"
gcsautoid="188"
>
</item>
<item
gcsfield1="Royaume-Unis"
gcsfield2="1"
gcsfield3="royaume unis/P1010075.JPG"
gcsfield4=""
gcsfield5="50p"
gcsfield6="BT Phonecard"
gcsfield7=""
gcsfield8="31/03/2000"
gcsfield9="23/6/001130/0581"
gcsfield10=""
gcsfield11="Telecom"
gcsautoid="189"
>
</item>
<item
gcsfield1="Royaume-Unis"
gcsfield2=""
gcsfield3="royaume unis/P1010076.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="£5 off your Pizza"
gcsfield7=""
gcsfield8=""
gcsfield9="529A31637"
gcsfield10=""
gcsfield11="Alimentation"
gcsautoid="190"
>
</item>
<item
gcsfield1="Royaume-Unis"
gcsfield2=""
gcsfield3="royaume unis/P1010077.JPG"
gcsfield4=""
gcsfield5="10"
gcsfield6="Princess Diana 1961-1997"
gcsfield7=""
gcsfield8=""
gcsfield9=""
gcsfield10="5000, card number 38"
gcsfield11="Personnalités"
gcsautoid="191"
>
</item>
<item
gcsfield1="Royaume-Unis"
gcsfield2=""
gcsfield3="royaume unis/P1010078.JPG"
gcsfield4="Cinéma"
gcsfield5="£10"
gcsfield6="&quot;Leonardo Di Caprio Phonecard&quot;"
gcsfield7=""
gcsfield8=""
gcsfield9=""
gcsfield10=""
gcsfield11="Personnalités"
gcsautoid="192"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney02.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="EuroDisney Resort"
gcsfield7="04/2002"
gcsfield8=""
gcsfield9="B230L0038"
gcsfield10="510000"
gcsfield11="Disney"
gcsautoid="31"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney04.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="Le cadeau"
gcsfield7="11/1993"
gcsfield8=""
gcsfield9="D41001756"
gcsfield10="6000000"
gcsfield11="Disney"
gcsautoid="32"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney05.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="EuroDisneyland Paris "
gcsfield7="05/1994"
gcsfield8=""
gcsfield9="649149506"
gcsfield10="2000000"
gcsfield11="Disney"
gcsautoid="33"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney08.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="Disneyland Paris 2"
gcsfield7="02/1995"
gcsfield8=""
gcsfield9="A 51014512 497464515"
gcsfield10="500000"
gcsfield11="Disney"
gcsautoid="34"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney06.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="&quot;séjour golf&quot;"
gcsfield7="06/1994"
gcsfield8=""
gcsfield9="C47045825"
gcsfield10="1400000"
gcsfield11="Disney"
gcsautoid="35"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney09.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="Disneyland Paris 1"
gcsfield7="04/1997"
gcsfield8=""
gcsfield9="C75107261 759776710"
gcsfield10="1000000"
gcsfield11="Disney"
gcsautoid="36"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney10.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="Sommet des enfants"
gcsfield7="05/1996"
gcsfield8=""
gcsfield9="B64179076 649123124"
gcsfield10="1000000"
gcsfield11="Disney"
gcsautoid="37"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney12.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="Walt Disney Studios 1"
gcsfield7="04/2002"
gcsfield8="01/05/2004"
gcsfield9="A23613745 500347337"
gcsfield10="1000000"
gcsfield11="Disney"
gcsautoid="38"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney13.JPG"
gcsfield4=""
gcsfield5="50"
gcsfield6="Walt Disney Studio 2"
gcsfield7="04/2002"
gcsfield8="01/05/2004"
gcsfield9="D23413735 498675383"
gcsfield10="1000000"
gcsfield11="Disney"
gcsautoid="39"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney14.jpg"
gcsfield4=""
gcsfield5="50"
gcsfield6="Disney Village"
gcsfield7="08/2003"
gcsfield8=""
gcsfield9="B37714275 557624551"
gcsfield10="1000000"
gcsfield11="Disney"
gcsautoid="40"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disney15.jpg"
gcsfield4=""
gcsfield5="50"
gcsfield6="La légende de Buffalo Bill"
gcsfield7="08/2003"
gcsfield8=""
gcsfield9="D38714278 558047652"
gcsfield10="1000000"
gcsfield11="Disney"
gcsautoid="41"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/buffalo bill 120.jpg"
gcsfield4=""
gcsfield5="120"
gcsfield6="La légende de Buffalo Bill"
gcsfield7="08/2003"
gcsfield8=""
gcsfield9="A38714287 558634813"
gcsfield10="1000000"
gcsfield11="Disney"
gcsautoid="42"
>
</item>
<item
gcsfield1="France"
gcsfield2="1"
gcsfield3="./france/Disney/disneyleroilion.jpg"
gcsfield4=""
gcsfield5="50"
gcsfield6="La légende du Roi Lion"
gcsfield7="07/2004"
gcsfield8="01/08/2006"
gcsfield9="B46714593 589401321"
gcsfield10="1000000"
gcsfield11="Disney"
gcsautoid="43"></item>
</collection>

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -0,0 +1,26 @@
[Picasa]
elisegosset_lh=5490112888880575825
[disney02.JPG]
IIDLIST_elisegosset_lh=4c30cc991b23b512
[disney04.JPG]
IIDLIST_elisegosset_lh=4c30cc9f31c8fd12
[disney05.JPG]
IIDLIST_elisegosset_lh=4c30cca344dea442
[disney08.JPG]
IIDLIST_elisegosset_lh=4c30cca61c068d92
[disney12.JPG]
IIDLIST_elisegosset_lh=4c30ccac2afcdb92
[disney13.JPG]
IIDLIST_elisegosset_lh=4c30ccaf9305cfb2
[disney14.jpg]
IIDLIST_elisegosset_lh=4c30ccb2a41ffe02
[disney15.jpg]
IIDLIST_elisegosset_lh=4c30ccb6e29ed222
[disneyleroilion.jpg]
IIDLIST_elisegosset_lh=4c30ccba80230772
[disney06.JPG]
IIDLIST_elisegosset_lh=4c30ccbe39e67d32
[disney10.JPG]
IIDLIST_elisegosset_lh=4c30ccc12085f3d2
[disney09.JPG]
IIDLIST_elisegosset_lh=4c30ccc4b4b79fe2

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -0,0 +1,26 @@
[Picasa]
elisegosset_lh=5490112888880575825
[disney02.JPG]
IIDLIST_elisegosset_lh=4c30cc991b23b512
[disney04.JPG]
IIDLIST_elisegosset_lh=4c30cc9f31c8fd12
[disney05.JPG]
IIDLIST_elisegosset_lh=4c30cca344dea442
[disney08.JPG]
IIDLIST_elisegosset_lh=4c30cca61c068d92
[disney12.JPG]
IIDLIST_elisegosset_lh=4c30ccac2afcdb92
[disney13.JPG]
IIDLIST_elisegosset_lh=4c30ccaf9305cfb2
[disney14.jpg]
IIDLIST_elisegosset_lh=4c30ccb2a41ffe02
[disney15.jpg]
IIDLIST_elisegosset_lh=4c30ccb6e29ed222
[disneyleroilion.jpg]
IIDLIST_elisegosset_lh=4c30ccba80230772
[disney06.JPG]
IIDLIST_elisegosset_lh=4c30ccbe39e67d32
[disney10.JPG]
IIDLIST_elisegosset_lh=4c30ccc12085f3d2
[disney09.JPG]
IIDLIST_elisegosset_lh=4c30ccc4b4b79fe2

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -0,0 +1,8 @@
[Picasa]
[P1010072.JPG]
[P1010073.JPG]
[P1010074.JPG]
[P1010075.JPG]
[P1010076.JPG]
[P1010077.JPG]
[P1010078.JPG]

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB