File size: 10,471 Bytes
9a0fe90
 
 
 
 
 
 
12e58e8
9a0fe90
 
12e58e8
9a0fe90
12e58e8
 
9a0fe90
 
 
 
 
12e58e8
 
 
 
9a0fe90
 
 
 
 
12e58e8
9a0fe90
 
 
12e58e8
9a0fe90
 
12e58e8
 
 
9a0fe90
 
 
 
 
 
 
 
 
12e58e8
9a0fe90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12e58e8
9a0fe90
 
 
 
 
 
 
 
 
12e58e8
 
 
 
 
 
 
 
9a0fe90
 
 
 
 
 
 
 
12e58e8
b265125
dd7aa55
9a0fe90
 
 
7654c22
971403e
9a0fe90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6beb66b
9a0fe90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eb7933
 
9a0fe90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12e58e8
9a0fe90
 
 
 
 
 
 
9acda46
 
 
 
 
 
 
9a0fe90
 
3eb7933
 
9a0fe90
12e58e8
9acda46
12e58e8
9a0fe90
 
 
3eb7933
 
9a0fe90
3eb7933
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0fe90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12e58e8
 
 
9a0fe90
 
 
 
 
12e58e8
d0e9f5b
12e58e8
9a0fe90
 
12e58e8
 
 
9a0fe90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12e58e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from src.donorframe import DonorFrame
from pathlib import Path
import pandas as pd
import numpy as np
import re
from pandas.errors import ParserError

from shiny import App, render, reactive, ui
from shiny.types import SilentException, FileInfo


def contains_special_characters(x):
    pattern = re.compile(r"[^a-zA-Z0-9\_\-\/]")
    return isinstance(x, str) and pattern.search(x)


def ui_card(title, *args):
    return (
        ui.div(
            {
                "class": "card mb-4",
                "style": "width: 300px; border-color: #1c66e2; border-width: 2px",
            },
            ui.div(title, class_="card-header"),
            ui.div({"class": "card-body"}, *args),
        ),
    )


www_dir = Path(__file__).parent / "www"

page_dependencies = ui.tags.head(
    ui.tags.link(rel="shortcut icon", href="www/favicon.ico"),
    ui.tags.link(
        href="https://fonts.googleapis.com/css2?family=Barlow&display=swap",
        rel="stylesheet",
    ),
)


app_ui = ui.page_fluid(
    page_dependencies,
    ui.tags.title("Donor Formatter"),
    # ui.tags.style(
    #     """
    #         body {
    #             font-family: Helvetica, sans-serif;
    #             background-color: #f8f9fa;
    #             color: #084fc9;
    #         }
    #         input {
    #             background-color: #f0f0f0; /* light gray for input fields */
    #             border: 1px solid #ccc;
    #             padding: 5px 10px;
    #             margin: 5px;
    #         }
    #         button {
    #             background-color: navy;
    #             color: white;
    #             border: none;
    #             padding: 10px 20px;
    #             text-align: center;
    #             text-decoration: none;
    #             font-size: 16px;
    #             margin: 4px 2px;
    #             transition-duration: 0.4s;
    #             cursor: pointer;
    #         }
    #         button:hover {
    #             background-color: #1c66e2; /* a darker shade of navy for hover effect */
    #             color: white;
    #         }
    #         h2 {
    #             font-size: 36px;
    #             color: #084fc9;
    #             font-family: 'Barlow', sans-serif;
    #         }
    #         /* Additional styles can be added here */
    #     """
    # ),
    ui.h2("The Donor Formatter"),
    ui.layout_sidebar(
        ui.sidebar(
            {"class": "data-panel"},
            ui.tags.a(
                "State Links",
                href="https://docs.google.com/spreadsheets/d/1IyVlvVcU2xiuYyZW31jSRFQ6MRrwBp1pRhz-QoUdEnI/edit?usp=sharing",
            ),
            ui.tags.a(
                "Format Index",
                href="https://docs.google.com/spreadsheets/d/1JNt3W_VA_HUAlxuDfh2g_--9zIu9NGizeUdq76j7yjI/edit?usp=sharing",
            ),
            ui.input_file("donor_file", "Choose files to upload:", multiple=True),
            ui.input_selectize(
                "source",
                "Select source:",
                {
                    "National": {"FEC": "FEC"},
                    "City": {
                        "ATL": "Atlanta",
                        "ATX": "Austin, TX",
                        "CLB": "Columbus, OH",
                        "DTX": "Dallas, TX",
                        "DEN": "Denver",
                        "DET": "Detroit",
                        "LA_C": "Los Angeles",
                        "MNSTP": "Minneapolis",
                        "MNSTP_OLD": "Minneapolis (Old)",
                        "NYC": "New York City",
                        "SLC": "Salt Lake City",
                        "SA": "San Antonio",
                        "SJ": "San Jose",
                        "DC": "Washington, D.C.",
                        "DC_NEW": "Washington, D.C. (*NEW*)",
                        "Wayne_Co": "Wayne County",
                    },
                    "State": {
                        "AL": "Alabama",
                        "AK": "Alaska",
                        "AZ": "Arizona",
                        "AR": "Arkansas",
                        "CA": "California",
                        "CO": "Colorado",
                        "CT": "Connecticut",
                        "DE": "Delaware",
                        "FL": "Florida",
                        "GA": "Georgia (New Transaction Search)",
                        "GA_old": "Georgia (Old Finance Website)",
                        "HI": "Hawaii",
                        "ID": "Idaho",
                        "IL": "Illinois",
                        "IN": "Indiana",
                        "IA": "Iowa",
                        "KS": "Kansas",
                        "KY": "Kentucky",
                        "LA": "Louisiana",
                        "ME": "Maine",
                        "MA": "Massachusetts",
                        "MD": "Maryland",
                        "MI": "Michigan",
                        "MN": "Minnesota",
                        "MO": "Missouri",
                        "MT": "Montana",
                        "NE": "Nebraska",
                        "NH": "New Hampshire",
                        "NJ": "New Jersey",
                        "NM": "New Mexico",
                        "NY": "New York",
                        "NV": "Nevada",
                        "NC": "North Carolina",
                        "ND": "North Dakota",
                        "OH": "Ohio",
                        "OK": "Oklahoma",
                        "OR": "Oregon",
                        "PA": "Pennsylvania",
                        "RI": "Rhode Island",
                        "SC": "South Carolina",
                        "TN": "Tennessee",
                        "TX": "Texas",
                        "UT": "Utah",
                        "VA": "Virginia",
                        "VT": "Vermont",
                        "WA": "Washington",
                        "WV": "West Virginia",
                        "WI": "Wisconsin",
                        "WY": "Wyoming",
                    },
                },
            ),
            ui.panel_conditional("input.source === 'VA'", ui.input_text("recipient_name", "Filter Recipient (TN Only)", "")),
            ui.panel_conditional("input.source === 'VA'", ui.input_text("va_report_link", "Contributions URL", "")),
            ui_card(
                "File Facts:",
                ui.output_text("total_donors"),
                ui.output_text("abba_check"),
            ),
            ui_card(
                "Download your files:",
                ui.download_button("export", "Download CSV"),
            ),
            open="always",
            width=350,
        ),
        ui.card(
            ui.div(
                {"class": "data-panel", "style": "color: #003591"},
                ui.output_data_frame("result"),
            ),
            ui.include_css(www_dir / "style.css"),
        ),
    ),
)


def server(input, output, session):

    @reactive.Calc
    def recip_filter():
        if input.recipient_name():
            return input.recipient_name()
        else:
            return None

    @reactive.Calc
    def get_file_name():
        if input.source() == 'VA':
            return "va_candidate_donors"
        paths = [file["name"] for file in input.donor_file()]
        tmp_path = paths[0].split(".")[0]
        return tmp_path

    @reactive.Calc
    def compile_donors():
        file: list[FileInfo] | None = input.donor_file()
        url = input.va_report_link()
        if file is None and not url:
            return pd.DataFrame()
        if file:
            paths = [file["datapath"] for file in input.donor_file()]
            files = [DonorFrame(path, input.source(), recip_filter()) for path in paths]
            print(files[0].recipient_name)
            try:
                dataframes = [file.format_donors(export=False) for file in files]
            except KeyError:
                raise SilentException()
            return pd.concat(dataframes, ignore_index=True)
        elif url and not file:
            try:
                dataframe = DonorFrame(data_path=url, source='VA').format_donors(export=False)
            except ValueError as e:
                raise e
            return dataframe

    @output
    @render.data_frame
    def result():
        try:
            donors_data = compile_donors()
        except ParserError:
            return pd.DataFrame({"No Data": ["No Data"]})
        try:
            tmp_path = get_file_name()
        except Exception as e:
            print(f"{e=}")
        if donors_data.empty:
            return pd.DataFrame({"No Data": ["No Data"]})

        @session.download(filename=f"{tmp_path}_formatted.csv")
        async def export():
            yield donors_data.drop("recipient_name", axis=1, errors="ignore").to_csv(
                index=False
            )

        display_df = donors_data.replace("", np.nan)
        display_df.dropna(thresh=4, inplace=True)
        display_df.dropna(how="all", axis=1, inplace=True)
        display_df.replace(np.nan, "", inplace=True)
        try:
            display_df['donation_amount'] = display_df['donation_amount'].replace('', '0').astype(int)
            display_df = display_df.sort_values("donation_amount", ascending=False)
        except KeyError:
            raise SilentException()
        display_df.columns = [
            name.title().replace("_", " ") for name in display_df.columns
        ]

        return display_df

    @output
    @render.text
    def total_donors():
        try:
            donors_data = compile_donors()
        except ParserError:
            return "Total Donors: 0"
        if donors_data.empty:
            return "Total Donors: 0"

        try:
            total_donors_count = len(donors_data)
        except (KeyError, ParserError):
            return "Total Donors: 0"

        return f"Total Donors: {total_donors_count}"

    @output
    @render.text
    def abba_check():
        try:
            donors_data = compile_donors()
        except ParserError:
            return "Formatted for ABBA: False"
        if donors_data.empty:
            return "Formatted for ABBA: False"

        abba = (len(donors_data.columns) == 16) and donors_data.applymap(
            contains_special_characters
        ).any().any()
        return f"Formatted for ABBA: {abba}"


src_dir = Path(__file__).parent / "src"
app = App(app_ui, server)  # , static_assets=www_dir)