-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from NUS-Fintech-Society/feature/TAS-9-extend-…
…user-model Feature/TAS 9 Extend User Model
- Loading branch information
Showing
22 changed files
with
251 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
from django.core.management import BaseCommand | ||
from django.contrib.auth.models import User | ||
|
||
from random import choice | ||
|
||
from authentication.models import AtlasUser | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Populates the database" | ||
|
@@ -16,18 +20,26 @@ def add_arguments(self, parser): | |
|
||
def create_users(self, verbose=False): | ||
User.objects.all().delete() | ||
AtlasUser.objects.all().delete() | ||
|
||
user = User.objects.create_superuser("admin", "[email protected]", "password") | ||
user.save() | ||
admin_profile = AtlasUser(user=user, department=choice(list(AtlasUser.DepartmentNames)), role=AtlasUser.Roles.ADMIN) | ||
admin_profile.save() | ||
|
||
if verbose: | ||
self.stdout.write(f"Created superuser {user.username} {user.email}") | ||
for i in range(1, 6): | ||
self.stdout.write(f"Created admin profile {admin_profile}") | ||
for i in range(5): | ||
user = User.objects.create_user( | ||
f"user{i}", f"user{i}@example.com", "password" | ||
f"user{i + 1}", f"user{i + 1}@example.com", "password" | ||
) | ||
user.save() | ||
profile = AtlasUser(user=user, department=choice(list(AtlasUser.DepartmentNames))) | ||
profile.save() | ||
if verbose: | ||
self.stdout.write(f"Created user {user.username} {user.email}") | ||
self.stdout.write(f"Created profile {profile}") | ||
|
||
def handle(self, *args, **options): | ||
self.stdout.write("Populating database...") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from django.contrib import admin | ||
from .models import AtlasUser | ||
|
||
# Register your models here. | ||
admin.site.register(AtlasUser) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class AuthConfig(AppConfig): | ||
class AuthenticationConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "auth" | ||
name = "authentication" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Generated by Django 5.0.2 on 2024-02-09 18:45 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="AtlasUser", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"department", | ||
models.CharField( | ||
blank=True, | ||
choices=[ | ||
("ML", "Machine Learning"), | ||
("BC", "Blockchain"), | ||
("SD", "Software Development"), | ||
("QD", "Quant Department"), | ||
("ER", "External Relations"), | ||
("IA", "Internal Affairs"), | ||
], | ||
max_length=50, | ||
), | ||
), | ||
( | ||
"user", | ||
models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
), | ||
] |
20 changes: 20 additions & 0 deletions
20
atlas_3/authentication/migrations/0002_atlasuser_profile_pic.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 5.0.2 on 2024-02-10 06:01 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("authentication", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="atlasuser", | ||
name="profile_pic", | ||
field=models.ImageField( | ||
default="profile/user/blank_profile.png", upload_to="profile/user/" | ||
), | ||
), | ||
] |
40 changes: 40 additions & 0 deletions
40
atlas_3/authentication/migrations/0003_atlasuser_role_atlasuser_telegram_handle_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Generated by Django 5.0.2 on 2024-02-10 08:34 | ||
|
||
import authentication.models | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("authentication", "0002_atlasuser_profile_pic"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="atlasuser", | ||
name="role", | ||
field=models.CharField( | ||
choices=[ | ||
("ADMIN", "Admin"), | ||
("MEMBER", "Member"), | ||
("APPLICANT", "Applicant"), | ||
], | ||
default="APPLICANT", | ||
max_length=50, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="atlasuser", | ||
name="telegram_handle", | ||
field=models.CharField(blank=True, max_length=250), | ||
), | ||
migrations.AlterField( | ||
model_name="atlasuser", | ||
name="profile_pic", | ||
field=models.ImageField( | ||
default="profile/blank_profile.png", | ||
upload_to=authentication.models.profile_pic_path, | ||
), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
atlas_3/authentication/migrations/0004_rename_profile_pic_atlasuser_profile_picture.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.0.2 on 2024-02-10 10:41 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("authentication", "0003_atlasuser_role_atlasuser_telegram_handle_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name="atlasuser", | ||
old_name="profile_pic", | ||
new_name="profile_picture", | ||
), | ||
] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.contrib.auth.models import User | ||
from django.db import models | ||
from django.utils.text import slugify | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from hashlib import sha1 | ||
|
||
|
||
def profile_pic_path(instance, filename: str) -> str: | ||
""" | ||
This function computes the path of where to store the profile picture | ||
:param instance: An instance of AtlasUser based on the current user | ||
:param filename: original filename | ||
:return: relative path of profile picture from MEDIA_ROOT | ||
""" | ||
ext = filename.split('.')[-1] | ||
return f"profile/{sha1(instance.user.username.encode()).hexdigest()}/{slugify(filename)}.{ext}" | ||
|
||
|
||
# Create your models here. | ||
class AtlasUser(models.Model): | ||
class DepartmentNames(models.TextChoices): | ||
MACHINE_LEARNING = "ML", _("Machine Learning") | ||
BLOCKCHAIN = "BC", _("Blockchain") | ||
SOFTWARE_DEVELOPMENT = "SD", _("Software Development") | ||
QUANT_DEPARTMENT = "QD", _("Quant Department") | ||
EXTERNAL_RELATIONS = "ER", _("External Relations") | ||
INTERNAL_AFFAIRS = "IA", _("Internal Affairs") | ||
|
||
class Roles(models.TextChoices): | ||
ADMIN = "ADMIN", _("Admin") | ||
MEMBER = "MEMBER", _("Member") | ||
APPLICANT = "APPLICANT", _("Applicant") | ||
|
||
user = models.OneToOneField(User, on_delete=models.CASCADE) | ||
department = models.CharField(max_length=50, choices=DepartmentNames.choices, blank=True) | ||
profile_picture = models.ImageField(upload_to=profile_pic_path, default='profile/blank_profile.png') | ||
role = models.CharField(max_length=50, choices=Roles.choices, default=Roles.APPLICANT) | ||
telegram_handle = models.CharField(max_length=250, blank=True) | ||
|
||
def __str__(self): | ||
return f"{self.user.username} in {self.get_department_display()} department" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.contrib.auth.models import User | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from django.test import TestCase | ||
|
||
import os | ||
|
||
from authentication.models import AtlasUser | ||
|
||
|
||
# Create your tests here. | ||
class AtlasUserModelTest(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create_user('test', email='[email protected]', password='password') | ||
self.user.save() | ||
self.deleteFiles = [] | ||
|
||
def tearDown(self): | ||
parent_folders = set() | ||
for file in self.deleteFiles: | ||
parent_folders.add(os.path.abspath(os.path.dirname(file))) | ||
os.remove(file) | ||
for folder in parent_folders: | ||
os.rmdir(folder) | ||
|
||
def test_can_create_atlas_user(self): | ||
atlas_user = AtlasUser(user=self.user, department=AtlasUser.DepartmentNames.SOFTWARE_DEVELOPMENT, role=AtlasUser.Roles.MEMBER) | ||
atlas_user.save() | ||
self.assertEqual(str(atlas_user), 'test in Software Development department') | ||
|
||
def test_atlas_user_can_save_profile_picture(self): | ||
atlas_user = AtlasUser(user=self.user, department=AtlasUser.DepartmentNames.SOFTWARE_DEVELOPMENT, role=AtlasUser.Roles.MEMBER) | ||
atlas_user.save() | ||
uploaded_file = SimpleUploadedFile('profile_picture.jpeg', open('./tests/assets/profile/profile_picture.jpeg', 'rb').read(), 'image/jpeg') | ||
atlas_user.profile_picture = uploaded_file | ||
atlas_user.save() | ||
self.deleteFiles.append(atlas_user.profile_picture.path) | ||
self.assertTrue(os.path.exists(atlas_user.profile_picture.path)) |
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters