From 4915771315021267e1d9175143272633f5c9302e Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Wed, 29 Apr 2015 01:40:00 -0700 Subject: [PATCH] Class -> section. Set up API for sections, for real this time --- flashcards/admin.py | 4 +-- flashcards/migrations/0004_auto_20150429_0827.py | 42 ++++++++++++++++++++++++ flashcards/models.py | 20 ++++++++--- flashcards/serializers.py | 16 ++++++++- flashcards/views.py | 16 +++++++-- flashy/settings.py | 2 +- flashy/urls.py | 7 ++++ schedule-scraper/scrape_schedule.js | 32 +++++++++--------- templates/rest_framework/api.html | 4 +++ 9 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 flashcards/migrations/0004_auto_20150429_0827.py create mode 100644 templates/rest_framework/api.html diff --git a/flashcards/admin.py b/flashcards/admin.py index 29892df..5392cff 100644 --- a/flashcards/admin.py +++ b/flashcards/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from flashcards.models import Flashcard, UserFlashcard, Class, FlashcardMask, \ +from flashcards.models import Flashcard, UserFlashcard, Section, FlashcardMask, \ UserFlashcardReview admin.site.register([ @@ -7,5 +7,5 @@ admin.site.register([ FlashcardMask, UserFlashcard, UserFlashcardReview, - Class + Section ]) \ No newline at end of file diff --git a/flashcards/migrations/0004_auto_20150429_0827.py b/flashcards/migrations/0004_auto_20150429_0827.py new file mode 100644 index 0000000..3926f5b --- /dev/null +++ b/flashcards/migrations/0004_auto_20150429_0827.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('flashcards', '0003_auto_20150429_0344'), + ] + + operations = [ + migrations.CreateModel( + name='LecturePeriod', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('week_day', models.IntegerField(help_text=b'0-indexed day of week, starting at Monday')), + ('start_time', models.TimeField()), + ('end_time', models.TimeField()), + ], + ), + migrations.RenameModel( + old_name='Class', + new_name='Section', + ), + migrations.RemoveField( + model_name='flashcard', + name='associated_class', + ), + migrations.AddField( + model_name='flashcard', + name='section', + field=models.ForeignKey(default=None, to='flashcards.Section', help_text=b'The section with which the card is associated'), + preserve_default=False, + ), + migrations.AddField( + model_name='lectureperiod', + name='section', + field=models.ForeignKey(to='flashcards.Section'), + ), + ] diff --git a/flashcards/models.py b/flashcards/models.py index 1706840..3096b4b 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -46,7 +46,7 @@ class FlashcardMask(Model): class Flashcard(Model): text = CharField(max_length=255, help_text='The text on the card') - associated_class = ForeignKey('Class', help_text='The class with which the card is associated') + section = ForeignKey('Section', help_text='The section with which the card is associated') pushed = DateTimeField(auto_now_add=True, help_text="When the card was first pushed") material_date = DateTimeField(help_text="The date with which the card is associated") previous = ForeignKey('Flashcard', null=True, blank=True, @@ -104,11 +104,11 @@ class UserFlashcardReview(Model): if self.response: return "answered" return "viewed" - -class Class(Model): +class Section(Model): """ A UCSD course taught by an instructor during a quarter. - Different sections taught by the same instructor in the same quarter are considered identical.s + Different sections taught by the same instructor in the same quarter are considered identical. + We use the term "section" to avoid collision with the builtin keyword "class" """ department = CharField(max_length=50) course_num = IntegerField() @@ -119,4 +119,14 @@ class Class(Model): class Meta: unique_together = (('department', 'course_num', 'quarter', 'instructor'),) - ordering = ['-quarter'] \ No newline at end of file + ordering = ['-quarter'] + + +class LecturePeriod(Model): + """ + A lecture period for a section + """ + section = ForeignKey(Section) + week_day = IntegerField(help_text="0-indexed day of week, starting at Monday") + start_time = TimeField() + end_time = TimeField() diff --git a/flashcards/serializers.py b/flashcards/serializers.py index a9ada3d..1be5ed4 100644 --- a/flashcards/serializers.py +++ b/flashcards/serializers.py @@ -1 +1,15 @@ -__author__ = 'andrew' +from flashcards.models import Section, LecturePeriod +from rest_framework.relations import HyperlinkedRelatedField +from rest_framework.serializers import HyperlinkedModelSerializer + + +class SectionSerializer(HyperlinkedModelSerializer): + lectureperiod_set = HyperlinkedRelatedField(many=True, view_name='lectureperiod-detail', read_only=True) + class Meta: + model = Section + exclude = ('members',) + + +class LecturePeriodSerializer(HyperlinkedModelSerializer): + class Meta: + model = LecturePeriod \ No newline at end of file diff --git a/flashcards/views.py b/flashcards/views.py index 91ea44a..858f169 100644 --- a/flashcards/views.py +++ b/flashcards/views.py @@ -1,3 +1,15 @@ -from django.shortcuts import render +from flashcards.models import Section, LecturePeriod +from flashcards.serializers import SectionSerializer, LecturePeriodSerializer +from rest_framework.permissions import IsAuthenticatedOrReadOnly +from rest_framework.viewsets import ModelViewSet -# Create your views here. + +class SectionViewSet(ModelViewSet): + queryset = Section.objects.all() + serializer_class = SectionSerializer + permission_classes = (IsAuthenticatedOrReadOnly,) + +class LecturePeriodViewSet(ModelViewSet): + queryset = LecturePeriod.objects.all() + serializer_class = LecturePeriodSerializer + permission_classes = (IsAuthenticatedOrReadOnly,) \ No newline at end of file diff --git a/flashy/settings.py b/flashy/settings.py index 0b76930..c979da3 100644 --- a/flashy/settings.py +++ b/flashy/settings.py @@ -58,7 +58,7 @@ ROOT_URLCONF = 'flashy.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': ['templates/'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ diff --git a/flashy/urls.py b/flashy/urls.py index 4514553..fc04576 100644 --- a/flashy/urls.py +++ b/flashy/urls.py @@ -1,7 +1,14 @@ from django.conf.urls import include, url from django.contrib import admin +from flashcards.views import SectionViewSet, LecturePeriodViewSet +from rest_framework.routers import DefaultRouter + +router = DefaultRouter() +router.register(r'sections', SectionViewSet) +router.register(r'lectureperiods', LecturePeriodViewSet) urlpatterns = [ + url(r'^api/', include(router.urls)), url(r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) diff --git a/schedule-scraper/scrape_schedule.js b/schedule-scraper/scrape_schedule.js index a21c51c..3ec2e99 100644 --- a/schedule-scraper/scrape_schedule.js +++ b/schedule-scraper/scrape_schedule.js @@ -1,23 +1,23 @@ var page = require('webpage').create(); var fs = require('fs'); -page.open('file:///home/andrew/Downloads/Student Class Info.html', function(status) { +page.open('Student Class Info.html', function (status) { if (status !== 'success') { - console.log('load failed'); + console.log('load failed'); } else { - console.log("loaded"); - page.injectJs("jquery.min.js"); - console.log("got jquery"); - page.includeJs("http://www.kunalbabre.com/projects/table2CSV.js", function() { - console.log("got script"); - page.evaluate(function() { - console.log("parsing"); - try { - fs.write("schedule.csv", $('table.tbrdr').table2CSV({'delivery':'value'}), 'w'); - } catch(e) { - console.log(e); - } - }); - }); + console.log("loaded"); + page.injectJs("jquery.min.js"); + console.log("got jquery"); + page.includeJs("http://www.kunalbabre.com/projects/table2CSV.js", function () { + console.log("got script"); + page.evaluate(function () { + console.log("parsing"); + try { + fs.write("schedule.csv", $('table.tbrdr').table2CSV({'delivery': 'value'}), 'w'); + } catch (e) { + console.log(e); + } + }); + }); } phantom.exit(); }); diff --git a/templates/rest_framework/api.html b/templates/rest_framework/api.html new file mode 100644 index 0000000..86991c7 --- /dev/null +++ b/templates/rest_framework/api.html @@ -0,0 +1,4 @@ +{% extends "rest_framework/base.html" %} +{% block bootstrap_theme %} + +{% endblock %} \ No newline at end of file -- 1.9.1