def test_manythings(self): import codecs from django.test.client import Client from prometapi.models import parse_burja, parse_burjaznaki, parse_counters, parse_events, parse_parkirisca_lpt from prometapi.models import Burja, BurjaZnaki, Counters, Events, ParkiriscaLPT from prometapi.models import _decode, _loads, _dumps modls = { 'burja': Burja, 'burjaznaki': BurjaZnaki, 'counters': Counters, 'events': Events, } for fn in ('burja', 'burjaznaki', 'counters', 'events'): for n in (1, 2, 3): obf = open(obffile('%s_%s.txt' % (fn, n))).read() self.assertEqual(_decode(obf), codecs.open(obffile('%s_%s.json' % (fn, n)), 'r', 'utf-8').read(), 'obfuscated vs. json mismatch, %s_%s.json' % (fn, n)) parser = locals()['parse_%s' % fn] json = parser(obf) self.assertEqual(type(json), type({})) m = modls[fn] original_data=zlib_encode(obf) obj = m(json_data=_dumps(json), original_data=original_data) obj.save() # load parkirisca park = open(testfile('parkirisca_1.xml')).read() occu = open(testfile('occupancy_1.xml')).read() json = parse_parkirisca_lpt(park, occu) obj = ParkiriscaLPT(json_data=_dumps(json), original_data=zlib_encode(_dumps([park, occu]))) obj.save() # GET c = Client() resp = c.get('/promet/events/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({})) resp = c.get('/promet/burja/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({})) resp = c.get('/promet/burjaznaki/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({})) resp = c.get('/promet/counters/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({})) resp = c.get('/promet/parkirisca/lpt/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({}))
def runTest(self): import codecs from django.test.client import Client from prometapi.models import parse_burja, parse_burjaznaki, parse_counters, parse_events, parse_parkirisca_lpt from prometapi.models import Burja, BurjaZnaki, Counters, Events, ParkiriscaLPT from prometapi.models import _decode, _loads, _dumps modls = { 'burja': Burja, 'burjaznaki': BurjaZnaki, 'counters': Counters, 'events': Events, } for fn in ('burja', 'burjaznaki', 'counters', 'events'): for n in (1, 2, 3): obf = open(obffile('%s_%s.txt' % (fn, n))).read() self.assertEqual(_decode(obf), codecs.open(obffile('%s_%s.json' % (fn, n)), 'r', 'utf-8').read(), 'obfuscated vs. json mismatch, %s_%s.json' % (fn, n)) parser = locals()['parse_%s' % fn] json = parser(obf) self.assertEqual(type(json), type({})) m = modls[fn] obj = m(json_data=_dumps(json), original_data=obf.encode('zlib').encode('base64')) obj.save() # load parkirisca park = open(testfile('parkirisca_1.xml')).read() occu = open(testfile('occupancy_1.xml')).read() json = parse_parkirisca_lpt(park, occu) obj = ParkiriscaLPT(json_data=_dumps(json), original_data=_dumps([park, occu]).encode('zlib').encode('base64')) obj.save() # GET c = Client() resp = c.get('/promet/events/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({})) resp = c.get('/promet/burja/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({})) resp = c.get('/promet/burjaznaki/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({})) resp = c.get('/promet/counters/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({})) resp = c.get('/promet/parkirisca/lpt/') self.assertEqual(resp.status_code, 200) json = _loads(resp.content) self.assertEqual(type(json), type({}))
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_promet, parse_promet, Events, Burja, Cameras, Counters import simplejson data = fetch_promet() try: full_json = parse_promet(data) except simplejson.decoder.JSONDecodeError: full_json = None dogodki = delo = kamere = stevci = burja = None if full_json: dogodki = [ i for i in full_json['Contents'] if i['ContentName'] == 'dogodki' ] delo = [ i for i in full_json['Contents'] if i['ContentName'] == 'delo' ] kamere = [ i for i in full_json['Contents'] if i['ContentName'] == 'kamere' ] stevci = [ i for i in full_json['Contents'] if i['ContentName'] == 'stevci' ] burja = [ i for i in full_json['Contents'] if i['ContentName'] == 'burja' ] if full_json is None or not all([dogodki, delo, kamere, stevci, burja ]): e = Events(json_data=_dumps(full_json), original_data=data) e.save() else: json_templ = dict([(k, v) for k, v in full_json.items() if k != 'Contents']) json = json_templ.copy() json['Contents'] = dogodki e = Events(json_data=_dumps(json), original_data='') e.save() json = json_templ.copy() json['Contents'] = kamere ca = Cameras(json_data=_dumps(json), original_data='') ca.save() json = json_templ.copy() json['Contents'] = stevci co = Counters(json_data=_dumps(json), original_data='') co.save() json = json_templ.copy() json['Contents'] = burja b = Burja(json_data=_dumps(json), original_data='') b.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_parkirisca_lpt, parse_parkirisca_lpt, ParkiriscaLPT import simplejson data = fetch_parkirisca_lpt() try: json = parse_parkirisca_lpt(*data) except simplejson.decoder.JSONDecodeError: json = None e = ParkiriscaLPT(json_data=_dumps(json), original_data=_dumps(data).encode('zlib').encode('base64')) e.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_parkirisca_lpt, parse_parkirisca_lpt, ParkiriscaLPT import simplejson data = fetch_parkirisca_lpt() try: json = parse_parkirisca_lpt(*data) except simplejson.decoder.JSONDecodeError: json = None e = ParkiriscaLPT( json_data=_dumps(json), original_data=_dumps(data).encode('zlib').encode('base64')) e.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_promet, parse_promet, Events, Burja, Cameras, Counters import simplejson data = fetch_promet() try: full_json = parse_promet(data) except simplejson.decoder.JSONDecodeError: full_json = None dogodki = delo = kamere = stevci = burja = None if full_json: dogodki = [i for i in full_json['Contents'] if i['ContentName'] == 'dogodki'] delo = [i for i in full_json['Contents'] if i['ContentName'] == 'delo'] kamere = [i for i in full_json['Contents'] if i['ContentName'] == 'kamere'] stevci = [i for i in full_json['Contents'] if i['ContentName'] == 'stevci'] burja = [i for i in full_json['Contents'] if i['ContentName'] == 'burja'] if full_json is None or not all([dogodki, delo, kamere, stevci, burja]): e = Events(json_data=_dumps(full_json), original_data=data) e.save() else: json_templ = dict([(k, v) for k, v in full_json.items() if k != 'Contents']) json = json_templ.copy() json['Contents'] = dogodki e = Events(json_data=_dumps(json), original_data='') e.save() json = json_templ.copy() json['Contents'] = kamere ca = Cameras(json_data=_dumps(json), original_data='') ca.save() json = json_templ.copy() json['Contents'] = stevci co = Counters(json_data=_dumps(json), original_data='') co.save() json = json_templ.copy() json['Contents'] = burja b = Burja(json_data=_dumps(json), original_data='') b.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_burja, parse_burja, Burja import simplejson data = fetch_burja() try: json = parse_burja(data) except simplejson.decoder.JSONDecodeError: json = None e = Burja(json_data=_dumps(json), original_data=data.encode('zlib').encode('base64')) e.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_events, parse_events, Events import simplejson eventsdata = fetch_events() try: json = parse_events(eventsdata) except simplejson.decoder.JSONDecodeError: json = None ev = Events(json_data=_dumps(json), original_data=eventsdata.encode('zlib').encode('base64')) ev.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_events, parse_events, Events import simplejson eventsdata = fetch_events() try: json = parse_events(eventsdata) except simplejson.decoder.JSONDecodeError: json = None if json is not None: eventsdata = '' ev = Events(json_data=_dumps(json), original_data=eventsdata) ev.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_burjaznaki, parse_burjaznaki, BurjaZnaki import simplejson data = fetch_burjaznaki() try: json = parse_burjaznaki(data) except simplejson.decoder.JSONDecodeError: json = None if json is not None: data = '' e = BurjaZnaki(json_data=_dumps(json), original_data=data) e.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_burja, parse_burja, Burja import simplejson data = fetch_burja() try: json = parse_burja(data) except simplejson.decoder.JSONDecodeError: json = None if json is not None: data = '' e = Burja(json_data=_dumps(json), original_data=data) e.save()
def handle(self, *args, **options): from prometapi.models import fetch_cameras, parse_cameras, Cameras, _dumps import simplejson cameradata = fetch_cameras() try: json = parse_cameras(cameradata) except simplejson.decoder.JSONDecodeError as e: json = None if json is not None: cameradata = '' cam = Cameras(json_data=_dumps(json), original_data=cameradata) cam.save()
def handle(self, *args, **options): from prometapi.models import _dumps, fetch_counters, parse_counters, Counters import simplejson data = fetch_counters() try: json = parse_counters(data) except simplejson.decoder.JSONDecodeError: json = None # if parsing succeeded, don't store original if json is not None: data = '' e = Counters(json_data=_dumps(json), original_data=data) e.save()