diff --git a/equipment_tracker/accounts/migrations/0001_initial.py b/equipment_tracker/accounts/migrations/0001_initial.py index d5bd51e..2b5a61b 100644 --- a/equipment_tracker/accounts/migrations/0001_initial.py +++ b/equipment_tracker/accounts/migrations/0001_initial.py @@ -1,10 +1,9 @@ -# Generated by Django 4.2.7 on 2024-01-05 10:26 +# Generated by Django 5.0.1 on 2024-01-14 12:55 -import accounts.models import django.contrib.auth.models import django.contrib.auth.validators -from django.db import migrations, models import django.utils.timezone +from django.db import migrations, models class Migration(migrations.Migration): @@ -33,7 +32,7 @@ class Migration(migrations.Migration): ('is_technician', models.BooleanField(default=False)), ('is_teacher', models.BooleanField(default=False)), ('course', models.CharField(choices=[('BS Chemistry', 'BS Chemistry'), ('BS Food Technology', 'BS Food Technology'), ('BS Applied Physics', 'BS Applied Physics'), ('BS Environmental Science', 'BS Environmental Science')], default=None, max_length=60, null=True)), - ('avatar', models.ImageField(null=True, upload_to=accounts.models.CustomUser._get_upload_to)), + ('section', models.CharField(default=None, max_length=60, null=True)), ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), ], diff --git a/equipment_tracker/accounts/migrations/__pycache__/0001_initial.cpython-311.pyc b/equipment_tracker/accounts/migrations/__pycache__/0001_initial.cpython-311.pyc index c2f5599..945b698 100644 Binary files a/equipment_tracker/accounts/migrations/__pycache__/0001_initial.cpython-311.pyc and b/equipment_tracker/accounts/migrations/__pycache__/0001_initial.cpython-311.pyc differ diff --git a/equipment_tracker/accounts/migrations/__pycache__/__init__.cpython-311.pyc b/equipment_tracker/accounts/migrations/__pycache__/__init__.cpython-311.pyc index 17377aa..5a70566 100644 Binary files a/equipment_tracker/accounts/migrations/__pycache__/__init__.cpython-311.pyc and b/equipment_tracker/accounts/migrations/__pycache__/__init__.cpython-311.pyc differ diff --git a/equipment_tracker/accounts/models.py b/equipment_tracker/accounts/models.py index 1a94350..113c6e7 100644 --- a/equipment_tracker/accounts/models.py +++ b/equipment_tracker/accounts/models.py @@ -1,35 +1,12 @@ -from django.db import models + +from django.db.models.signals import post_migrate from django.contrib.auth.models import AbstractUser from django.db import models -from django.db.models.signals import post_migrate from django.dispatch import receiver import os -from uuid import uuid4 class CustomUser(AbstractUser): - # Function for avatar uploads - def _get_upload_to(instance, filename): - base_filename, file_extension = os.path.splitext(filename) - # Get the student ID number - ext = base_filename.split('.')[-1] - filename = '{}.{}'.format(uuid4().hex, ext) - - student_id = str(instance.student_id_number) - new_filename = f"{student_id}_{filename}_{file_extension}" - return os.path.join('avatars', new_filename) - - # Delete old avatar file if new one is uploaded - def save(self, *args, **kwargs): - try: - # is the object in the database yet? - this = CustomUser.objects.get(id=self.id) - if this.avatar != self.avatar: - this.avatar.delete(save=False) - except: - pass # when new photo then we do nothing, normal case - super(CustomUser, self).save(*args, **kwargs) - first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) # Email inherited from base user class @@ -49,7 +26,7 @@ class CustomUser(AbstractUser): course = models.CharField( max_length=60, null=True, choices=COURSE_CHOICES, default=None) - avatar = models.ImageField(upload_to=_get_upload_to, null=True) + section = models.CharField(max_length=60, null=True, default=None) @property def full_name(self): diff --git a/equipment_tracker/accounts/serializers.py b/equipment_tracker/accounts/serializers.py index 9e7bf89..9130474 100644 --- a/equipment_tracker/accounts/serializers.py +++ b/equipment_tracker/accounts/serializers.py @@ -1,39 +1,38 @@ -from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer from djoser.serializers import UserSerializer as BaseUserSerializer from django.core import exceptions as django_exceptions from rest_framework import serializers from accounts.models import CustomUser from rest_framework.settings import api_settings from django.contrib.auth.password_validation import validate_password -from django.utils.encoding import smart_str -from drf_spectacular.utils import extend_schema_field -from drf_spectacular.types import OpenApiTypes -from drf_extra_fields.fields import Base64ImageField - # There can be multiple subject instances with the same name, only differing in course, year level, and semester. We filter them here class CustomUserSerializer(BaseUserSerializer): - avatar = Base64ImageField() + course = serializers.ChoiceField( + choices=CustomUser.COURSE_CHOICES, allow_blank=True, allow_null=True) + section = serializers.CharField(allow_blank=True, allow_null=True) class Meta(BaseUserSerializer.Meta): model = CustomUser - fields = ('id', 'username', 'email', 'avatar', 'first_name', + fields = ('id', 'username', 'email', 'first_name', 'course', 'section', 'last_name', 'is_teacher', 'is_technician') + read_only_fields = ('id', 'username', 'email', + 'is_teacher', 'is_technician') class UserRegistrationSerializer(serializers.ModelSerializer): email = serializers.EmailField(required=True) course = serializers.ChoiceField( - choices=CustomUser.COURSE_CHOICES, required=True) + choices=CustomUser.COURSE_CHOICES, allow_blank=True, allow_null=True) + section = serializers.CharField(allow_blank=True, allow_null=True) password = serializers.CharField( write_only=True, style={'input_type': 'password', 'placeholder': 'Password'}) class Meta: model = CustomUser # Use your custom user model here - fields = ('username', 'email', 'password', 'avatar', 'course', + fields = ('username', 'email', 'password', 'course', 'section', 'first_name', 'last_name') - read_only_fields = ('is_teacher', 'is_technician') + read_only_fields = ('username', 'email', 'is_teacher', 'is_technician') def validate(self, attrs): user = self.Meta.model(**attrs) diff --git a/equipment_tracker/equipments/migrations/__pycache__/0001_initial.cpython-311.pyc b/equipment_tracker/equipments/migrations/__pycache__/0001_initial.cpython-311.pyc index f0376dc..c4c6e3c 100644 Binary files a/equipment_tracker/equipments/migrations/__pycache__/0001_initial.cpython-311.pyc and b/equipment_tracker/equipments/migrations/__pycache__/0001_initial.cpython-311.pyc differ diff --git a/equipment_tracker/equipments/migrations/__pycache__/__init__.cpython-311.pyc b/equipment_tracker/equipments/migrations/__pycache__/__init__.cpython-311.pyc index b83f87d..2c4bf88 100644 Binary files a/equipment_tracker/equipments/migrations/__pycache__/__init__.cpython-311.pyc and b/equipment_tracker/equipments/migrations/__pycache__/__init__.cpython-311.pyc differ diff --git a/equipment_tracker/equipments/models.py b/equipment_tracker/equipments/models.py index fe7fa45..dc14018 100644 --- a/equipment_tracker/equipments/models.py +++ b/equipment_tracker/equipments/models.py @@ -80,7 +80,7 @@ def create_superuser(sender, **kwargs): # Get equipment information category = filename.split('-')[0] - name = row[2] + ' ' + row[1] + name = row[1] # If quantity is missing, set value as 0 if (row[3] == ''): available = 0 diff --git a/equipment_tracker/schema.yml b/equipment_tracker/schema.yml index ff2e56f..84602d4 100644 --- a/equipment_tracker/schema.yml +++ b/equipment_tracker/schema.yml @@ -1139,6 +1139,9 @@ components: required: - token - uid + BlankEnum: + enum: + - '' BreakageReport: type: object properties: @@ -1210,12 +1213,18 @@ components: format: email title: Email address maxLength: 254 - avatar: - type: string - format: uri first_name: type: string maxLength: 100 + course: + nullable: true + oneOf: + - $ref: '#/components/schemas/CourseEnum' + - $ref: '#/components/schemas/NullEnum' + section: + type: string + nullable: true + maxLength: 60 last_name: type: string maxLength: 100 @@ -1224,7 +1233,6 @@ components: is_technician: type: boolean required: - - avatar - first_name - id - last_name @@ -1448,6 +1456,9 @@ components: - history_user - id - name + NullEnum: + enum: + - null PasswordResetConfirm: type: object properties: @@ -1477,12 +1488,18 @@ components: format: email title: Email address maxLength: 254 - avatar: - type: string - format: uri first_name: type: string maxLength: 100 + course: + nullable: true + oneOf: + - $ref: '#/components/schemas/CourseEnum' + - $ref: '#/components/schemas/NullEnum' + section: + type: string + nullable: true + maxLength: 60 last_name: type: string maxLength: 100 @@ -1736,12 +1753,15 @@ components: password: type: string writeOnly: true - avatar: - type: string - format: uri - nullable: true course: - $ref: '#/components/schemas/CourseEnum' + nullable: true + oneOf: + - $ref: '#/components/schemas/CourseEnum' + - $ref: '#/components/schemas/BlankEnum' + - $ref: '#/components/schemas/NullEnum' + section: + type: string + nullable: true first_name: type: string maxLength: 100 @@ -1754,6 +1774,7 @@ components: - first_name - last_name - password + - section - username UsernameResetConfirm: type: object diff --git a/equipment_tracker/transactions/migrations/0003_transaction_section.py b/equipment_tracker/transactions/migrations/0003_transaction_section.py deleted file mode 100644 index cdc19b0..0000000 --- a/equipment_tracker/transactions/migrations/0003_transaction_section.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 5.0.1 on 2024-01-11 03:58 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('transactions', '0002_alter_transaction_transaction_status'), - ] - - operations = [ - migrations.AddField( - model_name='transaction', - name='section', - field=models.CharField(max_length=255, null=True), - ), - ] diff --git a/equipment_tracker/transactions/models.py b/equipment_tracker/transactions/models.py index 08551f4..65f735c 100644 --- a/equipment_tracker/transactions/models.py +++ b/equipment_tracker/transactions/models.py @@ -38,7 +38,6 @@ class Transaction(models.Model): max_length=40, choices=TRANSACTION_STATUS_CHOICES, default='Pending', db_index=True) subject = models.TextField(max_length=128) timestamp = models.DateTimeField(default=now, editable=False) - section = models.CharField(max_length=255, null=True) def __str__(self): return f"Transaction #{self.id} under {self.teacher} by {self.borrower}" diff --git a/equipment_tracker/transactions/serializers.py b/equipment_tracker/transactions/serializers.py index 71bb333..ee57a97 100644 --- a/equipment_tracker/transactions/serializers.py +++ b/equipment_tracker/transactions/serializers.py @@ -41,7 +41,6 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer): required=False, allow_null=True, allow_blank=True) timestamp = serializers.DateTimeField( format="%m-%d-%Y %I:%M %p", read_only=True) - section = serializers.CharField(required=True, allow_null=False) transaction_status = serializers.ChoiceField( choices=Transaction.TRANSACTION_STATUS_CHOICES) @@ -49,7 +48,7 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Transaction fields = ['id', 'borrower', 'teacher', 'subject', - 'equipments', 'remarks', 'transaction_status', 'additional_members', 'consumables', 'timestamp', 'section'] + 'equipments', 'remarks', 'transaction_status', 'additional_members', 'consumables', 'timestamp'] read_only_fields = ['id', 'timestamp'] def to_representation(self, instance):