def handle(self, *args, **options):
        f = open(args[0],'r')
        parent = Location.objects.get(slug='gipuzkoa')
        #fix this, location's parent must be described in data file
        kont = 1
        for pl in f.readlines()[1:]:
            print kont, len(pl.split('\t')), pl.split('\t')[0], pl.split('\t')[-2]
            (titulo, slug, ent_origen, cod_origen, 
            cat, direc, cp, pob, desc, tel, fax, url, 
            foto, lat, lon, foto_x, foto_x_tit, foto_x_alt, itinerarios,
            acc_fis, acc_vis, acc_aud, acc_int, 
            acc_org, imagen_titulo, imagen_alt, tipo_biblio,
            ano_inicio, institucion, tipo_institucion, codigo_portal,
            horarios, tipo_acceso, tipo_centro, tematica, servicios,
            snbe, snbe_url, bilgunea, bilgunea_url, bilgunea_fecha,
            acces_itinerario, acces_url_ficha ) = pl.split('\t')[:43]

            cat_obj = Category.objects.get(slug=cat)
            location_slug =slugify(pob)
            location = Location.objects.filter(slug__startswith=location_slug)

            if location:
                loc_obj = location[0]
            else:
                loc_obj = Location()
                loc_obj.name = pob.decode('utf-8')
                loc_obj.parent = parent
                loc_obj.save()
            place = Place()
            place.name = titulo.decode('utf-8')
            place.category = cat_obj
            place.description = desc.decode('utf-8')
            place.address1 = direc
            place.postalcode = cp
            place.city = loc_obj
            place.source = ent_origen
            place.source_id = cod_origen
            if lat:
                place.lat = lat
            if lon:
                place.lon = lon
            place.tlf = tel
            place.fax = fax
            place.url = url
            place.url_es = url
            place.url_eu = url
            place.url_en = url
            place.save()
            access = Access()
            access.aphysic = self.ADICT[acc_fis.lower().strip()]
            access.avisual = self.ADICT[acc_vis.lower().strip()]
            access.aaudio = self.ADICT[acc_aud.lower().strip()]
            access.aintelec = self.ADICT[acc_int.lower().strip()]
            access.aorganic = self.ADICT[acc_org.lower().strip()]
            access.description = acces_itinerario
            access.fileurl = acces_url_ficha
            access.place = place
            access.save()
            kont += 1
Exemplo n.º 2
0
    def handle(self, *args, **options):
        saving = 1
        filename = args[0]
        full_path = "%s/%s" % (IMPORT_FILES_FOLDER,filename)
        f = xlrd.open_workbook(full_path)
        sh = f.sheet_by_index(0)

        kont = 1
        now = datetime.now()
        print 'NOW!:'
        print now


        for rownum in range(sh.nrows)[2:]:
            fields = sh.row_values(rownum)

            if len(fields)!=19:
                print 'Tenemos mas o menos de 19 campos'
                n = 1
                for field in fields:
                    print n, field
                    n = n+1
                break
            else:
                (name, catname, address1, postalcode,
                city_name, province, lat,
                 lon, tlf,
                 acc_fis, acc_vis, acc_aud, acc_int, acc_org,
                 acc_desc, xw, xx, xy, xz) = fields[:19]

            """slug
                name
                category.slug
                description
                address1
                address2
                postalcode
                city.name
                locality
                source
                source_id
                lat
                lon
                tlf
                fax
                url
                email
                access.aphysic
                access.avisual
                access.aaudio
                access.aintelec
                access.aorganic
                access.description
                access.fileurl
            """

            location_slug = slugify(city_name)
            try:
                location = Location.objects.get(slug=location_slug)
                loc_obj = location
            except:
                location = Location.objects.filter(slug__startswith=location_slug)
                if location:
                    loc_obj = location[0]
                else:
                    print 'ERROREA', location_slug
                    break
            print location, city_name, location_slug

            # Line added to force slug creation for the first import
            slug = "abcdefghijklmnopqrs"
            source_id = "%04d" % rownum
            source = 'elkartu'


            cat = self.CATDICT[catname]
            cat_obj = Category.objects.get(slug=cat)


            places = Place.objects.filter(slug=slug)

            if len(places)>0:
                place = places[0]
                print 'EDIT:', place.slug, place.source, place.source_id
            else:
                place = Place()
                place.slug = slugify(name, instance=place)
                print 'NEW:', place.slug, source_id
            place.name = name.encode('utf-8')
            place.category = cat_obj
            place.description_es = ''
            place.description_eu = ''
            place.description_en = ''
            place.address1 = address1
            place.address2 = ''
            cp = str(int(postalcode))
            if len(cp)<5:
                cp = "0%s" % cp
            place.postalcode = cp.strip()
            place.city = loc_obj
            place.locality = ''
            place.source = source
            place.source_id = source_id
            latstr = str(lat)[:-2]
            if type(lon)==type(0.3):
                lonstr =str(lon)[:-2]
            else:
                lonstr = lon
            place.lat = "%s.%s" % (latstr[:2],latstr[2:])
            place.lon = "%s.%s" % (lonstr[:2],lonstr[2:])
            print place.lat
            print place.lon
            place.tlf = tlf
            place.fax = ''
            place.url = ''
            place.email = ''
            place.modified_date = now

            if saving:
                place.save()

            # ACCESS
            try:
                access = place.access.all()[0]
            except:
                access = Access()
                access.place = place

            access.aphysic = self.ADICT[acc_fis.lower().strip()]
            access.avisual = self.ADICT[acc_vis.lower().strip()]
            access.aaudio = self.ADICT[acc_aud.lower().strip()]
            access.aintelec = self.ADICT[acc_int.lower().strip()]
            access.aorganic = self.ADICT[acc_org.lower().strip()]
            access.description = acc_desc

            if saving:
                access.save()

            kont += 1
Exemplo n.º 3
0
    def handle(self, *args, **options):
        saving = 1
        filename = args[0]
        full_path = "%s/%s" % (IMPORT_FILES_FOLDER, filename)
        f = xlrd.open_workbook(full_path)
        sh = f.sheet_by_index(0)

        kont = 1
        now = datetime.now()
        print 'NOW!:'
        print now

        cat = 'train'
        cat_obj = Category.objects.get(slug=cat)

        for rownum in range(sh.nrows)[1:]:
            fields = sh.row_values(rownum)

            if len(fields) != 24:
                print 'Tenemos mas o menos de 24 campos'
                n = 1
                for field in fields:
                    print n, field
                    n = n + 1
                break
            else:
                (slug, name, cat_slug, description, address1, address2,
                 postalcode, city_name, locality, source, source_id, lat, lon,
                 tlf, fax, url, email, acc_fis, acc_vis, acc_aud, acc_int,
                 acc_org, acc_desc, acc_fileurl) = fields[:24]
            """slug
                name
                category.slug
                description
                address1
                address2
                postalcode
                city.name
                locality
                source
                source_id
                lat
                lon
                tlf
                fax
                url
                email
                access.aphysic
                access.avisual
                access.aaudio
                access.aintelec
                access.aorganic
                access.description
                access.fileurl
            """

            location_slug = slugify(city_name)
            location = Location.objects.filter(slug__startswith=location_slug)
            if location:
                loc_obj = location[0]
            else:
                print 'ERROREA', location_slug
                break

            places = Place.objects.filter(slug=slug)

            if len(places) > 0:
                place = places[0]
                print 'EDIT:', place.slug, place.source, place.source_id
            else:
                place = Place()
                place.slug = slugify(slug, instance=place)
                print 'NEW:', slug, source_id

            place.name = name
            place.category = cat_obj
            place.description_es = description
            place.description_eu = description
            place.description_en = description
            place.address1 = address1
            place.address2 = address2
            cp = str(int(postalcode))
            if len(cp) < 5:
                cp = "0%s" % cp
            place.postalcode = cp.strip()
            place.city = loc_obj
            place.locality = locality
            place.source = source
            place.source_id = source_id
            place.lat = str(lat)
            place.lon = str(lon)
            place.tlf = tlf
            place.fax = fax
            place.url = url
            place.email = email
            place.modified_date = now

            if saving:
                place.save()

            # ACCESS
            try:
                access = place.access.all()[0]
            except:
                access = Access()
                access.place = place

            access.aphysic = self.ADICT[acc_fis.lower().strip()]
            access.avisual = self.ADICT[acc_vis.lower().strip()]
            access.aaudio = self.ADICT[acc_aud.lower().strip()]
            access.aintelec = self.ADICT[acc_int.lower().strip()]
            access.aorganic = self.ADICT[acc_org.lower().strip()]
            access.description = acc_desc

            if saving:
                access.save()

            kont += 1
Exemplo n.º 4
0
    def handle(self, *args, **options):
        saving = 1
        filename = args[0]
        full_path = "%s/%s" % (IMPORT_FILES_FOLDER,filename)
        f = xlrd.open_workbook(full_path)
        sh = f.sheet_by_index(0)

        kont = 1
        now = datetime.now()
        print 'NOW!:'
        print now

        cat = 'train'
        cat_obj = Category.objects.get(slug=cat)

        for rownum in range(sh.nrows)[1:]:
            fields = sh.row_values(rownum)

            if len(fields)!=24:
                print 'Tenemos mas o menos de 24 campos'
                n = 1
                for field in fields:
                    print n, field
                    n = n+1
                break
            else:
                (slug, name, cat_slug, description, address1, address2,
                 postalcode, city_name, locality, source, source_id, lat,
                 lon, tlf, fax, url, email,
                 acc_fis, acc_vis, acc_aud, acc_int, acc_org,
                 acc_desc, acc_fileurl) = fields[:24]
            
            """slug
                name
                category.slug
                description
                address1
                address2
                postalcode
                city.name
                locality
                source
                source_id
                lat
                lon
                tlf
                fax
                url
                email
                access.aphysic
                access.avisual
                access.aaudio
                access.aintelec
                access.aorganic
                access.description
                access.fileurl
            """

            location_slug = slugify(city_name)
            location = Location.objects.filter(slug__startswith=location_slug)
            if location:
                loc_obj = location[0]
            else:
                print 'ERROREA', location_slug
                break
            
            places = Place.objects.filter(slug=slug)

            if len(places)>0:
                place = places[0]
                print 'EDIT:', place.slug, place.source, place.source_id
            else:
                place = Place()
                place.slug = slugify(slug, instance=place)
                print 'NEW:', slug, source_id

            place.name = name
            place.category = cat_obj
            place.description_es = description
            place.description_eu = description
            place.description_en = description
            place.address1 = address1
            place.address2 = address2
            cp = str(int(postalcode))
            if len(cp)<5:
                cp = "0%s" % cp
            place.postalcode = cp.strip()
            place.city = loc_obj
            place.locality = locality
            place.source = source
            place.source_id = source_id
            place.lat = str(lat)
            place.lon = str(lon)
            place.tlf = tlf
            place.fax = fax
            place.url = url
            place.email = email
            place.modified_date = now

            if saving:
                place.save()           

            # ACCESS
            try:
                access = place.access.all()[0]
            except:
                access = Access()
                access.place = place

            access.aphysic = self.ADICT[acc_fis.lower().strip()]
            access.avisual = self.ADICT[acc_vis.lower().strip()]
            access.aaudio = self.ADICT[acc_aud.lower().strip()]
            access.aintelec = self.ADICT[acc_int.lower().strip()]
            access.aorganic = self.ADICT[acc_org.lower().strip()]
            access.description = acc_desc
            
            if saving:
                access.save()

            kont += 1