diff --git a/admin_board_view/views.py b/admin_board_view/views.py index 912ba82..15549c0 100644 --- a/admin_board_view/views.py +++ b/admin_board_view/views.py @@ -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)) diff --git a/koala-migrate.py b/koala-migrate.py index 0e56714..ee9f492 100755 --- a/koala-migrate.py +++ b/koala-migrate.py @@ -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; """ @@ -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(): diff --git a/mongoose_app/migrations/0028_save_email_in_user_add_mollie.py b/mongoose_app/migrations/0028_save_email_in_user_add_mollie.py new file mode 100644 index 0000000..fcf547b --- /dev/null +++ b/mongoose_app/migrations/0028_save_email_in_user_add_mollie.py @@ -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), + ), + ] diff --git a/mongoose_app/models.py b/mongoose_app/models.py index 25664c5..384dff0 100644 --- a/mongoose_app/models.py +++ b/mongoose_app/models.py @@ -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): diff --git a/mongoose_app/views.py b/mongoose_app/views.py index fda482d..a08be95 100644 --- a/mongoose_app/views.py +++ b/mongoose_app/views.py @@ -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, diff --git a/undead_mongoose/oidc.py b/undead_mongoose/oidc.py index 5d2827c..f346390 100644 --- a/undead_mongoose/oidc.py +++ b/undead_mongoose/oidc.py @@ -1,5 +1,4 @@ from mozilla_django_oidc.auth import OIDCAuthenticationBackend -#from myapp.models import Profile class UndeadMongooseOIDC(OIDCAuthenticationBackend): def create_user(self, claims): @@ -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 @@ -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