import hashlib
from django.db import IntegrityError
from pillowtop.listener import BasicPillow
from couchforms.models import XFormInstance
from phonelog.models import UserLog, Log
import dateutil.parser as dparser

def force_list(obj_or_list):
    return obj_or_list if isinstance(obj_or_list, list) else [obj_or_list]

class PhoneLogPillow(BasicPillow):
    document_class = XFormInstance
    couch_filter = 'phonelog/all_logs'

    def change_transport(self, doc_dict):
        xform_id = doc_dict.get('_id')
        form = doc_dict.get('form', {})
        userlogs = (form.get('user_subreport', {}) or {}).get('user', [])
        for log in force_list(userlogs):
            u = UserLog(user_id=log["user_id"], username=log["username"],
                        sync_token=log["sync_token"], xform_id=xform_id)
            u.save()

        domain = doc_dict.get('domain')
        logs = (form.get('log_subreport', {}) or {}).get('log', [])
        logged_in_user = 'unknown'
        for log in filter(None, force_list(logs)):
            if log["type"] == 'login':
                logged_in_user = log["msg"].split('-')[1]

            uuid = hashlib.md5(log["type"] + log["@date"]).hexdigest()
            try:
                l, _ = Log.objects.get_or_create(id=uuid, xform_id=xform_id, domain=domain,
                                         type=log["type"], msg=log["msg"],
                                         date=dparser.parse(log["@date"]),
                                         app_version=form.get('app_version'),
                                         device_id=form.get('device_id'),
                                         username=logged_in_user)
            except IntegrityError:
                pass

@@ -313,16 +313,250 @@
 _list%5D%0A%0A
+def get_logs(form, report_name, report_slug):%0A    report = form.get(report_name, %7B%7D) or %7B%7D%0A    if isinstance(report, list):%0A        return filter(None, %5Blog.get(report_slug) for log in report%5D)%0A    return report.get(report_slug, %5B%5D)%0A%0A
 class Ph
@@ -797,26 +797,31 @@
 rlogs = 
-(form.get(
+get_logs(form, 
 'user_su
@@ -834,33 +834,14 @@
 t', 
-%7B%7D) or %7B%7D).get(
 'user'
-, %5B%5D
 )%0A  
@@ -1107,18 +1107,23 @@
 s = 
-(form.get(
+get_logs(form, 
 'log
@@ -1139,32 +1139,13 @@
 t', 
-%7B%7D) or %7B%7D).get(
 'log'
-, %5B%5D
 )%0A  

allow pillow to process oddly formatted phone logs