Added subject field to transaction and course field to customuser

This commit is contained in:
Keannu Bernasol 2023-12-29 18:38:59 +08:00
parent 0b1c065a80
commit 2ddc9da179
12 changed files with 55 additions and 52 deletions

View file

@ -9,7 +9,7 @@ class CustomUserAdmin(UserAdmin):
list_display = UserAdmin.list_display + \
('is_technician', 'is_teacher', 'id')
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('is_technician', 'is_teacher')}),
(None, {'fields': ('is_technician', 'is_teacher', 'course')}),
)

View file

@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2023-12-08 14:41
# Generated by Django 4.2.7 on 2023-12-29 10:21
import accounts.models
import django.contrib.auth.models
@ -32,6 +32,7 @@ class Migration(migrations.Migration):
('is_active', models.BooleanField(default=False)),
('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)),
('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')),

View file

@ -39,6 +39,16 @@ class CustomUser(AbstractUser):
is_active = models.BooleanField(default=False)
is_technician = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
COURSE_CHOICES = (
('BS Chemistry', 'BS Chemistry'),
('BS Food Technology', 'BS Food Technology'),
('BS Applied Physics', 'BS Applied Physics'),
('BS Environmental Science', 'BS Environmental Science'),
)
course = models.CharField(
max_length=60, null=True, choices=COURSE_CHOICES, default=None)
avatar = models.ImageField(upload_to=_get_upload_to, null=True)
@property
@ -108,7 +118,7 @@ def create_superuser(sender, **kwargs):
if not User.objects.filter(username=username).exists():
# Create the superuser with is_active set to False
user = User.objects.create_user(
username=username, email=email, password=password, first_name=first_name, last_name=last_name)
username=username, email=email, password=password, first_name=first_name, last_name=last_name, course="BS Chemistry")
# Activate the user
user.is_active = True

View file

@ -24,12 +24,14 @@ class CustomUserSerializer(BaseUserSerializer):
class UserRegistrationSerializer(serializers.ModelSerializer):
email = serializers.EmailField(required=True)
course = serializers.ChoiceField(
choices=CustomUser.COURSE_CHOICES, required=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',
fields = ('username', 'email', 'password', 'avatar', 'course',
'first_name', 'last_name')
read_only_fields = ('is_teacher', 'is_technician')

View file

@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2023-12-08 15:33
# Generated by Django 4.2.7 on 2023-12-29 10:09
from django.db import migrations, models
import django.db.models.deletion
@ -10,8 +10,8 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('transactions', '0001_initial'),
('equipments', '0001_initial'),
('transactions', '0001_initial'),
]
operations = [

View file

@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2023-12-08 14:41
# Generated by Django 4.2.7 on 2023-12-29 10:09
from django.conf import settings
from django.db import migrations, models
@ -31,7 +31,7 @@ class Migration(migrations.Migration):
name='HistoricalEquipmentInstance',
fields=[
('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('status', models.CharField(choices=[('Working', 'Working'), ('Broken', 'Broken'), ('Borrowed', 'Borrowed')], default='PENDING', max_length=20)),
('status', models.CharField(choices=[('Available', 'Available'), ('Pending', 'Pending'), ('Broken', 'Broken'), ('Borrowed', 'Borrowed')], default='Available', max_length=20)),
('remarks', models.TextField(max_length=512, null=True)),
('date_added', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
('last_updated', models.DateTimeField(blank=True, editable=False)),
@ -77,7 +77,7 @@ class Migration(migrations.Migration):
name='EquipmentInstance',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('status', models.CharField(choices=[('Working', 'Working'), ('Broken', 'Broken'), ('Borrowed', 'Borrowed')], default='PENDING', max_length=20)),
('status', models.CharField(choices=[('Available', 'Available'), ('Pending', 'Pending'), ('Broken', 'Broken'), ('Borrowed', 'Borrowed')], default='Available', max_length=20)),
('remarks', models.TextField(max_length=512, null=True)),
('date_added', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
('last_updated', models.DateTimeField(auto_now=True)),

View file

@ -1,23 +0,0 @@
# Generated by Django 4.2.7 on 2023-12-16 06:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('equipments', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='equipmentinstance',
name='status',
field=models.CharField(choices=[('Available', 'Available'), ('Broken', 'Broken'), ('Borrowed', 'Borrowed')], default='Available', max_length=20),
),
migrations.AlterField(
model_name='historicalequipmentinstance',
name='status',
field=models.CharField(choices=[('Available', 'Available'), ('Broken', 'Broken'), ('Borrowed', 'Borrowed')], default='Available', max_length=20),
),
]

View file

@ -1201,6 +1201,18 @@ components:
required:
- cleared
- uncleared_transactions
CourseEnum:
enum:
- BS Chemistry
- BS Food Technology
- BS Applied Physics
- BS Environmental Science
type: string
description: |-
* `BS Chemistry` - BS Chemistry
* `BS Food Technology` - BS Food Technology
* `BS Applied Physics` - BS Applied Physics
* `BS Environmental Science` - BS Environmental Science
CustomUser:
type: object
properties:
@ -1565,6 +1577,8 @@ components:
type: integer
teacher:
type: integer
subject:
type: string
equipments:
type: array
items:
@ -1674,6 +1688,8 @@ components:
type: integer
teacher:
type: integer
subject:
type: string
equipments:
type: array
items:
@ -1692,6 +1708,7 @@ components:
- borrower
- equipments
- id
- subject
- teacher
- timestamp
- transaction_status
@ -1734,6 +1751,8 @@ components:
type: string
format: uri
nullable: true
course:
$ref: '#/components/schemas/CourseEnum'
first_name:
type: string
maxLength: 100
@ -1741,6 +1760,7 @@ components:
type: string
maxLength: 100
required:
- course
- email
- first_name
- last_name

View file

@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2023-12-08 14:41
# Generated by Django 4.2.7 on 2023-12-29 10:09
from django.conf import settings
from django.db import migrations, models
@ -20,7 +20,9 @@ class Migration(migrations.Migration):
name='Transaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('remarks', models.TextField(max_length=512, null=True)),
('transaction_status', models.CharField(choices=[('Pending Approval', 'Pending Approval'), ('Approved', 'Approved'), ('Rejected', 'Rejected'), ('Cancelled', 'Cancelled'), ('Borrowed', 'Borrowed'), ('Returned: Pending Checking', 'Returned: Pending Checking'), ('With Breakages: Pending Resolution', 'With Breakages: Pending Resolution'), ('Finalized', 'Finalized')], default='Pending', max_length=40)),
('subject', models.TextField(max_length=128)),
('timestamp', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
('borrower', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='borrowed_transactions', to=settings.AUTH_USER_MODEL)),
('equipments', models.ManyToManyField(to='equipments.equipmentinstance')),

View file

@ -1,18 +0,0 @@
# Generated by Django 4.2.7 on 2023-12-08 15:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('transactions', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='transaction',
name='remarks',
field=models.TextField(max_length=512, null=True),
),
]

View file

@ -33,6 +33,7 @@ class Transaction(models.Model):
equipments = models.ManyToManyField(EquipmentInstance)
transaction_status = models.CharField(
max_length=40, choices=TRANSACTION_STATUS_CHOICES, default='Pending')
subject = models.TextField(max_length=128)
timestamp = models.DateTimeField(default=now, editable=False)
def __str__(self):

View file

@ -30,6 +30,7 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
many=False, slug_field='id', queryset=CustomUser.objects.all(), required=True, allow_null=False)
equipments = serializers.SlugRelatedField(
many=True, slug_field='id', queryset=EquipmentInstance.objects.all(), required=True)
subject = serializers.CharField(required=True, allow_null=False)
timestamp = serializers.DateTimeField(
format="%m-%d-%Y %I:%M %p", read_only=True)
@ -38,7 +39,7 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Transaction
fields = ['id', 'borrower', 'teacher',
fields = ['id', 'borrower', 'teacher', 'subject',
'equipments', 'remarks', 'transaction_status', 'timestamp']
read_only_fields = ['id', 'timestamp']
@ -149,6 +150,13 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
"You cannot change the equipments of an already created transaction"
)
# Subject Validation
# Do not allow changes to subject on created transactions
if 'subject' in validated_data:
raise serializers.ValidationError(
"You cannot change the subject of an already created transaction"
)
# Transaction Status Validation
# Check if the update involves the transaction status
if 'transaction_status' in validated_data: