Commit 2dc11d15d8b43b83231f149331cdc0c82e813d9c
1 parent
e3864c77ed
Exists in
master
password and user serialization some
Showing 3 changed files with 53 additions and 2 deletions Side-by-side Diff
flashcards/api.py
View file @
2dc11d1
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) | |
38 | + | |
39 | + def get(self, request,format=None): | |
40 | + serializer = UserSerializer(request.user) | |
41 | + return Response(serializer.data) |
flashcards/serializers.py
View file @
2dc11d1
1 | 1 | from flashcards.models import Section, LecturePeriod |
2 | 2 | from rest_framework.relations import HyperlinkedRelatedField |
3 | 3 | from rest_framework.serializers import HyperlinkedModelSerializer |
4 | +from django.contrib.auth.models import User | |
4 | 5 | |
5 | - | |
6 | 6 | class SectionSerializer(HyperlinkedModelSerializer): |
7 | 7 | lectureperiod_set = HyperlinkedRelatedField(many=True, view_name='lectureperiod-detail', read_only=True) |
8 | 8 | class Meta: |
... | ... | @@ -13,4 +13,11 @@ |
13 | 13 | class LecturePeriodSerializer(HyperlinkedModelSerializer): |
14 | 14 | class Meta: |
15 | 15 | model = LecturePeriod |
16 | + | |
17 | +class UserSerializer(HyperlinkedModelSerializer): | |
18 | + """ | |
19 | + """ | |
20 | + class Meta: | |
21 | + model = User | |
22 | + fields = ("email", "is_active", "last_login", "date_joined") |
flashy/urls.py
View file @
2dc11d1
... | ... | @@ -2,12 +2,14 @@ |
2 | 2 | from django.contrib import admin |
3 | 3 | from flashcards.views import SectionViewSet, LecturePeriodViewSet |
4 | 4 | from rest_framework.routers import DefaultRouter |
5 | +from flashcards.api import * | |
5 | 6 | |
6 | 7 | router = DefaultRouter() |
7 | 8 | router.register(r'sections', SectionViewSet) |
8 | 9 | router.register(r'lectureperiods', LecturePeriodViewSet) |
9 | 10 | |
10 | 11 | urlpatterns = [ |
12 | + url(r'^api/user/me$', UserDetail.as_view()), | |
11 | 13 | url(r'^api/', include(router.urls)), |
12 | 14 | url(r'^admin/doc/', include('django.contrib.admindocs.urls')), |
13 | 15 | url(r'^admin/', include(admin.site.urls)), |