From da6e4a15105a16578dd8245ecdbd2082f42f3cbd Mon Sep 17 00:00:00 2001 From: Roberamelaek Date: Fri, 25 Aug 2023 17:58:28 -0400 Subject: [PATCH 1/2] make root user and the test for the model students --- api/serializers.py | 14 +++++++-- api/urls.py | 3 +- api/views.py | 8 +++-- backend/admin.py | 2 ++ backend/migrations/0002_mainuser_student.py | 34 +++++++++++++++++++++ backend/migrations/0002_rootuser_student.py | 29 ------------------ backend/models.py | 20 +++++++----- backend/tests/test_models.py | 14 ++++++--- 8 files changed, 77 insertions(+), 47 deletions(-) create mode 100644 backend/migrations/0002_mainuser_student.py delete mode 100644 backend/migrations/0002_rootuser_student.py diff --git a/api/serializers.py b/api/serializers.py index cd9c66c6..f4924230 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -1,5 +1,6 @@ +from dataclasses import fields from rest_framework import serializers -from backend.models import Event, Student +from backend.models import Event, Mainuser, Student, Mentor class EventSerializer(serializers.ModelSerializer): class Meta: @@ -9,7 +10,16 @@ class Meta: # the student class class StudentSerializer(serializers.ModelSerializer): + class Meta: + model = Mainuser + fields = ('__all__') + +class MainSerializer(serializers.ModelSerializer): class Meta: model = Student - fields = ('id', 'first_name','last_name','email') + fields = ('__all__') +class MentorSerializer(serializers.ModelSerializer): + class Meta: + model = Mentor + fields = ('__all__') diff --git a/api/urls.py b/api/urls.py index 8314ea23..44dbc7e9 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,10 +1,11 @@ from django.urls import path -from .views import EventDetail, EventList# type: ignore +from .views import EventDetail, EventList, MainList app_name = 'api' urlpatterns = [ path('/', EventDetail.as_view(), name='detail'), path('', EventList.as_view(), name='list'), + path('mainlist/', MainList.as_view(), name='detail'), ] diff --git a/api/views.py b/api/views.py index 5906a993..b86f2a25 100644 --- a/api/views.py +++ b/api/views.py @@ -1,6 +1,6 @@ from rest_framework import generics -from backend.models import Event -from .serializers import EventSerializer +from backend.models import Event, Mainuser +from .serializers import EventSerializer, MentorSerializer, MainSerializer, StudentSerializer class EventList(generics.ListCreateAPIView): @@ -10,3 +10,7 @@ class EventList(generics.ListCreateAPIView): class EventDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Event.objects.all() serializer_class = EventSerializer + +class MainList(generics.ListCreateAPIView): + queryset = Mainuser.objects.all() + serializer_class = MainSerializer diff --git a/backend/admin.py b/backend/admin.py index ae2562c1..423bd37a 100644 --- a/backend/admin.py +++ b/backend/admin.py @@ -14,5 +14,7 @@ class EventAdmin(admin.ModelAdmin): admin.site.register(models.Category) # Register your models here. +admin.site.register(models.Mainuser) + admin.site.register(models.Student) diff --git a/backend/migrations/0002_mainuser_student.py b/backend/migrations/0002_mainuser_student.py new file mode 100644 index 00000000..a83e5629 --- /dev/null +++ b/backend/migrations/0002_mainuser_student.py @@ -0,0 +1,34 @@ +# Generated by Django 4.1.2 on 2023-08-25 17:28 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('backend', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Mainuser', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('fullname', models.CharField(max_length=100)), + ('username', models.CharField(max_length=200, unique=True)), + ('password', models.CharField(max_length=250)), + ('email', models.EmailField(max_length=254, unique=True)), + ('jnumber', models.IntegerField()), + ('role', models.CharField(max_length=100)), + ], + ), + migrations.CreateModel( + name='Student', + fields=[ + ('mainuser_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, to='backend.mainuser')), + ('student_id', models.AutoField(primary_key=True, serialize=False)), + ], + bases=('backend.mainuser',), + ), + ] diff --git a/backend/migrations/0002_rootuser_student.py b/backend/migrations/0002_rootuser_student.py deleted file mode 100644 index 3914aea2..00000000 --- a/backend/migrations/0002_rootuser_student.py +++ /dev/null @@ -1,29 +0,0 @@ -# Generated by Django 4.1.2 on 2023-08-15 23:18 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('backend', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='Rootuser', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=100)), - ], - ), - migrations.CreateModel( - name='Student', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('first_name', models.CharField(max_length=100)), - ('last_name', models.CharField(max_length=100)), - ('email', models.EmailField(max_length=254, unique=True)), - ], - ), - ] diff --git a/backend/models.py b/backend/models.py index 07693b34..8162e245 100644 --- a/backend/models.py +++ b/backend/models.py @@ -20,27 +20,30 @@ # is_superuser = models.BooleanField(default=False) # name = models.CharField(max_length=100, blank=False, null=True) # phone = models.CharField(max_length=20, blank=True, null=True) - - -class Rootuser(models.Model): - name = models.CharField(max_length=100) - class Category(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name -class Student(models.Model): +class Mainuser(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) + username = models.CharField(max_length=200,unique=True) + password = models.CharField(max_length=250) email = models.EmailField(unique=True) + jnumber = models.IntegerField() + role = models.CharField(max_length=100) +class Student(Mainuser): + + student_id = models.AutoField(primary_key=True) - def __str__(self): - return f"{self.first_name} {self.last_name}" +class Mentor(Mainuser): + + mentor_id = models.AutoField(primary_key=True) # # create a model with two different user # class Mentor(models.Model): # user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='mentor') @@ -52,6 +55,7 @@ def __str__(self): # user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='student') # category = models.ManyToManyField(Category) # def __str__(self): + # return self.user.username # Create Event model with title, content, date_posted, student, mentor, start_date, end_date, category, status diff --git a/backend/tests/test_models.py b/backend/tests/test_models.py index 9a4cefcd..ccb43c84 100644 --- a/backend/tests/test_models.py +++ b/backend/tests/test_models.py @@ -42,11 +42,15 @@ def test_event_content(self): self.assertEqual(str(category), 'django') def test_student(self): - test_user = Student.objects.create(first_name='Robera',last_name='Melaek',email='email@gmail.com') + test_user = Student.objects.create(first_name='Robera',last_name='Melaek',username='Melaek',password='Password',email='email@gmail.com',jnumber=1,role='student') username = Student.objects.get(id=1) - assert(username.first_name,'Robera') - - - + self.assertEqual(f'{username.username}','Melaek') + self.assertEqual(f'{username.last_name}','Melaek') + self.assertEqual(f'{username.password}','Password') + self.assertEqual(f'{username.jnumber}','1') + self.assertEqual(f'{username.role}','student') + self.assertEqual(f'{username.email}','email@gmail.com') + self.assertEqual(f'{username.first_name}','Robera') + self.assertEqual(f'{username.student_id}','1') From 7dad8fc7486586c068670bad7f9155b498ade0e0 Mon Sep 17 00:00:00 2001 From: Roberamelaek <69109461+Roberamelaek@users.noreply.github.com> Date: Tue, 29 Aug 2023 18:28:00 -0400 Subject: [PATCH 2/2] Delete 0002_mainuser_student.py --- backend/migrations/0002_mainuser_student.py | 34 --------------------- 1 file changed, 34 deletions(-) delete mode 100644 backend/migrations/0002_mainuser_student.py diff --git a/backend/migrations/0002_mainuser_student.py b/backend/migrations/0002_mainuser_student.py deleted file mode 100644 index a83e5629..00000000 --- a/backend/migrations/0002_mainuser_student.py +++ /dev/null @@ -1,34 +0,0 @@ -# Generated by Django 4.1.2 on 2023-08-25 17:28 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('backend', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='Mainuser', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('fullname', models.CharField(max_length=100)), - ('username', models.CharField(max_length=200, unique=True)), - ('password', models.CharField(max_length=250)), - ('email', models.EmailField(max_length=254, unique=True)), - ('jnumber', models.IntegerField()), - ('role', models.CharField(max_length=100)), - ], - ), - migrations.CreateModel( - name='Student', - fields=[ - ('mainuser_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, to='backend.mainuser')), - ('student_id', models.AutoField(primary_key=True, serialize=False)), - ], - bases=('backend.mainuser',), - ), - ]