From f16a3458f17eeb77292e71423c1499c587940ab6 Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Mon, 16 Oct 2023 20:23:41 +0530 Subject: [PATCH] rename location_dat -> ip_data fix analysis result filter on msgs --- bots/admin.py | 20 +++++++++++++++++++ gooeysite/custom_filters.py | 6 +++++- url_shortener/admin.py | 10 +++++----- ...edurl_enable_analytics_visitorclickinfo.py | 2 +- url_shortener/models.py | 2 +- url_shortener/tasks.py | 6 +++--- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/bots/admin.py b/bots/admin.py index 32bac880b..24f6408f6 100644 --- a/bots/admin.py +++ b/bots/admin.py @@ -1,4 +1,5 @@ import datetime +import json import django.db.models from django import forms @@ -374,6 +375,24 @@ 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"] @@ -381,6 +400,7 @@ class MessageAdmin(admin.ModelAdmin): "role", "conversation__bot_integration", "created_at", + AnalysisResultFilter, ] search_fields = [ "role", diff --git a/gooeysite/custom_filters.py b/gooeysite/custom_filters.py index 8a14090a9..1c62c896d 100644 --- a/gooeysite/custom_filters.py +++ b/gooeysite/custom_filters.py @@ -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: diff --git a/url_shortener/admin.py b/url_shortener/admin.py index e51ec46d4..c714760d9 100644 --- a/url_shortener/admin.py +++ b/url_shortener/admin.py @@ -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 += "

" + field.replace("_", " ").capitalize() + "

" html += loader.render_to_string( @@ -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): @@ -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"] diff --git a/url_shortener/migrations/0003_shortenedurl_enable_analytics_visitorclickinfo.py b/url_shortener/migrations/0003_shortenedurl_enable_analytics_visitorclickinfo.py index 95d1331f9..3f7d35119 100644 --- a/url_shortener/migrations/0003_shortenedurl_enable_analytics_visitorclickinfo.py +++ b/url_shortener/migrations/0003_shortenedurl_enable_analytics_visitorclickinfo.py @@ -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", diff --git a/url_shortener/models.py b/url_shortener/models.py index 46653baba..e1cbd2f54 100644 --- a/url_shortener/models.py +++ b/url_shortener/models.py @@ -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", ) diff --git a/url_shortener/tasks.py b/url_shortener/tasks.py index f6c29a31b..c544f0017 100644 --- a/url_shortener/tasks.py +++ b/url_shortener/tasks.py @@ -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, @@ -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, )