views.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from rest_framework.views import APIView
  2. from rest_framework.response import Response
  3. import sqlite3
  4. import logging
  5. import json
  6. province_dict = {
  7. "11": '北京市',
  8. "12": '天津市',
  9. "13": '河北省',
  10. "14": '山西省',
  11. "15": '内蒙古自治区',
  12. "21": '辽宁省',
  13. "22": '吉林省',
  14. "23": '黑龙江省',
  15. "31": '上海市',
  16. "32": '江苏省',
  17. "33": '浙江省',
  18. "34": '安徽省',
  19. "35": '福建省',
  20. "36": '江西省',
  21. "37": '山东省',
  22. "41": '河南省',
  23. "42": '湖北省',
  24. "43": '湖南省',
  25. "44": '广东省',
  26. "45": '广西壮族自治区',
  27. "46": '海南省',
  28. "50": '重庆市',
  29. "51": '四川省',
  30. "52": '贵州省',
  31. "53": '云南省',
  32. "54": '西藏自治区',
  33. "61": '陕西省',
  34. "62": '甘肃省',
  35. "63": '青海省',
  36. "64": '宁夏回族自治区',
  37. "65": '新疆维吾尔自治区'
  38. }
  39. logging.basicConfig(level=logging.DEBUG, filename='/data/logs/app.log', filemode='w',
  40. format='%(asctime)s - %(levelname)s - %(message)s')
  41. class GetWeather(APIView):
  42. def post(self, request):
  43. # 对外提供天气接口,需要提供省,市,区(县)
  44. try:
  45. db = "/data/weather/weather.db"
  46. data = request.data
  47. cityid = data.get("cityid", "")
  48. province = data.get("province")
  49. city = data.get("city")
  50. district = data.get("district")
  51. day_type = data.get("day_type")
  52. conn = sqlite3.connect(db)
  53. cursor = conn.cursor()
  54. username = data.get("username")
  55. password = data.get("password")
  56. # sql = """
  57. # select * from user where username = ? and password = ?
  58. # """
  59. # cursor.execute(sql, (username, password,))
  60. # user = cursor.fetchone()
  61. user_info = {
  62. "admin001": "why123456",
  63. "admin001": " yf@mtg",
  64. "fujinsuyuan": "yf@fj"
  65. }
  66. if not (user_info.get(username) == password):
  67. return Response({"msg":"认证失败", "code": 400})
  68. table = "day_data" if day_type == "1" else "serven_day_data"
  69. table = "day_data" if day_type == "1" else "server_day_data"
  70. if cityid:
  71. logging.warning(f"{cityid}: 使用cityid查询")
  72. sql = f"select content from {table} where cityid = '{cityid}'"
  73. cursor.execute(sql)
  74. result = cursor.fetchone()
  75. if result:
  76. temp = result[0].replace("'", '"')
  77. result = json.loads(temp)
  78. return Response({"content": result, "msg": "success", "code": 200})
  79. else:
  80. return Response({"msg": "请联系管理员排查", "code": 500})
  81. else:
  82. logging.warning("使用province,city,distinct查询")
  83. sql = f"select content from {table} where province = '{province}' and city ='{city}' and district ='{district}'"
  84. cursor.execute(sql)
  85. result = cursor.fetchone()
  86. if not result:
  87. # 省市正确
  88. sql = f"select content from {table} where province= '{province}' and city = '{city}'"
  89. cursor.execute(sql)
  90. result = cursor.fetchone()
  91. if not result:
  92. # 省正确
  93. sql = f"select content from {table} where province = '{province}'"
  94. cursor.execute(sql)
  95. result = cursor.fetchone()
  96. if result:
  97. try:
  98. temp = result[0].replace("'", '"')
  99. result = json.loads(temp)
  100. return Response({"content": result, "msg": "success", "code": 200})
  101. except Exception as e:
  102. logging.info(e)
  103. return Response({"msg": "请联系管理员", "code": "50001"})
  104. else:
  105. return Response({"content": "", "msg": "success", "code": 500})
  106. except Exception as e:
  107. logging.info(e)
  108. return Response({"msg": "请联系管理员", "code": "50001"})
  109. class TestAPI(APIView):
  110. def post(self, request):
  111. return Response({"test": 111})