Merge branch 'member_id'

This commit is contained in:
Tanner Collin 2021-11-28 05:30:58 +00:00
commit ef24a264a6
7 changed files with 57 additions and 99 deletions

View File

@ -86,6 +86,8 @@ class Transaction(models.Model):
history = HistoricalRecords() history = HistoricalRecords()
class PayPalHint(models.Model): class PayPalHint(models.Model):
user = models.ForeignKey(User, related_name='hints', blank=True, null=True, on_delete=models.SET_NULL)
account = models.CharField(unique=True, max_length=13) account = models.CharField(unique=True, max_length=13)
member_id = models.IntegerField() member_id = models.IntegerField()

View File

@ -81,8 +81,7 @@ class TransactionSerializer(serializers.ModelSerializer):
def create(self, validated_data): def create(self, validated_data):
member = get_object_or_404(models.Member, id=validated_data['member_id']) member = get_object_or_404(models.Member, id=validated_data['member_id'])
if member.user: validated_data['user'] = member.user
validated_data['user'] = member.user
if validated_data['account_type'] != 'Clearing': if validated_data['account_type'] != 'Clearing':
if validated_data['amount'] == 0: if validated_data['amount'] == 0:
@ -106,12 +105,9 @@ class TransactionSerializer(serializers.ModelSerializer):
return super().update(instance, validated_data) return super().update(instance, validated_data)
def get_member_name(self, obj): def get_member_name(self, obj):
if not obj.member_id: return 'Unknown' if not obj.user: return 'Unknown'
if obj.user: member = obj.user.member
member = obj.user.member
else:
member = models.Member.objects.get(id=obj.member_id)
return member.preferred_name + ' ' + member.last_name return member.preferred_name + ' ' + member.last_name
def get_recorder(self, obj): def get_recorder(self, obj):
@ -210,11 +206,8 @@ class MemberSerializer(serializers.ModelSerializer):
] ]
def update(self, instance, validated_data): def update(self, instance, validated_data):
if instance.user: instance.user.email = validated_data.get('email', instance.user.email)
instance.user.email = validated_data.get('email', instance.user.email) instance.user.save()
instance.user.save()
else:
instance.old_email = validated_data.get('email', instance.old_email)
photo = validated_data.get('photo', None) photo = validated_data.get('photo', None)
crop = validated_data.get('crop', None) crop = validated_data.get('crop', None)
@ -328,10 +321,7 @@ class InstructorSearchSerializer(serializers.Serializer):
return serializer.data return serializer.data
def get_training(self, obj): def get_training(self, obj):
if obj.user: queryset = obj.user.training
queryset = obj.user.training
else:
queryset = models.Training.objects.filter(member_id=obj.id)
serializer = UserTrainingSerializer(data=queryset, many=True) serializer = UserTrainingSerializer(data=queryset, many=True)
serializer.is_valid() serializer.is_valid()
return serializer.data return serializer.data
@ -349,39 +339,27 @@ class AdminSearchSerializer(serializers.Serializer):
return serializer.data return serializer.data
def get_cards(self, obj): def get_cards(self, obj):
if obj.user: queryset = obj.user.cards
queryset = obj.user.cards
else:
queryset = models.Card.objects.filter(member_id=obj.id)
queryset = queryset.order_by('-last_seen') queryset = queryset.order_by('-last_seen')
serializer = CardSerializer(data=queryset, many=True) serializer = CardSerializer(data=queryset, many=True)
serializer.is_valid() serializer.is_valid()
return serializer.data return serializer.data
def get_training(self, obj): def get_training(self, obj):
if obj.user: queryset = obj.user.training
queryset = obj.user.training
else:
queryset = models.Training.objects.filter(member_id=obj.id)
serializer = UserTrainingSerializer(data=queryset, many=True) serializer = UserTrainingSerializer(data=queryset, many=True)
serializer.is_valid() serializer.is_valid()
return serializer.data return serializer.data
def get_transactions(self, obj): def get_transactions(self, obj):
if obj.user: queryset = obj.user.transactions
queryset = obj.user.transactions
else:
queryset = models.Transaction.objects.filter(member_id=obj.id)
queryset = queryset.order_by('-id', '-date') queryset = queryset.order_by('-id', '-date')
serializer = TransactionSerializer(data=queryset, many=True) serializer = TransactionSerializer(data=queryset, many=True)
serializer.is_valid() serializer.is_valid()
return serializer.data return serializer.data
#def get_usages(self, obj): #def get_usages(self, obj):
# if obj.user: # queryset = obj.user.usages.order_by('-start_time')
# queryset = obj.user.usages.order_by('-start_time')
# else:
# queryset = []
# serializer = UsageSerializer(data=queryset, many=True) # serializer = UsageSerializer(data=queryset, many=True)
# serializer.is_valid() # serializer.is_valid()
# return serializer.data # return serializer.data
@ -411,8 +389,7 @@ class CardSerializer(serializers.ModelSerializer):
def create(self, validated_data): def create(self, validated_data):
member = get_object_or_404(models.Member, id=validated_data['member_id']) member = get_object_or_404(models.Member, id=validated_data['member_id'])
if member.user: validated_data['user'] = member.user
validated_data['user'] = member.user
if not member.vetted_date: if not member.vetted_date:
raise ValidationError(dict(non_field_errors='Member not vetted yet.')) raise ValidationError(dict(non_field_errors='Member not vetted yet.'))
@ -439,24 +416,14 @@ class TrainingSerializer(serializers.ModelSerializer):
read_only_fields = ['user', 'sign_up_date', 'paid_date'] read_only_fields = ['user', 'sign_up_date', 'paid_date']
def get_student_name(self, obj): def get_student_name(self, obj):
if obj.user: member = obj.user.member
member = obj.user.member
else:
member = models.Member.objects.get(id=obj.member_id)
return member.preferred_name + ' ' + member.last_name return member.preferred_name + ' ' + member.last_name
def get_student_email(self, obj): def get_student_email(self, obj):
if obj.user: return obj.user.email
return obj.user.email
else:
member = models.Member.objects.get(id=obj.member_id)
return member.old_email
def get_student_id(self, obj): def get_student_id(self, obj):
if obj.user: return obj.user.member.id
return obj.user.member.id
else:
return obj.member_id
class StudentTrainingSerializer(TrainingSerializer): class StudentTrainingSerializer(TrainingSerializer):

View File

@ -90,7 +90,7 @@ def fake_missing_membership_months(member):
missing_months = num_months_spanned(expire_date, start_date) missing_months = num_months_spanned(expire_date, start_date)
user = member.user if member.user else None user = member.user
tx = False tx = False
for i in range(missing_months): for i in range(missing_months):
memo = '{} / {} month membership dues accounting old portal import, {} to {} - hidden'.format( memo = '{} / {} month membership dues accounting old portal import, {} to {} - hidden'.format(
@ -101,7 +101,6 @@ def fake_missing_membership_months(member):
amount=0, amount=0,
user=user, user=user,
memo=memo, memo=memo,
member_id=member.id,
reference_number='', reference_number='',
info_source='System', info_source='System',
payment_method='N/A', payment_method='N/A',
@ -124,7 +123,7 @@ def tally_membership_months(member, fake_date=None):
if not start_date: return False if not start_date: return False
txs = models.Transaction.objects.filter( txs = models.Transaction.objects.filter(
member_id=member.id, user__member=member,
date__gte=start_date, date__gte=start_date,
) )
total_months_agg = txs.aggregate(Sum('number_of_membership_months')) total_months_agg = txs.aggregate(Sum('number_of_membership_months'))
@ -156,10 +155,7 @@ def gen_search_strings():
m.last_name, m.last_name,
) )
if m.old_email: string += ' | ' + m.user.email
string += ' | ' + m.old_email
if m.user:
string += ' | ' + m.user.email
string += ' | ' + str(m.id) string += ' | ' + str(m.id)
string = string.lower() string = string.lower()

View File

@ -45,10 +45,7 @@ def add_to_group(member, group):
try: try:
ldap_data = dict(group=group) ldap_data = dict(group=group)
if member.user: ldap_data['username'] = member.user.username
ldap_data['username'] = member.user.username
else:
ldap_data['email'] = member.old_email
if ldap_api('add-to-group', ldap_data) != 200: raise if ldap_api('add-to-group', ldap_data) != 200: raise
except BaseException as e: except BaseException as e:
@ -62,10 +59,7 @@ def remove_from_group(member, group):
try: try:
ldap_data = dict(group=group) ldap_data = dict(group=group)
if member.user: ldap_data['username'] = member.user.username
ldap_data['username'] = member.user.username
else:
ldap_data['email'] = member.old_email
if ldap_api('remove-from-group', ldap_data) != 200: raise if ldap_api('remove-from-group', ldap_data) != 200: raise
except BaseException as e: except BaseException as e:

View File

@ -161,7 +161,6 @@ def create_member_dues_tx(data, member, num_months, deal):
tx = transactions.create( tx = transactions.create(
**build_tx(data), **build_tx(data),
member_id=member.id,
memo=memo, memo=memo,
category='Membership', category='Membership',
number_of_membership_months=num_months, number_of_membership_months=num_months,
@ -184,7 +183,6 @@ def create_unmatched_purchase_tx(data, member):
return transactions.create( return transactions.create(
**build_tx(data), **build_tx(data),
member_id=member.id,
report_memo=report_memo, report_memo=report_memo,
report_type='Unmatched Purchase', report_type='Unmatched Purchase',
user=user, user=user,
@ -205,7 +203,6 @@ def create_member_training_tx(data, member, training):
return transactions.create( return transactions.create(
**build_tx(data), **build_tx(data),
member_id=member.id,
category='OnAcct', category='OnAcct',
memo=memo, memo=memo,
user=user, user=user,
@ -231,9 +228,6 @@ def check_training(data, training_id, amount):
if training.session.cost != amount: if training.session.cost != amount:
return False return False
if not training.user:
return False
member = training.user.member member = training.user.member
training.attendance_status = 'Confirmed' training.attendance_status = 'Confirmed'
@ -257,7 +251,6 @@ def create_category_tx(data, member, custom_json):
return transactions.create( return transactions.create(
**build_tx(data), **build_tx(data),
member_id=member.id,
category=custom_json['category'], category=custom_json['category'],
memo=memo, memo=memo,
user=user, user=user,
@ -324,21 +317,18 @@ def process_paypal_ipn(data):
update_ipn(ipn, 'Accepted, training') update_ipn(ipn, 'Accepted, training')
hints.update_or_create( hints.update_or_create(
account=data.get('payer_id', 'unknown'), account=data.get('payer_id', 'unknown'),
defaults=dict(member_id=tx.member_id), defaults=dict(user=tx.user),
) )
return tx return tx
member = False
member_id = False member_id = False
if not member_id and hints.filter(account=data.get('payer_id', False)).exists(): if hints.filter(account=data.get('payer_id', False)).exists():
member_id = hints.get(account=data['payer_id']).member_id member = hints.get(account=data['payer_id']).user.member
if not member_id and 'member' in custom_json: if not member_id and 'member' in custom_json:
member_id = custom_json['member'] member_id = custom_json['member']
hints.update_or_create(
account=data.get('payer_id', 'unknown'),
defaults=dict(member_id=member_id),
)
if not members.filter(id=member_id).exists(): if not members.filter(id=member_id).exists():
logger.info('IPN - Unable to associate with member, reporting') logger.info('IPN - Unable to associate with member, reporting')
@ -347,6 +337,11 @@ def process_paypal_ipn(data):
member = members.get(id=member_id) member = members.get(id=member_id)
hints.update_or_create(
account=data.get('payer_id', 'unknown'),
defaults=dict(user=member.user),
)
if custom_json.get('category', False) in ['Snacks', 'OnAcct', 'Donation']: if custom_json.get('category', False) in ['Snacks', 'OnAcct', 'Donation']:
logger.info('IPN - Category matched') logger.info('IPN - Category matched')
update_ipn(ipn, 'Accepted, category') update_ipn(ipn, 'Accepted, category')

View File

@ -321,16 +321,15 @@ class TrainingViewSet(Base, Retrieve, Create, Update):
raise exceptions.ValidationError('Not allowed to register others') raise exceptions.ValidationError('Not allowed to register others')
member = get_object_or_404(models.Member, id=data['member_id']) member = get_object_or_404(models.Member, id=data['member_id'])
user = getattr(member, 'user', None) user = member.user
training1 = models.Training.objects.filter(user=user, session=session) training1 = models.Training.objects.filter(user=user, session=session)
training2 = models.Training.objects.filter(member_id=member.id, session=session) if training1.exists()
if (user and training1.exists()) or training2.exists():
raise exceptions.ValidationError(dict(non_field_errors='Already registered.')) raise exceptions.ValidationError(dict(non_field_errors='Already registered.'))
self.update_cert(session, member, status) self.update_cert(session, member, status)
serializer.save(user=user, member_id=member.id, attendance_status=status) serializer.save(user=user, attendance_status=status)
else: else:
training = models.Training.objects.filter(user=user, session=session) training = models.Training.objects.filter(user=user, session=session)
if training.exists(): if training.exists():
@ -349,11 +348,7 @@ class TrainingViewSet(Base, Retrieve, Create, Update):
status = 'Confirmed' status = 'Confirmed'
training = serializer.save(attendance_status=status) training = serializer.save(attendance_status=status)
member = training.user.member
if training.user:
member = training.user.member
else:
member = models.Member.objects.get(id=training.member_id)
self.update_cert(session, member, status) self.update_cert(session, member, status)
@ -388,10 +383,10 @@ class TransactionViewSet(Base, List, Create, Retrieve, Update):
utils.tally_membership_months(member) utils.tally_membership_months(member)
def train_paypal_hint(self, tx): def train_paypal_hint(self, tx):
if tx.paypal_payer_id and tx.member_id: if tx.paypal_payer_id:
models.PayPalHint.objects.update_or_create( models.PayPalHint.objects.update_or_create(
account=tx.paypal_payer_id, account=tx.paypal_payer_id,
defaults=dict(member_id=tx.member_id), defaults=dict(user=tx.user),
) )
def perform_create(self, serializer): def perform_create(self, serializer):
@ -448,10 +443,7 @@ class DoorViewSet(viewsets.ViewSet, List):
active_member_cards = {} active_member_cards = {}
for card in cards: for card in cards:
try: member = card.user.member
member = models.Member.objects.get(id=card.member_id)
except models.Member.DoesNotExist:
continue
if member.paused_date: continue if member.paused_date: continue
if not member.is_allowed_entry: continue if not member.is_allowed_entry: continue
@ -468,10 +460,7 @@ class DoorViewSet(viewsets.ViewSet, List):
card.last_seen = now() card.last_seen = now()
card.save() card.save()
try: member = card.user.member
member = models.Member.objects.get(id=card.member_id)
except models.Member.DoesNotExist:
raise Http404
t = utils.now_alberta_tz().strftime('%Y-%m-%d %H:%M:%S, %a %I:%M %p') t = utils.now_alberta_tz().strftime('%Y-%m-%d %H:%M:%S, %a %I:%M %p')
logger.info('Time: {} - Name: {} {} ({})'.format(t, member.first_name, member.last_name, member.id)) logger.info('Time: {} - Name: {} {} ({})'.format(t, member.first_name, member.last_name, member.id))
@ -490,10 +479,7 @@ class LockoutViewSet(viewsets.ViewSet, List):
active_member_cards = {} active_member_cards = {}
for card in cards: for card in cards:
try: member = card.user.member
member = models.Member.objects.get(id=card.member_id)
except models.Member.DoesNotExist:
continue
if member.paused_date: continue if member.paused_date: continue
if not member.is_allowed_entry: continue if not member.is_allowed_entry: continue

View File

@ -71,8 +71,26 @@ for member in members:
x.update(user=user) x.update(user=user)
print(' Linked', x.count(), 'trainings') print(' Linked', x.count(), 'trainings')
x = models.PayPalHint.objects.filter(member_id=member.id)
x.update(user=user)
print(' Linked', x.count(), 'hints')
count += 1 count += 1
print() print()
print('Generated', count, 'users.') print('Generated', count, 'users.')
print('Deleting orphan cards...')
count = models.Card.objects.filter(user__isnull=True).delete()[0]
print('Deleted', count, 'cards.')
print('Deleting orphan trainings...')
count = models.Training.objects.filter(user__isnull=True).delete()[0]
print('Deleted', count, 'trainings.')
print('Deleting orphan hints...')
count = models.PayPalHint.objects.filter(user__isnull=True).delete()[0]
print('Deleted', count, 'hints.')
print('Done.')