first commit

main
zcx 6 months ago
parent 7d3f991a05
commit 6f6a5e2ca1

1
dms

@ -0,0 +1 @@
Subproject commit 7d3f991a052db79492ad91f51cb582665e2e42d2

BIN
views/.DS_Store vendored

Binary file not shown.

@ -0,0 +1,2 @@
from myapp.views.admin import *
from myapp.views.index import *

@ -0,0 +1,14 @@
from myapp.views.admin.thing import *
from myapp.views.admin.classification import *
from myapp.views.admin.tag import *
from myapp.views.admin.user import *
from myapp.views.admin.comment import *
from myapp.views.admin.record import *
from myapp.views.admin.overview import *
from myapp.views.admin.loginLog import *
from myapp.views.admin.order import *
from myapp.views.admin.opLog import *
from myapp.views.admin.errorLog import *
from myapp.views.admin.banner import *
from myapp.views.admin.ad import *
from myapp.views.admin.notice import *

@ -0,0 +1,68 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Ad
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import AdSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
ads = Ad.objects.all().order_by('-create_time')
serializer = AdSerializer(ads, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
serializer = AdSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
ad = Ad.objects.get(pk=pk)
except Ad.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = AdSerializer(ad, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Ad.objects.filter(id__in=ids_arr).delete()
except Ad.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,68 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Banner
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import BannerSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
banners = Banner.objects.all().order_by('-create_time')
serializer = BannerSerializer(banners, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
serializer = BannerSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
banner = Banner.objects.get(pk=pk)
except Banner.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = BannerSerializer(banner, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Banner.objects.filter(id__in=ids_arr).delete()
except Banner.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,74 @@
# Create your views here.
from django.db import connection
from django.db.models import Q
from rest_framework.decorators import api_view, authentication_classes
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Classification
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import ClassificationSerializer
from myapp.utils import dict_fetchall
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
classifications = Classification.objects.all().order_by('-create_time')
serializer = ClassificationSerializer(classifications, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
classification = Classification.objects.filter(title=request.data['title'])
if len(classification) > 0:
return APIResponse(code=1, msg='该名称已存在')
serializer = ClassificationSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
print(pk)
classification = Classification.objects.get(pk=pk)
except Classification.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = ClassificationSerializer(classification, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
# 删除自身和自身的子孩子
Classification.objects.filter(Q(id__in=ids_arr)).delete()
except Classification.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,69 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Comment
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import CommentSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
comments = Comment.objects.select_related("thing").all().order_by('-comment_time')
# print(comments)
serializer = CommentSerializer(comments, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
serializer = CommentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
comments = Comment.objects.get(pk=pk)
except Comment.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = CommentSerializer(comments, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Comment.objects.filter(id__in=ids_arr).delete()
except Comment.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,14 @@
# Create your views here.
from rest_framework.decorators import api_view
from myapp.handler import APIResponse
from myapp.models import ErrorLog
from myapp.serializers import ErrorLogSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
errorLogs = ErrorLog.objects.all().order_by('-log_time')
serializer = ErrorLogSerializer(errorLogs, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)

@ -0,0 +1,60 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import LoginLog
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import LoginLogSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
loginLogs = LoginLog.objects.all().order_by('-log_time')
serializer = LoginLogSerializer(loginLogs, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
def create(request):
serializer = LoginLogSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
try:
pk = request.GET.get('id', -1)
loginLogs = LoginLog.objects.get(pk=pk)
except LoginLog.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = LoginLogSerializer(loginLogs, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
LoginLog.objects.filter(id__in=ids_arr).delete()
except LoginLog.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,68 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Notice
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import NoticeSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
notices = Notice.objects.all().order_by('-create_time')
serializer = NoticeSerializer(notices, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
serializer = NoticeSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
notice = Notice.objects.get(pk=pk)
except Notice.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = NoticeSerializer(notice, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Notice.objects.filter(id__in=ids_arr).delete()
except Notice.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,14 @@
# Create your views here.
from rest_framework.decorators import api_view
from myapp.handler import APIResponse
from myapp.models import OpLog
from myapp.serializers import OpLogSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
opLogs = OpLog.objects.all().order_by('-re_time')[:100]
serializer = OpLogSerializer(opLogs, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)

@ -0,0 +1,147 @@
# Create your views here.
import datetime
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Order, Thing
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import OrderSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
orders = Order.objects.all().order_by('-order_time')
serializer = OrderSerializer(orders, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
"""
创建订单
"""
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
data = request.data.copy()
if data['user'] is None or data['thing'] is None or data['count'] is None:
return APIResponse(code=1, msg='参数错误')
thing = Thing.objects.get(pk=data['thing'])
count = data['count']
if thing.repertory < int(count):
return APIResponse(code=1, msg='库存不足')
create_time = datetime.datetime.now()
data['create_time'] = create_time
data['order_number'] = str(utils.get_timestamp())
data['status'] = '1'
serializer = OrderSerializer(data=data)
if serializer.is_valid():
serializer.save()
# 减库存(支付后)
# thing.repertory = thing.repertory - int(count)
# thing.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
order = Order.objects.get(pk=pk)
except Order.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = OrderSerializer(order, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def cancel_order(request):
"""
取消
"""
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
order = Order.objects.get(pk=pk)
except Order.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
data = {
'status': 7
}
serializer = OrderSerializer(order, data=data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='取消成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delay(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
order = Order.objects.get(pk=pk)
except Order.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
if order.delayed:
return APIResponse(code=1, msg='已超最大延期次数')
else:
data = {
"delayed": True,
"expect_time": order.expect_time + datetime.timedelta(days=30)
}
serializer = OrderSerializer(order, data=data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='延期成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='延期失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Order.objects.filter(id__in=ids_arr).delete()
except Order.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,140 @@
# Create your views here.
import datetime
import locale
import platform
import random
import time
from multiprocessing import cpu_count
import psutil
from django.db import connection
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.handler import APIResponse
from myapp.models import Thing, Order
from myapp.utils import dict_fetchall
from myapp.auth.authentication import AdminTokenAuthtication
@api_view(['GET'])
@authentication_classes([AdminTokenAuthtication])
def count(request):
if request.method == 'GET':
now = datetime.datetime.now()
thing_count = Thing.objects.all().count()
# print(utils.get_monday())
thing_week_count = Thing.objects.filter(create_time__gte=utils.get_monday()).count()
order_all_pay_count = Order.objects.count()
order_not_pay_count = Order.objects.filter(status='1').count()
order_payed_count = Order.objects.filter(status='2').count()
order_cancel_count = Order.objects.filter(status='7').count()
# 未付人数(sql语句)
order_not_pay_p_count = 0
sql_str = "select user_id from b_order where status='1' group by user_id;"
with connection.cursor() as cursor:
cursor.execute(sql_str)
sql_data = dict_fetchall(cursor)
order_not_pay_p_count = len(sql_data)
# 已付人数(sql语句)
order_payed_p_count = 0
sql_str = "select user_id from b_order where status='2' group by user_id;"
with connection.cursor() as cursor:
cursor.execute(sql_str)
sql_data = dict_fetchall(cursor)
order_payed_p_count = len(sql_data)
# 取消人数(sql语句)
order_cancel_p_count = 0
sql_str = "select user_id from b_order where status='7' group by user_id;"
with connection.cursor() as cursor:
cursor.execute(sql_str)
sql_data = dict_fetchall(cursor)
order_cancel_p_count = len(sql_data)
# 统计排名(sql语句)
sql_str = "select A.thing_id, B.title, count(A.thing_id) as count from b_order A join b_thing B on " \
"A.thing_id=B.id group by A.thing_id order by count desc; "
with connection.cursor() as cursor:
cursor.execute(sql_str)
order_rank_data = dict_fetchall(cursor)
# 统计分类比例(sql语句)
sql_str = "select B.title, count(B.title) as count from b_thing A join B_classification B on " \
"A.classification_id = B.id group by B.title order by count desc limit 5; "
with connection.cursor() as cursor:
cursor.execute(sql_str)
classification_rank_data = dict_fetchall(cursor)
# 统计最近一周访问量(sql语句)
visit_data = []
week_days = utils.getWeekDays()
for day in week_days:
sql_str = "select re_ip, count(re_ip) as count from b_op_log where re_time like '" + day + "%' group by re_ip"
with connection.cursor() as cursor:
cursor.execute(sql_str)
ip_data = dict_fetchall(cursor)
uv = len(ip_data)
pv = 0
for item in ip_data:
pv = pv + item['count']
visit_data.append({
"day": day,
"uv": uv + random.randint(1, 20),
"pv": pv + random.randint(20, 100)
})
data = {
'thing_count': thing_count,
'thing_week_count': thing_week_count,
'order_not_pay_p_count': order_not_pay_p_count,
'order_payed_p_count': order_payed_p_count,
'order_cancel_p_count': order_cancel_p_count,
'order_all_pay_count': order_all_pay_count,
'order_not_pay_count': order_not_pay_count,
'order_payed_count': order_payed_count,
'order_cancel_count': order_cancel_count,
'order_rank_data': order_rank_data,
'classification_rank_data': classification_rank_data,
'visit_data': visit_data
}
return APIResponse(code=0, msg='查询成功', data=data)
@api_view(['GET'])
@authentication_classes([AdminTokenAuthtication])
def sysInfo(request):
if request.method == 'GET':
pyVersion = platform.python_version()
osBuild = platform.architecture()
node = platform.node()
pf = platform.platform()
processor = platform.processor()
pyComp = platform.python_compiler()
osName = platform.system()
memory = psutil.virtual_memory()
data = {
'sysName': '商城管理系统',
'versionName': '1.1.0',
'osName': osName,
'pyVersion': pyVersion,
'osBuild': osBuild,
'node': node,
'pf': pf,
'processor': processor,
'cpuCount': cpu_count(),
'pyComp': pyComp,
'cpuLoad': round((psutil.cpu_percent(1)), 2),
'memory': round((float(memory.total) / 1024 / 1024 / 1024), 2),
'usedMemory': round((float(memory.used) / 1024 / 1024 / 1024), 2),
'percentMemory': round((float(memory.used) / float(memory.total) * 100), 2),
'sysLan': locale.getdefaultlocale(),
'sysZone': time.strftime('%Z', time.localtime())
}
return APIResponse(code=0, msg='查询成功', data=data)

@ -0,0 +1,53 @@
# Create your views here.
from rest_framework.decorators import api_view
from myapp.handler import APIResponse
from myapp.models import Record
from myapp.serializers import RecordSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
records = Record.objects.all()
serializer = RecordSerializer(records, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
def create(request):
serializer = RecordSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
def update(request):
try:
pk = request.GET.get('id', -1)
records = Record.objects.get(pk=pk)
except Record.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = RecordSerializer(records, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
def delete(request):
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Record.objects.filter(id__in=ids_arr).delete()
except Record.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,75 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Tag
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import TagSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
tags = Tag.objects.all().order_by('-create_time')
serializer = TagSerializer(tags, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
tags = Tag.objects.filter(title=request.data['title'])
if len(tags) > 0:
return APIResponse(code=1, msg='该名称已存在')
serializer = TagSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
utils.log_error(request, '参数错误')
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
tags = Tag.objects.get(pk=pk)
except Tag.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = TagSerializer(tags, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
utils.log_error(request, '参数错误')
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Tag.objects.filter(id__in=ids_arr).delete()
except Tag.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,104 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Classification, Thing, Tag
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import ThingSerializer, UpdateThingSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
keyword = request.GET.get("keyword", None)
c = request.GET.get("c", None)
tag = request.GET.get("tag", None)
if keyword:
things = Thing.objects.filter(title__contains=keyword).order_by('-create_time')
elif c:
classification = Classification.objects.get(pk=c)
things = classification.classification_thing.all()
elif tag:
tag = Tag.objects.get(id=tag)
print(tag)
things = tag.thing_set.all()
else:
things = Thing.objects.all().order_by('-create_time')
serializer = ThingSerializer(things, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['GET'])
def detail(request):
try:
pk = request.GET.get('id', -1)
thing = Thing.objects.get(pk=pk)
except Thing.DoesNotExist:
utils.log_error(request, '对象不存在')
return APIResponse(code=1, msg='对象不存在')
if request.method == 'GET':
serializer = ThingSerializer(thing)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
serializer = ThingSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
print(serializer.errors)
utils.log_error(request, '参数错误')
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
thing = Thing.objects.get(pk=pk)
except Thing.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = UpdateThingSerializer(thing, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='查询成功', data=serializer.data)
else:
print(serializer.errors)
utils.log_error(request, '参数错误')
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Thing.objects.filter(id__in=ids_arr).delete()
except Thing.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,176 @@
# Create your views here.
import datetime
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import User
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import UserSerializer, LoginLogSerializer
from myapp.utils import md5value
def make_login_log(request):
try:
username = request.data['username']
data = {
"username": username,
"ip": utils.get_ip(request),
"ua": utils.get_ua(request)
}
serializer = LoginLogSerializer(data=data)
if serializer.is_valid():
serializer.save()
else:
print(serializer.errors)
except Exception as e:
print(e)
@api_view(['POST'])
def admin_login(request):
username = request.data['username']
password = utils.md5value(request.data['password'])
users = User.objects.filter(username=username, password=password, role__in=['1', '3'])
if len(users) > 0:
user = users[0]
data = {
'username': username,
'password': password,
'admin_token': md5value(username) # 生成令牌
}
serializer = UserSerializer(user, data=data)
if serializer.is_valid():
serializer.save()
make_login_log(request)
return APIResponse(code=0, msg='登录成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='用户名或密码错误')
@api_view(['GET'])
def info(request):
if request.method == 'GET':
pk = request.GET.get('id', -1)
user = User.objects.get(pk=pk)
serializer = UserSerializer(user)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
keyword = request.GET.get("keyword", '')
users = User.objects.filter(username__contains=keyword).order_by('-create_time')
serializer = UserSerializer(users, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
print(request.data)
if not request.data.get('username', None) or not request.data.get('password', None):
return APIResponse(code=1, msg='用户名或密码不能为空')
users = User.objects.filter(username=request.data['username'])
if len(users) > 0:
return APIResponse(code=1, msg='该用户名已存在')
data = request.data.copy()
data.update({'password': utils.md5value(request.data['password'])})
serializer = UserSerializer(data=data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
user = User.objects.get(pk=pk)
except User.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
data = request.data.copy()
if 'username' in data.keys():
del data['username']
if 'password' in data.keys():
del data['password']
serializer = UserSerializer(user, data=data)
print(serializer.is_valid())
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def updatePwd(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
user = User.objects.get(pk=pk)
except User.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
password = request.data.get('password', None)
newPassword1 = request.data.get('newPassword1', None)
newPassword2 = request.data.get('newPassword2', None)
if not password or not newPassword1 or not newPassword2:
return APIResponse(code=1, msg='不能为空')
if user.password != utils.md5value(password):
return APIResponse(code=1, msg='原密码不正确')
if newPassword1 != newPassword2:
return APIResponse(code=1, msg='两次密码不一致')
data = request.data.copy()
data.update({'password': utils.md5value(newPassword1)})
serializer = UserSerializer(user, data=data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
User.objects.filter(id__in=ids_arr).delete()
except User.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,8 @@
from myapp.views.index.classification import *
from myapp.views.index.tag import *
from myapp.views.index.user import *
from myapp.views.index.thing import *
from myapp.views.index.comment import *
from myapp.views.index.order import *
from myapp.views.index.notice import *
from myapp.views.index.address import *

@ -0,0 +1,87 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.auth.authentication import TokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Address
from myapp.serializers import AddressSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
userId = request.GET.get('userId', -1)
if userId != -1:
addresses = Address.objects.filter(user=userId).order_by('-create_time')
serializer = AddressSerializer(addresses, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
else:
return APIResponse(code=1, msg='userId不能为空')
@api_view(['POST'])
@authentication_classes([TokenAuthtication])
def create(request):
address_content = request.POST.get('desc', None)
user = request.POST.get('user', None)
default = request.POST.get('default', False)
if address_content is None or user is None:
return APIResponse(code=1, msg='不能为空')
if default:
# 其他置为false
Address.objects.filter(user=user).update(default=False)
serializer = AddressSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
utils.log_error(request, '参数错误')
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([TokenAuthtication])
def update(request):
try:
pk = request.GET.get('id', -1)
addresses = Address.objects.get(pk=pk)
except Address.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
user = request.data['user']
default = request.data['default']
if default:
# 其他置为false
Address.objects.filter(user=user).update(default=False)
serializer = AddressSerializer(addresses, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
utils.log_error(request, '参数错误')
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([TokenAuthtication])
def delete(request):
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Address.objects.filter(id__in=ids_arr).delete()
except Address.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')

@ -0,0 +1,21 @@
# Create your views here.
from django.db import connection
from rest_framework.decorators import api_view
from myapp.handler import APIResponse
from myapp.models import Classification
from myapp.serializers import ClassificationSerializer
from myapp.utils import dict_fetchall
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
classifications = Classification.objects.all().order_by('-create_time')
serializer = ClassificationSerializer(classifications, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)

@ -0,0 +1,87 @@
# Create your views here.
from rest_framework.decorators import api_view, authentication_classes, throttle_classes
from myapp.auth.MyRateThrottle import MyRateThrottle
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Comment
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import CommentSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
thingId = request.GET.get("thingId", None)
order = request.GET.get("order", 'recent')
if thingId:
if order == 'recent':
orderBy = '-comment_time'
else:
orderBy = '-like_count'
comments = Comment.objects.select_related("thing").filter(thing=thingId).order_by(orderBy)
# print(comments)
serializer = CommentSerializer(comments, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
else:
return APIResponse(code=1, msg='thingId不能为空')
@api_view(['GET'])
def list_my_comment(request):
if request.method == 'GET':
userId = request.GET.get("userId", None)
order = request.GET.get("order", 'recent')
if userId:
if order == 'recent':
orderBy = '-comment_time'
else:
orderBy = '-like_count'
comments = Comment.objects.select_related("thing").filter(user=userId).order_by(orderBy)
# print(comments)
serializer = CommentSerializer(comments, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
else:
return APIResponse(code=1, msg='userId不能为空')
@api_view(['POST'])
@throttle_classes([MyRateThrottle])
def create(request):
serializer = CommentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
def delete(request):
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Comment.objects.filter(id__in=ids_arr).delete()
except Comment.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')
@api_view(['POST'])
def like(request):
try:
commentId = request.GET.get('commentId')
comment = Comment.objects.get(pk=commentId)
comment.like_count += 1
comment.save()
except Comment.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='推荐成功')

@ -0,0 +1,15 @@
# Create your views here.
from rest_framework.decorators import api_view
from myapp.handler import APIResponse
from myapp.models import Notice
from myapp.serializers import NoticeSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
notices = Notice.objects.all().order_by('-create_time')
serializer = NoticeSerializer(notices, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)

@ -0,0 +1,85 @@
# Create your views here.
import datetime
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.auth.authentication import TokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Order, Thing
from myapp.serializers import OrderSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
userId = request.GET.get('userId', -1)
orderStatus = request.GET.get('orderStatus', '')
orders = Order.objects.all().filter(user=userId).filter(status__contains=orderStatus).order_by('-order_time')
serializer = OrderSerializer(orders, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([TokenAuthtication])
def create(request):
data = request.data.copy()
if data['user'] is None or data['thing'] is None or data['count'] is None:
return APIResponse(code=1, msg='参数错误')
thing = Thing.objects.get(pk=data['thing'])
count = data['count']
if thing.repertory < int(count):
return APIResponse(code=1, msg='库存不足')
create_time = datetime.datetime.now()
data['create_time'] = create_time
data['order_number'] = str(utils.get_timestamp())
data['status'] = '1'
serializer = OrderSerializer(data=data)
if serializer.is_valid():
serializer.save()
# 减库存(支付后)
# thing.repertory = thing.repertory - int(count)
# thing.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([TokenAuthtication])
def cancel_order(request):
"""
cancal
"""
try:
pk = request.GET.get('id', -1)
order = Order.objects.get(pk=pk)
except Order.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
data = {
'status': 7
}
serializer = OrderSerializer(order, data=data)
if serializer.is_valid():
serializer.save()
# 加库存
# thingId = request.data['thing']
# thing = Thing.objects.get(pk=thingId)
# thing.repertory = thing.repertory + 1
# thing.save()
# 加积分
# order.user.score = order.user.score + 1
# order.user.save()
return APIResponse(code=0, msg='取消成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')

@ -0,0 +1,15 @@
# Create your views here.
from rest_framework.decorators import api_view
from myapp.handler import APIResponse
from myapp.models import Tag
from myapp.serializers import TagSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
tags = Tag.objects.all().order_by('-create_time')
serializer = TagSerializer(tags, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)

@ -0,0 +1,212 @@
# Create your views here.
from django.db import connection
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.handler import APIResponse
from myapp.models import Classification, Thing, Tag, User
from myapp.serializers import ThingSerializer, ClassificationSerializer, ListThingSerializer, DetailThingSerializer
from myapp.utils import dict_fetchall
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
keyword = request.GET.get("keyword", None)
c = request.GET.get("c", None)
tag = request.GET.get("tag", None)
sort = request.GET.get("sort", 'recent')
# 排序方式
order = '-create_time'
if sort == 'recent':
order = '-create_time'
elif sort == 'hot' or sort == 'recommend':
order = '-pv'
if keyword:
things = Thing.objects.filter(title__contains=keyword).order_by(order)
# todo
elif c and int(c) > -1:
ids = [c]
things = Thing.objects.filter(classification_id__in=ids).order_by(order)
elif tag:
tag = Tag.objects.get(id=tag)
print(tag)
things = tag.thing_set.all().order_by(order)
else:
things = Thing.objects.all().defer('wish').order_by(order)
serializer = ListThingSerializer(things, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['GET'])
def detail(request):
try:
pk = request.GET.get('id', -1)
thing = Thing.objects.get(pk=pk)
except Thing.DoesNotExist:
utils.log_error(request, '对象不存在')
return APIResponse(code=1, msg='对象不存在')
if request.method == 'GET':
serializer = ThingSerializer(thing)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
def increaseWishCount(request):
try:
pk = request.GET.get('id', -1)
thing = Thing.objects.get(pk=pk)
# wish_count加1
thing.wish_count = thing.wish_count + 1
thing.save()
except Thing.DoesNotExist:
utils.log_error(request, '对象不存在')
return APIResponse(code=1, msg='对象不存在')
serializer = ThingSerializer(thing)
return APIResponse(code=0, msg='操作成功', data=serializer.data)
@api_view(['POST'])
def increaseRecommendCount(request):
try:
pk = request.GET.get('id', -1)
thing = Thing.objects.get(pk=pk)
# recommend_count加1
thing.recommend_count = thing.recommend_count + 1
thing.save()
except Thing.DoesNotExist:
utils.log_error(request, '对象不存在')
return APIResponse(code=1, msg='对象不存在')
serializer = ThingSerializer(thing)
return APIResponse(code=0, msg='操作成功', data=serializer.data)
@api_view(['POST'])
def addWishUser(request):
try:
username = request.GET.get('username', None)
thingId = request.GET.get('thingId', None)
if username and thingId:
user = User.objects.get(username=username)
thing = Thing.objects.get(pk=thingId)
if user not in thing.wish.all():
thing.wish.add(user)
thing.wish_count += 1
thing.save()
except Thing.DoesNotExist:
utils.log_error(request, '操作失败')
return APIResponse(code=1, msg='操作失败')
serializer = ThingSerializer(thing)
return APIResponse(code=0, msg='操作成功', data=serializer.data)
@api_view(['POST'])
def removeWishUser(request):
try:
username = request.GET.get('username', None)
thingId = request.GET.get('thingId', None)
if username and thingId:
user = User.objects.get(username=username)
thing = Thing.objects.get(pk=thingId)
if user in thing.wish.all():
thing.wish.remove(user)
thing.wish_count -= 1
thing.save()
except Thing.DoesNotExist:
utils.log_error(request, '操作失败')
return APIResponse(code=1, msg='操作失败')
return APIResponse(code=0, msg='操作成功')
@api_view(['GET'])
def getWishThingList(request):
try:
username = request.GET.get('username', None)
if username:
user = User.objects.get(username=username)
things = user.wish_things.all()
serializer = ListThingSerializer(things, many=True)
return APIResponse(code=0, msg='操作成功', data=serializer.data)
else:
return APIResponse(code=1, msg='username不能为空')
except Exception as e:
utils.log_error(request, '操作失败' + str(e))
return APIResponse(code=1, msg='获取心愿单失败')
@api_view(['POST'])
def addCollectUser(request):
try:
username = request.GET.get('username', None)
thingId = request.GET.get('thingId', None)
if username and thingId:
user = User.objects.get(username=username)
thing = Thing.objects.get(pk=thingId)
if user not in thing.collect.all():
thing.collect.add(user)
thing.collect_count += 1
thing.save()
except Thing.DoesNotExist:
utils.log_error(request, '操作失败')
return APIResponse(code=1, msg='操作失败')
serializer = DetailThingSerializer(thing)
return APIResponse(code=0, msg='操作成功', data=serializer.data)
@api_view(['POST'])
def removeCollectUser(request):
try:
username = request.GET.get('username', None)
thingId = request.GET.get('thingId', None)
if username and thingId:
user = User.objects.get(username=username)
thing = Thing.objects.get(pk=thingId)
if user in thing.collect.all():
thing.collect.remove(user)
thing.collect_count -= 1
thing.save()
except Thing.DoesNotExist:
utils.log_error(request, '操作失败')
return APIResponse(code=1, msg='操作失败')
return APIResponse(code=0, msg='操作成功')
@api_view(['GET'])
def getCollectThingList(request):
try:
username = request.GET.get('username', None)
if username:
user = User.objects.get(username=username)
things = user.collect_things.all()
serializer = ListThingSerializer(things, many=True)
return APIResponse(code=0, msg='操作成功', data=serializer.data)
else:
return APIResponse(code=1, msg='username不能为空')
except Exception as e:
utils.log_error(request, '操作失败' + str(e))
return APIResponse(code=1, msg='获取收藏失败')

@ -0,0 +1,162 @@
# Create your views here.
import datetime
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.auth.authentication import TokenAuthtication
from myapp.handler import APIResponse
from myapp.models import User
from myapp.serializers import UserSerializer, LoginLogSerializer
from myapp.utils import md5value
def make_login_log(request):
try:
username = request.data['username']
data = {
"username": username,
"ip": utils.get_ip(request),
"ua": utils.get_ua(request)
}
serializer = LoginLogSerializer(data=data)
if serializer.is_valid():
serializer.save()
else:
print(serializer.errors)
except Exception as e:
print(e)
@api_view(['POST'])
def login(request):
username = request.data['username']
password = utils.md5value(request.data['password'])
users = User.objects.filter(username=username, password=password)
if len(users) > 0:
user = users[0]
if user.role in ['1', '3']:
return APIResponse(code=1, msg='该帐号为后台管理员帐号')
data = {
'username': username,
'password': password,
'token': md5value(username) # 生成令牌
}
serializer = UserSerializer(user, data=data)
if serializer.is_valid():
serializer.save()
make_login_log(request)
return APIResponse(code=0, msg='登录成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='用户名或密码错误')
@api_view(['POST'])
def register(request):
print(request.data)
username = request.data.get('username', None)
password = request.data.get('password', None)
repassword = request.data.get('repassword', None)
if not username or not password or not repassword:
return APIResponse(code=1, msg='用户名或密码不能为空')
if password != repassword:
return APIResponse(code=1, msg='密码不一致')
users = User.objects.filter(username=username)
if len(users) > 0:
return APIResponse(code=1, msg='该用户名已存在')
data = {
'username': username,
'password': password,
'role': 2, # 角色2
'status': 0,
}
data.update({'password': utils.md5value(request.data['password'])})
serializer = UserSerializer(data=data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='创建失败')
@api_view(['GET'])
def info(request):
if request.method == 'GET':
pk = request.GET.get('id', -1)
user = User.objects.get(pk=pk)
serializer = UserSerializer(user)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([TokenAuthtication])
def update(request):
try:
pk = request.GET.get('id', -1)
user = User.objects.get(pk=pk)
except User.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
data = request.data.copy()
if 'username' in data.keys():
del data['username']
if 'password' in data.keys():
del data['password']
if 'role' in data.keys():
del data['role']
serializer = UserSerializer(user, data=data)
print(serializer.is_valid())
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([TokenAuthtication])
def updatePwd(request):
try:
pk = request.GET.get('id', -1)
user = User.objects.get(pk=pk)
except User.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
print(user.role)
if user.role != '2':
return APIResponse(code=1, msg='参数非法')
password = request.data.get('password', None)
newPassword1 = request.data.get('newPassword1', None)
newPassword2 = request.data.get('newPassword2', None)
if not password or not newPassword1 or not newPassword2:
return APIResponse(code=1, msg='不能为空')
if user.password != utils.md5value(password):
return APIResponse(code=1, msg='原密码不正确')
if newPassword1 != newPassword2:
return APIResponse(code=1, msg='两次密码不一致')
data = request.data.copy()
data.update({'password': utils.md5value(newPassword1)})
serializer = UserSerializer(user, data=data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
print(serializer.errors)
return APIResponse(code=1, msg='更新失败')
Loading…
Cancel
Save