Merge pull request 'Refactoring 42Oauth' (#48) from refactor/42OAuth into main

Reviewed-on: 42CTF/website#48
This commit is contained in:
Starthur 2022-05-17 15:48:05 +02:00
commit 4213883774
5 changed files with 81 additions and 5 deletions

View File

@ -1,5 +1,6 @@
from .models import UserProfileInfo from .models import UserProfileInfo
from django.contrib import admin from django.contrib import admin
from .models import Campus
#admin.site.register(UserProfileInfo) #admin.site.register(UserProfileInfo)
# Register your models here. # Register your models here.
@ -10,3 +11,7 @@ class userprofile(admin.ModelAdmin):
list_display = ['user', 'score', 'last_submission_date', 'intra42_campus'] list_display = ['user', 'score', 'last_submission_date', 'intra42_campus']
# search list # search list
search_fields = ['score', 'user__username', 'intra42_campus'] search_fields = ['score', 'user__username', 'intra42_campus']
@admin.register(Campus)
class campus(admin.ModelAdmin):
list_display = ['name']

View File

@ -0,0 +1,31 @@
# Generated by Django 3.2.11 on 2022-05-17 12:52
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('accounts', '0009_auto_20220329_1339'),
]
operations = [
migrations.CreateModel(
name='Campus',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False, unique=True)),
('name', models.CharField(max_length=50)),
],
options={
'verbose_name': 'campus',
'verbose_name_plural': 'campuses',
'permissions': (('view_info', 'View user info'),),
},
),
migrations.AddField(
model_name='userprofileinfo',
name='cmapus',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='accounts.campus'),
),
]

View File

@ -0,0 +1,23 @@
# Generated by Django 3.2.11 on 2022-05-17 12:54
from django.db import migrations
from accounts.models import UserProfileInfo
from accounts.models import Campus
def migrate_campus(apps, schema_editor):
UserProfileInfo = apps.get_model('accounts', 'UserProfileInfo')
Campus = apps.get_model('accounts', 'Campus')
for user in UserProfileInfo.objects.all():
if user.intra42_campus_id:
user.campus, created = Campus.objects.get_or_create(id=user.intra42_campus_id, name=user.intra42_campus)
user.save()
class Migration(migrations.Migration):
dependencies = [
('accounts', '0010_auto_20220517_1452'),
]
operations = [
migrations.RunPython(migrate_campus),
]

View File

@ -13,6 +13,7 @@ class UserProfileInfo(models.Model):
token = models.CharField(max_length=200, blank=True) token = models.CharField(max_length=200, blank=True)
discord_id = models.CharField(max_length=20, null=True, blank=True, unique=True) discord_id = models.CharField(max_length=20, null=True, blank=True, unique=True)
intra42_id = models.CharField(max_length=20, null=True, blank=True, unique=True) intra42_id = models.CharField(max_length=20, null=True, blank=True, unique=True)
cmapus = models.ForeignKey('Campus', on_delete=models.DO_NOTHING, null=True, blank=True)
intra42_campus = models.CharField(max_length=50, null=True, blank=True) intra42_campus = models.CharField(max_length=50, null=True, blank=True)
intra42_campus_id = models.CharField(max_length=10, null=True, blank=True) intra42_campus_id = models.CharField(max_length=10, null=True, blank=True)
member = models.BooleanField(default=False) member = models.BooleanField(default=False)
@ -26,4 +27,14 @@ class UserProfileInfo(models.Model):
verbose_name_plural = 'profiles' verbose_name_plural = 'profiles'
permissions = (("view_info", "View user info"),) permissions = (("view_info", "View user info"),)
class Campus(models.Model):
id = models.IntegerField(primary_key=True, unique=True)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Meta:
verbose_name = 'campus'
verbose_name_plural = 'campuses'
permissions = (("view_info", "View user info"),)
# Create your models here. # Create your models here.

View File

@ -5,6 +5,8 @@ from django.views.defaults import bad_request
from django.urls import reverse from django.urls import reverse
from django.shortcuts import redirect from django.shortcuts import redirect
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from accounts.models import Campus
from django.db import IntegrityError
import os import os
oauth = OAuth() oauth = OAuth()
@ -52,10 +54,12 @@ def authorize_intra42(request):
intra42_campus = response['campus'][0]['name'] intra42_campus = response['campus'][0]['name']
intra42_campus_id = response['campus'][0]['id'] intra42_campus_id = response['campus'][0]['id']
request.user.userprofileinfo.intra42_id = intra42_id request.user.userprofileinfo.intra42_id = intra42_id
request.user.userprofileinfo.intra42_campus = intra42_campus request.user.userprofileinfo.campus, created = Campus.objects.get_or_create(id=intra42_campus_id, name=intra42_campus)
request.user.userprofileinfo.intra42_campus_id = intra42_campus_id try:
request.user.userprofileinfo.save() request.user.userprofileinfo.save()
return redirect('accounts:edit') return redirect('accounts:edit')
except IntegrityError:
return redirect('accounts:edit')
@login_required @login_required
@require_POST @require_POST
@ -64,6 +68,8 @@ def disconnect_intra42(request):
return bad_request(request, "Already disconnected") return bad_request(request, "Already disconnected")
request.user.userprofileinfo.intra42_id = None request.user.userprofileinfo.intra42_id = None
request.user.userprofileinfo.intra42_campus = None request.user.userprofileinfo.intra42_campus = None
request.user.userprofileinfo.intra42_campus_id = None
request.user.userprofileinfo.campus = None
request.user.userprofileinfo.save() request.user.userprofileinfo.save()
return redirect('accounts:edit') return redirect('accounts:edit')