yf_yzl пре 2 година
родитељ
комит
3486c13eba
1 измењених фајлова са 15 додато и 30 уклоњено
  1. 15 30
      apps/Weather/views.py

+ 15 - 30
apps/Weather/views.py

@@ -1,6 +1,6 @@
 from rest_framework.views import APIView
 from rest_framework.response import Response
-import sqlite3
+from models import DayData, ServerDayData
 import logging
 import json
 
@@ -48,23 +48,15 @@ class GetWeather(APIView):
 
     def post(self, request):
         # 对外提供天气接口,需要提供省,市,区(县)
-        try:        
-            db = "/data/weather/weather.db"
+        try:
             data = request.data
             cityid = data.get("cityid", "")
             province = data.get("province")
             city = data.get("city")
             district = data.get("district")
             day_type = data.get("day_type")
-            conn = sqlite3.connect(db)
-            cursor = conn.cursor()
             username = data.get("username")
             password = data.get("password")
-            # sql = """
-            # select * from user where username = ? and password = ?
-            # """
-            # cursor.execute(sql, (username, password,))
-            # user = cursor.fetchone()
             user_info = {
                 "admin001": "why123456",
                 "admin001": " yf@mtg",
@@ -72,37 +64,30 @@ class GetWeather(APIView):
             }
             if not (user_info.get(username) == password):
                 return Response({"msg":"认证失败", "code": 400})
-            table = "day_data" if day_type == "1" else "serven_day_data"
-            table = "day_data" if day_type == "1" else "server_day_data"
+            table = DayData if day_type == "1" else ServerDayData
             if cityid:
                 logging.warning(f"{cityid}: 使用cityid查询")
-                sql = f"select content from {table} where cityid = '{cityid}'"
-                cursor.execute(sql)
-                result = cursor.fetchone()
-                if result:
-                    temp = result[0].replace("'", '"')
+
+                query = table.objects.filter(cityid=cityid)
+                if  query:
+                    temp = query.first().content.replace("'", '"')
                     result = json.loads(temp)
                     return Response({"content": result, "msg": "success", "code": 200})
                 else:
                     return Response({"msg": "请联系管理员排查", "code": 500})
             else:
                 logging.warning("使用province,city,distinct查询")
-                sql = f"select content from {table} where province = '{province}' and city ='{city}' and district ='{district}'"
-                cursor.execute(sql)
-                result = cursor.fetchone()
-                if not result:
+                query = table.objects.filter(province=province, city=city, district=district)
+                if not query:
                     # 省市正确
-                    sql = f"select content from {table} where province= '{province}' and city = '{city}'"
-                    cursor.execute(sql)
-                    result = cursor.fetchone()
-                    if not result:
+                    query = table.objects.filter(province=province, city=city)
+                    if not query:
                         # 省正确
-                        sql = f"select content from {table} where province = '{province}'"
-                        cursor.execute(sql)
-                        result = cursor.fetchone()
-                if result:
+                        query = table.objects.filter(province=province)
+                        
+                if query:
                     try:
-                        temp = result[0].replace("'", '"')
+                        temp = query.first().content.replace("'", '"')
                         result = json.loads(temp)
                         return Response({"content": result, "msg": "success", "code": 200})
                     except Exception as e: