Remove all references to member_id hint

This commit is contained in:
Tanner Collin 2021-11-16 22:18:47 +00:00
parent be40624331
commit bbc9f03bdf
7 changed files with 57 additions and 99 deletions

View File

@ -86,6 +86,8 @@ class Transaction(models.Model):
history = HistoricalRecords()
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)
member_id = models.IntegerField()

View File

@ -81,8 +81,7 @@ class TransactionSerializer(serializers.ModelSerializer):
def create(self, validated_data):
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['amount'] == 0:
@ -106,12 +105,9 @@ class TransactionSerializer(serializers.ModelSerializer):
return super().update(instance, validated_data)
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
else:
member = models.Member.objects.get(id=obj.member_id)
member = obj.user.member
return member.preferred_name + ' ' + member.last_name
def get_recorder(self, obj):
@ -210,11 +206,8 @@ class MemberSerializer(serializers.ModelSerializer):
]
def update(self, instance, validated_data):
if instance.user:
instance.user.email = validated_data.get('email', instance.user.email)
instance.user.save()
else:
instance.old_email = validated_data.get('email', instance.old_email)
instance.user.email = validated_data.get('email', instance.user.email)
instance.user.save()
photo = validated_data.get('photo', None)
crop = validated_data.get('crop', None)
@ -328,10 +321,7 @@ class InstructorSearchSerializer(serializers.Serializer):
return serializer.data
def get_training(self, obj):
if obj.user:
queryset = obj.user.training
else:
queryset = models.Training.objects.filter(member_id=obj.id)
queryset = obj.user.training
serializer = UserTrainingSerializer(data=queryset, many=True)
serializer.is_valid()
return serializer.data
@ -349,39 +339,27 @@ class AdminSearchSerializer(serializers.Serializer):
return serializer.data
def get_cards(self, obj):
if obj.user:
queryset = obj.user.cards
else:
queryset = models.Card.objects.filter(member_id=obj.id)
queryset = obj.user.cards
queryset = queryset.order_by('-last_seen')
serializer = CardSerializer(data=queryset, many=True)
serializer.is_valid()
return serializer.data
def get_training(self, obj):
if obj.user:
queryset = obj.user.training
else:
queryset = models.Training.objects.filter(member_id=obj.id)
queryset = obj.user.training
serializer = UserTrainingSerializer(data=queryset, many=True)
serializer.is_valid()
return serializer.data
def get_transactions(self, obj):
if obj.user:
queryset = obj.user.transactions
else:
queryset = models.Transaction.objects.filter(member_id=obj.id)
queryset = obj.user.transactions
queryset = queryset.order_by('-id', '-date')
serializer = TransactionSerializer(data=queryset, many=True)
serializer.is_valid()
return serializer.data
#def get_usages(self, obj):
# if obj.user:
# queryset = obj.user.usages.order_by('-start_time')
# else:
# queryset = []
# queryset = obj.user.usages.order_by('-start_time')
# serializer = UsageSerializer(data=queryset, many=True)
# serializer.is_valid()
# return serializer.data
@ -411,8 +389,7 @@ class CardSerializer(serializers.ModelSerializer):
def create(self, validated_data):
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:
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']
def get_student_name(self, obj):
if obj.user:
member = obj.user.member
else:
member = models.Member.objects.get(id=obj.member_id)
member = obj.user.member
return member.preferred_name + ' ' + member.last_name
def get_student_email(self, obj):
if obj.user:
return obj.user.email
else:
member = models.Member.objects.get(id=obj.member_id)
return member.old_email
return obj.user.email
def get_student_id(self, obj):
if obj.user:
return obj.user.member.id
else:
return obj.member_id
return obj.user.member.id
class StudentTrainingSerializer(TrainingSerializer):

View File

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

View File

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

View File

@ -161,7 +161,6 @@ def create_member_dues_tx(data, member, num_months, deal):
tx = transactions.create(
**build_tx(data),
member_id=member.id,
memo=memo,
category='Membership',
number_of_membership_months=num_months,
@ -184,7 +183,6 @@ def create_unmatched_purchase_tx(data, member):
return transactions.create(
**build_tx(data),
member_id=member.id,
report_memo=report_memo,
report_type='Unmatched Purchase',
user=user,
@ -205,7 +203,6 @@ def create_member_training_tx(data, member, training):
return transactions.create(
**build_tx(data),
member_id=member.id,
category='OnAcct',
memo=memo,
user=user,
@ -231,9 +228,6 @@ def check_training(data, training_id, amount):
if training.session.cost != amount:
return False
if not training.user:
return False
member = training.user.member
training.attendance_status = 'Confirmed'
@ -257,7 +251,6 @@ def create_category_tx(data, member, custom_json):
return transactions.create(
**build_tx(data),
member_id=member.id,
category=custom_json['category'],
memo=memo,
user=user,
@ -324,21 +317,18 @@ def process_paypal_ipn(data):
update_ipn(ipn, 'Accepted, training')
hints.update_or_create(
account=data.get('payer_id', 'unknown'),
defaults=dict(member_id=tx.member_id),
defaults=dict(user=tx.user),
)
return tx
member = False
member_id = False
if not member_id and hints.filter(account=data.get('payer_id', False)).exists():
member_id = hints.get(account=data['payer_id']).member_id
if hints.filter(account=data.get('payer_id', False)).exists():
member = hints.get(account=data['payer_id']).user.member
if not member_id and 'member' in custom_json:
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():
logger.info('IPN - Unable to associate with member, reporting')
@ -347,6 +337,11 @@ def process_paypal_ipn(data):
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']:
logger.info('IPN - Category matched')
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')
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)
training2 = models.Training.objects.filter(member_id=member.id, session=session)
if (user and training1.exists()) or training2.exists():
if training1.exists()
raise exceptions.ValidationError(dict(non_field_errors='Already registered.'))
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:
training = models.Training.objects.filter(user=user, session=session)
if training.exists():
@ -349,11 +348,7 @@ class TrainingViewSet(Base, Retrieve, Create, Update):
status = 'Confirmed'
training = serializer.save(attendance_status=status)
if training.user:
member = training.user.member
else:
member = models.Member.objects.get(id=training.member_id)
member = training.user.member
self.update_cert(session, member, status)
@ -388,10 +383,10 @@ class TransactionViewSet(Base, List, Create, Retrieve, Update):
utils.tally_membership_months(member)
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(
account=tx.paypal_payer_id,
defaults=dict(member_id=tx.member_id),
defaults=dict(user=tx.user),
)
def perform_create(self, serializer):
@ -448,10 +443,7 @@ class DoorViewSet(viewsets.ViewSet, List):
active_member_cards = {}
for card in cards:
try:
member = models.Member.objects.get(id=card.member_id)
except models.Member.DoesNotExist:
continue
member = card.user.member
if member.paused_date: continue
if not member.is_allowed_entry: continue
@ -468,10 +460,7 @@ class DoorViewSet(viewsets.ViewSet, List):
card.last_seen = now()
card.save()
try:
member = models.Member.objects.get(id=card.member_id)
except models.Member.DoesNotExist:
raise Http404
member = card.user.member
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))
@ -490,10 +479,7 @@ class LockoutViewSet(viewsets.ViewSet, List):
active_member_cards = {}
for card in cards:
try:
member = models.Member.objects.get(id=card.member_id)
except models.Member.DoesNotExist:
continue
member = card.user.member
if member.paused_date: continue
if not member.is_allowed_entry: continue

View File

@ -71,8 +71,26 @@ for member in members:
x.update(user=user)
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
print()
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.')