增加用户登录

包括用户model、用户登录模板、登录api和测试。
This commit is contained in:
virusdefender 2015-06-29 12:43:17 +08:00
parent 00052d8e9b
commit 592720dd01
11 changed files with 175 additions and 7 deletions

View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)),
('username', models.CharField(unique=True, max_length=30)),
],
options={
'db_table': 'user',
},
),
migrations.CreateModel(
name='AdminGroup',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
],
),
migrations.AddField(
model_name='user',
name='admin_group',
field=models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, to='account.AdminGroup', null=True),
),
]

View File

@ -7,9 +7,21 @@ class AdminGroup(models.Model):
pass
class UserManager(models.Manager):
use_in_migrations = True
def get_by_natural_key(self, username):
return self.get(**{self.model.USERNAME_FIELD: username})
class User(AbstractBaseUser):
username = models.CharField(max_length=30, unique=True)
admin_group = models.ForeignKey(AdminGroup, null=True, on_delete=models.SET_NULL)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
objects = UserManager()
class Meta:
db_table = "user"

7
account/serializers.py Normal file
View File

@ -0,0 +1,7 @@
# coding=utf-8
from rest_framework import serializers
class UserLoginSerializer(serializers.Serializer):
username = serializers.CharField(max_length=30)
password = serializers.CharField(max_length=30)

View File

@ -1,3 +1,38 @@
from django.test import TestCase
# coding=utf-8
from django.core.urlresolvers import reverse
from django.test import TestCase, Client
# Create your tests here.
from rest_framework.test import APITestCase, APIClient
from .models import User
class UserLoginTest(TestCase):
def test_login_page(self):
client = Client()
response = client.get(reverse("user_login_page"))
self.assertTemplateUsed(response, "account/login.html")
class UserLoginAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("user_login_api")
user = User.objects.create(username="test")
user.set_password("test")
user.save()
def test_invalid_data(self):
data = {"username": "test"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_error_username_or_password(self):
error_data = {"username": "test", "password": "test11"}
response = self.client.post(self.url, data=error_data)
self.assertEqual(response.data, {"code": 1, "data": u"用户名或密码错误"})
def test_success_login(self):
data = {"username": "test", "password": "test"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"登录成功"})

View File

@ -1 +1,38 @@
# coding=utf-8
from django.contrib import auth
from django.shortcuts import render
from rest_framework.views import APIView
from utils.shortcuts import serializer_invalid_response, error_response, success_response
from .models import User
from .serializers import UserLoginSerializer
class UserLoginAPIView(APIView):
def post(self, request):
"""
用户登录json api接口
---
request_serializer: UserLoginSerializer
"""
serializer = UserLoginSerializer(data=request.DATA)
if serializer.is_valid():
data = serializer.data
user = auth.authenticate(username=data["username"], password=data["password"])
# 用户名或密码错误的话 返回None
if user:
auth.login(request, user)
return success_response(u"登录成功")
else:
return error_response(u"用户名或密码错误")
else:
return serializer_invalid_response(serializer)
class UserRegisterView(APIView):
def get(self, request):
pass
def post(self, request):
pass

View File

@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.

View File

@ -46,6 +46,12 @@ INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'account',
'rest_framework',
'rest_framework_swagger',
)
MIDDLEWARE_CLASSES = (
@ -113,6 +119,12 @@ STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, "template"),
)
AUTH_USER_MODEL = 'account.User'
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
@ -147,7 +159,7 @@ LOGGING = {
},
'django.db.backends': {
'handlers': ['console'],
'level': 'DEBUG',
'level': 'ERROR',
'propagate': True,
}
},

View File

@ -3,10 +3,17 @@ from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView
from account.views import UserLoginAPIView
urlpatterns = [
# Examples:
# url(r'^$', 'qduoj.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^docs/', include('rest_framework_swagger.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^login/$', TemplateView.as_view(template_name="account/login.html"), name="user_login_page"),
url(r'^api/login/$', UserLoginAPIView.as_view(), name="user_login_api"),
]

View File

@ -3,4 +3,5 @@ MySQL-python
redis
django-redis-sessions
djangorestframework
django-rest-swagger
celery

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
</html>

View File

@ -1 +1,14 @@
# coding=utf-8
from rest_framework.response import Response
def error_response(error_reason):
return Response(data={"code": 1, "data": error_reason})
def serializer_invalid_response(serializer):
return error_response(serializer.errors)
def success_response(data):
return Response(data={"code": 0, "data": data})