Exemple #1
0
 def row2instance(self,nr,title,last_name,first_name,street,zip_code,city_name,phone,gsm,birth_date,bez,datum,mg,mgnr,email):
     kw = dict(last_name=last_name,first_name=first_name)
     if nr:
         kw.update(id=nr)
     kw.update(phone=phone)
     kw.update(gsm=gsm)
     kw.update(zip_code=zip_code)
     if email:
         if isinstance(email,basestring) and is_valid_email(email):
             kw.update(email=email)
         else:
             logger.warning("Ignored invalid email address %r",email)
     #~ kw.update(street=street)
     
     #~ kw.update(pupil_type=mg)
     
     kw.update(street2kw(street))
     
     if title == "Herr":
         kw.update(gender=dd.Genders.male)
     elif title == "Frau":
         kw.update(gender=dd.Genders.female)
     elif title:
         kw.update(title=title)
     if city_name:
         #~ countries.City.objects.get(name)
         kw.update(city=countries.City.lookup_or_create('name',city_name,country=self.country))
     #~ print birth_date
     kw.update(birth_date=self.date_converter(birth_date))
     return courses.Pupil(**kw)
Exemple #2
0
def country2kw(row, kw):
    # for both PAR and ADR

    if row.has_key('PROF'):
        activity = row['PROF']
        if activity:
            try:
                activity = int(activity)
            except ValueError:
                dblogger.debug("Ignored invalid value PROF = %r", activity)
            else:
                if activity:
                    try:
                        activity = Activity.objects.get(pk=activity)
                    except Activity.DoesNotExist:
                        activity = Activity(
                            id=activity, name=unicode(activity))
                        activity.save(force_insert=True)
                    kw.update(activity=activity)

    country = row['PAYS']
    if country:
        try:
            country = Country.objects.get(short_code__exact=country)
        except Country.DoesNotExist:
            country = Country(isocode=country, name=country,
                              short_code=country)
            country.save()
        kw.update(country=country)

    store(kw,
          phone=row['TEL'],
          fax=row['FAX'],
          email=row['EMAIL'],
          )

    kw.update(street2kw(join_words(row['RUE'], row['RUENUM'], row['RUEBTE'])))

    zip_code = row['CP']
    if zip_code:
        kw.update(zip_code=zip_code)
        try:
            city = Place.objects.get(
                country=country,
                zip_code__exact=zip_code,
            )
            kw.update(city=city)
        except Place.DoesNotExist, e:
            city = Place(zip_code=zip_code, name=zip_code, country=country)
            city.save()
            kw.update(city=city)
            #~ logger.warning("%s-%s : %s",row['PAYS'],row['CP'],e)
        except Place.MultipleObjectsReturned, e:
            dblogger.warning("%s-%s : %s", row['PAYS'], row['CP'], e)
Exemple #3
0
def load_P_(row):
    kw = {}
    #~ kw.update(street2kw(join_words(row['RUE'],row['RUENUM'],row['RUEBTE'])))
    store(kw, last_name=row['AN'])
    store(kw, first_name=row['AP'])
    store(kw, crl=CRL(row['P'].encode('cp437')))
    #~ store(kw,crl=crl2hex(row['P']))
    OU = row['OU']
    if OU:
        kw.update(street2kw(OU))
    title = row['PQ']
    if title:
        if title == 'Mme':
            kw.update(language='fr', gender=dd.Genders.female)
        elif title == 'Mlle':
            kw.update(language='fr', gender=dd.Genders.female)
        elif title == 'M.':
            kw.update(language='fr', gender=dd.Genders.male)
        elif title == 'dHr':
            kw.update(language='nl', gender=dd.Genders.male)
        elif title == 'Mvw':
            kw.update(language='nl', gender=dd.Genders.female)
        elif title == 'Mr':
            kw.update(language='en', gender=dd.Genders.male)
        elif title == 'Mrs':
            kw.update(language='en', gender=dd.Genders.female)
        elif title == 'Hrrn':
            kw.update(language='de', gender=dd.Genders.male)
        elif title == 'Fr':
            kw.update(language='de', gender=dd.Genders.female)
        elif title == 'Fr.':
            kw.update(language='fr', gender=dd.Genders.male, title=u"Frère")
        elif title == 'Frl':
            kw.update(language='de', gender=dd.Genders.female)
        elif title == 'Bx':
            kw.update(gender=dd.Genders.male, title="Bx")
        elif title == 'Bse':
            kw.update(gender=dd.Genders.female, title="Bse")
        elif title == 'St':
            kw.update(gender=dd.Genders.male, title="St")
        elif title == 'Ste':
            kw.update(gender=dd.Genders.female, title="Ste")
        else:
            dblogger.warning("Ignored PQ value %r" % title)

    a = parsedate(row['T'])
    if a:
        kw.update(birth_date=a[0], birth_date_circa=a[1])
    a = parsedate(row['T'])
    if a:
        kw.update(died_date=a[0])
        if a[1]:
            logger.warning("Ignored 'circa' flag for died_date")
    return Person(**kw)
Exemple #4
0
def load_P_(row):
    kw = {}
    #~ kw.update(street2kw(join_words(row['RUE'],row['RUENUM'],row['RUEBTE'])))
    store(kw, last_name=row['AN'])
    store(kw, first_name=row['AP'])
    store(kw, crl=CRL(row['P'].encode('cp437')))
    #~ store(kw,crl=crl2hex(row['P']))
    OU = row['OU']
    if OU:
        kw.update(street2kw(OU))
    title = row['PQ']
    if title:
        if title == 'Mme':
            kw.update(language='fr', gender=dd.Genders.female)
        elif title == 'Mlle':
            kw.update(language='fr', gender=dd.Genders.female)
        elif title == 'M.':
            kw.update(language='fr', gender=dd.Genders.male)
        elif title == 'dHr':
            kw.update(language='nl', gender=dd.Genders.male)
        elif title == 'Mvw':
            kw.update(language='nl', gender=dd.Genders.female)
        elif title == 'Mr':
            kw.update(language='en', gender=dd.Genders.male)
        elif title == 'Mrs':
            kw.update(language='en', gender=dd.Genders.female)
        elif title == 'Hrrn':
            kw.update(language='de', gender=dd.Genders.male)
        elif title == 'Fr':
            kw.update(language='de', gender=dd.Genders.female)
        elif title == 'Fr.':
            kw.update(language='fr', gender=dd.Genders.male, title=u"Frère")
        elif title == 'Frl':
            kw.update(language='de', gender=dd.Genders.female)
        elif title == 'Bx':
            kw.update(gender=dd.Genders.male, title="Bx")
        elif title == 'Bse':
            kw.update(gender=dd.Genders.female, title="Bse")
        elif title == 'St':
            kw.update(gender=dd.Genders.male, title="St")
        elif title == 'Ste':
            kw.update(gender=dd.Genders.female, title="Ste")
        else:
            dblogger.warning("Ignored PQ value %r" % title)

    a = parsedate(row['T'])
    if a:
        kw.update(birth_date=a[0], birth_date_circa=a[1])
    a = parsedate(row['T'])
    if a:
        kw.update(died_date=a[0])
        if a[1]:
            logger.warning("Ignored 'circa' flag for died_date")
    return Person(**kw)
Exemple #5
0
def country2kw(row, kw):
    # for both PAR and ADR

    if row.has_key('PROF'):
        activity = row['PROF']
        if activity:
            try:
                activity = int(activity)
            except ValueError:
                dblogger.debug("Ignored invalid value PROF = %r", activity)
            else:
                if activity:
                    try:
                        activity = Activity.objects.get(pk=activity)
                    except Activity.DoesNotExist:
                        activity = Activity(
                            id=activity, name=unicode(activity))
                        activity.save(force_insert=True)
                    kw.update(activity=activity)

    country = row['PAYS']
    if country:
        try:
            country = Country.objects.get(short_code__exact=country)
        except Country.DoesNotExist:
            country = Country(isocode=country, name=country,
                              short_code=country)
            country.save()
        kw.update(country=country)

    store(kw,
          phone=row['TEL'],
          fax=row['FAX'],
          email=row['EMAIL'],
          )

    kw.update(street2kw(join_words(row['RUE'], row['RUENUM'], row['RUEBTE'])))

    zip_code = row['CP']
    if zip_code:
        kw.update(zip_code=zip_code)
        try:
            city = Place.objects.get(
                country=country,
                zip_code__exact=zip_code,
            )
            kw.update(city=city)
        except Place.DoesNotExist, e:
            city = Place(zip_code=zip_code, name=zip_code, country=country)
            city.save()
            kw.update(city=city)
            #~ logger.warning("%s-%s : %s",row['PAYS'],row['CP'],e)
        except Place.MultipleObjectsReturned, e:
            dblogger.warning("%s-%s : %s", row['PAYS'], row['CP'], e)
Exemple #6
0
    def card2client(self, data):
        "does the actual conversion"

        countries = dd.resolve_app('countries', strict=True)

        kw = dict()
        raw_data = data['card_data']
        if not '\n' in raw_data:
            # a one-line string means that some error occured (e.g. no
            # card in reader). of course we want to show this to the
            # user.
            raise Warning(raw_data)

        #~ print cd
        data = AttrDict(yaml.load(raw_data))
        #~ raise Exception("20131108 cool: %s" % cd)

        kw.update(national_id=ssin.format_ssin(str(data.nationalNumber)))
        kw.update(first_name=join_words(
            data.firstName,
            data.middleName))
        kw.update(last_name=data.name)

        card_number = str(data.cardNumber)

        if data.photo:
            fn = config.card_number_to_picture_file(card_number)
            if os.path.exists(fn):
                logger.warning("Overwriting existing image file %s.", fn)
            fp = file(fn, 'wb')
            fp.write(base64.b64decode(data.photo))
            fp.close()
            #~ print 20121117, repr(data['picture'])
            #~ kw.update(picture_data_encoded=data['picture'])

        if isinstance(data.dateOfBirth, basestring):
            data.dateOfBirth = IncompleteDate(*data.dateOfBirth.split('-'))
        kw.update(birth_date=data.dateOfBirth)
        kw.update(card_valid_from=data.cardValidityDateBegin)
        kw.update(card_valid_until=data.cardValidityDateEnd)

        kw.update(card_number=card_number)
        kw.update(card_issuer=data.cardDeliveryMunicipality)
        if data.nobleCondition:
            kw.update(noble_condition=data.nobleCondition)
        kw.update(street=data.streetAndNumber)
        #~ kw.update(street_no=data['streetNumber'])
        #~ kw.update(street_box=data['boxNumber'])
        if True:  # kw['street'] and not (kw['street_no'] or kw['street_box']):
            kw = street2kw(kw['street'], **kw)
        kw.update(zip_code=str(data.zip))
        if data.placeOfBirth:
            kw.update(birth_place=data.placeOfBirth)
        pk = data.reader.upper()

        msg1 = "BeIdReadCardToClientAction %s" % kw.get('national_id')

        #~ try:
        country = countries.Country.objects.get(isocode=pk)
        kw.update(country=country)
        #~ except countries.Country.DoesNotExist,e:
        #~ except Exception,e:
            #~ logger.warning("%s : no country with code %r",msg1,pk)
        #~ BE = countries.Country.objects.get(isocode='BE')
        #~ fld = countries.Place._meta.get_field()
        kw.update(city=countries.Place.lookup_or_create(
            'name', data.municipality, country=country))

        def sex2gender(sex):
            if sex == 'MALE':
                return dd.Genders.male
            if sex == 'FEMALE':
                return dd.Genders.female
            logger.warning("%s : invalid gender code %r", msg1, sex)
        kw.update(gender=sex2gender(data.gender))

        def doctype2cardtype(dt):
            #~ if dt == 1: return BeIdCardTypes.get_by_value("1")
            rv = BeIdCardTypes.get_by_value(str(dt))
            # logger.info("20130103 documentType %r --> %r", dt, rv)
            return rv
        kw.update(card_type=doctype2cardtype(data.documentType))

        if config.data_collector_dir:
            fn = os.path.join(
                config.data_collector_dir,
                card_number + '.txt')
            file(fn, "w").write(raw_data)
            logger.info("Wrote eid card data to file %s", fn)

        return kw
Exemple #7
0
    def card2client(cls, data):
        "does the actual conversion"

        self = cls

        from lino.utils import ssin
        from lino.api import dd, rt
        from lino.mixins.beid import BeIdCardTypes
        from lino.utils import join_words
        from lino.utils import IncompleteDate
        from lino.modlib.contacts.utils import street2kw

        countries = dd.resolve_app('countries', strict=True)

        kw = dict()
        #~ def func(fldname,qname):
            #~ kw[fldname] = data[qname]
        kw.update(national_id=ssin.format_ssin(data['nationalNumber']))
        kw.update(first_name=join_words(
            data['firstName1'],
            data['firstName2'],
            data['firstName3']))
        #~ func('first_name','firstName1')
        kw.update(last_name=data['surname'])

        card_number = data['cardNumber']

        if 'picture' in data:
            fn = self.card_number_to_picture_file(card_number)
            if os.path.exists(fn):
                logger.warning("Overwriting existing image file %s.", fn)
            fp = file(fn, 'wb')
            fp.write(base64.b64decode(data['picture']))
            fp.close()
            #~ print 20121117, repr(data['picture'])
            #~ kw.update(picture_data_encoded=data['picture'])

        #~ func('card_valid_from','validityBeginDate')
        #~ func('card_valid_until','validityEndDate')
        #~ func('birth_date','birthDate')
        kw.update(birth_date=IncompleteDate(
            *settings.SITE.parse_date(data['birthDate'])))
        kw.update(card_valid_from=datetime.date(
            *settings.SITE.parse_date(data['validityBeginDate'])))
        kw.update(card_valid_until=datetime.date(
            *settings.SITE.parse_date(data['validityEndDate'])))
        kw.update(card_number=card_number)
        kw.update(card_issuer=data['issuingMunicipality'])
        kw.update(noble_condition=data['nobleCondition'])
        kw.update(street=data['street'])
        kw.update(street_no=data['streetNumber'])
        kw.update(street_box=data['boxNumber'])
        if kw['street'] and not (kw['street_no'] or kw['street_box']):
            kw = street2kw(kw['street'], **kw)
        kw.update(zip_code=data['zipCode'])
        kw.update(birth_place=data['birthLocation'])
        pk = data['country'].upper()

        msg1 = "BeIdReadCardToClientAction %s" % kw.get('national_id')

        #~ try:
        country = countries.Country.objects.get(isocode=pk)
        kw.update(country=country)
        #~ except countries.Country.DoesNotExist,e:
        #~ except Exception,e:
            #~ logger.warning("%s : no country with code %r",msg1,pk)
        #~ BE = countries.Country.objects.get(isocode='BE')
        #~ fld = countries.Place._meta.get_field()
        kw.update(city=countries.Place.lookup_or_create(
            'name', data['municipality'], country=country))

        def sex2gender(sex):
            if sex == 'M':
                return dd.Genders.male
            if sex in 'FVW':
                return dd.Genders.female
            logger.warning("%s : invalid gender code %r", msg1, sex)
        kw.update(gender=sex2gender(data['sex']))

        if False:
            def nationality2country(nationality):
                try:
                    return countries.Country.objects.get(
                        nationalities__icontains=nationality)
                except countries.Country.DoesNotExist, e:
                    logger.warning("%s : no country for nationality %r",
                                   msg1, nationality)
                except MultipleObjectsReturned, e:
                    logger.warning(
                        "%s : found more than one country for nationality %r",
                        msg1, nationality)
Exemple #8
0
    def card2client(cls,data):
        "does the actual conversion"
        kw = dict()
        #~ assert not settings.SITE.use_eid_jslib
        #~ assert not settings.SITE.has_plugin(BeIdJsLibPlugin):
        data = data['card_data']
        if not '\n' in data:
            raise Warning(data)
        #~ print cd
        data = AttrDict(yaml.load(data))
        #~ raise Exception("20131108 cool: %s" % cd)
        
        
        kw.update(national_id=ssin.format_ssin(str(data.nationalNumber)))
        kw.update(first_name=join_words(
            data.firstName,
            data.middleName))
        kw.update(last_name=data.name)
        
        card_number = str(data.cardNumber)
        
        if data.photo:
            fn = card_number_to_picture_file(card_number)
            if os.path.exists(fn):
                logger.warning("Overwriting existing image file %s.",fn)
            fp = file(fn,'wb')
            fp.write(base64.b64decode(data.photo))
            fp.close()
            #~ print 20121117, repr(data['picture'])
            #~ kw.update(picture_data_encoded=data['picture'])
        
        if isinstance(data.dateOfBirth,basestring):
            data.dateOfBirth = IncompleteDate(*data.dateOfBirth.split('-'))
        kw.update(birth_date=data.dateOfBirth)
        kw.update(card_valid_from=data.cardValidityDateBegin)
        kw.update(card_valid_until=data.cardValidityDateEnd)
        
        kw.update(card_number=card_number)
        kw.update(card_issuer=data.cardDeliveryMunicipality)
        if data.nobleCondition:
            kw.update(noble_condition=data.nobleCondition)
        kw.update(street=data.streetAndNumber)
        #~ kw.update(street_no=data['streetNumber'])
        #~ kw.update(street_box=data['boxNumber'])
        if True: # kw['street'] and not (kw['street_no'] or kw['street_box']):
            kw = street2kw(kw['street'],**kw)
        kw.update(zip_code=str(data.zip))
        if data.placeOfBirth:
            kw.update(birth_place=data.placeOfBirth)
        pk = data.reader.upper()
        
        msg1 = "BeIdReadCardToClientAction %s" % kw.get('national_id')

        #~ try:
        country = countries.Country.objects.get(isocode=pk)
        kw.update(country=country)
        #~ except countries.Country.DoesNotExist,e:
        #~ except Exception,e:
            #~ logger.warning("%s : no country with code %r",msg1,pk)
        #~ BE = countries.Country.objects.get(isocode='BE')
        #~ fld = countries.City._meta.get_field()
        kw.update(city=countries.City.lookup_or_create(
            'name',data.municipality,country=country))
        def sex2gender(sex):
            if sex == 'MALE' : return dd.Genders.male
            if sex == 'FEMALE' : return dd.Genders.female
            logger.warning("%s : invalid gender code %r",msg1,sex)
        kw.update(gender=sex2gender(data.gender))
        
        def doctype2cardtype(dt):
            #~ if dt == 1: return BeIdCardTypes.get_by_value("1")
            rv = BeIdCardTypes.get_by_value(str(dt))
            logger.info("20130103 documentType %r --> %r",dt,rv)
            return rv
        kw.update(card_type=doctype2cardtype(data.documentType))
        return kw
Exemple #9
0
    def card2client(self, data):
        """Convert the data coming from the card into database fields to be
        stored in the card holder.

        """
        countries = dd.resolve_app('countries', strict=True)

        kw = dict()
        raw_data = data['card_data']
        if not '\n' in raw_data:
            # a one-line string means that some error occured (e.g. no
            # card in reader). of course we want to show this to the
            # user.
            raise Warning(raw_data)

        #~ print cd
        data = AttrDict(yaml.load(raw_data))
        #~ raise Exception("20131108 cool: %s" % cd)

        kw.update(national_id=ssin.format_ssin(str(data.nationalNumber)))
        kw.update(first_name=data.firstName or '')
        kw.update(middle_name=data.middleName or '')
        kw.update(last_name=data.name or '')

        card_number = str(data.cardNumber)

        if data.photo:
            if not card_number:
                raise Exception("20150730 photo data but no card_number ")
            fn = get_image_path(card_number)
            if fn.exists():
                logger.warning("Overwriting existing image file %s.", fn)
            try:
                fp = file(fn, 'wb')
                fp.write(base64.b64decode(data.photo))
                fp.close()
            except IOError as e:
                logger.warning("Failed to store image file %s : %s", fn, e)

            #~ print 20121117, repr(data['picture'])
            #~ kw.update(picture_data_encoded=data['picture'])

        if isinstance(data.dateOfBirth, basestring):
            data.dateOfBirth = IncompleteDate(*data.dateOfBirth.split('-'))
        kw.update(birth_date=data.dateOfBirth)
        kw.update(card_valid_from=data.cardValidityDateBegin)
        kw.update(card_valid_until=data.cardValidityDateEnd)

        kw.update(card_number=card_number)
        kw.update(card_issuer=data.cardDeliveryMunicipality)
        if data.nobleCondition:
            kw.update(noble_condition=data.nobleCondition)
        if data.streetAndNumber:
            # kw.update(street=data.streetAndNumber)
            kw = street2kw(data.streetAndNumber, **kw)
        if data.zip:
            kw.update(zip_code=str(data.zip))
        if data.placeOfBirth:
            kw.update(birth_place=data.placeOfBirth)
        pk = data.reader.upper()

        msg1 = "BeIdReadCardToClientAction %s" % kw.get('national_id')

        country = countries.Country.objects.get(isocode=pk)
        kw.update(country=country)
        if data.municipality:
            kw.update(city=countries.Place.lookup_or_create(
                'name', data.municipality, country=country))

        def sex2gender(sex):
            if sex == 'MALE':
                return dd.Genders.male
            if sex == 'FEMALE':
                return dd.Genders.female
            logger.warning("%s : invalid gender code %r", msg1, sex)

        kw.update(gender=sex2gender(data.gender))

        def doctype2cardtype(dt):
            #~ if dt == 1: return BeIdCardTypes.get_by_value("1")
            rv = BeIdCardTypes.get_by_value(str(dt))
            # logger.info("20130103 documentType %r --> %r", dt, rv)
            return rv

        kw.update(card_type=doctype2cardtype(data.documentType))

        if config.data_collector_dir:
            logger.info("Gonna write raw eid card data: %r", raw_data)
            fn = os.path.join(config.data_collector_dir, card_number + '.txt')
            file(fn, "w").write(raw_data.encode('utf-8'))
            logger.info("Wrote eid card data to file %s", fn)

        return kw
Exemple #10
0
    def card2client(cls, data):
        "does the actual conversion"

        self = cls

        from lino.utils import ssin
        from lino.api import dd, rt
        from lino.mixins.beid import BeIdCardTypes
        from lino.utils import join_words
        from lino.utils import IncompleteDate
        from lino.modlib.contacts.utils import street2kw

        countries = dd.resolve_app('countries', strict=True)

        kw = dict()
        #~ def func(fldname,qname):
        #~ kw[fldname] = data[qname]
        kw.update(national_id=ssin.format_ssin(data['nationalNumber']))
        kw.update(first_name=join_words(data['firstName1'], data['firstName2'],
                                        data['firstName3']))
        #~ func('first_name','firstName1')
        kw.update(last_name=data['surname'])

        card_number = data['cardNumber']

        if 'picture' in data:
            fn = self.card_number_to_picture_file(card_number)
            if os.path.exists(fn):
                logger.warning("Overwriting existing image file %s.", fn)
            fp = file(fn, 'wb')
            fp.write(base64.b64decode(data['picture']))
            fp.close()
            #~ print 20121117, repr(data['picture'])
            #~ kw.update(picture_data_encoded=data['picture'])

        #~ func('card_valid_from','validityBeginDate')
        #~ func('card_valid_until','validityEndDate')
        #~ func('birth_date','birthDate')
        kw.update(birth_date=IncompleteDate(
            *settings.SITE.parse_date(data['birthDate'])))
        kw.update(card_valid_from=datetime.date(
            *settings.SITE.parse_date(data['validityBeginDate'])))
        kw.update(card_valid_until=datetime.date(
            *settings.SITE.parse_date(data['validityEndDate'])))
        kw.update(card_number=card_number)
        kw.update(card_issuer=data['issuingMunicipality'])
        kw.update(noble_condition=data['nobleCondition'])
        kw.update(street=data['street'])
        kw.update(street_no=data['streetNumber'])
        kw.update(street_box=data['boxNumber'])
        if kw['street'] and not (kw['street_no'] or kw['street_box']):
            kw = street2kw(kw['street'], **kw)
        kw.update(zip_code=data['zipCode'])
        kw.update(birth_place=data['birthLocation'])
        pk = data['country'].upper()

        msg1 = "BeIdReadCardToClientAction %s" % kw.get('national_id')

        #~ try:
        country = countries.Country.objects.get(isocode=pk)
        kw.update(country=country)
        #~ except countries.Country.DoesNotExist,e:
        #~ except Exception,e:
        #~ logger.warning("%s : no country with code %r",msg1,pk)
        #~ BE = countries.Country.objects.get(isocode='BE')
        #~ fld = countries.Place._meta.get_field()
        kw.update(city=countries.Place.lookup_or_create(
            'name', data['municipality'], country=country))

        def sex2gender(sex):
            if sex == 'M':
                return dd.Genders.male
            if sex in 'FVW':
                return dd.Genders.female
            logger.warning("%s : invalid gender code %r", msg1, sex)

        kw.update(gender=sex2gender(data['sex']))

        if False:

            def nationality2country(nationality):
                try:
                    return countries.Country.objects.get(
                        nationalities__icontains=nationality)
                except countries.Country.DoesNotExist, e:
                    logger.warning("%s : no country for nationality %r", msg1,
                                   nationality)
                except MultipleObjectsReturned, e:
                    logger.warning(
                        "%s : found more than one country for nationality %r",
                        msg1, nationality)
Exemple #11
0
    def card2client(self, data):
        """Convert the data coming from the card into database fields to be
        stored in the card holder.

        """
        countries = dd.resolve_app('countries', strict=True)

        kw = dict()
        raw_data = data['card_data']
        if not '\n' in raw_data:
            # a one-line string means that some error occured (e.g. no
            # card in reader). of course we want to show this to the
            # user.
            raise Warning(raw_data)

        #~ print cd
        data = AttrDict(yaml.load(raw_data))
        #~ raise Exception("20131108 cool: %s" % cd)

        kw.update(national_id=ssin.format_ssin(str(data.nationalNumber)))
        kw.update(first_name=data.firstName or '')
        kw.update(middle_name=data.middleName or '')
        kw.update(last_name=data.name or '')

        card_number = str(data.cardNumber)

        if data.photo:
            if not card_number:
                raise Exception("20150730 photo data but no card_number ")
            fn = get_image_path(card_number)
            if fn.exists():
                logger.warning("Overwriting existing image file %s.", fn)
            try:
                fp = file(fn, 'wb')
                fp.write(base64.b64decode(data.photo))
                fp.close()
            except IOError as e:
                logger.warning("Failed to store image file %s : %s", fn, e)
                
            #~ print 20121117, repr(data['picture'])
            #~ kw.update(picture_data_encoded=data['picture'])

        if isinstance(data.dateOfBirth, basestring):
            data.dateOfBirth = IncompleteDate(*data.dateOfBirth.split('-'))
        kw.update(birth_date=data.dateOfBirth)
        kw.update(card_valid_from=data.cardValidityDateBegin)
        kw.update(card_valid_until=data.cardValidityDateEnd)

        kw.update(card_number=card_number)
        kw.update(card_issuer=data.cardDeliveryMunicipality)
        if data.nobleCondition:
            kw.update(noble_condition=data.nobleCondition)
        if data.streetAndNumber:
            # kw.update(street=data.streetAndNumber)
            kw = street2kw(data.streetAndNumber, **kw)
        if data.zip:
            kw.update(zip_code=str(data.zip))
        if data.placeOfBirth:
            kw.update(birth_place=data.placeOfBirth)
        pk = data.reader.upper()

        msg1 = "BeIdReadCardToClientAction %s" % kw.get('national_id')

        country = countries.Country.objects.get(isocode=pk)
        kw.update(country=country)
        if data.municipality:
            kw.update(city=countries.Place.lookup_or_create(
                'name', data.municipality, country=country))

        def sex2gender(sex):
            if sex == 'MALE':
                return dd.Genders.male
            if sex == 'FEMALE':
                return dd.Genders.female
            logger.warning("%s : invalid gender code %r", msg1, sex)
        kw.update(gender=sex2gender(data.gender))

        def doctype2cardtype(dt):
            #~ if dt == 1: return BeIdCardTypes.get_by_value("1")
            rv = BeIdCardTypes.get_by_value(str(dt))
            # logger.info("20130103 documentType %r --> %r", dt, rv)
            return rv
        kw.update(card_type=doctype2cardtype(data.documentType))

        if config.data_collector_dir:
            logger.info("Gonna write raw eid card data: %r", raw_data)
            fn = os.path.join(
                config.data_collector_dir,
                card_number + '.txt')
            file(fn, "w").write(raw_data.encode('utf-8'))
            logger.info("Wrote eid card data to file %s", fn)

        return kw