Commit 2a72f1a8adb4ae54bba77eb8fff6a6c4e4e9d256

Authored by Andrew Buss
1 parent 8488fececd
Exists in master

Development server now serves the frontend too

Showing 5 changed files with 44 additions and 9 deletions Side-by-side Diff

... ... @@ -16,5 +16,7 @@
16 16  
17 17 Run development server (local):
18 18  
19   - python manage.py runserver
  19 + scripts/run_local.sh
  20 +
  21 +This requires that flashy-backend be in the directory as flashy-frontend.
flashcards/views.py View file @ 2a72f1a
... ... @@ -13,21 +13,24 @@
13 13 from rest_framework.generics import RetrieveAPIView
14 14 from rest_framework.exceptions import AuthenticationFailed, NotAuthenticated, ValidationError
15 15 from simple_email_confirmation import EmailAddress
16   -from rest_framework import filters
17 16  
  17 +
18 18 class SectionViewSet(ReadOnlyModelViewSet):
19 19 queryset = Section.objects.all()
20 20 serializer_class = SectionSerializer
21 21 pagination_class = StandardResultsSetPagination
22 22  
  23 +
23 24 class UserSectionViewSet(ModelViewSet):
24 25 serializer_class = SectionSerializer
25 26 permission_classes = [IsAuthenticated]
  27 +
26 28 def get_queryset(self):
27 29 return self.request.user.sections.all()
28   -
  30 +
29 31 def paginate_queryset(self, queryset): return None
30 32  
  33 +
31 34 class UserDetail(APIView):
32 35 def patch(self, request, format=None):
33 36 """
... ... @@ -127,6 +130,7 @@
127 130  
128 131 class UserLogout(APIView):
129 132 permission_classes = (IsAuthenticated,)
  133 +
130 134 def post(self, request, format=None):
131 135 """
132 136 Logs the authenticated user out.
133 137  
... ... @@ -184,9 +188,11 @@
184 188 raise ValidationError('Could not verify reset token')
185 189 return Response(status=HTTP_204_NO_CONTENT)
186 190  
  191 +
187 192 class FlashcardDetail(RetrieveAPIView):
188 193 queryset = Flashcard.objects.all()
189 194 serializer_class = FlashcardSerializer
  195 +
190 196 ## def get(self, request):
191 197  
192 198  
flashy/frontend_serve.py View file @ 2a72f1a
  1 +from django.http import Http404
  2 +from django.views.static import serve
  3 +
  4 +
  5 +def serve_with_default(*args, **kwargs):
  6 + """
  7 + Wrap Django's static file serving view. If 404, return a default file
  8 + """
  9 + default_file = kwargs.get('default_file', 'index.html')
  10 + del kwargs['default_file']
  11 + try:
  12 + return serve(*args, **kwargs)
  13 + except Http404:
  14 + kwargs['path'] = default_file
  15 + return serve(*args, **kwargs)
flashy/urls.py View file @ 2a72f1a
1 1 from django.conf.urls import include, url
2 2 from django.contrib import admin
3   -from flashcards.views import SectionViewSet, UserDetail, UserLogin, UserLogout, PasswordReset, UserSectionViewSet, FlashcardDetail
  3 +from flashcards.views import SectionViewSet, UserDetail, UserLogin, UserLogout, PasswordReset, UserSectionViewSet, \
  4 + FlashcardDetail
  5 +from flashy.frontend_serve import serve_with_default
  6 +from flashy.settings import DEBUG
4 7 from rest_framework.routers import DefaultRouter
5 8 from flashcards.api import *
6 9  
7 10 router = DefaultRouter()
8 11 router.register(r'sections', SectionViewSet)
9 12  
10   -router.register(r'users/me/sections', UserSectionViewSet, base_name = 'usersection')
  13 +router.register(r'users/me/sections', UserSectionViewSet, base_name='usersection')
11 14  
12 15 urlpatterns = [
13 16 url(r'^api/docs/', include('rest_framework_swagger.urls')),
14 17  
... ... @@ -18,11 +21,12 @@
18 21 url(r'^api/', include(router.urls)),
19 22 url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
20 23 url(r'^admin/', include(admin.site.urls)),
21   - url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'
22   -)),
23   - url(r'^api/flashcards/(?P<pk>[0-9]+)$', FlashcardDetail.as_view(), name \
24   - ="flashcard-detail"),
  24 + url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
  25 + url(r'^api/flashcards/(?P<pk>[0-9]+)$', FlashcardDetail.as_view(), name="flashcard-detail"),
25 26 ]
26 27  
27 28 urlpatterns += (url(r'^admin/django-ses/', include('django_ses.urls')),)
  29 +
  30 +if DEBUG: urlpatterns += [url(r'^app/(?P<path>.*)$', serve_with_default,
  31 + {'document_root': '../flashy-frontend', 'default_file': 'home.html'})]
scripts/run_local.sh View file @ 2a72f1a
  1 +#!/bin/bash -xe
  2 +if [ ! -d "../flashy-frontend/" ]; then
  3 + echo "In order to serve the frontend, flashy-frontend must be in the parent dir"
  4 + exit 1
  5 +fi
  6 +source venv/bin/activate
  7 +python manage.py runserver 127.0.0.1:8080