Skip to content

Commit

Permalink
rename location_dat -> ip_data
Browse files Browse the repository at this point in the history
fix analysis result filter on msgs
  • Loading branch information
devxpy committed Oct 16, 2023
1 parent b90d74e commit f16a345
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
20 changes: 20 additions & 0 deletions bots/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import json

import django.db.models
from django import forms
Expand Down Expand Up @@ -374,13 +375,32 @@ class FeedbackInline(admin.TabularInline):
readonly_fields = ["created_at"]


class AnalysisResultFilter(admin.SimpleListFilter):
title = "analysis_result"
parameter_name = "analysis_result"

def lookups(self, request, model_admin):
val = self.value()
if val is None:
return []
return [(val, val)]

def queryset(self, request, queryset):
val = self.value()
if val is None:
return queryset
k, v = json.loads(val)
return queryset.filter(**{k: v})


@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
autocomplete_fields = ["conversation"]
list_filter = [
"role",
"conversation__bot_integration",
"created_at",
AnalysisResultFilter,
]
search_fields = [
"role",
Expand Down
6 changes: 5 additions & 1 deletion gooeysite/custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ def json_field_nested_lookup_keys(


def related_json_field_summary(
manager, field, qs=None, query_param=None, instance_id=None
manager,
field: str,
qs: QuerySet = None,
query_param: str = None,
instance_id: int = None,
):
if query_param is None:
try:
Expand Down
10 changes: 5 additions & 5 deletions url_shortener/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def view_visitors(self, obj: models.ShortenedURL):
@admin.display(description="Visitor Summary")
def view_visitor_summary(self, surl: models.ShortenedURL):
html = ""
for field in ["browser", "device", "os", "location_data"]:
for field in ["browser", "device", "os", "ip_data"]:
results = related_json_field_summary(surl.visitors, field)
html += "<h2>" + field.replace("_", " ").capitalize() + "</h2>"
html += loader.render_to_string(
Expand All @@ -77,7 +77,7 @@ def view_visitor_summary(self, surl: models.ShortenedURL):

def jsonfieldlistfilter(field: str):
class JSONFieldListFilter(admin.SimpleListFilter):
title = field
title = field.replace("_", " ").capitalize()
parameter_name = field

def lookups(self, request, model_admin):
Expand Down Expand Up @@ -108,16 +108,16 @@ class VisitorClickInfoAdmin(admin.ModelAdmin):
jsonfieldlistfilter("browser"),
jsonfieldlistfilter("device"),
jsonfieldlistfilter("os"),
jsonfieldlistfilter("location_data"),
jsonfieldlistfilter("ip_data"),
"created_at",
]
search_fields = ["ip_address", "user_agent", "location_data"] + [
search_fields = ["ip_address", "user_agent", "ip_data"] + [
f"shortened_url__{field}" for field in ShortenedURLAdmin.search_fields
]
list_display = [
"__str__",
"user_agent",
"location_data",
"ip_data",
"created_at",
]
ordering = ["created_at"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Migration(migrations.Migration):
("device", models.JSONField(blank=True)),
("os", models.JSONField(blank=True)),
(
"location_data",
"ip_data",
models.JSONField(
blank=True,
help_text="The location data of the user who clicked the shortened url",
Expand Down
2 changes: 1 addition & 1 deletion url_shortener/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class VisitorClickInfo(models.Model):
browser = models.JSONField(blank=True)
device = models.JSONField(blank=True)
os = models.JSONField(blank=True)
location_data = models.JSONField(
ip_data = models.JSONField(
blank=True,
help_text="The location data of the user who clicked the shortened url",
)
Expand Down
6 changes: 3 additions & 3 deletions url_shortener/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def save_click_info(surl_id: int, ip_address: str, user_agent: str):

res = requests.get(str(furl("https://iplist.cc/api/") / ip_address))
if res.ok:
location_data = res.json()
ip_data = res.json()
else:
location_data = {}
ip_data = {}

VisitorClickInfo.objects.create(
shortened_url_id=surl_id,
Expand All @@ -31,5 +31,5 @@ def save_click_info(surl_id: int, ip_address: str, user_agent: str):
browser=browser,
device=device,
os=os,
location_data=location_data,
ip_data=ip_data,
)

0 comments on commit f16a345

Please sign in to comment.