cablegates/pandora/event/models.py

91 lines
2.9 KiB
Python
Raw Normal View History

2011-05-28 11:35:57 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
from django.db import models
from django.contrib.auth.models import User, Group
2011-06-04 15:44:29 +00:00
from django.db.models import Q
2011-05-28 11:35:57 +00:00
2011-05-28 13:57:47 +00:00
import ox
2011-05-28 11:35:57 +00:00
from ox.django import fields
2011-06-04 15:44:29 +00:00
from annotation.models import Annotation
2011-06-05 16:30:30 +00:00
from item.models import Item
2011-05-28 11:35:57 +00:00
import managers
class Event(models.Model):
'''
Events are events in time that can be once or recurring,
From Mondays to Spring to 1989 to Roman Empire
'''
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, null=True, related_name='events')
name = models.CharField(null=True, max_length=255, unique=True)
name_sort = models.CharField(null=True, max_length=255, unique=True)
name_find = models.TextField(default='', editable=True)
wikipediaId = models.CharField(max_length=1000, blank=True)
alternativeNames = fields.TupleField(default=[])
objects = managers.EventManager()
class Meta:
ordering = ('name_sort', )
#start yyyy-mm-dd|mm-dd|dow 00:00|00:00
start = models.CharField(default='', max_length=255)
startTime = models.BigIntegerField(default=0)
2011-06-04 15:44:29 +00:00
#end yyyy-mm-dd|mm-dd|dow 00:00|00:01
end = models.CharField(default='', max_length=255)
2011-05-28 11:35:57 +00:00
endTime = models.BigIntegerField(default=0)
2011-06-04 15:44:29 +00:00
duration = models.CharField(default='', max_length=255)
durationTime = models.BigIntegerField(default=0)
2011-05-28 11:35:57 +00:00
type = models.CharField(default='', max_length=255)
2011-06-05 16:30:30 +00:00
2011-06-04 15:44:29 +00:00
matches = models.IntegerField(default=0)
2011-06-05 16:30:30 +00:00
items = models.ManyToManyField(Item, blank=True, related_name='events')
2011-06-04 15:44:29 +00:00
def get_matches(self):
q = Q(value__icontains=" " + self.name)|Q(value__startswith=self.name)
for name in self.alternativeNames:
q = q|Q(value__icontains=" " + name)|Q(value__startswith=name)
return Annotation.objects.filter(q)
def update_matches(self):
2011-06-05 16:30:30 +00:00
matches = self.get_matches()
self.matches = matches.count()
ids = list(set([a.item.id for a in matches]))
for i in self.items.exclude(id__in=ids):
self.items.remove(i)
for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()):
self.items.add(i)
2011-06-04 15:44:29 +00:00
self.save()
2011-05-28 11:35:57 +00:00
def save(self, *args, **kwargs):
if not self.name_sort:
self.name_sort = self.name
self.name_find = self.name + '||'.join(self.alternativeNames)
super(Event, self).save(*args, **kwargs)
2011-05-28 13:57:47 +00:00
def get_id(self):
return ox.to32(self.id)
def json(self, user=None):
j = {
'id': self.get_id(),
'user': self.user.username,
2011-05-28 11:35:57 +00:00
}
2011-05-28 13:57:47 +00:00
for key in ('created', 'modified',
2011-06-04 15:44:29 +00:00
'name', 'alternativeNames',
'start', 'end', 'duration',
'type', 'matches'):
2011-05-28 13:57:47 +00:00
j[key] = getattr(self, key)
return j