Skip to content

Commit

Permalink
Matching users using email instead of wrong ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Mstiekema committed Dec 12, 2023
1 parent 25ff808 commit c355a3c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion admin_board_view/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def index(request):
total_balance = sum(user.balance for user in User.objects.all())
return render(request, "home.html", {"users": User.objects.all(), "product_amount": product_amount, "total_balance": total_balance, "top_types": top_up_types })
else:
user = User.objects.get(user_id=request.user.id)
user = User.objects.get(email=request.user.email)

# Get product sales
product_sales = list(ProductTransactions.objects.all().filter(transaction_id__user_id=user))
Expand Down
46 changes: 38 additions & 8 deletions koala-migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,39 @@ def get_mongoose_connection():
koala = get_koala_connection()
mongoose = get_mongoose_connection()


def migrate_email():
query = """
select members.id, members.email
from public.members
inner join checkout_balances on members.id = checkout_balances.member_id;
"""

with koala:
koala_cursor = koala.cursor()
koala_cursor.execute(query)

for user in koala_cursor.fetchall():
id = user[0]
email = user[1]
update_email(id, email)


def update_email(id, email):
query = """
update public.mongoose_app_user
set email = %s
where user_id = %s;
"""

with mongoose:
mongoose_cursor = mongoose.cursor()
mongoose_cursor.execute(query, (email, id))


def migrate_users():
select_members_with_tegoed = """
select members.id, members.first_name, members.infix, members.last_name, members.birth_date, checkout_balances.balance
select members.id, members.first_name, members.infix, members.last_name, members.birth_date, checkout_balances.balance, members.email
from public.members
inner join checkout_balances on members.id = checkout_balances.member_id;
"""
Expand All @@ -46,20 +76,20 @@ def migrate_users():

birthday = user[4]
balance = user[5]
email = user[6]

create_user_with_tegoed(id, balance, name, birthday)
create_user_with_tegoed(id, balance, name, birthday, email)

# TODO: use execute_many to insert a whole batch at once and reuse the connection
def create_user_with_tegoed(id, balance, name, birthday):

def create_user_with_tegoed(id, balance, name, birthday, email):
query = """
insert into public.mongoose_app_user(user_id, balance, name, birthday)
values (%s, %s, %s, %s);
insert into public.mongoose_app_user(user_id, balance, name, birthday, email)
values (%s, %s, %s, %s, %s);
"""

with mongoose:
mongoose_cursor = mongoose.cursor()

mongoose_cursor.execute(query, (id, balance, name, birthday))
mongoose_cursor.execute(query, (id, balance, name, birthday, email))


def migrate_cards():
Expand Down
23 changes: 23 additions & 0 deletions mongoose_app/migrations/0028_save_email_in_user_add_mollie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.0.2 on 2023-12-12 15:39

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mongoose_app', '0027_added_configuration_model'),
]

operations = [
migrations.AddField(
model_name='user',
name='email',
field=models.EmailField(blank=True, max_length=254, null=True),
),
migrations.AlterField(
model_name='topuptransaction',
name='type',
field=models.IntegerField(choices=[(1, 'Pin'), (2, 'Credit card'), (3, 'Mollie')], default=1),
),
]
1 change: 1 addition & 0 deletions mongoose_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class User(models.Model):
user_id = models.IntegerField(unique=True)
name = models.CharField(max_length=50)
birthday = models.DateField()
email = models.EmailField(max_length=254, null=True, blank=True)
balance = models.DecimalField(decimal_places=2, max_digits=6, default=Decimal('0.00'))

def __str__(self):
Expand Down
3 changes: 2 additions & 1 deletion mongoose_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def register_card(request):
user = User.objects.create(
user_id=user_id,
name=f'{first_name} {infix} {last_name}' if infix else f'{first_name} {last_name}',
birthday=born
birthday=born,
email=koala_response['email']
)
card = Card.objects.create(
card_id=card_id,
Expand Down
3 changes: 0 additions & 3 deletions undead_mongoose/oidc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from mozilla_django_oidc.auth import OIDCAuthenticationBackend
#from myapp.models import Profile

class UndeadMongooseOIDC(OIDCAuthenticationBackend):
def create_user(self, claims):
Expand All @@ -8,7 +7,6 @@ def create_user(self, claims):
user.is_superuser = True
user.is_staff = True
user.username = claims['email']
user.id = claims['sub']
user.save()

return user
Expand All @@ -18,7 +16,6 @@ def update_user(self, user, claims):
user.is_superuser = True
user.is_staff = True
user.username = claims['email']
user.id = claims['sub']
user.save()

return user

0 comments on commit c355a3c

Please sign in to comment.