views.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from rest_framework.views import APIView
  2. from rest_framework.response import Response
  3. from django.contrib.auth import authenticate
  4. from apps.Weather.models import DayData, ServerDayData, AddressInfo, HistoryData
  5. from apps.UserApp.models import MyUser
  6. import logging
  7. import json
  8. province_dict = {
  9. "11": '北京市',
  10. "12": '天津市',
  11. "13": '河北省',
  12. "14": '山西省',
  13. "15": '内蒙古自治区',
  14. "21": '辽宁省',
  15. "22": '吉林省',
  16. "23": '黑龙江省',
  17. "31": '上海市',
  18. "32": '江苏省',
  19. "33": '浙江省',
  20. "34": '安徽省',
  21. "35": '福建省',
  22. "36": '江西省',
  23. "37": '山东省',
  24. "41": '河南省',
  25. "42": '湖北省',
  26. "43": '湖南省',
  27. "44": '广东省',
  28. "45": '广西壮族自治区',
  29. "46": '海南省',
  30. "50": '重庆市',
  31. "51": '四川省',
  32. "52": '贵州省',
  33. "53": '云南省',
  34. "54": '西藏自治区',
  35. "61": '陕西省',
  36. "62": '甘肃省',
  37. "63": '青海省',
  38. "64": '宁夏回族自治区',
  39. "65": '新疆维吾尔自治区'
  40. }
  41. logging.basicConfig(level=logging.DEBUG, filename='/data/logs/weather.log', filemode='w',
  42. format='%(asctime)s - %(levelname)s - %(message)s')
  43. class GetWeather(APIView):
  44. def post(self, request):
  45. # 对外提供天气接口,需要提供省,市,区(县)
  46. try:
  47. data = request.data
  48. cityid = data.get("cityid", "")
  49. province = data.get("province")
  50. city = data.get("city")
  51. district = data.get("district")
  52. day_type = data.get("day_type")
  53. username = data.get("username")
  54. password = data.get("password")
  55. user = authenticate(username=username, password=password)
  56. if not user:
  57. return Response({"msg":"认证失败", "code": 400})
  58. table = DayData if day_type == "1" else ServerDayData
  59. if cityid:
  60. logging.warning(f"{cityid}: 使用cityid查询")
  61. query = table.objects.filter(cityid=cityid)
  62. if query:
  63. temp = query.first().content.replace("'", '"')
  64. result = json.loads(temp)
  65. return Response({"content": result, "msg": "success", "code": 200})
  66. else:
  67. return Response({"msg": "请联系管理员排查", "code": 500})
  68. else:
  69. logging.warning("使用province,city,distinct查询")
  70. query = table.objects.filter(province=province, city=city, district=district)
  71. if not query:
  72. # 省市正确
  73. query = table.objects.filter(province=province, city=city)
  74. if not query:
  75. # 省正确
  76. query = table.objects.filter(province=province)
  77. if query:
  78. try:
  79. temp = query.first().content.replace("'", '"')
  80. result = json.loads(temp)
  81. return Response({"content": result, "msg": "success", "code": 200})
  82. except Exception as e:
  83. logging.info(e)
  84. return Response({"msg": "请联系管理员", "code": "50001"})
  85. else:
  86. return Response({"content": "", "msg": "success", "code": 500})
  87. except Exception as e:
  88. logging.info(e)
  89. return Response({"msg": "请联系管理员", "code": "50001"})
  90. def HistoryAPIView(APIView):
  91. def post(self, request):
  92. data = request.data
  93. timestamp = data.get("timestamp")
  94. province = data.get("province")
  95. city = data.get("city")
  96. district = data.get("district")
  97. district = AddressInfo.objects.filter(province=province, city=city, district=district)
  98. if district:
  99. cityid = district.first().cityid
  100. else:
  101. msg = f"地区:{district} 取不到,使用 {province} {city} 取"
  102. logging.warning(msg)
  103. city = AddressInfo.objects.filter(province=province, city=city)
  104. if city:
  105. cityid = city.first().cityid
  106. else:
  107. return Response({"msg": "暂无数据", "code": 500})
  108. # 去历史表中查询数据
  109. try:
  110. history_data = HistoryData.objects.get(cityid=cityid, timestamp=timestamp)
  111. content = history_data.content
  112. low_heigh = json.loads(content)
  113. return Response(
  114. {
  115. "data": [low_heigh["yWendu"], low_heigh["bWendu"]],
  116. "province": province,
  117. "city": city,
  118. "district": district
  119. }
  120. )
  121. except Exception as e:
  122. return Response({"msg": "暂无指定城市数据", "code": 500})
  123. class TestAPI(APIView):
  124. def post(self, request):
  125. return Response({"test": 111})