from rest_framework.response import Response from rest_framework.views import APIView from database import models import os from django.http import JsonResponse import wordcloud import traceback BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # class CiYun(APIView): def post(self, request): message = {} try: user_openid = request.data.get("user_openid") try: user_id = models.WxUserInfo.objects.get(openId=user_openid).pk except: message['code'] = 444 return JsonResponse(message) keywords_list = models.Search.objects.get(user_id=user_id).keywords_list w = wordcloud.WordCloud(width=300,height= 150,background_color='white', font_path='msyh.ttc') w.generate(str(keywords_list)) w.to_file(BASE_DIR + '/static/ciyun/output1.png') message['code'] = 200 message['path'] = "/static/ciyun/output1.png" return Response(message) except Exception as e: print(traceback.format_exc()) message['data'] = 400 return Response(message)