def create_or_update_single_bill(self, proposal, pdf_link, link_file): if not(proposal['date']) or CUTOFF_DATE and proposal['date'] < CUTOFF_DATE: return law_name = proposal['law'] (law, created) = Law.objects.get_or_create(title=law_name) if created: law.save() if law.merged_into: law = law.merged_into title = u'' if proposal['correction']: title += proposal['correction'] if proposal['comment']: title += ' ' + proposal['comment'] if len(title)<=1: title = u'חוק חדש' (gp,created) = GovProposal.objects.get_or_create(booklet_number=proposal['booklet'], knesset_id=18, source_url=proposal['link'], title=title, law=law, date=proposal['date']) if created: gp.save() logger.debug("created GovProposal id = %d" % gp.id) bill_params = dict(law=law, title=title, stage='3', stage_date=proposal['date']) similar_bills = Bill.objects.filter(**bill_params).order_by('id') if len(similar_bills) >= 1: b = similar_bills[0] if len(similar_bills) > 1: logger.debug("multiple bills detected") for bill in similar_bills: if bill.id == b.id: logger.debug("bill being used now - %d" % bill.id) else: logger.debug("bill with same fields - %d" % bill.id) else: b = Bill(**bill_params) b.save() gp.bill = b gp.save() if link_file.link is None: link = Link(title=pdf_link, url=pdf_link, content_type=ContentType.objects.get_for_model(gp), object_pk=str(gp.id)) link.save() link_file.link = link link_file.save() logger.debug("check updated %s" % b.get_absolute_url())
def create_or_update_single_bill(self, data, pdf_link, link_file, gp=None): """ data - a dict of data for this gov proposal pdf_link - the source url from which the bill is taken link_file - a cached version of the pdf gp - an existing GovProposal objects. if this is given, it will be updated, instead of creating a new object """ if not(data['date']) or CUTOFF_DATE and data['date'] < CUTOFF_DATE: return law_name = data['law'] (law, created) = Law.objects.get_or_create(title=law_name) if created: law.save() if law.merged_into: law = law.merged_into title = u'' if data['correction']: title += data['correction'] if data['comment']: title += ' ' + data['comment'] if len(title) <= 1: title = u'חוק חדש' if data['date'] > Knesset.objects.current_knesset().start_date: k_id = 19 else: k_id = 18 if gp is None: # create new GovProposal, or look for an identical one (gp, created) = GovProposal.objects.get_or_create( booklet_number=data['booklet'], source_url=data['link'], title=title, law=law, date=data['date'], defaults={'knesset_id': k_id}) if created: gp.save() logger.debug("created GovProposal id = %d" % gp.id) # look for similar bills bill_params = dict(law=law, title=title, stage='3', stage_date=data['date']) similar_bills = Bill.objects.filter(**bill_params).order_by('id') if len(similar_bills) >= 1: b = similar_bills[0] if len(similar_bills) > 1: logger.debug("multiple bills detected") for bill in similar_bills: if bill.id == b.id: logger.debug("bill being used now: %d" % bill.id) else: logger.debug("bill with same fields: %d" % bill.id) else: # create a bill b = Bill(**bill_params) b.save() # see if the found bill is already linked to a gov proposal try: bill_gp_id = b.gov_proposal.id except GovProposal.DoesNotExist: bill_gp_id = None if (bill_gp_id is None) or (gp.id == b.gov_proposal.id): # b is not linked to gp, or linked to the current gp gp.bill = b gp.save() else: logger.debug("processing gp %d - matching bill (%d) already has gp" " (%d)" % (gp.id, b.id, b.gov_proposal.id)) else: # update a given GovProposal gp.booklet_number = data['booklet'] gp.knesset_id = k_id gp.source_url = data['link'] gp.title = title gp.law = law gp.date = data['date'] gp.save() gp.bill.title = title gp.bill.law = law gp.bill.save() b = gp.bill if (link_file is not None) and (link_file.link is None): link = Link(title=pdf_link, url=pdf_link, content_type=ContentType.objects.get_for_model(gp), object_pk=str(gp.id)) link.save() link_file.link = link link_file.save() logger.debug("check updated %s" % b.get_absolute_url())
def create_or_update_single_bill(self, data, pdf_link, link_file, gp=None): """ data - a dict of data for this gov proposal pdf_link - the source url from which the bill is taken link_file - a cached version of the pdf gp - an existing GovProposal objects. if this is given, it will be updated, instead of creating a new object """ if not (data['date']) or CUTOFF_DATE and data['date'] < CUTOFF_DATE: return law_name = data['law'] try: law, created = Law.objects.get_or_create(title=law_name) except Law.MultipleObjectsReturned: created = False try: law = Law.objects.filter(title=law_name, merged_into=None).last() except Law.MultipleObjectsReturned: # How is this possible? probably another bug somewhere law = Law.objects.filter(title=law_name).last() if created: law.save() if law.merged_into: law = law.merged_into title = u'' if data['correction']: title += data['correction'] if data['comment']: title += ' ' + data['comment'] if len(title) <= 1: title = u'חוק חדש' k_id = Knesset.objects.get_knesset_by_date(data['date']).pk if gp is None: # create new GovProposal, or look for an identical one (gp, created) = GovProposal.objects.get_or_create( booklet_number=data['booklet'], source_url=data['link'], title=title, law=law, date=data['date'], defaults={'knesset_id': k_id}) if created: gp.save() logger.debug("created GovProposal id = %d" % gp.id) # look for similar bills bill_params = dict(law=law, title=title, stage='3', stage_date=data['date']) similar_bills = Bill.objects.filter(**bill_params).order_by('id') if len(similar_bills) >= 1: b = similar_bills[0] if len(similar_bills) > 1: logger.debug("multiple bills detected") for bill in similar_bills: if bill.id == b.id: logger.debug("bill being used now: %d" % bill.id) else: logger.debug("bill with same fields: %d" % bill.id) else: # create a bill b = Bill(**bill_params) b.save() logger.debug("created bill %d" % b.id) # see if the found bill is already linked to a gov proposal try: bill_gp_id = b.gov_proposal.id except GovProposal.DoesNotExist: bill_gp_id = None if (bill_gp_id is None) or (gp.id == b.gov_proposal.id): # b is not linked to gp, or linked to the current gp gp.bill = b gp.save() else: logger.debug( "processing gp %d - matching bill (%d) already has gp" " (%d)" % (gp.id, b.id, b.gov_proposal.id)) else: # update a given GovProposal # TODO: move to a classmethod gp.booklet_number = data['booklet'] gp.knesset_id = k_id gp.source_url = data['link'] gp.title = title gp.law = law gp.date = data['date'] gp.save() gp.bill.title = title gp.bill.law = law gp.bill.save() b = gp.bill if (link_file is not None) and (link_file.link is None): link = Link(title=pdf_link, url=pdf_link, content_type=ContentType.objects.get_for_model(gp), object_pk=str(gp.id)) link.save() link_file.link = link link_file.save() logger.debug("check updated %s" % b.get_absolute_url())