cablegates/pandora/itemlist/models.py

106 lines
3.1 KiB
Python
Raw Normal View History

2010-11-26 15:42:54 +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
from ox.django.fields import DictField
2010-11-26 15:42:54 +00:00
2011-01-11 10:18:18 +00:00
import managers
2010-11-26 15:42:54 +00:00
class List(models.Model):
2011-01-01 11:44:42 +00:00
2010-11-26 15:42:54 +00:00
class Meta:
unique_together = ("user", "name")
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
2011-01-11 14:56:08 +00:00
user = models.ForeignKey(User, related_name='lists')
2010-11-26 15:42:54 +00:00
name = models.CharField(max_length=255)
2011-01-11 11:51:22 +00:00
status = models.CharField(max_length=20, default='private')
_status = ['private', 'public', 'featured']
query = DictField(default={"static": True})
2011-01-01 11:44:42 +00:00
items = models.ManyToManyField('item.Item', related_name='lists',
through='ListItem')
2010-11-26 15:42:54 +00:00
2011-01-11 14:56:08 +00:00
subscribed_users = models.ManyToManyField(User, related_name='subscribed_lists')
2011-01-11 10:18:18 +00:00
objects = managers.ListManager()
def save(self, *args, **kwargs):
super(List, self).save(*args, **kwargs)
def get_number_of_items(self, user=None):
2011-01-11 11:51:22 +00:00
if self.query.get('static', False):
2011-01-11 10:18:18 +00:00
return self.items.count()
else:
2011-01-11 20:12:35 +00:00
from item.models import Item
2011-01-11 10:18:18 +00:00
return Item.objects.find({'query': self.query}, user).count()
2010-11-26 15:42:54 +00:00
def add(self, item):
q = self.items.filter(id=item.id)
if q.count() == 0:
l = ListItem()
l.list = self
l.item = item
l.save()
def remove(self, item):
self.ListItem.objects.all().filter(item=item, list=self).delete()
def __unicode__(self):
2011-01-11 16:19:27 +00:00
return self.get_id()
2010-11-26 15:42:54 +00:00
2011-01-11 14:56:08 +00:00
def get_id(self):
2011-01-11 20:12:35 +00:00
return u'%s.%s' % (self.user.username, self.name)
2011-01-11 14:56:08 +00:00
2010-11-26 15:42:54 +00:00
def editable(self, user):
#FIXME: make permissions work
2011-01-11 14:56:08 +00:00
if self.user == user or user.is_staff:
2010-11-26 15:42:54 +00:00
return True
return False
2011-01-11 14:56:08 +00:00
def json(self, keys=['id', 'name', 'user', 'query', 'status'], user=None):
2011-01-11 10:35:39 +00:00
response = {}
for key in keys:
if key == 'items':
response[key] = self.get_number_of_items(user)
2011-01-11 14:56:08 +00:00
elif key == 'id':
response[key] = self.get_id()
2011-01-11 10:35:39 +00:00
elif key == 'user':
response[key] = self.user.username
2011-01-11 11:51:22 +00:00
elif key == 'query':
if not self.query.get('static', False):
response[key] = self.query
2011-01-11 10:35:39 +00:00
else:
response[key] = getattr(self, key)
return response
2011-01-01 11:44:42 +00:00
2010-11-26 15:42:54 +00:00
class ListItem(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
list = models.ForeignKey(List)
item = models.ForeignKey('item.Item')
def __unicode__(self):
2010-12-28 10:16:16 +00:00
return u'%s in %s' % (self.item, self.list)
2011-01-11 14:56:08 +00:00
2011-01-11 16:19:27 +00:00
class Position(models.Model):
class Meta:
unique_together = ("user", "list", "section")
list = models.ForeignKey(List, related_name='position')
user = models.ForeignKey(User)
section = models.CharField(max_length='255')
position = models.IntegerField(default=0)
2011-01-11 20:12:35 +00:00
def __unicode__(self):
return u'%s/%s/%s' % (self.section, self.position, self.list)