Commit 2dc11d15d8b43b83231f149331cdc0c82e813d9c
1 parent
e3864c77ed
Exists in
master
password and user serialization some
Showing 3 changed files with 53 additions and 2 deletions Inline Diff
flashcards/api.py
View file @
2dc11d1
File was created | 1 | from django.http import Http404 | ||
2 | from rest_framework.views import APIView | |||
3 | from rest_framework.response import Response | |||
4 | from rest_framework import status | |||
5 | from rest_framework.exceptions import ValidationError | |||
6 | from flashcards.serializers import * | |||
7 | from django.http import HttpResponse | |||
8 | from rest_framework.renderers import JSONRenderer | |||
9 | ||||
10 | class JSONResponse(HttpResponse): | |||
11 | """ | |||
12 | An HttpResponse that renders its content into JSON. | |||
13 | """ | |||
14 | def __init__(self, data, **kwargs): | |||
15 | content = JSONRenderer().render(data) | |||
16 | kwargs['content_type'] = 'application/json' | |||
17 | super(JSONResponse, self).__init__(content, **kwargs) | |||
18 | ||||
19 | ||||
20 | class UserDetail(APIView): | |||
21 | def patch(self, request,format=None): | |||
22 | """ | |||
23 | Updates a user's password after they enter a valid old password. | |||
24 | TODO: email verification | |||
25 | """ | |||
26 | currentUser = request.user | |||
27 | if 'old_password' not in request.data: | |||
28 | raise ValidationError('Old password is required') | |||
29 | if 'new_password' not in request.data: | |||
30 | raise ValidationError('New password is required') | |||
31 | if not request.data['new_password']: | |||
32 | raise ValidationError('Password cannot be blank') | |||
33 | if not currentUser.check_password(request.data['old_password']): | |||
34 | raise ValidationError('Invalid old password') | |||
35 | currentUser.set_password(request.data['new_password']) | |||
36 | currentUser.save() | |||
37 | return Response(status=status.HTTP_204_NO_CONTENT) |
flashcards/serializers.py
View file @
2dc11d1
from flashcards.models import Section, LecturePeriod | 1 | 1 | from flashcards.models import Section, LecturePeriod | |
from rest_framework.relations import HyperlinkedRelatedField | 2 | 2 | from rest_framework.relations import HyperlinkedRelatedField | |
from rest_framework.serializers import HyperlinkedModelSerializer | 3 | 3 | from rest_framework.serializers import HyperlinkedModelSerializer | |
4 | from django.contrib.auth.models import User | |||
4 | 5 | |||
5 | ||||
class SectionSerializer(HyperlinkedModelSerializer): | 6 | 6 | class SectionSerializer(HyperlinkedModelSerializer): | |
lectureperiod_set = HyperlinkedRelatedField(many=True, view_name='lectureperiod-detail', read_only=True) | 7 | 7 | lectureperiod_set = HyperlinkedRelatedField(many=True, view_name='lectureperiod-detail', read_only=True) | |
class Meta: | 8 | 8 | class Meta: | |
model = Section | 9 | 9 | model = Section | |
exclude = ('members',) | 10 | 10 | exclude = ('members',) | |
11 | 11 | |||
12 | 12 | |||
class LecturePeriodSerializer(HyperlinkedModelSerializer): | 13 | 13 | class LecturePeriodSerializer(HyperlinkedModelSerializer): | |
class Meta: | 14 | 14 | class Meta: | |
model = LecturePeriod | 15 | 15 | model = LecturePeriod | |
16 | ||||
17 | class UserSerializer(HyperlinkedModelSerializer): | |||
18 | """ |
flashy/urls.py
View file @
2dc11d1
from django.conf.urls import include, url | 1 | 1 | from django.conf.urls import include, url | |
from django.contrib import admin | 2 | 2 | from django.contrib import admin | |
from flashcards.views import SectionViewSet, LecturePeriodViewSet | 3 | 3 | from flashcards.views import SectionViewSet, LecturePeriodViewSet | |
from rest_framework.routers import DefaultRouter | 4 | 4 | from rest_framework.routers import DefaultRouter | |
5 | from flashcards.api import * | |||
5 | 6 | |||
router = DefaultRouter() | 6 | 7 | router = DefaultRouter() | |
router.register(r'sections', SectionViewSet) | 7 | 8 | router.register(r'sections', SectionViewSet) | |
router.register(r'lectureperiods', LecturePeriodViewSet) | 8 | 9 | router.register(r'lectureperiods', LecturePeriodViewSet) | |
9 | 10 | |||
urlpatterns = [ | 10 | 11 | urlpatterns = [ | |
12 | url(r'^api/user/me$', UserDetail.as_view()), | |||
url(r'^api/', include(router.urls)), | 11 | 13 | url(r'^api/', include(router.urls)), | |
url(r'^admin/doc/', include('django.contrib.admindocs.urls')), | 12 | 14 | url(r'^admin/doc/', include('django.contrib.admindocs.urls')), | |
url(r'^admin/', include(admin.site.urls)), | 13 | 15 | url(r'^admin/', include(admin.site.urls)), |