# 自定义用户model
This commit is contained in:
parent
b8f82ba73c
commit
d6b1c4e81f
|
@ -1,3 +1,41 @@
|
||||||
|
import os
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.contrib.auth.models import AbstractUser
|
||||||
|
from imagekit.models import ImageSpecField
|
||||||
|
from imagekit.processors import ResizeToFill
|
||||||
|
from imagekit.exceptions import MissingSource
|
||||||
|
|
||||||
# Create your models here.
|
|
||||||
|
def avatar_path(instance, filename):
|
||||||
|
return os.path.join('avatar', instance.username, filename)
|
||||||
|
|
||||||
|
|
||||||
|
class User(AbstractUser):
|
||||||
|
ROLES = (
|
||||||
|
('admin', 'admin'),
|
||||||
|
('editor', 'editor')
|
||||||
|
)
|
||||||
|
phone = models.CharField(max_length=11, null=True, blank=True, default='', verbose_name='电话')
|
||||||
|
department = models.CharField(max_length=20, null=True, blank=True, default='', verbose_name='部门')
|
||||||
|
position = models.CharField(max_length=50, null=True, blank=True, default='', verbose_name='职位')
|
||||||
|
introduction = models.TextField(verbose_name='简介')
|
||||||
|
role = models.CharField(max_length=20, default='admin', choices=ROLES, verbose_name='角色')
|
||||||
|
avatar = models.ImageField(upload_to=avatar_path, null=True, blank=True)
|
||||||
|
avatar_thumbnail = ImageSpecField(source='avatar',
|
||||||
|
processors=[ResizeToFill(100, 100)],
|
||||||
|
format='JPEG',
|
||||||
|
options={'quality': 100})
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = '用户'
|
||||||
|
verbose_name_plural = verbose_name
|
||||||
|
|
||||||
|
def avatar_url(self):
|
||||||
|
try:
|
||||||
|
avatar_thumbnail_url = self.avatar_thumbnail.url
|
||||||
|
except MissingSource:
|
||||||
|
avatar_thumbnail_url = ''
|
||||||
|
return avatar_thumbnail_url
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.username
|
||||||
|
|
|
@ -14,6 +14,7 @@ from .serializers import MosqListSerializer, MosqPostListSerializer
|
||||||
|
|
||||||
class MosquitoListAPIView(ListAPIView):
|
class MosquitoListAPIView(ListAPIView):
|
||||||
serializer_class = MosqListSerializer
|
serializer_class = MosqListSerializer
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
filter_backends = [SearchFilter, OrderingFilter]
|
filter_backends = [SearchFilter, OrderingFilter]
|
||||||
pagination_class = PostLimitOffsetPagination
|
pagination_class = PostLimitOffsetPagination
|
||||||
search_fields = ['name', 'device_id', 'region']
|
search_fields = ['name', 'device_id', 'region']
|
||||||
|
@ -32,6 +33,7 @@ class MosquitoListAPIView(ListAPIView):
|
||||||
|
|
||||||
class MosquitoPostListAPIView(ListAPIView):
|
class MosquitoPostListAPIView(ListAPIView):
|
||||||
serializer_class = MosqPostListSerializer
|
serializer_class = MosqPostListSerializer
|
||||||
|
permission_classes = [IsAuthenticated,]
|
||||||
filter_backends = [SearchFilter, OrderingFilter]
|
filter_backends = [SearchFilter, OrderingFilter]
|
||||||
pagination_class = PostLimitOffsetPagination
|
pagination_class = PostLimitOffsetPagination
|
||||||
search_fields = ['mosq__name', 'mosq__region']
|
search_fields = ['mosq__name', 'mosq__region']
|
||||||
|
|
|
@ -14,6 +14,7 @@ from ..models import SmartModule, SmartPush
|
||||||
|
|
||||||
class SmartListAPIView(ListAPIView):
|
class SmartListAPIView(ListAPIView):
|
||||||
serializer_class = SmartListSerializer
|
serializer_class = SmartListSerializer
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
filter_backends = [SearchFilter, OrderingFilter]
|
filter_backends = [SearchFilter, OrderingFilter]
|
||||||
pagination_class = PostLimitOffsetPagination
|
pagination_class = PostLimitOffsetPagination
|
||||||
search_fields = ['name', 'device_id', 'chip_id', 'region']
|
search_fields = ['name', 'device_id', 'chip_id', 'region']
|
||||||
|
@ -34,6 +35,7 @@ class SmartListAPIView(ListAPIView):
|
||||||
|
|
||||||
class SmartPushListAPIView(ListAPIView):
|
class SmartPushListAPIView(ListAPIView):
|
||||||
serializer_class = SmartPushListSerializer
|
serializer_class = SmartPushListSerializer
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
filter_backends = [SearchFilter, OrderingFilter]
|
filter_backends = [SearchFilter, OrderingFilter]
|
||||||
pagination_class = PostLimitOffsetPagination
|
pagination_class = PostLimitOffsetPagination
|
||||||
search_fields = ['smart__name']
|
search_fields = ['smart__name']
|
||||||
|
|
|
@ -41,6 +41,7 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'corsheaders',
|
'corsheaders',
|
||||||
|
'accounts',
|
||||||
'mosquito',
|
'mosquito',
|
||||||
'smart',
|
'smart',
|
||||||
]
|
]
|
||||||
|
@ -114,6 +115,7 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
AUTH_USER_MODEL = 'accounts.User'
|
||||||
|
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/2.0/topics/i18n/
|
# https://docs.djangoproject.com/en/2.0/topics/i18n/
|
||||||
|
@ -138,7 +140,7 @@ STATIC_URL = '/static/'
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_RENDERER_CLASSES': (
|
'DEFAULT_RENDERER_CLASSES': (
|
||||||
'rest_framework.renderers.JSONRenderer',
|
'rest_framework.renderers.JSONRenderer',
|
||||||
'rest_framework.renderers.BrowsableAPIRenderer',
|
# 'rest_framework.renderers.BrowsableAPIRenderer',
|
||||||
),
|
),
|
||||||
# 'DEFAULT_PARSER_CLASSES': (
|
# 'DEFAULT_PARSER_CLASSES': (
|
||||||
# 'rest_framework.parsers.JSONParser',
|
# 'rest_framework.parsers.JSONParser',
|
||||||
|
|
Loading…
Reference in New Issue