def post(self, request, *args, **kwargs): token = request.data.get('token') userid = CACHE.get(token) articleagree = self.get_obj(request, userid, *args, **kwargs) # 判断是否已经点过赞 if articleagree: if request.data.get('action') == 'disagree': if articleagree.agree == 2: articleagree.agree = 0 else: articleagree.agree = 2 else: if articleagree.agree == 1: articleagree.agree = 0 else: articleagree.agree = 1 articleagree.save() obj = self.changeAgreeCount(articleagree) return Response(data={ 'agree': obj.agree, 'disagree': obj.disagree }) agree_obj = self.create_obj(request, userid, *args, **kwargs) if isinstance(agree_obj, Response): return agree_obj obj = self.changeAgreeCount(agree_obj) return Response(data={'agree': obj.agree, 'disagree': obj.disagree})
def celery_sendemail(user_email, userid): subject = '用户激活' # 邮件标题 message = 'Hello' # 文档 from_email = EMAIL_HOST_USER # 邮件来源地址 recipient_list = [ user_email, ] # 接收邮件地址 uid = uuid.uuid4().hex # 创建激活码 activateURL = ACTIVATE_URL + uid CACHE.set(uid, userid, timeout=300) html_message = '<h1>请点击以下链接激活邮箱</h1><p><a href="{}">{}</a></p>'.format( activateURL, activateURL) # html文档 send_mail(subject, message, from_email, recipient_list=recipient_list, html_message=html_message)
def process_request(self, request): if request.method == 'GET': if request.path in GET_AUTHORITY_PATH: token = request.GET.get('token') if CACHE.get(token): pass else: return HttpResponseBadRequest('用户未登陆') elif request.method == 'POST': for patten in POST_AUTHORITY_PATH: if re.match(patten, request.path): token = request.POST.get('token') if CACHE.get(token): pass else: return HttpResponseBadRequest('用户未登陆')
def post(self, request, *args, **kwargs): pk = kwargs['pk'] try: article = Article.objects.get(id=pk) token = request.data.get('token') userid = CACHE.get(token) if CACHE.get(userid): userid_set = CACHE.get(userid) if article.id in userid_set: return Response(data={'message': '重复点赞'}, status=status.HTTP_403_FORBIDDEN) else: userid_set.add(article.id) CACHE.set(userid, userid_set) else: CACHE.set(userid, { article.id, }) article.agree += 1 article.save() return Response(data={ 'message': '点赞成功', 'articleAgree': article.agree }) except: return Response(data={'message': '没有该篇文章'}, status=status.HTTP_404_NOT_FOUND)
def get(self, request, *args, **kwargs): activate_code = kwargs['pk'] userid = CACHE.get(activate_code) try: user = User.objects.get(id=userid) user.is_active = True user.save() return HttpResponsePermanentRedirect(FONTEND_URL) except: return HttpResponse('用户不存在,请先创建用户')
def post(self, request, *args, **kwargs): if request.data.get('token', None): token = request.data.get('token') userid = CACHE.get(token) try: User.objects.get(id=userid) request.data._mutable = True request.data['user'] = userid request.data._mutable = False except: return Response(data={'message': '无效的token'}, status=status.HTTP_401_UNAUTHORIZED) return self.create(request, *args, **kwargs) return Response(data={'message': '用户未登陆'}, status=status.HTTP_401_UNAUTHORIZED)
def post(self, request, *args, **kwargs): # 登陆功能 if request.data.get('action') == 'login': username = request.data.get('username') password = request.data.get('password') try: user = User.objects.get(username=username) # 验证通过,返回用户实例与token if check_password(password, user.password): uid = uuid.uuid4().hex # 利用ObjectMultipleModelAPIView的list源代码来序列化数据 querylist = [{ 'queryset': User.objects.filter(username=username), 'serializer_class': UserSerializer }] results = self.get_empty_results() for query_data in querylist: self.check_query_data(query_data) queryset = self.load_queryset(query_data, request, *args, **kwargs) # Run the paired serializer context = self.get_serializer_context() data = query_data['serializer_class']( queryset, many=True, context=context).data label = self.get_label(queryset, query_data) # Add the serializer data to the running results tally results = self.add_to_results(data, label, results) formatted_results = self.format_results(results, request) formatted_results.update({'token': uid}) # 添加token到缓存(要启动redis要不然会报错) CACHE.set(uid, user.id) return Response(formatted_results) else: return Response(data={'message': '密码错误'}, status=status.HTTP_400_BAD_REQUEST) except: return Response(data={'message': '用户未注册'}, status=status.HTTP_404_NOT_FOUND) # 这里是执行注册功能 username = request.data.get('username') email = request.data.get('email', '') if not email: return Response(data={'message': '邮箱未填写'}, status=status.HTTP_406_NOT_ACCEPTABLE) try: password = request.data.get('password') if not password: return Response(data={'message': '密码不能为空'}, status=status.HTTP_406_NOT_ACCEPTABLE) password = make_password(password) user = User.objects.create(username=username, password=password, email=email, headimg=request.data.get('headimg')) celery_sendemail.delay(email, user.id) except: return Response(data={'message': '用户已存在'}, status=status.HTTP_400_BAD_REQUEST) queryset = User.objects.filter(id=user.id) serializer = self.get_serializer(queryset, many=True) return Response(data=serializer.data)
def get(self, request, *args, **kwargs): token = request.query_params.get('token') if CACHE.get(token): return Response(data={'message': 'token有效'}) return Response(data={'message': '无效token'}, status=status.HTTP_401_UNAUTHORIZED)