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

Issue 59 v2:Filter Posts By Role #74

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
7 changes: 4 additions & 3 deletions posts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
class PostSerializer(HyperlinkedModelSerializer):
creator = HyperlinkedRelatedField(
queryset=BaseUser.objects.all(), many=False,
view_name='user-detail')
view_name='baseuser-detail')
comments = HyperlinkedRelatedField(
required=False,
queryset=Comment.objects.all(), many=True,
view_name='comment-detail')

Expand All @@ -20,13 +21,13 @@ class Meta:
class CommentSerializer(HyperlinkedModelSerializer):
user = HyperlinkedRelatedField(
queryset=BaseUser.objects.all(), many=False,
view_name='user-detail')
view_name='baseuser-detail')
post = HyperlinkedRelatedField(
queryset=Post.objects.all(), many=False,
view_name='post-detail')
likes = HyperlinkedRelatedField(
queryset=BaseUser.objects.all(), many=True,
view_name='user-detail')
view_name='baseuser-detail')

class Meta:
model = Comment
Expand Down
39 changes: 31 additions & 8 deletions posts/views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
from posts.models import Post, Comment
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from posts.serializers import CommentSerializer, PostSerializer
from posts.serializers import *
from user_mgmt.serializers import *


class PostViewSet(ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer

def get_queryset(self):
search_term = self.request.query_params.get('search_terms')
if search_term:
queryset = Post.objects.filter(creotor__contains=search_term)

class PostByRole(generics.RetrieveAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
# renderer_classes = [TemplateHTMLRenderer]

def get(self, request, *args, **kwargs):
if kwargs == {'role': 'students'}:
print('role is student')
student_ids = Student.objects.values_list('baseuser_ptr_id')
student_posts = Post.objects.filter(creator__id__in=student_ids)
serializer = PostSerializer(student_posts, many=True, context={'request': request})
return Response({"student_posts": serializer.data}, template_name='posts/posts_by_role.html')
elif kwargs == {'role': 'teachers'}:
print('role is teacher')
teacher_ids = Teacher.objects.values_list('baseuser_ptr_id')
teacher_posts = Post.objects.filter(creator__id__in=teacher_ids)
serializer = PostSerializer(teacher_posts, many=True, context={'request': request})
return Response({"teacher_posts": serializer.data}, template_name='posts/posts_by_role.html')
elif kwargs == {'role': 'institutions'}:
print('role is institution')
institution_ids = Institution.objects.values_list('baseuser_ptr_id')
institution_posts = Post.objects.filter(creator__id__in=institution_ids)
serializer = PostSerializer(institution_posts, many=True, context={'request': request})
return Response({"institution_posts": serializer.data}, template_name='posts/posts_by_role.html')
else:
queryset = Post.objects.all()
return queryset
serializer = PostSerializer([], many=True, context={'request': request})
return Response({"No Posts by this role yet": serializer.data}, template_name='posts/posts_by_role.html')


class CommentViewSet(ModelViewSet):
Expand Down
12 changes: 9 additions & 3 deletions tdd_class_social/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter

from posts.views import PostViewSet, CommentViewSet
from user_mgmt.views import BaseUserViewSet
from posts.views import PostViewSet, CommentViewSet, PostByRole
from user_mgmt.views import BaseUserViewSet, InstitutionViewSet, StudentViewSet, TeacherViewSet, RegisterInstitution

router = DefaultRouter()
router.register(r'users', BaseUserViewSet)
router.register(r'institutions', InstitutionViewSet)
router.register(r'students', StudentViewSet)
router.register(r'teachers', TeacherViewSet)
router.register(r'posts', PostViewSet)
router.register(r'comments', CommentViewSet)

urlpatterns = [
path('', include(router.urls)),
path('admin/', admin.site.urls),
]
path('institutions/register', RegisterInstitution.as_view()),
path('posts_by=<str:role>/', PostByRole.as_view(), name='<str:role>_posts'),
]
15 changes: 9 additions & 6 deletions user_mgmt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class BaseUser(models.Model):
avatar = models.ImageField(null=True, blank=True)
website = models.URLField(max_length=300, null=True, blank=True)
about = models.TextField(max_length=1000, null=True, blank=True)
connections = models.ManyToManyField('self', symmetrical=False, related_name='users', blank=True)
connections = models.ManyToManyField('self', symmetrical=False, null=True, blank=True)

def __str__(self):
return self.username


class Course(models.Model):

id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)

Expand All @@ -35,30 +36,32 @@ def __str__(self):


class Institution(BaseUser):

name = models.CharField(max_length=300)
associates = models.ManyToManyField(BaseUser, symmetrical=False, related_name='institutions', blank=True)
head_of_organization = models.CharField(max_length=300)
research_institution = models.BooleanField(default=False, null=True, blank=True)
education_institution = models.BooleanField(default=False, null=True, blank=True)
courses = models.ManyToManyField(Course, symmetrical=False, related_name='courses', blank=True)
courses = models.ManyToManyField(Course, symmetrical=False, related_name='courses', null=True, blank=True)


class Student(BaseUser):

first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
courses = models.ManyToManyField(Course, symmetrical=False, related_name='students')
institution = models.ManyToManyField(Institution, symmetrical=False, related_name='students')
courses = models.ManyToManyField(Course, symmetrical=False, related_name='students', null=True, blank=True)
institution = models.ManyToManyField(Institution, symmetrical=False, related_name='students', null=True, blank=True)
interests = models.TextField(max_length=200, null=True, blank=True)
date_of_birth = models.DateField(null=True, blank=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='Unicorn')


class Teacher(BaseUser):

first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
courses = models.ManyToManyField(Course, symmetrical=False, related_name='teacher')
institution = models.ManyToManyField(Institution, symmetrical=False, related_name='teacher')
courses = models.ManyToManyField(Course, symmetrical=False, related_name='teacher', null=True, blank=True)
institution = models.ManyToManyField(Institution, symmetrical=False, related_name='teacher', null=True, blank=True)
interests = models.TextField(max_length=200, null=True, blank=True)
date_of_birth = models.DateField(null=True, blank=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='Unicorn')
35 changes: 32 additions & 3 deletions user_mgmt/serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
from rest_framework.serializers import HyperlinkedModelSerializer

from user_mgmt.models import BaseUser
from user_mgmt.models import BaseUser, Institution, Student, Teacher


class BaseUserSerializer(HyperlinkedModelSerializer):

class Meta:
model = BaseUser
fields = ['id', 'username', 'password', 'email', 'created_on', 'is_active', 'address',
'phone_number', 'avatar', 'website', 'about', 'connections', 'url']
fields = ['id', 'username', 'password', 'email', 'created_on', 'is_active', 'address', 'phone_number', 'avatar',
'website', 'about', 'connections', 'url']


class UpdateSerializer(HyperlinkedModelSerializer):

class Meta:
model = BaseUser
fields = '__all__'


class InstitutionSerializer(HyperlinkedModelSerializer):

class Meta:
model = Institution
fields = ['id', 'username', 'password', 'email', 'created_on', 'is_active', 'address', 'phone_number', 'avatar',
'website', 'about', 'url', 'name', 'head_of_organization', 'research_institution', 'education_institution']


class StudentSerializer(HyperlinkedModelSerializer):

class Meta:
model = Student
fields = '__all__'


class TeacherSerializer(HyperlinkedModelSerializer):

class Meta:
model = Teacher
fields = '__all__'
41 changes: 39 additions & 2 deletions user_mgmt/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
from rest_framework import generics
from rest_framework.viewsets import ModelViewSet

from user_mgmt.models import BaseUser
from user_mgmt.serializers import BaseUserSerializer
from user_mgmt.models import BaseUser, Institution, Student, Teacher
from user_mgmt.serializers import BaseUserSerializer, InstitutionSerializer, StudentSerializer, TeacherSerializer, \
UpdateSerializer


class BaseUserViewSet(ModelViewSet):
queryset = BaseUser.objects.all()
serializer_class = BaseUserSerializer


class InstitutionViewSet(ModelViewSet):
queryset = Institution.objects.all()
serializer_class = InstitutionSerializer


class RegisterInstitution(generics.CreateAPIView):
queryset = Institution.objects.all()
serializer_class = InstitutionSerializer

def create(self, validated_data):
institution = Institution(**validated_data)
institution.save()
return institution


class UpdateBaseUser(generics.UpdateAPIView):
queryset = BaseUser.objects.all()
serializer_class = UpdateSerializer

def update(self, validated_data):
user = BaseUser(**validated_data)
user.save()
return user


class StudentViewSet(ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer


class TeacherViewSet(ModelViewSet):
queryset = Teacher.objects.all()
serializer_class = TeacherSerializer