Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pretix newsletter CSV import #563

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions fragdenstaat_de/fds_newsletter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,47 @@ def get_onboarding_subscribers(date: datetime.date, schedule_item):
)


def check_csv_format(csv_data):
csv_format = {}
headers = csv_data.fieldnames

# TODO: find better place for csv format definitions
pretix = ["Bestellnummer", "E-Mail", "Anfragedatum", "Name", "Vorname", "Nachname"]

if headers == pretix:
csv_format = {
"name": "Name",
"email": "E-Mail",
}
elif "first_name" in headers and "last_name" in headers:
csv_format = {
"name": "combine",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of storing different things in name: both a column name and a magic constant which implies that two columns should be combined. I suggest to instead return a list of fields here and checking in import_csv whether a list was returned (isinstance(row['name'], list))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To simplify this could also always return a list for name, which for the other cases only contains a single entry

"email": "email",
}
else:
csv_format = {
"name": "name",
"email": "email",
}

return csv_format


def import_csv(csv_file, newsletter, reference="", email_confirmed=False):
reader = csv.DictReader(csv_file)
csv_format = check_csv_format(reader)

for row in reader:
email = row["email"]
if "name" in row:
name = row.get("name", "")
elif "first_name" in row and "last_name" in row:
email = row[csv_format["email"]]

if csv_format["name"] == "combine":
name = "{} {}".format(row["first_name"], row["last_name"])
else:
name = ""
name = row.get(csv_format["name"], "")

subscribe_email(
newsletter,
email,
email=email,
name=name,
email_confirmed=email_confirmed,
reference=reference,
Expand Down
Loading