membershipworks: Ensure that all expected fields are present in data

This commit is contained in:
Adam Goldsmith 2022-03-02 17:17:30 -05:00
parent cd63a169aa
commit 7563e5dcea

View File

@ -15,23 +15,18 @@ class BaseModel(models.Model):
class Meta:
abstract = True
@classmethod
def _get_header_map(cls):
header_map = {
field.db_column: field.name
for field in cls._meta.get_fields()
if not field.auto_created
}
header_map.update(cls._csv_headers_override)
return header_map
@classmethod
def _remap_headers(cls, data):
header_map = cls._get_header_map()
for k, v in data.items():
if k in header_map:
yield header_map.get(k), v
for field in cls._meta.get_fields():
# TODO: more robust filtering of fields that don't have a column
if field.auto_created or field.many_to_many or not field.concrete:
continue
csv_header = field.column
if field.name in cls._csv_headers_override:
csv_header = cls._csv_headers_override[field.name]
yield field.name, data[csv_header]
@classmethod
def from_csv_dict(cls, data):
@ -215,10 +210,10 @@ class Member(BaseModel):
flags = models.ManyToManyField(Flag, through="MemberFlag", related_name="members")
_csv_headers_override = {
"Account ID": "uid",
"Please tell us how you heard about the Claremont MakerSpace and what tools or shops you are most excited to start using:": "how_did_you_hear",
"Yes - I authorize TwinState MakerSpaces, Inc. to charge my credit card for the membership and other options that I have selected.": "authorize_charge",
"I have read the Claremont MakerSpace Membership Agreement & Policies, and agree to all terms stated therein.": "policy_agreement",
"uid": "Account ID",
"how_did_you_hear": "Please tell us how you heard about the Claremont MakerSpace and what tools or shops you are most excited to start using:",
"authorize_charge": "Yes - I authorize TwinState MakerSpaces, Inc. to charge my credit card for the membership and other options that I have selected.",
"policy_agreement": "I have read the Claremont MakerSpace Membership Agreement & Policies, and agree to all terms stated therein.",
}
_date_fields = {
@ -318,10 +313,10 @@ class Transaction(BaseModel):
return super().from_csv_dict(txn)
_csv_headers_override = {
"uid": "member_id",
"_dp": "timestamp",
"Transaction Type": "type",
"Event/Form Name": "for_what",
"member_id": "uid",
"timestamp": "_dp",
"type": "Transaction Type",
"for_what": "Event/Form Name",
}
def __str__(self):