def handle(self, *args, **options):
        with open(options["source"]) as source:
            contents = source.read()

        # for remove xml declaration without regexes I use lxml
        # (xmltodict is good, but can't read xml declaration)
        contents = etree.tostring(
            etree.XML(contents, etree.XMLParser(strip_cdata=False, encoding="utf-8")), encoding="utf-8"
        )

        parser = XmlParser(contents)

        if (options["events"] is None) and (options["places"] is None) and not options["schedule"]:
            options.update({"events": [], "places": [], "schedule": True})

        if options["events"] is not None:
            Event.objects.load(parser.get_events(*list(map(int, options["events"]))))

        if options["places"] is not None:
            Place.objects.load(parser.get_places(*list(map(int, options["places"]))))

        if options["schedule"]:
            Session.objects.load(parser.get_schedule())
예제 #2
0
class XmlParserTestCase(TestCase):

    def setUp(self):
        self.parser = XmlParser('''
        <feed>
            <events>
                <event id="1" price="true" type="other" kids="true">
                    <title><![CDATA[Title]]></title>
                    <runtime>60</runtime>
                    <age_restricted>0+</age_restricted>
                    <tags><tag>tag</tag></tags>
                    <persons><person><name>Name Surname</name><role>role</role></person></persons>
                    <gallery><image href="http://example.com/image.jpg"/></gallery>
                    <text><![CDATA[Text]]></text>
                    <description><![CDATA[Description]]></description>
                    <stage_theatre>Stage</stage_theatre>
                </event>
            </events>
            <places>
                <place id="1" type="other">
                    <city>City</city>
                    <title>Title</title>
                    <address>Address</address>
                    <coordinates latitude="59.9" longitude="30.1"/>
                    <phones><phone type="other">Phone</phone></phones>
                    <work_times><work_time type="openhours">Work Time</work_time></work_times>
                    <tags><tag>tag</tag></tags>
                    <metros><metro>Metro</metro></metros>
                    <gallery><image href="http://example.com/image.jpg"/></gallery>
                    <text><![CDATA[Text]]></text>
                    <url>http://example.com/</url>
                </place>
            </places>
            <schedule>
                <session date="2015-10-31" event="1" place="1" time="19:00" timetill="21:30"/>
            </schedule>
        </feed>
        ''')

        self.event = {
            'id': 1,
            'price': True,
            'type': 'other',
            'kids': True,
            'title': 'Title',
            'runtime': 60,
            'age_restricted': 0,
            'tags': ['tag'],
            'persons': [{'name': 'Name Surname', 'role': 'role'}],
            'gallery': [{'href': 'http://example.com/image.jpg'}],
            'text': 'Text',
            'description': 'Description',
            'stage_theatre': 'Stage'
        }

        self.place = {
            'id': 1,
            'type': 'other',
            'city': 'City',
            'title': 'Title',
            'address': 'Address',
            'coordinates': {'latitude': 59.9, 'longitude': 30.1},
            'phones': [{'type': 'other', 'phone': 'Phone'}],
            'work_times': [{'type': 'openhours', 'work_time': 'Work Time'}],
            'tags': ['tag'],
            'metros': ['Metro'],
            'gallery': [{'href': 'http://example.com/image.jpg'}],
            'text': 'Text',
            'url': 'http://example.com/'
        }

        self.session = {
            'date': '2015-10-31',
            'event': 1,
            'place': 1,
            'time': '19:00',
            'time_till': '21:30'
        }

    def test_parse_events(self):
        self.assertEqual([self.event], self.parser.get_events())

    def test_parse_places(self):
        self.assertEqual([self.place], self.parser.get_places())

    def test_parse_schedule(self):
        self.assertEqual([self.session], self.parser.get_schedule())

    def test_parse_all(self):
        self.assertEqual(
            {
                'events': [self.event],
                'places': [self.place],
                'schedule': [self.session]
            },
            self.parser.data
        )