more update_or_create

This commit is contained in:
Andreas Madsack 2018-01-13 12:55:33 +01:00
parent c1fd9d6365
commit cc2f729e39

View File

@ -130,33 +130,36 @@ def _import_transactions(member_data, member):
def import_member(member_data): def import_member(member_data):
member = Member.objects.create( member, _ = Member.objects.update_or_create(
number=member_data['number'], number=member_data['number'],
name=member_data['name'], defaults={
address=member_data['address'], 'name': member_data['name'],
email=member_data['email'], 'address': member_data['address'],
) 'email': member_data['email'],
profile = ShackProfile.objects.create( })
profile, _ = ShackProfile.objects.update_or_create(
member=member, member=member,
has_loeffelhardt_account = member_data.get('has_loeffelhardt_account', False), defaults={
has_matomat_key = member_data.get('has_matomat_key', False), 'has_loeffelhardt_account': member_data.get('has_loeffelhardt_account', False),
has_metro_card = member_data.get('has_metro_card', False), 'has_matomat_key': member_data.get('has_matomat_key', False),
has_selgros_card = member_data.get('has_selgros_card', False), 'has_metro_card': member_data.get('has_metro_card', False),
has_shack_iron_key = member_data.get('has_shack_iron_key', False), 'has_selgros_card': member_data.get('has_selgros_card', False),
has_snackomat_key = member_data.get('has_snackomat_key', False), 'has_shack_iron_key': member_data.get('has_shack_iron_key', False),
is_keyholder = member_data.get('is_keyholder', False), 'has_snackomat_key': member_data.get('has_snackomat_key', False),
signed_DSV = member_data.get('signed_DSV', False), 'is_keyholder': member_data.get('is_keyholder', False),
ssh_public_key = member_data.get('ssh_public_key', False), 'signed_DSV': member_data.get('signed_DSV', False),
) 'ssh_public_key': member_data.get('ssh_public_key', False),
})
memberships = member_data.get('memberships') memberships = member_data.get('memberships')
last = None last = None
for membership in sorted(memberships, key=lambda m: m['membership_start']): for membership in sorted(memberships, key=lambda m: m['membership_start']):
obj = Membership.objects.create( obj = Membership.objects.update_or_create(
member=member, member=member,
start=parse_date(membership['membership_start']), defaults={
amount=Decimal(membership['membership_fee_monthly'])*membership['membership_fee_interval'], 'start': parse_date(membership['membership_start']),
interval=membership['membership_fee_interval'], 'amount': Decimal(membership['membership_fee_monthly'])*membership['membership_fee_interval'],
) 'interval': membership['membership_fee_interval'],
})
if last: if last:
last.end = obj.start - timedelta(days=1) last.end = obj.start - timedelta(days=1)
last.save(update_fields=['end']) last.save(update_fields=['end'])