Django REST_framework APIView
Project description
Screenshots
User in your app
Install using pip:
pip install django_restframework_apiview
Add ‘apiview’ to your INSTALLED_APPS setting:
INSTALLED_APPS = (
...
'apiview',
)
JSONP Renderer And debug apiview with paramslist:
REST_FRAMEWORK = {
...
'DEFAULT_RENDERER_CLASSES': [
...
'apiview.renderers.JSONPRenderer',
],
}
if DEBUG:
REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES'].append('apiview.renderers.BrowsableAPIRenderer')
Add error code to settings.py:
ERROR_CODE_DEFINE = (
('ERR_AUTH_NOLOGIN', 10001, 'No login'),
('ERR_AUTH_USER_EXISTS', 10002, 'User name has exists'),
('ERR_AUTH_USER_NOT_EXISTS', 10003, 'User not exists'),
('ERR_AUTH_PASSWORD', 10005, 'Password error'),
)
edit myapp/views.py:
#! usr/bin/env python
# encoding: utf-8
from __future__ import absolute_import, unicode_literals
from django.db import IntegrityError, transaction
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from apiview.err_code import ErrCode
from apiview.exceptions import CustomError
from apiview.view import APIView
from apiview.views import ViewSite
from apiview.views import fields
site = ViewSite(name='base', app_name='base')
class APIBase(APIView):
class Meta:
path = '/'
param_fields = (
('channel', fields.CharField(help_text='App Channel', required=False)),
('version', fields.CharField(help_text='App Version', required=False)),
)
class UserAPIBase(APIBase):
def check_user_permission(self, request):
user = request.user
if not user.is_authenticated() or not isinstance(user, User):
raise CustomError(ErrCode.ERR_AUTH_NOLOGIN)
def view(self, request, *args, **kwargs):
self.check_user_permission(request)
return super(APIBase, self).view(request, *args, **kwargs)
class Meta:
path = '/user'
@site
class Login(APIBase):
name = '用户登陆'
def get_context(self, request, *args, **kwargs):
user = authenticate(username=request.params.username, password=request.params.password)
if user is None:
raise CustomError(ErrCode.ERR_AUTH_PASSWORD)
login(request, user)
return self.get_default_context(user_info={'username':request.user.username, 'email':request.user.email})
class Meta:
path = 'user/login'
param_fields = (
('username', fields.CharField(help_text='用户名')),
('password', fields.CharField(help_text='密码')),
)
@site
class Register(APIBase):
name = '用户注册'
def get_context(self, request, *args, **kwargs):
try:
user = User.objects.create_user(request.params.username, request.params.email, request.params.password)
except IntegrityError:
raise CustomError(ErrCode.ERR_AUTH_USER_EXISTS)
return self.get_default_context()
class Meta:
path = 'user/register'
param_fields = (
('username', fields.RegexField(help_text='用户名', regex=r'^[a-zA-Z0-9_]{3,10}$')),
('password', fields.CharField(help_text='密码')),
('email', fields.EmailField(help_text='Email')),
)
@site
class Info(UserAPIBase):
name = '用户信息'
def get_context(self, request, *args, **kwargs):
user_info = {'username':request.user.username, 'email':request.user.email}
return self.get_default_context(user_info=user_info)
@site
class Logout(UserAPIBase):
name = '退出登陆'
def get_context(self, request, *args, **kwargs):
logout(request)
return self.get_default_context()
urlpatterns = site.urlpatterns
add to urls.py end:
urlpatterns.append(url(r’^test/’, include(“myapp.views”)))
Example
run example:
git clone https://github.com/007gzs/django_restframework_apiview.git cd django_restframework_apiview/example pip install -r requirements.txt python manage.py makemigrations python manage.py migrate python manage.py runserver open http://127.0.0.1:8000/example.html in browser
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_restframework_apiview-1.3.0.tar.gz.
File metadata
- Download URL: django_restframework_apiview-1.3.0.tar.gz
- Upload date:
- Size: 59.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/38.2.4 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
772460f8e90ce8dfae76f2567345bacd9062045f971439c2f3af236f7fc0ae7f
|
|
| MD5 |
5de1ee5b4170e315b2cbebad55cd377b
|
|
| BLAKE2b-256 |
ca3f9087657f48f831521d56f3506238dd826e796bc6a4601a6280275336fc6d
|
File details
Details for the file django_restframework_apiview-1.3.0-py3.6.egg.
File metadata
- Download URL: django_restframework_apiview-1.3.0-py3.6.egg
- Upload date:
- Size: 166.7 kB
- Tags: Egg
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/38.2.4 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
613b3b56559ddee8fae041515b6551476032b391d43ac7621be93f54a56043da
|
|
| MD5 |
b5cc82f0649baaebd2e1be66f3aaef03
|
|
| BLAKE2b-256 |
396f8157276f6d137b506ae0183b2c80c8222bb70f6a44c3d326b4c31e9eb6d2
|
File details
Details for the file django_restframework_apiview-1.3.0-py3-none-any.whl.
File metadata
- Download URL: django_restframework_apiview-1.3.0-py3-none-any.whl
- Upload date:
- Size: 76.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/38.2.4 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c2666dad94b64ff64c94e87b23085d88253bbd90b557a80ad7a1c0b8369b91e
|
|
| MD5 |
0d1454564f23680d16a36188f2aaf65a
|
|
| BLAKE2b-256 |
2ce0c848387d65af8810686de07694945f7dfc4562f7786787e8fcfb30362d58
|