Skip to content

Commit

Permalink
make root user and the test for the model students
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberamelaek committed Aug 29, 2023
1 parent cb464f9 commit e36a1fc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 47 deletions.
11 changes: 8 additions & 3 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -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

class EventSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -10,6 +11,10 @@ class Meta:

class StudentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = ('id', 'first_name','last_name','email')
model = Mainuser
fields = ('__all__')

class MainSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = ('__all__')
2 changes: 2 additions & 0 deletions backend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

34 changes: 34 additions & 0 deletions backend/migrations/0002_mainuser_student.py
Original file line number Diff line number Diff line change
@@ -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',),
),
]
29 changes: 0 additions & 29 deletions backend/migrations/0002_rootuser_student.py

This file was deleted.

22 changes: 10 additions & 12 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,24 @@
# 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)
fullname = 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)


def __str__(self):
return f"{self.first_name} {self.last_name}"

class Student(Mainuser):

student_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')
Expand All @@ -52,6 +49,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
Expand Down
5 changes: 2 additions & 3 deletions backend/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ 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 protected]')
test_user = Student.objects.create(fullname='Robera',username='Melaek',email='[email protected]',jnumber=1,role='teacher')
username = Student.objects.get(id=1)
assert(username.first_name,'Robera')

self.assertEqual(f'{username.username}','Melaek')



Expand Down

0 comments on commit e36a1fc

Please sign in to comment.