20131002 (Wednesday, 02 October 2013)

Miscellaneous little changes in Lino Welfare

  • jobs.JobsOverview now displays full name and partner id of clients.

  • Changed the logging level from debug to info for certain messages in watch_tim. Principally every modification made by watch_tim should get logged.

  • Fixed a test failure which occurred when use_eid_jslib is False and a client must read eID card. This means that Lino is not able to read the card himself. The warning should still appear, but should not be clickable.

  • TableRequest.__repr__() failed when setup() had not yet been called. Added class attributes filter and known_value with default value None.

Checkin and pull on Online demo sites.

Fixed a bug which caused the wrong layout manager of the parameters panel when the panel had only one row of fields. In that case it needs a hbox layout. Force a form layout only when there is either exactly 1 field or several hboxes.

Reading Excel files

"""
Custom-specific script to import pupils into :ref:`faggio`
from an .xls file. To be invoked using something like::

  python manage.py run /path/lino/blog/2013/1002.py Input_file.xls
  


"""

import sys

import logging
logger = logging.getLogger('lino')

from dateutil.parser import parse as parse_date

from lino.utils import iif
from lino.utils import IncompleteDate
from xlrd import open_workbook, xldate_as_tuple

from lino_xl.lib.contacts.utils import street2kw

from lino.api.shell import courses, countries
from lino.api import dd, rt
from lino.core.utils import is_valid_email


def string2date(s):
    return parse_date(s,fuzzy=True)

class MyBook():

    column_headers = """
    Nr.
    Titel
    Name
    Vorname
    Strasse
    PLZ
    Ort
    -
    Handy
    GEBURTSTAG
    BEZ.
    Datum
    -
    COK-MG
    E-Mail
    """.split()
    column_headers = [iif(x=='-','',x) for x in column_headers]


    def date_converter(self,v):
        if not v: return None
        if isinstance(v,basestring):
            v = string2date(v)
            return IncompleteDate.from_date(v)
            #~ return v
        t = xldate_as_tuple(v,self.book.datemode)
        assert len(t) == 6
        t = t[:3]
        #~ print t
        return IncompleteDate(*t)

    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.Place.objects.get(name)
            kw.update(city=countries.Place.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)
        

    def __init__(self,filename):
        #~ filename = '/home/luc/Downloads/Eiche 2013.xls'

        self.country = countries.Country.objects.get(isocode="BE")
        self.book = open_workbook(filename)
        s = self.book.sheet_by_index(0)
        #~ print 'Sheet:',s.name
        found = False
        ncols = len(self.column_headers)
        for row in range(s.nrows):
            values = [s.cell(row,col).value for col in range(ncols)]
            if found:
                obj = self.row2instance(*values)
                obj.full_clean()
                obj.save()
                logger.info("%s has been saved",obj)
            elif values == self.column_headers:
                found = True
            elif row < 5:
                print "Ignored line %s" % values
       
if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "must specify input filename as argument.", sys.argv
        sys.exit(-1)
    MyBook(sys.argv[1])

Adding users

"""
How to add users to your Lino database::

  $ python manage.py run /path/to/lino/blog/2013/1002b.py

"""

from django.conf import settings
from lino.modlib.users.choicelists import UserProfiles

User = settings.SITE.user_model
u = User(username="rolf", profile=UserProfiles.admin)
u.save()
u.set_password('1234')