-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use python-user-agents to parse user agent string
if https://iplist.cc/api/ fails, the redirect will fail too, hence do this call in a celery task. set enable_analytics - default=True add visitor summary to shortened url admin
- Loading branch information
Showing
10 changed files
with
315 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import json | ||
|
||
from django.db import DataError | ||
from django.db.models import F, Func, QuerySet, Count | ||
from django.urls import reverse | ||
from furl import furl | ||
|
||
|
||
def json_field_nested_lookup_keys( | ||
qs: QuerySet, field: str, max_depth: int = 3 | ||
) -> list[str]: | ||
nested_keys = [field] | ||
for _ in range(max_depth): | ||
next_keys = [] | ||
for parent in nested_keys: | ||
try: | ||
next_keys.extend( | ||
f"{parent}__{child}" | ||
for child in ( | ||
qs.values(parent) | ||
.annotate(keys=Func(F(parent), function="jsonb_object_keys")) | ||
.order_by() | ||
.distinct() | ||
.values_list("keys", flat=True) | ||
) | ||
) | ||
except DataError: | ||
next_keys.append(parent) | ||
nested_keys = next_keys | ||
return nested_keys | ||
|
||
|
||
def related_json_field_summary( | ||
manager, field, qs=None, query_param=None, instance_id=None | ||
): | ||
if query_param is None: | ||
try: | ||
query_field_name = manager.field.name | ||
except AttributeError: | ||
query_field_name = manager.query_field_name | ||
query_param = f"{query_field_name}__id__exact" | ||
|
||
model = manager.model | ||
meta = model._meta | ||
|
||
if instance_id is None: | ||
instance_id = manager.instance.id | ||
if instance_id is None: | ||
raise model.DoesNotExist | ||
instance_id = str(instance_id) | ||
|
||
if qs is None: | ||
qs = manager.all() | ||
|
||
nested_keys = json_field_nested_lookup_keys(qs, field) | ||
|
||
results = { | ||
key.split(field + "__")[-1]: [ | ||
( | ||
json.dumps(val).strip('"'), | ||
count, | ||
furl( | ||
reverse( | ||
f"admin:{meta.app_label}_{model.__name__.lower()}_changelist" | ||
), | ||
query_params={ | ||
query_param: instance_id, | ||
field: json.dumps([key, val]), | ||
}, | ||
), | ||
) | ||
for val, count in ( | ||
qs.values(key) | ||
.annotate(count=Count("id")) | ||
.order_by("-count") | ||
.values_list(key, "count") | ||
) | ||
if val is not None | ||
] | ||
for key in nested_keys | ||
} | ||
if not results: | ||
raise model.DoesNotExist | ||
return results |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.