2019-03-22 07:17:22 +00:00
|
|
|
import csv
|
|
|
|
import codecs
|
|
|
|
from django.http import HttpResponse
|
2019-11-05 10:46:29 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from common.utils import validate_ip, get_ip_city
|
2019-03-22 07:17:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_excel_response(filename):
|
|
|
|
excel_response = HttpResponse(content_type='text/csv')
|
|
|
|
excel_response[
|
|
|
|
'Content-Disposition'] = 'attachment; filename="%s"' % filename
|
|
|
|
excel_response.write(codecs.BOM_UTF8)
|
|
|
|
return excel_response
|
|
|
|
|
|
|
|
|
|
|
|
def write_content_to_excel(response, header=None, login_logs=None, fields=None):
|
|
|
|
writer = csv.writer(response, dialect='excel', quoting=csv.QUOTE_MINIMAL)
|
|
|
|
if header:
|
|
|
|
writer.writerow(header)
|
|
|
|
if login_logs:
|
|
|
|
for log in login_logs:
|
|
|
|
data = [getattr(log, field.name) for field in fields]
|
|
|
|
writer.writerow(data)
|
2019-11-05 10:46:29 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def write_login_log(*args, **kwargs):
|
|
|
|
from audits.models import UserLoginLog
|
|
|
|
default_city = _("Unknown")
|
|
|
|
ip = kwargs.get('ip') or ''
|
|
|
|
if not (ip and validate_ip(ip)):
|
|
|
|
ip = ip[:15]
|
|
|
|
city = default_city
|
|
|
|
else:
|
|
|
|
city = get_ip_city(ip) or default_city
|
|
|
|
kwargs.update({'ip': ip, 'city': city})
|
|
|
|
UserLoginLog.objects.create(**kwargs)
|