Went on a coding binge and added api support for all new model objects.

This commit is contained in:
Andrew Lalis 2018-09-25 22:52:38 +02:00
parent d48e1547ad
commit 15f259f0ee
9 changed files with 234 additions and 8 deletions

View File

@ -19,7 +19,10 @@ from django.urls import re_path,path,include
from . import views
urlpatterns = [
# / Shows the main index page of the website.
path('', views.index, name='index'),
# /universities/1/ Shows the page for a university.
# TODO: add pages for each rateable entity.
path('admin/', admin.site.urls),
re_path(r'^api/postings/', include(('postings.api.urls','postings'), namespace='api-postings')),
]

Binary file not shown.

View File

@ -1,6 +1,12 @@
from django.contrib import admin
from .models import UniversityReview
from .models import *
# Register your models here.
admin.site.register(UniversityReview)
admin.site.register(UniversityReview)
admin.site.register(Review)
admin.site.register(RateableEntity)
admin.site.register(ReviewHelpfulVote)
admin.site.register(University)
admin.site.register(Professor)
admin.site.register(Course)

View File

@ -1,5 +1,5 @@
from rest_framework import serializers
from postings.models import UniversityReview
from postings.models import *
class UniversityReviewSerializer(serializers.ModelSerializer):
@ -16,4 +16,62 @@ class UniversityReviewSerializer(serializers.ModelSerializer):
read_only_fields =[
'pk',
'username'
]
# Serializes the generic Review object.
class ReviewSerializer(serializers.ModelSerializer):
class Meta:
model = Review
fields = [
'pk',
'rating',
'title',
'content',
'rateable_entity',
'created_date',
'last_updated_date'
]
read_only_fields = [
'pk',
'created_date',
'rateable_entity'
]
# Serializes Universities.
class UniversitySerializer(serializers.ModelSerializer):
class Meta:
model = University
fields = [
'pk',
'name'
]
read_only_fields = [
'pk'
]
# Serializes Courses.
class CourseSerializer(serializers.ModelSerializer):
class Meta:
model = Course
fields = [
'pk',
'name',
'taught_at_university',
'professors'
]
read_only_fields = [
'pk'
]
# Serializes Professors.
class ProfessorSerializer(serializers.ModelSerializer):
class Meta:
model = Professor
fields = [
'pk',
'name',
'universities'
]
read_only_fields = [
'pk'
]

View File

@ -1,11 +1,31 @@
from .views import UniReviewRudView,UniReviewAPIView
from django.urls import re_path
from .views import *
from django.urls import path, re_path
urlpatterns = [
# /api/postings/reviews/ Lists all review objects.
path('reviews/', ReviewsView.as_view(), name='reviews'),
# /api/postings/reviews/1/ Returns data for one Review.
path('reviews/<int:pk>', ReviewView.as_view(), name='review'),
# /api/postings/universities/ Lists all university objects.
path('universities/', UniversitiesView.as_view(), name='universities'),
# /api/postings/universities/1/ Returns data for one University.
path('universities/<int:pk>', UniversityView.as_view(), name='university'),
# /api/postings/courses/ Lists all course objects.
path('courses/', CoursesView.as_view(), name='courses'),
# /api/postings/courses/1/ Returns data for one Course.
path('courses/<int:pk>', CourseView.as_view(), name='course'),
# /api/postings/professors/ Lists all professor objects.
path('professors/', ProfessorsView.as_view(), name='professors'),
# /api/postings/professors/1/ Returns data for one Professor.
path('professors/<int:pk>', ProfessorView.as_view(), name='professor'),
# Deprecated
re_path(r'^(?P<pk>\d+)/$', UniReviewRudView.as_view(), name='post-rud'),
re_path(r'^$', UniReviewAPIView.as_view(), name='post-create')
]

View File

@ -1,6 +1,6 @@
from rest_framework import generics, mixins
from postings.models import UniversityReview
from .serializers import UniversityReviewSerializer
from postings.models import UniversityReview, Review
from .serializers import *
from django.db.models import Q
@ -26,3 +26,43 @@ class UniReviewAPIView(mixins.CreateModelMixin, generics.ListAPIView):
def post(self,request,*args,**kwargs):
return self.create(request, *args, **kwargs)
# The view for listing all generic Review objects.
class ReviewsView(mixins.CreateModelMixin, generics.ListAPIView):
queryset = Review.objects.all()
serializer_class = ReviewSerializer
# View for an individual Review object.
class ReviewView(generics.RetrieveUpdateDestroyAPIView):
queryset = Review.objects.all()
serializer_class = ReviewSerializer
# The view for listing all Universities.
class UniversitiesView(generics.ListAPIView):
serializer_class = UniversitySerializer
queryset = University.objects.all()
# The view for an individual University.
class UniversityView(generics.RetrieveUpdateDestroyAPIView):
queryset = University.objects.all()
serializer_class = UniversitySerializer
# The view for listing all Courses.
class CoursesView(generics.ListAPIView):
serializer_class = CourseSerializer
queryset = Course.objects.all()
# The view for an individual Course.
class CourseView(generics.RetrieveUpdateDestroyAPIView):
queryset = Course.objects.all()
serializer_class = CourseSerializer
# The view for listing all Professors.
class ProfessorsView(generics.ListAPIView):
queryset = Professor.objects.all()
serializer_class = ProfessorSerializer
# The view for an individual Professor.
class ProfessorView(generics.RetrieveUpdateDestroyAPIView):
queryset = Professor.objects.all()
serializer_class = ProfessorSerializer

View File

@ -0,0 +1,81 @@
# Generated by Django 2.1.1 on 2018-09-25 19:41
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('postings', '0003_universityreview_title'),
]
operations = [
migrations.CreateModel(
name='RateableEntity',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=256)),
],
),
migrations.CreateModel(
name='Review',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating', models.IntegerField(default=1)),
('title', models.CharField(max_length=128)),
('content', models.TextField()),
('created_date', models.DateTimeField(auto_now_add=True)),
('last_updated_date', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='ReviewHelpfulVote',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('helpful', models.BooleanField()),
('review', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='postings.Review')),
],
),
migrations.CreateModel(
name='Course',
fields=[
('rateableentity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='postings.RateableEntity')),
],
bases=('postings.rateableentity',),
),
migrations.CreateModel(
name='Professor',
fields=[
('rateableentity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='postings.RateableEntity')),
],
bases=('postings.rateableentity',),
),
migrations.CreateModel(
name='University',
fields=[
('rateableentity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='postings.RateableEntity')),
],
bases=('postings.rateableentity',),
),
migrations.AddField(
model_name='review',
name='rateable_entity',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='postings.RateableEntity'),
),
migrations.AddField(
model_name='professor',
name='university',
field=models.ManyToManyField(to='postings.University'),
),
migrations.AddField(
model_name='course',
name='professors',
field=models.ManyToManyField(to='postings.Professor'),
),
migrations.AddField(
model_name='course',
name='taught_at_university',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='postings.University'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1.1 on 2018-09-25 20:32
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('postings', '0004_auto_20180925_1941'),
]
operations = [
migrations.RenameField(
model_name='professor',
old_name='university',
new_name='universities',
),
]

View File

@ -35,7 +35,7 @@ class University(RateableEntity):
# A RateableEntity for professors, who belong to one or more university.
class Professor(RateableEntity):
# The universities that this professor teaches or has taught at.
university = models.ManyToManyField('postings.University')
universities = models.ManyToManyField('postings.University')
# A RateableEntity for courses, which belong to a university, and have one or more professors.
class Course(RateableEntity):