def sync_data(conn, filename, council, batch_time):
    backup_filename = "{}-{:02d}-{:02d}-{}".format(batch_time.year,
                                                   batch_time.month,
                                                   batch_time.day, filename)
    print("\n\n-- ----------------------------- ")
    print("-- {} ".format(council))
    print("-- File: {}".format(backup_filename))

    summary_rows = []
    with open(filename) as data_file:
        content = json.load(data_file)
        pdf_date_str = content["metadata"]["pdf_creation_date"]
        pdf_date = datetime.strptime(
            pdf_date_str, "%Y-%m-%d %H:%M:%S")  # 2019-07-12 14:55:08
        archive_pdf_name = content["metadata"]["archive_pdf_name"]
        print("-- PDF creation date: {}".format(pdf_date))
        print("-- PDF archive file: {}".format(archive_pdf_name))
        print("-- ----------------------------- ")
        count = 1
        for parlamentarier in content["data"]:

            #load info about parlamentarier
            kanton_id = db.get_kanton_id(conn, parlamentarier["canton"])
            partei_id = db.get_partei_id(conn, parlamentarier["party"])
            parlamentarier_id = db.get_parlamentarier_id(
                conn, parlamentarier["names"], kanton_id, partei_id)
            parlamentarier["id"] = parlamentarier_id
            parlamentarier_db_dict = db.get_parlamentarier_dict(
                conn, parlamentarier_id)
            parlamentarier_active = parlamentarier_db_dict[
                'im_rat_bis'] == None or parlamentarier_db_dict[
                    'im_rat_bis'] > date.today()

            #existing guests (from database)
            existing_guest_1, existing_guest_2 = db.get_guests(
                conn, parlamentarier_id)

            #new guests (from JSON file)
            new_guests = parlamentarier["guests"]
            new_guest_1 = new_guests[0] if len(new_guests) > 0 else None
            new_guest_2 = new_guests[1] if len(new_guests) > 1 else None

            #summary row
            summary_row = summary.SummaryRow(parlamentarier, count,
                                             parlamentarier_db_dict)
            count += 1

            #check if existing guest 1 left or stayed
            if name_logic.are_guests_equal(existing_guest_1, new_guest_1):
                summary_row.set_guest_1(existing_guest_1)
                funktion_equal = guest_remained(parlamentarier,
                                                existing_guest_1, new_guest_1,
                                                batch_time, pdf_date)
                if not funktion_equal:
                    summary_row.set_guest_1_changes("funktion")

            elif name_logic.are_guests_equal(existing_guest_1, new_guest_2):
                summary_row.set_guest_1(existing_guest_1)
                funktion_equal = guest_remained(parlamentarier,
                                                existing_guest_1, new_guest_2,
                                                batch_time, pdf_date)
                if not funktion_equal:
                    summary_row.set_guest_1_changes("funktion")
            else:
                guest_removed(parlamentarier, existing_guest_1, batch_time,
                              pdf_date)
                summary_row.set_removed_guest_1(existing_guest_1)

            #check if existing guest 2 left or stayed
            if name_logic.are_guests_equal(existing_guest_2, new_guest_1):
                summary_row.set_guest_2(existing_guest_2)
                funktion_equal = guest_remained(parlamentarier,
                                                existing_guest_2, new_guest_1,
                                                batch_time, pdf_date)
                if not funktion_equal:
                    summary_row.set_guest_2_changes("funktion")

            elif name_logic.are_guests_equal(existing_guest_2, new_guest_2):
                summary_row.set_guest_2(existing_guest_2)
                funktion_equal = guest_remained(parlamentarier,
                                                existing_guest_2, new_guest_2,
                                                batch_time, pdf_date)
                if not funktion_equal:
                    summary_row.set_guest_2_changes("funktion")

            else:
                guest_removed(parlamentarier, existing_guest_2, batch_time,
                              pdf_date)
                summary_row.set_removed_guest_2(existing_guest_2)

            # check if new guest 1 was already here
            if not name_logic.are_guests_equal(
                    new_guest_1,
                    existing_guest_1) and not name_logic.are_guests_equal(
                        new_guest_1,
                        existing_guest_2) and parlamentarier_active:
                guest_added(conn, parlamentarier, new_guest_1, batch_time,
                            pdf_date)
                summary_row.set_new_guest_1(new_guest_1)

            # check if new guest 2 was already here
            if not name_logic.are_guests_equal(
                    new_guest_2,
                    existing_guest_1) and not name_logic.are_guests_equal(
                        new_guest_2,
                        existing_guest_2) and parlamentarier_active:
                # and not (parlamentarier_id == 223 and new_guest_2 != None and new_guest_2["names"] != None and new_guest_2["names"][0] == "Egger") # Quick and dirty fix for SR Engler + ZB Egger (new NR)
                guest_added(conn, parlamentarier, new_guest_2, batch_time,
                            pdf_date)

                if name_logic.are_guests_equal(new_guest_1, existing_guest_2):
                    summary_row.set_new_guest_1(new_guest_2)
                else:
                    summary_row.set_new_guest_2(new_guest_2)

            summary_rows.append(summary_row)

    #return("\n".join(summary_rows))
    return (summary_rows)
Example #2
0
def sync_data(conn, filename, council, batch_time):
    backup_filename = "{}-{:02d}-{:02d}-{}".format(batch_time.year, batch_time.month, batch_time.day, filename)
    print("\n\n-- ----------------------------- ")
    print("-- {} ".format(council))
    print("-- File: {}".format(backup_filename))

    summary_rows = []
    with open(filename) as data_file:
        content = json.load(data_file)
        print("-- PDF creation date: {}".format(content["metadata"]["pdf_creation_date"]))
        print("-- PDF archive file: {}".format(content["metadata"]["archive_pdf_name"]))
        print("-- ----------------------------- ")
        count = 1
        for parlamentarier in content["data"]:

            #load info about parlamentarier
            kanton_id = db.get_kanton_id(conn, parlamentarier["canton"])
            partei_id = db.get_partei_id(conn, parlamentarier["party"])
            parlamentarier_id = db.get_parlamentarier_id(conn, parlamentarier["names"], kanton_id, partei_id)
            parlamentarier["id"] = parlamentarier_id
            parlamentarier_db_dict = db.get_parlamentarier_dict(conn, parlamentarier_id)
            parlamentarier_active = parlamentarier_db_dict['im_rat_bis'] == None

            #existing guests (from database)
            existing_guest_1, existing_guest_2  = db.get_guests(conn, parlamentarier_id)

            #new guests (from JSON file)
            new_guests = parlamentarier["guests"]
            new_guest_1 = new_guests[0] if len(new_guests) > 0 else None
            new_guest_2 = new_guests[1] if len(new_guests) > 1 else None

            #summary row
            summary_row = summary.SummaryRow(parlamentarier, count, parlamentarier_db_dict)
            count += 1


            #check if existing guest 1 left or stayed
            if name_logic.are_guests_equal(existing_guest_1, new_guest_1):
                summary_row.set_guest_1(existing_guest_1)
                funktion_equal = guest_remained(parlamentarier, existing_guest_1, new_guest_1, batch_time)
                if not funktion_equal:
                    summary_row.set_guest_1_changes("funktion")

            elif name_logic.are_guests_equal(existing_guest_1, new_guest_2):
                summary_row.set_guest_1(existing_guest_1)
                funktion_equal = guest_remained(parlamentarier, existing_guest_1, new_guest_2, batch_time)
                if not funktion_equal:
                    summary_row.set_guest_1_changes("funktion")
            else:
                guest_removed(parlamentarier, existing_guest_1, batch_time)
                summary_row.set_removed_guest_1(existing_guest_1)

            #check if existing guest 2 left or stayed
            if name_logic.are_guests_equal(existing_guest_2, new_guest_1):
                summary_row.set_guest_2(existing_guest_2)
                funktion_equal = guest_remained(parlamentarier, existing_guest_2, new_guest_1, batch_time)
                if not funktion_equal:
                    summary_row.set_guest_2_changes("funktion")

            elif name_logic.are_guests_equal(existing_guest_2, new_guest_2):
                summary_row.set_guest_2(existing_guest_2)
                funktion_equal = guest_remained(parlamentarier, existing_guest_2, new_guest_2, batch_time)
                if not funktion_equal:
                    summary_row.set_guest_2_changes("funktion")

            else:
                guest_removed(parlamentarier, existing_guest_2, batch_time)
                summary_row.set_removed_guest_2(existing_guest_2)

            # check if new guest 1 was already here
            if not name_logic.are_guests_equal(new_guest_1, existing_guest_1) and not name_logic.are_guests_equal(new_guest_1, existing_guest_2) and parlamentarier_active:
                guest_added(conn, parlamentarier, new_guest_1, batch_time)
                summary_row.set_new_guest_1(new_guest_1)

            # check if new guest 2 was already here
            if not name_logic.are_guests_equal(new_guest_2, existing_guest_1) and not name_logic.are_guests_equal(new_guest_2, existing_guest_2) and parlamentarier_active:
                # and not (parlamentarier_id == 223 and new_guest_2 != None and new_guest_2["names"] != None and new_guest_2["names"][0] == "Egger") # Quick and dirty fix for SR Engler + ZB Egger (new NR)
                guest_added(conn, parlamentarier, new_guest_2, batch_time)

                if name_logic.are_guests_equal(new_guest_1, existing_guest_2):
                    summary_row.set_new_guest_1(new_guest_2)
                else:
                    summary_row.set_new_guest_2(new_guest_2)

            summary_rows.append(summary_row)

    #return("\n".join(summary_rows))
    return(summary_rows)