Skip to content

Commit

Permalink
fix #217 and add heatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Sep 9, 2024
1 parent c43c99e commit 9496f21
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
5 changes: 3 additions & 2 deletions auctions/management/commands/info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.core.management.base import BaseCommand

from auctions.models import find_image
from auctions.models import UserData


def compare_model_instances(instance1, instance2):
Expand Down Expand Up @@ -29,7 +29,8 @@ def compare_model_instances(instance1, instance2):

class Command(BaseCommand):
help = "Just a scratchpad to do things"
print(find_image("blue shrimp", None))
d = UserData.objects.get(user__username="ira")
print(d.unsubscribe_link)
# def handle(self, *args, **options):
# campaigns = AuctionCampaign.objects.all()
# for campaign in campaigns:
Expand Down
9 changes: 9 additions & 0 deletions auctions/templates/base_page_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
<script>
var urlParams = new URLSearchParams(window.location.search);
var src = urlParams.get('src');
var uid = urlParams.get('uid');
// Remove the "src" parameter from the URL
var newUrl = window.location.href.replace(/(\?|&)src=[^&]*(&|$)/, '$1').replace(/(&|\?)$/, '');
// Remove uid
newUrl = newUrl.replace(/(\?|&)uid=[^&]*(&|$)/, '$1').replace(/(&|\?)$/, '');
window.history.replaceState(null, document.title, newUrl)
var first_view = true;
function pageView(data) {
setTimeout(() => {
sendPageView(data);
}, 2000);
}
function sendPageView(data) {
data = data ?? {};
data.src = src;
data.uid = uid;
data.url = newUrl;
data.title = document.title;
data.referrer = document.referrer;
Expand Down
21 changes: 20 additions & 1 deletion auctions/templates/user_map.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{% block extra_js %}
<script
src="https://maps.googleapis.com/maps/api/js?key={{google_maps_api_key}}&callback=initMap&libraries=&v=weekly&libraries=marker"
src="https://maps.googleapis.com/maps/api/js?key={{google_maps_api_key}}&callback=initMap&libraries=&v=weekly&libraries=marker,visualization"
async
></script>
<script type='text/javascript'>pageView();</script>
Expand Down Expand Up @@ -51,13 +51,32 @@
}
})(marker, i));
}
// Heat map
var map = new google.maps.Map(document.getElementById("map_visualization"), {
zoom: 5,
center: { lat: 39.095963, lng: -83.662005 },
});

var heatmapData = [
{% for view in pageviews %}
new google.maps.LatLng({{ view.latitude}}, {{ view.longitude}}),
{% endfor %}
];

var heatmap = new google.maps.visualization.HeatmapLayer({
data: heatmapData,
radius: 20,
});

heatmap.setMap(map);
}
</script>
{% endblock %}
{% block content %}
<h4>User map</h4>
<div id="map_wrapper" style='height: 600px;'>
<div id="map_canvas" class="mapping" style='width: 100%; height: 100%;'></div>
<div id="map_visualization" class="mapping mt-2" style='width: 100%; height: 100%;'></div>
</div>
{% if not has_user_location %}
<span><a href="#" onclick="setLocation()">{{ location_message }}</a></span>
Expand Down
9 changes: 9 additions & 0 deletions auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,13 +1162,19 @@ def pageview(request):
user_agent = user_agent[:200]
referrer = clean_referrer(data.get("referrer", None)[:600])
source = data.get("src", None)
uid = data.get("uid", None)
# mark auction campaign results if applicable present
ip = ""
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
if x_forwarded_for:
ip = x_forwarded_for.split(",")[0]
else:
ip = request.META.get("REMOTE_ADDR")
if uid: # and not request.user.is_authenticated:
userdata = UserData.objects.filter(unsubscribe_link=uid).first()
if userdata:
userdata.last_activity = timezone.now()
userdata.save()
if source:
campaign = AuctionCampaign.objects.filter(uuid=source).first()
if campaign and campaign.result == "NONE":
Expand Down Expand Up @@ -5176,6 +5182,7 @@ def get_context_data(self, **kwargs):
filter1 = data["filter"]
except:
filter1 = None
view_qs = PageView.objects.exclude(latitude=0)
qs = User.objects.filter(userdata__latitude__isnull=False, is_active=True).annotate(
lots_sold=Count("lot"), lots_bought=Count("winner")
)
Expand All @@ -5189,8 +5196,10 @@ def get_context_data(self, **kwargs):
# users by top volume_percentile
qs = qs.filter(userdata__volume_percentile__lte=filter1)
elif view == "recent" and filter1:
view_qs = view_qs.filter(date_start__gte=timezone.now() - timedelta(hours=int(filter1)))
qs = qs.filter(userdata__last_activity__gte=timezone.now() - timedelta(hours=int(filter1)))
context["users"] = qs
context["pageviews"] = view_qs
return context


Expand Down

0 comments on commit 9496f21

Please sign in to comment.