Move geoip detection from MailLogTrack.save method to view function
kaleb opened this issue · 2 comments
kaleb commented
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Currently on save, MailLogTrack will attempt to perform geoip detection which fails for me on Google App Engine due to its custom python build which does not have _ctypes module. Currently, I have to monkey pach the MailLogTrack.detect_geo method to do nothing and then in my own mail_read_tracker view function, I add the geoip data which comes from request headers. It would be ideal to not have to both monkey patch a model method and provide my own view function.
kaleb commented
This is my view function in case anybody is curious:
def mail_read_tracker(request, encrypted):
try:
if not defaults.TRACK_ENABLE or not defaults.ENABLE_LOGGING:
return
mail_log_id = signing.loads(encrypted)
mail_log = MailLog.objects.get(log_id=mail_log_id)
req = MockRequest(request)
ip_addr = get_ip(req)
user_agent = req.META.get('HTTP_USER_AGENT')
track_log = MailLogTrack.objects.filter(mail_log=mail_log, ip=ip_addr, ua=user_agent)
if not track_log.exists():
lat_lon = request.META.get('HTTP_X_APPENGINE_CITYLATLONG')
lat, lon = lat_lon.split(',') if lat_lon else (None, None)
MailLogTrack.objects.create(
mail_log=mail_log,
ip=ip_addr,
ua=user_agent,
ip_country_code=request.META.get('HTTP_X_APPENGINE_COUNTRY'),
ip_region=request.META.get('HTTP_X_APPENGINE_REGION'),
ip_city=request.META.get('HTTP_X_APPENGINE_CITY'),
ip_latitude=lat,
ip_longitude=lon,
is_read=True,
)
else:
track_log[0].save()
finally:
return HttpResponse(
content=defaults.TRACK_PIXEL[1],
content_type=defaults.TRACK_PIXEL[0],
)