Skip to content

Commit

Permalink
✨ feat: 新增用户接口与审核权限分割
Browse files Browse the repository at this point in the history
  • Loading branch information
121812 committed Jul 16, 2024
1 parent 53fd91e commit cb2b5aa
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 36 deletions.
2 changes: 2 additions & 0 deletions SeeHarbergertaxsInDAW/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,5 @@
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = 'media/'

SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
5 changes: 3 additions & 2 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from django.contrib.auth import get_user_model
import datetime


# 用户表
class User(models.Model):
class Holder(models.Model):
id = models.AutoField(verbose_name='用户ID', db_index=True, primary_key=True)
useraddr = models.CharField(verbose_name='用户钱包地址', max_length=1024, )
sns = models.CharField(verbose_name='SNS名称', max_length=1024, blank=True, null=True)
Expand All @@ -18,7 +19,7 @@ def __str__(self):

class Meta:
ordering = ['-advtimestart']
verbose_name = "User"
verbose_name = "Holder"
# 广告表
class Advertise(models.Model):
audStatusChoices = (
Expand Down
17 changes: 13 additions & 4 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
from rest_framework import serializers
from .models import User
from .models import Holder
from .models import Advertise
from .models import Image
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User

usermodel = get_user_model()

# 用户
class UserSerializer(serializers.HyperlinkedModelSerializer):
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id', 'username']

# 持有者
class HolderSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Holder
fields = ['id', 'useraddr', 'sns', 'advtimestart', 'advtimeend', 'price', 'upstreamuseraddr', 'txhash']

# 广告提交
Expand All @@ -21,7 +29,7 @@ class Meta:
class AuditSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Advertise
fields = ['id','useraddr', 'pcimage', 'mobimage', 'audstatus', 'auddate', 'audmsg']
fields = ['id','useraddr', 'pcimage', 'mobimage', 'audstatus', 'auddate', 'audmsg', 'applymsg']

# 广告查询
class AdvertiseListSerializer(serializers.HyperlinkedModelSerializer):
Expand All @@ -33,4 +41,5 @@ class Meta:
class ImageSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Image
fields = ['id', 'image', 'type']
fields = ['id', 'image', 'type']

6 changes: 4 additions & 2 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from . import views

router = routers.DefaultRouter()
router.register(r'user', views.UserViewSet, basename='user')
router.register(r'holder', views.HolderViewSet, basename='holder')
router.register(r'advertise', views.AdvertiseViewSet, basename='advertise')
router.register(r'image', views.imageViewSet, basename='image')
router.register(r'audit', views.AuditViewSet, basename="audit")
router.register(r'image', views.ImageViewSet, basename='image')
router.register(r'login', views.LoginViewSet, basename='login')



Expand Down
113 changes: 85 additions & 28 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,75 @@
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import MultiPartParser, FormParser, FileUploadParser, JSONParser
from rest_framework.pagination import PageNumberPagination
from django.contrib.auth.models import User
from django.contrib.auth import login
from django.contrib.auth import logout

import uuid
from .models import User, Advertise, Image
from .serializers import UserSerializer, AdvertiseSerializer, AuditSerializer, AdvertiseListSerializer, ImageSerializer
from .models import Holder, Advertise, Image
from .serializers import HolderSerializer, AdvertiseSerializer, AuditSerializer, AdvertiseListSerializer, ImageSerializer, UserSerializer

class MyPageNumberPagination(PageNumberPagination):
page_size = 2 # default page size
page_size_query_param = 'size' # ?page=xx&size=??
max_page_size = 10 # max page size

class LoginViewSet(viewsets.ViewSet):
def create(self, request):
useraddr = request.data['useraddr']
signature = request.data['signature']
message = request.data['message']
print(message)
isValild = validate(msg=message,signature=signature, useraddr=useraddr)
if isValild == False:
return Response('Signature Error', status=status.HTTP_400_BAD_REQUEST)
try:
user = User.objects.get(username=useraddr)
except User.DoesNotExist:
# 如果用户不存在,可以在此处创建新用户
user = User.objects.create_user(username=useraddr, password='')
login(request, user)

serializer = UserSerializer(user)
return Response(serializer.data)

class LogoutViewSet(viewsets.ViewSet):
def create(self, request):
useraddr = request.data['useraddr']
signature = request.data['signature']
message = request.data['message']
print(message)
isValild = validate(msg=message,signature=signature, useraddr=useraddr)
if isValild == False:
return Response('Signature Error', status=status.HTTP_400_BAD_REQUEST)
try:
user = User.objects.get(username=useraddr)
except User.DoesNotExist:
# 如果用户不存在,可以在此处创建新用户
user = User.objects.create_user(username=useraddr, password='')
logout(request, user)

serializer = UserSerializer(user)
return Response(serializer.data)



# 用户相关视图集
class UserViewSet(viewsets.ViewSet):
class HolderViewSet(viewsets.ViewSet):
def list(self, request):
queryset = User.objects.all()
serializer = UserSerializer(queryset, many=True)
queryset = Holder.objects.all()
serializer = HolderSerializer(queryset, many=True)
return Response(serializer.data)

def retrieve(self, request, pk=None):
queryset = User.objects.all()
user = get_object_or_404(queryset, pk=pk)
serializer = UserSerializer(user)
queryset = Holder.objects.all()
Holder = get_object_or_404(queryset, pk=pk)
serializer = HolderSerializer(Holder)
return Response(serializer.data)

# 待删除
def create(self, request):
serializer = UserSerializer(data=request.data)
serializer = HolderSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
Expand All @@ -43,9 +85,9 @@ def create(self, request):
class AdvertiseViewSet(viewsets.ViewSet):
pagination_class = MyPageNumberPagination

# 获取数据
# 获取审核通过数据
def list(self, request):
queryset = Advertise.objects.all()
queryset = Advertise.objects.filter(audstatus=0)
serializer = AdvertiseListSerializer(queryset, many=True, context={'request': request})
return Response(serializer.data)
# 筛选数据
Expand All @@ -57,31 +99,31 @@ def retrieve(self, request, pk=None):

# 广告数据新建
def create(self, request):
serializer = AdvertiseSerializer(data=request.data, context={'request': request})
if serializer.is_valid():
# 提交数据
useraddr = request.data['useraddr']
pcimage = request.data['pcimage']
mobimage = request.data['mobimage']
applymsg = request.data['applymsg']
signatureMsg = """useraddr:%s\npcimage:%s\nmobimage:%s\napplymsg:%s"""%(useraddr, pcimage, mobimage, applymsg)
print(signatureMsg)
isValild = validate(msg=signatureMsg,signature=request.data['usersignature'], useraddr=useraddr)
if isValild:
# 提交数据
useraddr = str(request.user)
pcimage = request.data['pcimage']
mobimage = request.data['mobimage']
applymsg = request.data['applymsg']
signatureMsg = """useraddr:%s\npcimage:%s\nmobimage:%s\napplymsg:%s"""%(useraddr, pcimage, mobimage, applymsg)
print(signatureMsg)
isValild = validate(msg=signatureMsg,signature=request.data['usersignature'], useraddr=useraddr)
if isValild:
data = {'useraddr': useraddr, 'mobimage': mobimage, 'pcimage': pcimage, 'applymsg': applymsg}
serializer = AdvertiseSerializer(data=data, context={'request': request})
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
# 钱包验证不通过则返回 400
return Response('Signature Error', status=status.HTTP_400_BAD_REQUEST)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# 钱包验证不通过则返回 400
return Response('Signature Error', status=status.HTTP_400_BAD_REQUEST)

# 广告审核
def partial_update(self, request, pk=None):
queryset = Advertise.objects.all()
user = get_object_or_404(queryset, pk=pk)
# 获取编号
id = user.id
useraddr = request.data['useraddr']
useraddr = str(request.user)
pcimage = user.pcimage
mobimage = user.mobimage
audstatus = request.data['audstatus']
Expand All @@ -103,8 +145,23 @@ def partial_update(self, request, pk=None):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


# 审核相关视图集,审核通过在 AdvertiseViewSet.partial_update
class AuditViewSet(viewsets.ViewSet):
# 获取数据
def list(self, request):
# 审核地址则返回所有数据
if str(request.user) == "0xfF7ca7Fe8FdAF2a602191048E10A4b3B072aA1a0":
queryset = Advertise.objects.all()
serializer = AdvertiseListSerializer(queryset, many=True)
return Response(serializer.data)
# 用户地址则返回用户数据
queryset = Advertise.objects.filter(useraddr=request.user)
serializer = AuditSerializer(queryset, many=True, context={'request': request}, )
print(request.user)
return Response(serializer.data)

# 图片上传
class imageViewSet(viewsets.ViewSet):
class ImageViewSet(viewsets.ViewSet):
parser_classes = (MultiPartParser, FormParser, JSONParser)
queryset = Image.objects.all()

Expand Down

0 comments on commit cb2b5aa

Please sign in to comment.