Ejemplo n.º 1
0
class FronterExport(LMSExport):
    """Class for handling export to Fronter."""

    STATUS_ADD = 1
    STATUS_UPDATE = 2
    STATUS_DELETE = 3

    ROLE_READ = '01'
    ROLE_WRITE = '02'
    ROLE_DELETE = '03'
    ROLE_CHANGE = '07'

    def __init__(self, output=None, include_password=True):
        LMSExport.__init__(self)
        self.output = output
        self.xml = XMLWriter(output)
        self.xml.startDocument(encoding='UTF-8')
        self.rootEl = 'enterprise'
        self.DataSource = 'Cerebrum'
        self.include_password = include_password
        #self.cf_id = self.fronter.fronter_host # TODO: Get this somewhere else
        logger.debug("Fronterexport initialized")


    def pwd(self, p):
        pwtype, password = p.split(":")
        type_map = {'md5': 1,
                    'unix': 2,
                    'nt': 3,
                    'plain': 4,
                    'ldap': 5}
        ret = {'pwencryptiontype': type_map['ldap']}
        if password:
            ret['password'] = password
        return ret


    def useraccess(self, access):
        # TODO: move to config section
        mapping = {
            # Not allowed to log in
            0: 'None',
            # Normal user
            'viewmygroups': 'User',
            'allowlogin': '******',
            # Admin
            'administrator': 'SysAdmin',
            }
        return mapping[access]


    def start_xml_file(self, kurs):
        self.xml.comment("Eksporterer data om følgende emner:\n  " + 
                         "\n  ".join(kurs))
        self.xml.startTag(self.rootEl)
        self.xml.startTag('properties')
        self.xml.dataElement('datasource', self.DataSource)
        self.xml.dataElement('target', "ClassFronter/")#%s" % self.cf_id)
        # :TODO: Tell Fronter (again) that they need to define the set of
        # codes for the TYPE element.
        # self.xml.dataElement('TYPE', "REFRESH")
        self.xml.dataElement('datetime', time.strftime("%F %T %z"))
        self.xml.endTag('properties')

    begin = start_xml_file

    def user_to_XML(self, id, recstatus, data):
        """Lager XML for en person"""
        self.xml.startTag('person', {'recstatus': recstatus})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', id)
        self.xml.endTag('sourcedid')
        if self.include_password:
            self.xml.dataElement('userid', id,
                                 self.pwd(data['PASSWORD']))
        self.xml.startTag('name')
        self.xml.dataElement('fn',
                             " ".join([x for x in (data['GIVEN'],
                                                   data['FAMILY'])
                                       if x]))
        self.xml.startTag('n')
        self.xml.dataElement('family', data['FAMILY'])
        self.xml.dataElement('given', data['GIVEN'])
        self.xml.endTag('n')
        self.xml.endTag('name')
        self.xml.dataElement('email', data['EMAIL'])
        self.xml.emptyTag('systemrole',
                          {'systemroletype':
                           self.useraccess(data['USERACCESS'])})
        self.xml.startTag('extension')
        self.xml.emptyTag('emailsettings',
                          {'mail_username': id,
                           'mail_password': '******',
                           'description': 'UiO-email',
                           'mailserver': 'imap.uio.no',
                           'mailtype': 'imap',
                           'imap_serverdirectory': 'INBOX.',
                           'imap_sentfolder': 'Sent',
                           'imap_draftfolder': 'Drafts',
                           'imap_trashfolder': 'Trash',
                           'use_ssl': 1,
                           'defaultmailbox': 'INBOX',
                           'on_delete_action': 'trash',
                           'is_primary': 1,
                           })
        self.xml.endTag('extension')
        self.xml.endTag('person')


    def group_to_XML(self, id, recstatus, data):
        # Lager XML for en gruppe
        self.xml.startTag('group', {'recstatus': recstatus})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', id)
        self.xml.endTag('sourcedid')
        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', 'FronterStructure1.0')
        allow_room = data.get('allow_room', 0)
        allow_contact = data.get('allow_contact', 0)
        # Convert booleans allow_room and allow_contact to bits
        allow_room = allow_room and 1 or 0
        allow_contact = allow_contact and 2 or 0
        self.xml.emptyTag('typevalue',
                          {'level': allow_room | allow_contact})
        self.xml.endTag('grouptype')
        self.xml.startTag('description')
        if (len(data['title']) > 60):
            self.xml.emptyTag('short')
            self.xml.dataElement('long', data['title'])
        else:
            self.xml.dataElement('short', data['title'])
        self.xml.endTag('description')
        self.xml.startTag('relationship', {'relation': 1})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', data['parent'])
        self.xml.endTag('sourcedid')
        self.xml.emptyTag('label')
        self.xml.endTag('relationship')
        self.xml.endTag('group')


    def room_to_XML(self, id, recstatus, data):
        # Lager XML for et rom
        #
        # Gamle rom skal aldri slettes automatisk.
        if recstatus == FronterExport.STATUS_DELETE:
            return 
        self.xml.startTag('group', {'recstatus': recstatus})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', id)
        self.xml.endTag('sourcedid')
        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', 'FronterStructure1.0')
        self.xml.emptyTag('typevalue', {'level': 4})
        self.xml.endTag('grouptype')
        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', 'Roomprofile1.0')
        self.xml.emptyTag('typevalue', {'level': data['profile']})
        self.xml.endTag('grouptype')
        self.xml.startTag('description')
        if (len(data['title']) > 60):
            self.xml.emptyTag('short')
            self.xml.dataElement('long', data['title'])
        else:
            self.xml.dataElement('short', data['title'])
        self.xml.endTag('description')
        self.xml.startTag('relationship', {'relation': 1})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', data['parent'])
        self.xml.endTag('sourcedid')
        self.xml.emptyTag('label')
        self.xml.endTag('relationship')
        self.xml.endTag('group')

    def personmembers_to_XML(self, gid, recstatus, members):
        # lager XML av medlemer
        self.xml.startTag('membership')
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', gid)
        self.xml.endTag('sourcedid')
        for uname in members:
            self.xml.startTag('member')
            self.xml.startTag('sourcedid')
            self.xml.dataElement('source', self.DataSource)
            self.xml.dataElement('id', uname)
            self.xml.endTag('sourcedid')
            # This is a person member (as opposed to a group).
            self.xml.dataElement('idtype', '1')
            self.xml.startTag('role', {'recstatus': recstatus,
                                       'roletype': FronterExport.ROLE_READ})
            self.xml.dataElement('status', '1')
            self.xml.startTag('extension')
            # Member of group, not room.
            self.xml.emptyTag('memberof', {'type': 1})
            self.xml.endTag('extension')
            self.xml.endTag('role')
            self.xml.endTag('member')
        self.xml.endTag('membership')

    def acl_to_XML(self, node, recstatus, groups):
        self.xml.startTag('membership')
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', node)
        self.xml.endTag('sourcedid')
        for gname in groups.keys():
            self.xml.startTag('member')
            self.xml.startTag('sourcedid')
            self.xml.dataElement('source', self.DataSource)
            self.xml.dataElement('id', gname)
            self.xml.endTag('sourcedid')
            # The following member ids are groups.
            self.xml.dataElement('idtype', '2')
            acl = groups[gname]
            if acl.has_key('role'):
                self.xml.startTag('role', {'recstatus': recstatus,
                                           'roletype': acl['role']})
                self.xml.dataElement('status', '1')
                self.xml.startTag('extension')
                self.xml.emptyTag('memberof', {'type': 2}) # Member of room.
            else:
                self.xml.startTag('role', {'recstatus': recstatus})
                self.xml.dataElement('status', '1')
                self.xml.startTag('extension')
                self.xml.emptyTag('memberof', {'type': 1}) # Member of group.
                self.xml.emptyTag('groupaccess',
                                  {'contactAccess': acl['gacc'],
                                   'roomAccess': acl['racc']})
            self.xml.endTag('extension')
            self.xml.endTag('role')
            self.xml.endTag('member')
        self.xml.endTag('membership')

    def end(self):
        self.xml.endTag(self.rootEl)
        self.xml.endDocument()
Ejemplo n.º 2
0
class FronterExport(LMSExport):
    """Class for handling export to Fronter."""

    STATUS_ADD = 1
    STATUS_UPDATE = 2
    STATUS_DELETE = 3

    ROLE_READ = '01'
    ROLE_WRITE = '02'
    ROLE_DELETE = '03'
    ROLE_CHANGE = '07'

    def __init__(self, output=None, include_password=True):
        LMSExport.__init__(self)
        self.output = output
        self.xml = XMLWriter(output)
        self.xml.startDocument(encoding='UTF-8')
        self.rootEl = 'enterprise'
        self.DataSource = 'Cerebrum'
        self.include_password = include_password
        #self.cf_id = self.fronter.fronter_host # TODO: Get this somewhere else
        logger.debug("Fronterexport initialized")

    def pwd(self, p):
        pwtype, password = p.split(":")
        type_map = {'md5': 1, 'unix': 2, 'nt': 3, 'plain': 4, 'ldap': 5}
        ret = {'pwencryptiontype': type_map['ldap']}
        if password:
            ret['password'] = password
        return ret

    def useraccess(self, access):
        # TODO: move to config section
        mapping = {
            # Not allowed to log in
            0: 'None',
            # Normal user
            'viewmygroups': 'User',
            'allowlogin': '******',
            # Admin
            'administrator': 'SysAdmin',
        }
        return mapping[access]

    def start_xml_file(self, kurs):
        self.xml.comment("Eksporterer data om følgende emner:\n  " +
                         "\n  ".join(kurs))
        self.xml.startTag(self.rootEl)
        self.xml.startTag('properties')
        self.xml.dataElement('datasource', self.DataSource)
        self.xml.dataElement('target', "ClassFronter/")  #%s" % self.cf_id)
        # :TODO: Tell Fronter (again) that they need to define the set of
        # codes for the TYPE element.
        # self.xml.dataElement('TYPE', "REFRESH")
        self.xml.dataElement('datetime', time.strftime("%F %T %z"))
        self.xml.endTag('properties')

    begin = start_xml_file

    def user_to_XML(self, id, recstatus, data):
        """Lager XML for en person"""
        self.xml.startTag('person', {'recstatus': recstatus})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', id)
        self.xml.endTag('sourcedid')
        if self.include_password:
            self.xml.dataElement('userid', id, self.pwd(data['PASSWORD']))
        self.xml.startTag('name')
        self.xml.dataElement(
            'fn', " ".join([x for x in (data['GIVEN'], data['FAMILY']) if x]))
        self.xml.startTag('n')
        self.xml.dataElement('family', data['FAMILY'])
        self.xml.dataElement('given', data['GIVEN'])
        self.xml.endTag('n')
        self.xml.endTag('name')
        self.xml.dataElement('email', data['EMAIL'])
        self.xml.emptyTag(
            'systemrole',
            {'systemroletype': self.useraccess(data['USERACCESS'])})
        self.xml.startTag('extension')
        self.xml.emptyTag(
            'emailsettings', {
                'mail_username': id,
                'mail_password': '******',
                'description': 'UiO-email',
                'mailserver': 'imap.uio.no',
                'mailtype': 'imap',
                'imap_serverdirectory': 'INBOX.',
                'imap_sentfolder': 'Sent',
                'imap_draftfolder': 'Drafts',
                'imap_trashfolder': 'Trash',
                'use_ssl': 1,
                'defaultmailbox': 'INBOX',
                'on_delete_action': 'trash',
                'is_primary': 1,
            })
        self.xml.endTag('extension')
        self.xml.endTag('person')

    def group_to_XML(self, id, recstatus, data):
        # Lager XML for en gruppe
        self.xml.startTag('group', {'recstatus': recstatus})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', id)
        self.xml.endTag('sourcedid')
        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', 'FronterStructure1.0')
        allow_room = data.get('allow_room', 0)
        allow_contact = data.get('allow_contact', 0)
        # Convert booleans allow_room and allow_contact to bits
        allow_room = allow_room and 1 or 0
        allow_contact = allow_contact and 2 or 0
        self.xml.emptyTag('typevalue', {'level': allow_room | allow_contact})
        self.xml.endTag('grouptype')
        self.xml.startTag('description')
        if (len(data['title']) > 60):
            self.xml.emptyTag('short')
            self.xml.dataElement('long', data['title'])
        else:
            self.xml.dataElement('short', data['title'])
        self.xml.endTag('description')
        self.xml.startTag('relationship', {'relation': 1})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', data['parent'])
        self.xml.endTag('sourcedid')
        self.xml.emptyTag('label')
        self.xml.endTag('relationship')
        self.xml.endTag('group')

    def room_to_XML(self, id, recstatus, data):
        # Lager XML for et rom
        #
        # Gamle rom skal aldri slettes automatisk.
        if recstatus == FronterExport.STATUS_DELETE:
            return
        self.xml.startTag('group', {'recstatus': recstatus})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', id)
        self.xml.endTag('sourcedid')
        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', 'FronterStructure1.0')
        self.xml.emptyTag('typevalue', {'level': 4})
        self.xml.endTag('grouptype')
        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', 'Roomprofile1.0')
        self.xml.emptyTag('typevalue', {'level': data['profile']})
        self.xml.endTag('grouptype')
        self.xml.startTag('description')
        if (len(data['title']) > 60):
            self.xml.emptyTag('short')
            self.xml.dataElement('long', data['title'])
        else:
            self.xml.dataElement('short', data['title'])
        self.xml.endTag('description')
        self.xml.startTag('relationship', {'relation': 1})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', data['parent'])
        self.xml.endTag('sourcedid')
        self.xml.emptyTag('label')
        self.xml.endTag('relationship')
        self.xml.endTag('group')

    def personmembers_to_XML(self, gid, recstatus, members):
        # lager XML av medlemer
        self.xml.startTag('membership')
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', gid)
        self.xml.endTag('sourcedid')
        for uname in members:
            self.xml.startTag('member')
            self.xml.startTag('sourcedid')
            self.xml.dataElement('source', self.DataSource)
            self.xml.dataElement('id', uname)
            self.xml.endTag('sourcedid')
            # This is a person member (as opposed to a group).
            self.xml.dataElement('idtype', '1')
            self.xml.startTag('role', {
                'recstatus': recstatus,
                'roletype': FronterExport.ROLE_READ
            })
            self.xml.dataElement('status', '1')
            self.xml.startTag('extension')
            # Member of group, not room.
            self.xml.emptyTag('memberof', {'type': 1})
            self.xml.endTag('extension')
            self.xml.endTag('role')
            self.xml.endTag('member')
        self.xml.endTag('membership')

    def acl_to_XML(self, node, recstatus, groups):
        self.xml.startTag('membership')
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', node)
        self.xml.endTag('sourcedid')
        for gname in groups.keys():
            self.xml.startTag('member')
            self.xml.startTag('sourcedid')
            self.xml.dataElement('source', self.DataSource)
            self.xml.dataElement('id', gname)
            self.xml.endTag('sourcedid')
            # The following member ids are groups.
            self.xml.dataElement('idtype', '2')
            acl = groups[gname]
            if acl.has_key('role'):
                self.xml.startTag('role', {
                    'recstatus': recstatus,
                    'roletype': acl['role']
                })
                self.xml.dataElement('status', '1')
                self.xml.startTag('extension')
                self.xml.emptyTag('memberof', {'type': 2})  # Member of room.
            else:
                self.xml.startTag('role', {'recstatus': recstatus})
                self.xml.dataElement('status', '1')
                self.xml.startTag('extension')
                self.xml.emptyTag('memberof', {'type': 1})  # Member of group.
                self.xml.emptyTag('groupaccess', {
                    'contactAccess': acl['gacc'],
                    'roomAccess': acl['racc']
                })
            self.xml.endTag('extension')
            self.xml.endTag('role')
            self.xml.endTag('member')
        self.xml.endTag('membership')

    def end(self):
        self.xml.endTag(self.rootEl)
        self.xml.endDocument()
Ejemplo n.º 3
0
class ILExport(LMSExport):
    """Class for handling export to It's Learning."""

    gender_values = {"F": "1",
                     "M": "2",
                     "X": None}
    "This contains the mapping from Cerebrum's gender-codes to It's Learning's."

    def __init__(self, output=None):
        LMSExport.__init__(self)
        self.xml = XMLWriter(output)
        self.xml.startDocument(encoding="ISO-8859-1")
        self.xml.notationDecl("enterprise",
                              public_id="IMS Enterprise/LMS Interoperability DTD",
                              system_id="http://www.fs.usit.uio.no/DTD/ims_epv1p1.dtd")
        self.rootEl = 'enterprise'
        self.DataSource = 'Cerebrum'
        logger.debug("ItsLearningexport initialized")


    def begin(self):
        self.xml.startTag(self.rootEl)
        self.xml.startTag('properties', {'lang': 'NO'})
        self.xml.dataElement('datasource', self.DataSource)
        self.xml.dataElement('datetime', time.strftime("%F"))
        self.xml.endTag('properties')

        # Need to have root-group defined
        self.xml.startTag('group', {'recstatus': 1})
        
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', self.IDstcode)
        self.xml.endTag('sourcedid')

        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', 'NO-FS', {'level': 0})
        self.xml.dataElement('typevalue', 'Sted')
        self.xml.endTag('grouptype')

        self.xml.startTag('description')
        self.xml.dataElement('short', self.IDshort)
        self.xml.dataElement('long', self.ID)
        self.xml.dataElement('full', self.ID)
        self.xml.endTag('description')

        self.xml.startTag('org')
        self.xml.dataElement('orgname', self.ID)
        self.xml.dataElement('orgunit', self.ID)
        self.xml.dataElement('type', self.org_type)
        self.xml.dataElement('id', self.IDcode)
        self.xml.endTag('org')

        self.xml.dataElement('email', "")
        self.xml.dataElement('url', "")

        self.xml.startTag('relationship', {'relation': 1})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', self.IDstcode)
        self.xml.endTag('sourcedid')
        self.xml.dataElement('label', 'Sted')
        self.xml.endTag('relationship')
        
        self.xml.endTag('group')
        
        
    def end(self):
        self.xml.endTag(self.rootEl)
        self.xml.endDocument()


    def group_to_xml(self, id, grouptype, parentcode,
                     grouptype_level=None, nameshort="", namelong="", namefull=""):
        self.xml.startTag('group', {'recstatus': 1})
        
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', id)
        self.xml.endTag('sourcedid')

        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', "NO-FS")
        level = {}
        if grouptype_level is not None:
            level["level"] = grouptype_level
        self.xml.dataElement('typevalue', grouptype, level)
        self.xml.endTag('grouptype')

        self.xml.startTag('description')
        self.xml.dataElement('short', nameshort)
        self.xml.dataElement('long', namelong)
        self.xml.dataElement('full', namefull)
        self.xml.endTag('description')

        self.xml.startTag('org')
        self.xml.dataElement('orgname', self.ID)
        self.xml.dataElement('orgunit', self.ID)
        self.xml.dataElement('type', self.org_type)
        self.xml.dataElement('id', self.IDcode)
        self.xml.endTag('org')

        self.xml.dataElement('email', "")
        self.xml.dataElement('url', "")

        self.xml.startTag('relationship', {'relation': 1})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', parentcode)
        self.xml.endTag('sourcedid')
        self.xml.dataElement('label', grouptype)
        self.xml.endTag('relationship')

        self.xml.endTag('group')


    def export_people(self):
        self.export_students()
        self.export_faculty()


    def export_faculty(self):
        for fnr in self.faculty.keys():
            faculty = self.faculty[fnr]
            sort_name = "%s %s" % (faculty["family_name"], faculty["given_name"])
            logger.debug("Exporting data for person with username '%s'" % faculty["username"])
            self.person_to_xml(faculty["username"], account_name=faculty["username"],
                               institution_roletype="Faculty",
                               full_name=faculty["full_name"], sort_name=sort_name,
                               family_name=faculty["family_name"], given_name=faculty["given_name"],
                               gender=faculty["gender"], email=faculty["email"],
                               birth_date=faculty["birth_date"])
    


    def export_students(self):
        for fnr in self.students.keys():
            student = self.students[fnr]
            sort_name = "%s %s" % (student["family_name"], student["given_name"])
            logger.debug("Exporting data for person with username '%s'" % student["username"])
            self.person_to_xml(student["username"], account_name=student["username"],
                               institution_roletype="Student",
                               full_name=student["full_name"], sort_name=sort_name,
                               family_name=student["family_name"], given_name=student["given_name"],
                               gender=student["gender"], email=student["email"],
                               birth_date=student["birth_date"])
    


    def person_to_xml(self, id, account_name="", institution_roletype="Student",
                      full_name="", sort_name="", family_name="", given_name="",
                      gender=None, email=None, birth_date=""):
        self.xml.startTag('person', {'recstatus': 2})
        
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', str(id))
        self.xml.endTag('sourcedid')

        self.xml.dataElement('userid', account_name)

        self.xml.startTag('name')
        self.xml.dataElement('fn', full_name)
        self.xml.dataElement('sort', sort_name)
        self.xml.startTag('n')
        self.xml.dataElement('family', family_name)
        self.xml.dataElement('given', given_name)
        self.xml.endTag('n')
        self.xml.endTag('name')
        
        self.xml.startTag('demographics') # Is this necessary
        if ILExport.gender_values[gender] is not None:
            self.xml.dataElement('gender', ILExport.gender_values[gender])
        self.xml.dataElement('bday', birth_date)
        self.xml.endTag('demographics')

        if email is not None:
            self.xml.dataElement('email', email)
            
##         self.xml.startTag('adr') # May be added when SAP-data is available
##         self.xml.startTag('tel') # May be added when SAP-data is available

        self.xml.emptyTag('institutionrole',
                          {'primaryrole': 'yes',
                           'institutionroletype': institution_roletype})

        self.xml.endTag('person')


    def membership_to_xml(self, id, responsibles, students):

        self.xml.startTag('membership')

        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', str(id))
        self.xml.endTag('sourcedid')

        for member in responsibles:
            self.member_to_xml(member, "02")
        for member in students:
            self.member_to_xml(member, "01")

        self.xml.endTag('membership')



    def member_to_xml(self, id, roletype):
        
        self.xml.startTag('member')
        
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', str(id))
        self.xml.endTag('sourcedid')

        self.xml.dataElement('idtype', "1")

        self.xml.startTag('role', {'roletype': roletype, 'recstatus': "1"})
        self.xml.dataElement('subrole', "")
        self.xml.dataElement('status', "1")
        self.xml.startTag('timeframe')
        self.xml.dataElement('begin', "", {'restrict': '0'})
        self.xml.dataElement('end', "", {'restrict': '0'})
        self.xml.endTag('timeframe')
        self.xml.endTag('role')

        self.xml.endTag('member')
Ejemplo n.º 4
0
class ILExport(LMSExport):
    """Class for handling export to It's Learning."""

    gender_values = {"F": "1", "M": "2", "X": None}
    "This contains the mapping from Cerebrum's gender-codes to It's Learning's."

    def __init__(self, output=None):
        LMSExport.__init__(self)
        self.xml = XMLWriter(output)
        self.xml.startDocument(encoding="ISO-8859-1")
        self.xml.notationDecl(
            "enterprise",
            public_id="IMS Enterprise/LMS Interoperability DTD",
            system_id="http://www.fs.usit.uio.no/DTD/ims_epv1p1.dtd")
        self.rootEl = 'enterprise'
        self.DataSource = 'Cerebrum'
        logger.debug("ItsLearningexport initialized")

    def begin(self):
        self.xml.startTag(self.rootEl)
        self.xml.startTag('properties', {'lang': 'NO'})
        self.xml.dataElement('datasource', self.DataSource)
        self.xml.dataElement('datetime', time.strftime("%F"))
        self.xml.endTag('properties')

        # Need to have root-group defined
        self.xml.startTag('group', {'recstatus': 1})

        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', self.IDstcode)
        self.xml.endTag('sourcedid')

        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', 'NO-FS', {'level': 0})
        self.xml.dataElement('typevalue', 'Sted')
        self.xml.endTag('grouptype')

        self.xml.startTag('description')
        self.xml.dataElement('short', self.IDshort)
        self.xml.dataElement('long', self.ID)
        self.xml.dataElement('full', self.ID)
        self.xml.endTag('description')

        self.xml.startTag('org')
        self.xml.dataElement('orgname', self.ID)
        self.xml.dataElement('orgunit', self.ID)
        self.xml.dataElement('type', self.org_type)
        self.xml.dataElement('id', self.IDcode)
        self.xml.endTag('org')

        self.xml.dataElement('email', "")
        self.xml.dataElement('url', "")

        self.xml.startTag('relationship', {'relation': 1})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', self.IDstcode)
        self.xml.endTag('sourcedid')
        self.xml.dataElement('label', 'Sted')
        self.xml.endTag('relationship')

        self.xml.endTag('group')

    def end(self):
        self.xml.endTag(self.rootEl)
        self.xml.endDocument()

    def group_to_xml(self,
                     id,
                     grouptype,
                     parentcode,
                     grouptype_level=None,
                     nameshort="",
                     namelong="",
                     namefull=""):
        self.xml.startTag('group', {'recstatus': 1})

        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', id)
        self.xml.endTag('sourcedid')

        self.xml.startTag('grouptype')
        self.xml.dataElement('scheme', "NO-FS")
        level = {}
        if grouptype_level is not None:
            level["level"] = grouptype_level
        self.xml.dataElement('typevalue', grouptype, level)
        self.xml.endTag('grouptype')

        self.xml.startTag('description')
        self.xml.dataElement('short', nameshort)
        self.xml.dataElement('long', namelong)
        self.xml.dataElement('full', namefull)
        self.xml.endTag('description')

        self.xml.startTag('org')
        self.xml.dataElement('orgname', self.ID)
        self.xml.dataElement('orgunit', self.ID)
        self.xml.dataElement('type', self.org_type)
        self.xml.dataElement('id', self.IDcode)
        self.xml.endTag('org')

        self.xml.dataElement('email', "")
        self.xml.dataElement('url', "")

        self.xml.startTag('relationship', {'relation': 1})
        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', parentcode)
        self.xml.endTag('sourcedid')
        self.xml.dataElement('label', grouptype)
        self.xml.endTag('relationship')

        self.xml.endTag('group')

    def export_people(self):
        self.export_students()
        self.export_faculty()

    def export_faculty(self):
        for fnr in self.faculty.keys():
            faculty = self.faculty[fnr]
            sort_name = "%s %s" % (faculty["family_name"],
                                   faculty["given_name"])
            logger.debug("Exporting data for person with username '%s'" %
                         faculty["username"])
            self.person_to_xml(faculty["username"],
                               account_name=faculty["username"],
                               institution_roletype="Faculty",
                               full_name=faculty["full_name"],
                               sort_name=sort_name,
                               family_name=faculty["family_name"],
                               given_name=faculty["given_name"],
                               gender=faculty["gender"],
                               email=faculty["email"],
                               birth_date=faculty["birth_date"])

    def export_students(self):
        for fnr in self.students.keys():
            student = self.students[fnr]
            sort_name = "%s %s" % (student["family_name"],
                                   student["given_name"])
            logger.debug("Exporting data for person with username '%s'" %
                         student["username"])
            self.person_to_xml(student["username"],
                               account_name=student["username"],
                               institution_roletype="Student",
                               full_name=student["full_name"],
                               sort_name=sort_name,
                               family_name=student["family_name"],
                               given_name=student["given_name"],
                               gender=student["gender"],
                               email=student["email"],
                               birth_date=student["birth_date"])

    def person_to_xml(self,
                      id,
                      account_name="",
                      institution_roletype="Student",
                      full_name="",
                      sort_name="",
                      family_name="",
                      given_name="",
                      gender=None,
                      email=None,
                      birth_date=""):
        self.xml.startTag('person', {'recstatus': 2})

        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', str(id))
        self.xml.endTag('sourcedid')

        self.xml.dataElement('userid', account_name)

        self.xml.startTag('name')
        self.xml.dataElement('fn', full_name)
        self.xml.dataElement('sort', sort_name)
        self.xml.startTag('n')
        self.xml.dataElement('family', family_name)
        self.xml.dataElement('given', given_name)
        self.xml.endTag('n')
        self.xml.endTag('name')

        self.xml.startTag('demographics')  # Is this necessary
        if ILExport.gender_values[gender] is not None:
            self.xml.dataElement('gender', ILExport.gender_values[gender])
        self.xml.dataElement('bday', birth_date)
        self.xml.endTag('demographics')

        if email is not None:
            self.xml.dataElement('email', email)


##         self.xml.startTag('adr') # May be added when SAP-data is available
##         self.xml.startTag('tel') # May be added when SAP-data is available

        self.xml.emptyTag('institutionrole', {
            'primaryrole': 'yes',
            'institutionroletype': institution_roletype
        })

        self.xml.endTag('person')

    def membership_to_xml(self, id, responsibles, students):

        self.xml.startTag('membership')

        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', str(id))
        self.xml.endTag('sourcedid')

        for member in responsibles:
            self.member_to_xml(member, "02")
        for member in students:
            self.member_to_xml(member, "01")

        self.xml.endTag('membership')

    def member_to_xml(self, id, roletype):

        self.xml.startTag('member')

        self.xml.startTag('sourcedid')
        self.xml.dataElement('source', self.DataSource)
        self.xml.dataElement('id', str(id))
        self.xml.endTag('sourcedid')

        self.xml.dataElement('idtype', "1")

        self.xml.startTag('role', {'roletype': roletype, 'recstatus': "1"})
        self.xml.dataElement('subrole', "")
        self.xml.dataElement('status', "1")
        self.xml.startTag('timeframe')
        self.xml.dataElement('begin', "", {'restrict': '0'})
        self.xml.dataElement('end', "", {'restrict': '0'})
        self.xml.endTag('timeframe')
        self.xml.endTag('role')

        self.xml.endTag('member')