Skip to content

Commit

Permalink
[fix] Fixed get database process.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Aug 13, 2023
1 parent 2f3e0b8 commit 2e0e89d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
29 changes: 19 additions & 10 deletions jpnic_admin/resource/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,30 @@ def get_queryset(self, page=1):
# ネットワーク名フィルタ
if network_name != "":
q &= Q(network_name__contains=network_name)
network_name = "%%%s%%" % network_name

# 住所/住所(English)一部含むフィルタ
if address != "":
q &= Q(address__contains=address) | Q(address_en__contains=address)
address = "%%%s%%" % address

abuse_filter = ""

# Abuse一部含むフィルタ
if is_abuse_none:
abuse_match = True
q &= Q(abuse='')
else:
elif abuse:
abuse_filter = "%%%s%%" % abuse
if abuse != "":
q &= Q(abuse__contains=abuse)

sql = sqlDateSelect(abuse_match)
sql = sqlDateSelect(
network_name=network_name,
address=address,
abuse=abuse_filter,
abuse_match=abuse_match
)
# 日付フィルタ
# フィルタなし時現在の日付にする
if select_date:
Expand All @@ -100,13 +107,16 @@ def get_queryset(self, page=1):
start_time,
end_time,
jpnic_id.id,
"%%%s%%" % network_name,
"%%%s%%" % address,
"%%%s%%" % address,
abuse_filter
]
with connection.cursor() as cursor:
cursor.execute(sqlDateSelectCount(abuse_match), input_array)
cursor.execute(
sqlDateSelectCount(
network_name=network_name,
address=address,
abuse=abuse_filter,
abuse_match=abuse_match
),
input_array)
count = len(cursor.fetchall())

# 1つのpageあたりに入るリスト
Expand All @@ -126,13 +136,12 @@ def get_queryset(self, page=1):
next_page = page + 1
if page != 1:
prev_page = page - 1

input_array.append(jpnic_id.id)
input_array.append(jpnic_id.id)
# データ出力
with connection.cursor() as cursor:
input_array.append(list_count)
input_array.append((page - 1) * list_count)
input_array.append(jpnic_id.id)
input_array.append(jpnic_id.id)
cursor.execute(sql, input_array)
sql_result = cursor.fetchall()
data = []
Expand Down
34 changes: 19 additions & 15 deletions jpnic_admin/resource/sql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def sqlDateSelect(abuse_match=False):
def sqlDateSelect(network_name=None, address=None, abuse=None, abuse_match=False):
sql = """
SELECT t1.id AS ID,
t1.jpnic_id AS JPNIC_ID,
Expand All @@ -24,13 +24,15 @@ def sqlDateSelect(abuse_match=False):
FROM resource_addrlist
WHERE NOT (resource_addrlist.last_checked_at <= %s OR %s <= resource_addrlist.created_at)
AND resource_addrlist.jpnic_id = %s
AND resource_addrlist.network_name like %s
AND (resource_addrlist.address like %s OR resource_addrlist.address_en like %s)
"""
if abuse_match:
sql += " AND resource_addrlist.abuse = %s"
else:
sql += " AND resource_addrlist.abuse like %s"
if network_name:
sql += " AND resource_addrlist.network_name like %s" % (network_name,)
if address:
sql += " AND (resource_addrlist.address like %s OR resource_addrlist.address_en like %s)" % (address, address)
if abuse_match and abuse:
sql += " AND resource_addrlist.abuse = %s" % (abuse,)
elif not abuse_match and abuse:
sql += " AND resource_addrlist.abuse like %s" % (abuse,)

sql += """
GROUP BY ip_address, admin_handle, assign_date, type, division
Expand Down Expand Up @@ -59,20 +61,22 @@ def sqlDateSelect(abuse_match=False):
return sql


def sqlDateSelectCount(abuse_match=False):
def sqlDateSelectCount(network_name=None, address=None, abuse=None, abuse_match=False):
sql = """
SELECT resource_addrlist.ip_address AS ip_address,
resource_addrlist.division AS division
FROM resource_addrlist
WHERE NOT (resource_addrlist.last_checked_at <= %s OR %s <= resource_addrlist.created_at)
AND resource_addrlist.jpnic_id = %s
AND resource_addrlist.network_name like %s
AND (resource_addrlist.address like %s OR resource_addrlist.address_en like %s)
"""
if abuse_match:
sql += " AND resource_addrlist.abuse = %s"
else:
sql += " AND resource_addrlist.abuse like %s"
"""
if network_name:
sql += " AND resource_addrlist.network_name like %s" % (network_name,)
if address:
sql += " AND (resource_addrlist.address like %s OR resource_addrlist.address_en like %s)" % (address, address)
if abuse_match and abuse:
sql += " AND resource_addrlist.abuse = %s" % (abuse,)
elif not abuse_match and abuse:
sql += " AND resource_addrlist.abuse like %s" % (abuse,)

sql += """
GROUP BY ip_address, division
Expand Down

0 comments on commit 2e0e89d

Please sign in to comment.