api attempt

This commit is contained in:
Raffu Khondaker 2020-06-06 14:44:13 -04:00
parent 41c8edfdbb
commit 60d16b9e6a
24 changed files with 527 additions and 6 deletions

0
Website/api/__init__.py Normal file
View File

3
Website/api/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
Website/api/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'

View File

@ -0,0 +1,61 @@
# Generated by Django 3.0.7 on 2020-06-06 17:44
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Classes',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('url', models.URLField()),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Teacher',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('url', models.URLField()),
('created', models.DateTimeField(auto_now_add=True)),
('first_name', models.CharField(max_length=100)),
('last_name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Student',
fields=[
('url', models.URLField()),
('created', models.DateTimeField(auto_now_add=True)),
('first_name', models.CharField(max_length=100)),
('last_name', models.CharField(max_length=100)),
('student_id', models.IntegerField(primary_key=True, serialize=False)),
('grade', models.IntegerField()),
('classes', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Classes')),
],
),
migrations.AddField(
model_name='classes',
name='teachers',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Teacher'),
),
migrations.CreateModel(
name='Assignment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('url', models.URLField()),
('name', models.CharField(max_length=100)),
('due_date', models.DateTimeField()),
('classes', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Classes')),
('students', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Student')),
],
),
]

View File

@ -0,0 +1,24 @@
# Generated by Django 3.0.7 on 2020-06-06 18:10
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='assignment',
name='classes',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='api.Classes'),
),
migrations.AlterField(
model_name='assignment',
name='students',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='api.Student'),
),
]

View File

@ -0,0 +1,26 @@
# Generated by Django 3.0.7 on 2020-06-06 18:17
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('api', '0002_auto_20200606_1810'),
]
operations = [
migrations.AlterField(
model_name='assignment',
name='classes',
field=models.ForeignKey(blank=True, default='', on_delete=django.db.models.deletion.CASCADE, to='api.Classes'),
preserve_default=False,
),
migrations.AlterField(
model_name='assignment',
name='students',
field=models.ForeignKey(blank=True, default='', on_delete=django.db.models.deletion.CASCADE, to='api.Student'),
preserve_default=False,
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-06 18:22
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0003_auto_20200606_1817'),
]
operations = [
migrations.AddField(
model_name='student',
name='webmail',
field=models.EmailField(blank=True, max_length=254),
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 3.0.7 on 2020-06-06 18:22
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('api', '0004_student_webmail'),
]
operations = [
migrations.AlterField(
model_name='student',
name='classes',
field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to='api.Classes'),
),
]

View File

@ -0,0 +1,24 @@
# Generated by Django 3.0.7 on 2020-06-06 18:24
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('api', '0005_auto_20200606_1822'),
]
operations = [
migrations.AlterField(
model_name='assignment',
name='classes',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='api.Classes'),
),
migrations.AlterField(
model_name='assignment',
name='students',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='api.Student'),
),
]

View File

@ -0,0 +1,21 @@
# Generated by Django 3.0.7 on 2020-06-06 18:25
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0006_auto_20200606_1824'),
]
operations = [
migrations.RemoveField(
model_name='assignment',
name='classes',
),
migrations.RemoveField(
model_name='assignment',
name='students',
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 3.0.7 on 2020-06-06 18:39
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('api', '0007_auto_20200606_1825'),
]
operations = [
migrations.AlterField(
model_name='classes',
name='teachers',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='api.Teacher'),
),
]

View File

36
Website/api/models.py Normal file
View File

@ -0,0 +1,36 @@
from django.db import models
class Student(models.Model):
url = models.URLField()
created = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
student_id = models.IntegerField(primary_key=True)
webmail = models.EmailField(blank=True)
grade = models.IntegerField()
classes = models.ForeignKey('Classes', on_delete=models.CASCADE,blank=True)
class Teacher(models.Model):
url = models.URLField()
created = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
#student_id = models.IntegerField(primary_key=True)
class Classes(models.Model):
url = models.URLField()
name = models.CharField(max_length=100)
teachers = models.ForeignKey('Teacher', on_delete=models.CASCADE,null=True)
def save(self, *args, **kwargs):
return super(Classes, self).save(*args, **kwargs)
class Assignment(models.Model):
url = models.URLField()
name=models.CharField(max_length=100)
due_date=models.DateTimeField()
def __str__(self):
return '%d' % (self.name)

View File

@ -0,0 +1,32 @@
from django.contrib.auth.models import User, Group
from .models import Student, Teacher, Classes, Assignment
from rest_framework import serializers
class AssignmentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Assignment
fields = ['name', 'due_date', 'url']
class StudentSerializer(serializers.HyperlinkedModelSerializer):
assignments = AssignmentSerializer(many=True, read_only=True)
class Meta:
model = Student
fields = ['url', 'first_name', 'last_name', 'assignments']
class ClassesSerializer(serializers.HyperlinkedModelSerializer):
assignments = AssignmentSerializer(many=True, read_only=True,allow_null=True)
students = StudentSerializer(many=True, read_only=True, allow_null=True)
class Meta:
model = Classes
fields = ['url', 'name', 'students', 'assignments']
def create(self, validated_data):
return Classes.objects.create(**validated_data)
class TeacherSerializer(serializers.ModelSerializer):
classes = ClassesSerializer(many=True, read_only=True)
class Meta:
model = Teacher
fields = ['url', 'first_name', 'last_name', 'classes']

3
Website/api/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

0
Website/api/urls.py Normal file
View File

34
Website/api/views.py Normal file
View File

@ -0,0 +1,34 @@
from .models import Student, Teacher, Classes, Assignment
from .serializers import StudentSerializer, TeacherSerializer, ClassesSerializer, AssignmentSerializer
from rest_framework import generics, viewsets
class StudentViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Student.objects.all()
serializer_class = StudentSerializer
class TeacherViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Teacher.objects.all()
serializer_class = TeacherSerializer
class ClassesViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Classes.objects.all()
serializer_class = ClassesSerializer
class AssignmentViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Assignment.objects.all()
serializer_class = AssignmentSerializer

21
Website/manage.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'skoolsite.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()

View File

16
Website/skoolsite/asgi.py Normal file
View File

@ -0,0 +1,16 @@
"""
ASGI config for skoolsite project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'skoolsite.settings')
application = get_asgi_application()

View File

@ -0,0 +1,128 @@
"""
Django settings for skoolsite project.
Generated by 'django-admin startproject' using Django 3.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '$v+-0wm%yys7r3&e0s&*tyh-vyc9v&twb_8yk6==290io9yq3('
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api',
]
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'skoolsite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'skoolsite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'

16
Website/skoolsite/urls.py Normal file
View File

@ -0,0 +1,16 @@
from django.urls import include, path
from rest_framework import routers
from api import views
router = routers.DefaultRouter()
router.register(r'students', views.StudentViewSet)
router.register(r'teachers', views.TeacherViewSet)
router.register(r'assignments', views.AssignmentViewSet)
router.register(r'classes', views.ClassesViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

16
Website/skoolsite/wsgi.py Normal file
View File

@ -0,0 +1,16 @@
"""
WSGI config for skoolsite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'skoolsite.settings')
application = get_wsgi_application()

View File

@ -1,14 +1,13 @@
certifi==2020.4.5.1 asgiref==3.2.7
chardet==3.0.4 click==7.1.2
idna==2.9 Django==3.0.7
oauthlib==3.1.0
prompt-toolkit==1.0.14 prompt-toolkit==1.0.14
Pygments==2.6.1 Pygments==2.6.1
PyInquirer==1.0.3 PyInquirer==1.0.3
pytz==2020.1
regex==2020.5.14 regex==2020.5.14
requests==2.23.0
requests-oauthlib==1.3.0
selenium==3.141.0 selenium==3.141.0
six==1.15.0 six==1.15.0
sqlparse==0.3.1
urllib3==1.25.9 urllib3==1.25.9
wcwidth==0.2.3 wcwidth==0.2.3