def find_dossier(data): ''' Find dossier with reference matching either 'ref_an' or 'ref_sen', create it if not found. Ensure its reference is 'ref_an' if both fields are present. ''' changed = False dossier = None reffield = None for field in [k for k in ('ref_an', 'ref_sen') if k in data]: try: dossier = Dossier.objects.get(reference=data[field]) reffield = field break except Dossier.DoesNotExist: pass if dossier is None: reffield = 'ref_an' if 'ref_an' in data else 'ref_sen' dossier = Dossier(reference=data[reffield]) logger.debug('Created dossier %s' % data[reffield]) changed = True if 'ref_an' in data and reffield != 'ref_an': logger.debug('Changed dossier reference to %s' % data['ref_an']) dossier.reference = data['ref_an'] changed = True return dossier, changed
def parse_dossier_data(data, ep): """Parse data from parltarck dossier export (1 dossier) Update dossier if it existed before, this function goal is to import and update a dossier, not to import all parltrack data """ changed = False doc_changed = False ref = data['procedure']['reference'] logger.debug('Processing dossier %s', ref) with transaction.atomic(): try: dossier = Dossier.objects.get(reference=ref) except Dossier.DoesNotExist: dossier = Dossier(reference=ref) logger.debug('Dossier did not exist') changed = True if dossier.title != data['procedure']['title']: logger.debug('Title changed from "%s" to "%s"', dossier.title, data['procedure']['title']) dossier.title = data['procedure']['title'] changed = True if changed: logger.info('Updated dossier %s', ref) dossier.save() source = data['meta']['source'].replace('&l=en', '') try: doc = Document.objects.get(dossier=dossier, kind='procedure-file') except Document.DoesNotExist: doc = Document(dossier=dossier, kind='procedure-file', chamber=ep) logger.debug('Document for dossier %s did not exist', ref) doc_changed = True if doc.link != source: logger.debug('Link changed from %s to %s', doc.link, source) doc.link = source doc_changed = True if doc_changed: logger.info('Updated document %s for dossier %s', doc.link, ref) doc.save() if 'votes' in data.keys() and 'epref' in data['votes']: command = Command() command.init_cache() command.parse_vote_data(data['votes'])