Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move geoip detection from MailLogTrack.save method to view function #109

Open
kaleb opened this issue Feb 23, 2018 · 2 comments
Open

Move geoip detection from MailLogTrack.save method to view function #109

kaleb opened this issue Feb 23, 2018 · 2 comments

Comments

@kaleb
Copy link
Contributor

kaleb commented Feb 23, 2018

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
Copy link
Contributor Author

kaleb commented Feb 23, 2018

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],
        )

@kaleb
Copy link
Contributor Author

kaleb commented Feb 23, 2018

This would have also made #99 not necessary as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant