parse_institution_xml.py 11.4 KB
Newer Older
1 2
# -*- coding: utf-8 -*- vim:encoding=utf-8:
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

# Copyright © 2011-2015 Greek Research and Technology Network (GRNET S.A.)
#
# Developed by Leonidas Poulopoulos (leopoul-at-noc-dot-grnet-dot-gr),
# Zenon Mousmoulas (zmousm-at-noc-dot-grnet-dot-gr) and Stavros Kroustouris
# (staurosk-at-noc-dot-grnet-dot-gr), GRNET NOC
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD
# TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
21
from django.core.management.base import BaseCommand, CommandError
22
from edumanage.models import *
23
from xml.etree import ElementTree
24
import sys
25

26

27
class Command(BaseCommand):
28
    help = '''
29 30
    Parses an institution XML file and creates institution,
    institution realm, contact and service location entries
31
    '''
32 33
    args = '<file>'
    label = 'file name to be imported'
34 35

    def handle(self, *args, **options):
36 37 38
        '''
        Handle command
        '''
39

40 41 42
        if args is None or len(args) != 1:
            raise CommandError('You must supply a file name')

43 44 45 46 47
        if int(options['verbosity']) > 0:
            write = self.stdout.write
        else:
            write = lambda *args: None

48 49 50 51
        self.parse_and_create(args[0])

    def parse_and_create(self, instxmlfile):
        doc = ElementTree.parse(instxmlfile)
52 53 54 55 56
        realmid = Realm.objects.get(pk=1)
        root = doc.getroot()
        institutions = []
        institutions = root.getchildren()
        for institution in institutions:
57
            created_inst_details = False
58 59
            instcontactslist = []
            for instdetails in institution:
60
                write('Parsing: %s\n' % (instdetails.tag))
61 62 63 64
                if instdetails.tag == 'country':
                    continue
                if instdetails.tag == 'type':
                    type = instdetails.text
65 66 67 68 69
                    institution_obj = Institution(
                        realmid=realmid,
                        ertype=int(type)
                    )
                    institution_obj.save()
70
                    write('Created inst %s\n' % institution_obj.pk)
71 72 73
                    continue
                if instdetails.tag == 'inst_realm':
                    inst_realm = instdetails.text
74 75 76 77 78
                    inst_realm_obj = InstRealm(
                        realm=inst_realm,
                        instid=institution_obj
                    )
                    inst_realm_obj.save()
79 80 81 82
                    continue
                if instdetails.tag == 'org_name':
                    org_name_lang = instdetails.items()[0][1]
                    org_name = instdetails.text
83 84 85 86 87
                    t = Name_i18n(
                        content_object=institution_obj,
                        name=org_name,
                        lang=org_name_lang
                    )
88 89 90 91 92 93 94 95 96
                    t.save()
                    continue
                if instdetails.tag == 'address':
                    for address in instdetails.getchildren():
                        if address.tag == 'street':
                            street = address.text
                        if address.tag == 'city':
                            city = address.text
                    continue
97
                if instdetails.tag == 'contact':
98 99 100 101 102 103 104
                    for contact in instdetails.getchildren():
                        if contact.tag == 'name':
                            contact_name = contact.text
                        if contact.tag == 'email':
                            contact_email = contact.text
                        if contact.tag == 'phone':
                            contact_phone = contact.text
105 106 107 108 109 110 111 112 113 114 115 116
                    contact_obj = Contact(
                        name=contact_name,
                        email=contact_email,
                        phone=contact_phone
                    )
                    contact_obj.save()
                    instcontactslist.append(contact_obj)
                    institution_contact_obj = InstitutionContactPool(
                        contact=contact_obj,
                        institution=institution_obj
                    )
                    institution_contact_obj.save()
117
                    continue
118 119 120 121 122 123 124 125

                if not created_inst_details:
                    instdets_obj = InstitutionDetails(
                        institution=institution_obj,
                        address_street=street,
                        address_city=city,
                        number_id=1
                    )
126
                    print instcontactslist
127 128 129 130 131
                    instdets_obj.save()
                    instdets_obj.contact = instcontactslist
                    instdets_obj.save()
                    created_inst_details = True

132
                if instdetails.tag == 'info_URL':
133 134 135 136 137 138 139 140
                    info_url_lang = instdetails.items()[0][1]
                    info_url = instdetails.text
                    u = URL_i18n(
                        content_object=instdets_obj,
                        urltype='info',
                        lang=info_url_lang,
                        url=info_url
                    )
141 142 143
                    u.save()
                    continue
                if instdetails.tag == 'policy_URL':
144 145 146 147 148 149 150 151
                    policy_url_lang = instdetails.items()[0][1]
                    policy_url = instdetails.text
                    u = URL_i18n(
                        content_object=instdets_obj,
                        urltype='policy',
                        lang=policy_url_lang,
                        url=policy_url
                    )
152 153
                    u.save()
                    continue
154

155
                if instdetails.tag == 'location':
156 157
                    location_names_list = []
                    location_address_list = []
158 159 160 161 162 163 164 165 166
                    parsedLocation = False
                    for locationdets in instdetails.getchildren():
                        if locationdets.tag == 'longitude':
                            location_long = locationdets.text
                            continue
                        if locationdets.tag == 'latitude':
                            location_lat = locationdets.text
                            continue
                        if locationdets.tag == 'loc_name':
167
                            loc_name_dict = {}
168 169
                            loc_name_lang = locationdets.items()[0][1]
                            loc_name = locationdets.text
170 171 172
                            loc_name_dict['name'] = loc_name
                            loc_name_dict['lang'] = loc_name_lang
                            location_names_list.append(loc_name_dict)
173 174
                            continue
                        if locationdets.tag == 'address':
175
                            loc_addr_dict = {}
176 177 178
                            for locaddress in locationdets.getchildren():
                                if locaddress.tag == 'street':
                                    locstreet = locaddress.text
179
                                    loc_addr_dict['street'] = locstreet
180 181
                                if locaddress.tag == 'city':
                                    loccity = locaddress.text
182 183
                                    loc_addr_dict['city'] = loccity
                            location_address_list.append(loc_addr_dict)
184 185
                            continue
                        if locationdets.tag == 'SSID':
186
                            loc_ssid = locationdets.text
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                            continue
                        if locationdets.tag == 'enc_level':
                            loc_enc_level = locationdets.text
                            continue
                        if locationdets.tag == 'port_restrict':
                            loc_port_restrict_txt = locationdets.text
                            loc_port_restrict = False
                            if loc_port_restrict_txt in ('true', '1'):
                                loc_port_restrict = True
                            continue
                        if locationdets.tag == 'transp_proxy':
                            loc_transp_proxy_txt = locationdets.text
                            loc_transp_proxy = False
                            if loc_transp_proxy_txt in ('true', '1'):
                                loc_transp_proxy = True
                            continue
                        if locationdets.tag == 'IPv6':
204 205 206 207
                            loc_ipv6_txt = locationdets.text
                            loc_ipv6 = False
                            if loc_ipv6_txt in ('true', '1'):
                                loc_ipv6 = True
208 209
                            continue
                        if locationdets.tag == 'NAT':
210 211 212 213
                            loc_nat_txt = locationdets.text
                            loc_nat = False
                            if loc_nat_txt in ('true', '1'):
                                loc_nat = True
214 215
                            continue
                        if locationdets.tag == 'AP_no':
216
                            loc_ap_no = int(locationdets.text)
217 218 219 220 221
                            continue
                        if locationdets.tag == 'wired':
                            loc_wired_txt = locationdets.text
                            loc_wired = False
                            if loc_wired_txt in ('true', '1'):
222
                                loc_wired = True
223
                        if not parsedLocation:
224
                            write('Creating location:\n')
225
                            try:
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                                serviceloc = ServiceLoc(
                                    institutionid=institution_obj,
                                    longitude=location_long,
                                    latitude=location_lat,
                                    address_street=locstreet,
                                    address_city=loccity,
                                    SSID=loc_ssid,
                                    enc_level=loc_enc_level,
                                    port_restrict=loc_port_restrict,
                                    transp_proxy=loc_transp_proxy,
                                    IPv6=loc_ipv6,
                                    NAT=loc_nat,
                                    AP_no=loc_ap_no,
                                    wired=loc_wired
                                )
241
                                serviceloc.save()
242 243 244 245 246 247
                                for locat_name in location_names_list:
                                    t = Name_i18n(
                                        content_object=serviceloc,
                                        name=locat_name['name'],
                                        lang=locat_name['lang']
                                    )
248 249
                                    t.save()
                            except Exception as e:
250
                                write('ERROR: %s\n' % e)
251 252
                    continue
        return True