summaryrefslogtreecommitdiff
path: root/meetingtools/apps/stats/models.py
blob: 39e7bbbcd5912cb92751e1b85214829b57aeb0f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import logging
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from iso8601 import iso8601
import tagging
from tagging.models import Tag
from meetingtools.apps.sco.models import ACObject, get_sco

__author__ = 'leifj'

from django.db import models
from django.db.models import fields, ForeignKey, DateTimeField, IntegerField
import lxml.etree as etree

class UserMeetingTransaction(models.Model):
    sco = ForeignKey(ACObject)
    user = ForeignKey(User)
    txid = IntegerField()
    date_created = DateTimeField()
    date_closed = DateTimeField()

    def seconds(self):
        delta = self.date_closed - self.date_created
        if hasattr(delta, 'total_seconds'):
            # Python 2.7
            return delta.total_seconds()
        else:
            # Python 2.6
            return (delta.microseconds + (delta.seconds + delta.days * 24 * 3600) * 10 ** 6) / 10 ** 6

    def __unicode__(self):
        return "(%d) %d seconds in %s" % (self.txid,self.seconds(),self.sco)

    @staticmethod
    def create(acc,row):
        txid = int(row.get('transaction-id'))
        sco_id = int(row.get('sco-id'))

        status = row.findtext("status")
        if not status or status != "completed":
            logging.debug("Ignoring transaction %s" % etree.tostring(row))
            return

        txo = None
        try:
            txo = UserMeetingTransaction.objects.get(sco__acc=acc,txid=txid)
        except MultipleObjectsReturned,ex:
            logging.error(ex)
        except ObjectDoesNotExist:
            login = row.findtext("login")
            if not login:
                raise ValueError("No user for transaction %d" % txid)
            user,created = User.objects.get_or_create(username=login)
            date_created=iso8601.parse_date(row.findtext("date-created"))
            #date_created = date_created.replace(tzinfo=None)
            date_closed=iso8601.parse_date(row.findtext("date-closed"))
            #date_close = date_closed.replace(tzinfo=None)
            txo = UserMeetingTransaction.objects.create(sco=get_sco(acc,sco_id),
                                                        txid=txid,
                                                        user=user,
                                                        date_created=date_created,
                                                        date_closed=date_closed)
        tags = []
        for group in txo.user.groups.all():
            tags.append("group:%s" % group.name)

        (local,domain) = txo.user.username.split("@")
        tags.append("domain:%s" % domain)
        Tag.objects.update_tags(txo, ' '.join(tags))

tagging.register(UserMeetingTransaction)