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 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 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)