Skip to content

Commit

Permalink
username fix (#61)
Browse files Browse the repository at this point in the history
* Removed intra_username

* changed intra_username >> username in views.py
  • Loading branch information
PukieDiederik authored Oct 22, 2023
1 parent 75ea0ea commit 4bb0d2f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion backend/portfolio42_api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .models import User, Project, Skill, Cursus

class UserAdmin(admin.ModelAdmin):
search_fields = ['intra_username', 'first_name', 'last_name', 'email', 'intra_id']
search_fields = ['username', 'first_name', 'last_name', 'email', 'intra_id']

class ProjectAdmin(admin.ModelAdmin):
search_fields = ['name', 'description', 'intra_id']
Expand Down
2 changes: 1 addition & 1 deletion backend/portfolio42_api/management/commands/sync_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def update_from_db(api : Api42, table, endpoint : str, func : callable, is_basic
json = api.get(ep)
except ApiException as e:
logging.error(e)
break
continue

if (is_basic):
func(json)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.3 on 2023-10-14 00:15

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('portfolio42_api', '0008_cursususer_updated_at'),
]

operations = [
migrations.RemoveField(
model_name='user',
name='intra_username',
),
]
8 changes: 3 additions & 5 deletions backend/portfolio42_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def log_update(t, is_created):

# User model
class User(AbstractUser, IntraBaseModel):
intra_username = models.CharField(max_length=30, unique=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(max_length=130)
Expand All @@ -38,11 +37,10 @@ class User(AbstractUser, IntraBaseModel):

cursus = models.ManyToManyField('Cursus', through='CursusUser', related_name='users')

REQUIRED_FIELDS = ["email", "intra_id", "first_name", "last_name", 'intra_url', 'username']
USERNAME_FIELD = "intra_username"
REQUIRED_FIELDS = ["email", "intra_id", "first_name", "last_name", 'intra_url']

def __str__(self):
return f"({self.intra_username})"
return f"({self.username})"

def update(user):
try:
Expand All @@ -51,7 +49,7 @@ def update(user):
logging.error(f"Could not find User with (intra_id: {user['id']})")
return None

u.intra_username = user['login']
u.username = user['login']
u.first_name = user['first_name']
u.last_name = user['last_name']
u.email = user['email']
Expand Down
6 changes: 3 additions & 3 deletions backend/portfolio42_api/serializers/CursusSerializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class Meta():
'description',
'exam',
'solo']

class CursusUserSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(source='id_user.id')
intra_id = serializers.IntegerField(source='id_user.intra_id')
intra_username = serializers.CharField(source='id_user.intra_username')
username = serializers.CharField(source='id_user.username')
first_name = serializers.CharField(source='id_user.first_name')
last_name = serializers.CharField(source='id_user.last_name')
email = serializers.EmailField(source='id_user.email')
Expand All @@ -33,7 +33,7 @@ class Meta():
model = CursusUser
fields = ['id',
'intra_id',
'intra_username',
'username',
'first_name',
'last_name',
'email',
Expand Down
2 changes: 1 addition & 1 deletion backend/portfolio42_api/serializers/ProjectSerializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Meta():
model = User
fields = ['id',
'intra_id',
'intra_username',
'username',
'first_name',
'last_name',
'email',
Expand Down
2 changes: 1 addition & 1 deletion backend/portfolio42_api/serializers/UserSerializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Meta():
model = User
fields = ['id',
'intra_id',
'intra_username',
'username',
'first_name',
'last_name',
'email',
Expand Down
8 changes: 4 additions & 4 deletions backend/portfolio42_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def callback_intra(request):
'redirect_uri': 'http://localhost:%s/api/auth/callback_intra' % os.environ.get('BACKEND_PORT'),
}
r = requests.post(get_token_path, data=data)

"""
Check if request was succesfull, if not,
return an error Response with the reason
Expand All @@ -53,7 +53,7 @@ def callback_intra(request):
headers = {"Authorization": "Bearer %s" % token}
except:
return HttpResponse("[Error] Reason: %s" % intra_fail_reason("json"));

"""
Check if request was succesfull, if not,
return an error Response with the reason
Expand All @@ -71,11 +71,11 @@ def callback_intra(request):
"""
TODO
Missing image_url, intra_url, check other
stuff that might be predefined...
stuff that might be predefined...
"""
user, created = User.objects.get_or_create(
intra_id=user_response_json['id'],
intra_username=user_response_json['login'],
username=user_response_json['login'],
first_name=user_response_json['first_name'],
last_name=user_response_json['last_name'],
email=user_response_json['email'],
Expand Down

0 comments on commit 4bb0d2f

Please sign in to comment.