Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add rootuser model to models.py #77

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 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, Mentor

class EventSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -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__')
3 changes: 2 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
@@ -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('<int:pk>/', EventDetail.as_view(), name='detail'),
path('', EventList.as_view(), name='list'),
path('mainlist/', MainList.as_view(), name='detail'),

]
8 changes: 6 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
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)

29 changes: 0 additions & 29 deletions backend/migrations/0002_rootuser_student.py

This file was deleted.

20 changes: 12 additions & 8 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions backend/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]')
test_user = Student.objects.create(first_name='Robera',last_name='Melaek',username='Melaek',password='Password',email='[email protected]',jnumber=1,role='student')
username = Student.objects.get(id=1)
kirubeltadesse marked this conversation as resolved.
Show resolved Hide resolved
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 protected]')
self.assertEqual(f'{username.first_name}','Robera')
self.assertEqual(f'{username.student_id}','1')