diff --git a/program/serializers.py b/program/serializers.py index fc82e6089b2e090482f0100794cb2ed9de2ac95f..2850bb3961d782df84b0cc6eca31e0a6449a4e79 100644 --- a/program/serializers.py +++ b/program/serializers.py @@ -2,7 +2,7 @@ from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth.models import User from rest_framework import serializers, status from rest_framework.response import Response -from program.models import Show, TimeSlot, Category, Host, Language, Topic, MusicFocus, Note +from program.models import Show, Schedule, TimeSlot, Category, RTRCategory, Host, Language, Topic, MusicFocus, Note, Type, Language from profile.models import Profile @@ -79,6 +79,25 @@ class MusicFocusSerializer(serializers.ModelSerializer): fields = '__all__' +class TypeSerializer(serializers.ModelSerializer): + class Meta: + model = Type + fields = '__all__' + + +class RTRCategorySerializer(serializers.ModelSerializer): + class Meta: + model = RTRCategory + fields = '__all__' + + +''' +class OwnersSerializer(serializers.ModelSerializer): + class Meta: + model = Owners + fields = '__all__' +''' + class ShowSerializer(serializers.HyperlinkedModelSerializer): category = CategorySerializer(many=True) hosts = HostSerializer(many=True) diff --git a/program/views.py b/program/views.py index efef269ed1e4e34f1c27d3b991721d748e99216c..5c53b8ee41997aad2aaa7ed4a5cb7a3ca0ae3e1a 100644 --- a/program/views.py +++ b/program/views.py @@ -14,16 +14,14 @@ from django.views.generic.list import ListView from rest_framework import permissions, serializers, status, viewsets from rest_framework.views import APIView from rest_framework.response import Response -from rest_framework.decorators import detail_route, list_route from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope -from program.models import Type, MusicFocus, Note, Show, Category, RTRCategory, Topic, TimeSlot, Host +from program.models import Type, MusicFocus, Language, Note, Show, Category, RTRCategory, Topic, TimeSlot, Host, Schedule from profile.models import Profile -from program.serializers import ShowSerializer, TimeSlotSerializer, UserSerializer, NoteSerializer +from program.serializers import TypeSerializer, LanguageSerializer, MusicFocusSerializer, NoteSerializer, ShowSerializer, CategorySerializer, RTRCategorySerializer, TopicSerializer, TimeSlotSerializer, HostSerializer, UserSerializer from program.utils import tofirstdayinisoweek, get_cached_shows - class CalendarView(TemplateView): template_name = 'calendar.html' @@ -562,7 +560,7 @@ class APITimeSlotViewSet(viewsets.ModelViewSet): class APINoteViewSet(viewsets.ModelViewSet): """ /api/v1/notes/ Returns nothing - /ap1/v1/notes/1 Returns a single not (if owned) + /ap1/v1/notes/1 Returns a single note (if owned) /api/v1/notes/?ids=1,2,3,4,5 Returns given notes (if owned) Superusers may access and update all notes @@ -661,4 +659,90 @@ class APINoteViewSet(viewsets.ModelViewSet): Note.objects.delete(pk=pk) return Response(status=status.HTTP_204_NO_CONTENT) - return Response(status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file + return Response(status=status.HTTP_400_BAD_REQUEST) + + +class APICategoryViewSet(viewsets.ModelViewSet): + """ + """ + + queryset = Category.objects.all() + serializer_class = CategorySerializer + permission_classes = [permissions.IsAdminUser] + required_scopes = ['categories'] + + + +class APITypeViewSet(viewsets.ModelViewSet): + """ + """ + + queryset = Type.objects.all() + serializer_class = TypeSerializer + permission_classes = [permissions.IsAdminUser] + required_scopes = ['types'] + + + +class APITopicViewSet(viewsets.ModelViewSet): + """ + """ + + queryset = Topic.objects.all() + serializer_class = TopicSerializer + permission_classes = [permissions.IsAdminUser] + required_scopes = ['topics'] + + + +class APIMusicFocusViewSet(viewsets.ModelViewSet): + """ + """ + + queryset = MusicFocus.objects.all() + serializer_class = MusicFocusSerializer + permission_classes = [permissions.IsAdminUser + required_scopes = ['musicfocus'] + + + +class APIRTRCategoryViewSet(viewsets.ModelViewSet): + """ + """ + + queryset = RTRCategory.objects.all() + serializer_class = RTRCategorySerializer + permission_classes = [permissions.IsAdminUser] + required_scopes = ['rtrcategories'] + + +class APILanguageViewSet(viewsets.ModelViewSet): + """ + """ + + queryset = Language.objects.all() + serializer_class = LanguageSerializer + permission_classes = [permissions.IsAdminUser] + required_scopes = ['languages'] + + +class APIHostViewSet(viewsets.ModelViewSet): + """ + """ + + queryset = Host.objects.all() + serializer_class = HostSerializer + permission_classes = [permissions.IsAdminUser] + required_scopes = ['hosts'] + + +''' +class APIOwnersViewSet(viewsets.ModelViewSet): + """ + """ + + queryset = Owners.objects.all() + serializer_class = OwnersSerializer + permission_classes = [permissions.IsAdminUser] + required_scopes = ['owners'] +''' \ No newline at end of file diff --git a/pv/site_media/js/note_change.js b/pv/site_media/js/note_change.js index 7a397d57041d5e35cc57ad8c39fcec21545413fe..449da2124e370f5b64400a3c47e720d261f83b72 100644 --- a/pv/site_media/js/note_change.js +++ b/pv/site_media/js/note_change.js @@ -2,6 +2,7 @@ django.jQuery(document).ready( function() { /* Get the already saved timeslot_id to preserve if past */ var selected_timeslot_id = django.jQuery('select#id_timeslot option:selected').val() || 0; + var selected_timeslot_val = django.jQuery('select#id_timeslot option:selected').text() || '' /* If a show is selected load its timeslots into the corresponding select */ django.jQuery("select#id_show").on("change", function() { @@ -17,19 +18,25 @@ django.jQuery(document).ready( function() { /* Call ajax function and retrieve array containing objects */ django.jQuery.ajax({ - url: '/export/get_timeslots_by_show', + url: '/api/v1/timeslots/', type: 'GET', data: { 'show_id': show_id, - 'timeslot_id': selected_timeslot_id, 'csrfmiddlewaretoken': django.jQuery('input[name="csrfmiddlewartetoken"]').val() }, success: function(timeslots) { /* Populate timeslot select */ var options = new Array(); + i = 0; - for( var i=0; i < timeslots.length; i++ ) { - options[i] = new Option( timeslots[i].timeslot, parseInt(timeslots[i].timeslot_id) ); //+ " " + moment.utc( timeslots[i].start ).format('dddd, D.M. YYYY HH:mm') + ' - ' + moment.utc( timeslots[i].end ).format('HH:mm'), timeslots[i].timeslot_id ); + // Preserve an already selected timeslot + if( selected_timeslot_id > 0 ) { + options[0] = new Option( selected_timeslot_val, selected_timeslot_id ); + i = 1; + } + + for( var i=i; i < timeslots.length; i++ ) { + options[i] = new Option( moment.utc( timeslots[i].start ).format('dd, D.M. YYYY HH:mm') + ' - ' + moment.utc( timeslots[i].end ).format('HH:mm'), parseInt(timeslots[i].id) ); } django.jQuery('select#id_timeslot').html( options ).fadeIn(); diff --git a/pv/urls.py b/pv/urls.py index ff20888bc11d29c3027d8e25c6d94bbfb7c31369..7c782f99e5bb0ae2e66428b0b9b87b0e1aba3ab8 100644 --- a/pv/urls.py +++ b/pv/urls.py @@ -5,7 +5,7 @@ from django.views.static import serve from rest_framework import routers from rest_framework.authtoken import views -from program.views import APIUserViewSet, APIShowViewSet, APITimeSlotViewSet, APINoteViewSet, json_day_schedule, json_week_schedule, json_timeslots_specials, json_get_timeslots_by_show +from program.views import APIUserViewSet, APIHostViewSet, APIShowViewSet, APITimeSlotViewSet, APINoteViewSet, APICategoryViewSet, APITypeViewSet, APITopicViewSet, APIMusicFocusViewSet, APIRTRCategoryViewSet, APILanguageViewSet, json_day_schedule, json_week_schedule, json_timeslots_specials, json_get_timeslots_by_show from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope @@ -13,23 +13,30 @@ admin.autodiscover() router = routers.DefaultRouter() router.register(r'users', APIUserViewSet) +#router.register(r'owners', APIOwnerViewSet) +router.register(r'hosts', APIHostViewSet) router.register(r'shows', APIShowViewSet) router.register(r'timeslots', APITimeSlotViewSet) router.register(r'notes', APINoteViewSet) +router.register(r'categories', APICategoryViewSet) +router.register(r'topics', APITopicViewSet) +router.register(r'types', APITypeViewSet) +router.register(r'musicfocus', APIMusicFocusViewSet) +router.register(r'rtrcategories', APIRTRCategoryViewSet) +router.register(r'languages', APILanguageViewSet) urlpatterns = [ url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')), - url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), url(r'^api-token-auth/', views.obtain_auth_token), url(r'^api/v1/', include(router.urls) ), + url(r'^api/v1/program$', json_week_schedule), url(r'^api/v1/timeslots-by-show$', json_get_timeslots_by_show, name='json_get_timeslots_by_show'), + url(r'^api/v1/week_schedule$', json_week_schedule), url(r'^admin/', admin.site.urls), url(r'^program/', include('program.urls')), url(r'^nop', include('nop.urls')), - url(r'^tinymce/', include('tinymce.urls')), + #url(r'^tinymce/', include('tinymce.urls')), url(r'^export/day_schedule/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$', json_day_schedule), - url(r'^api/v1/program$', json_week_schedule), - url(r'^api/v1/week_schedule$', json_week_schedule), url(r'^export/timeslots_specials.json$', json_timeslots_specials), ]