-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathrest.py
36 lines (26 loc) · 1.14 KB
/
rest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#encoding: utf-8
from django.conf import settings
from django.shortcuts import redirect
from rest_framework import exceptions, generics, response, serializers as s
from wechat_django.models import WeChatUser
from wechat_django.oauth import (WeChatAuthenticated,
WeChatOAuthAuthentication,
WeChatOAuthViewMixin)
class UserSerializer(s.ModelSerializer):
class Meta:
model = WeChatUser
fields = ("openid", "nickname")
class TestAPIView(WeChatOAuthViewMixin, generics.RetrieveAPIView,
generics.CreateAPIView):
appname = settings.SAMPLEAPPNAME
permission_classes = (WeChatAuthenticated,)
queryset = WeChatUser.objects
serializer_class = UserSerializer
def create(self, request, *args, **kwargs):
return response.Response(request.wechat.user.nickname)
def get_object(self):
return self.request.wechat.user
def handle_exception(self, exc):
if isinstance(exc, exceptions.NotAuthenticated):
return redirect(self.request.wechat.oauth_uri)
return super(TestAPIView, self).handle_exception(exc)