1.增加用户积分
1.在apps/users/views.py新增 增加用户积分类AddScore:
#…… from .models import AddJiFen #…… class AddScore(APIView): """增加用户积分""" renderer_classes = [JSONRenderer] # 渲染器 def get(self,request): token=request.GET.get(‘token‘) # print(token) user=UserProfile.objects.filter(token=token).first() if user: note=AddJiFen.objects.filter(user=user).last() if note: today = datetime.today() j = datetime(today.year, today.month, today.day, 0, 0, 0) note_num=AddJiFen.objects.filter(add_time__gte=j,user=user).count() # print(note_num) # 每个用户每天不能增加积分超过2010分 if (note_num*15)<=2010: add_note = AddJiFen() add_note.user = user add_note.ad_jifen = 15 add_note.save() user.jifen += 15 user.save() return HttpResponse(200) else: return HttpResponse(‘今日赚取的积分已经够多了,请明天再来吧。‘) else: add_note=AddJiFen() add_note.user=user add_note.ad_jifen=15 add_note.save() user.jifen+=15 user.save() # print(user.jifen) return HttpResponse(200) else: return HttpResponse(‘没有此用户‘)
2.在apps/users/urls.py中配置路由:
from django.urls import path from .views import LoginView,AddScore urlpatterns = [ path(‘login/‘,LoginView.as_view()), path(‘addscore/‘,AddScore.as_view()) ]