cablegates/pandora/itemlist/models.py

56 lines
1.6 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
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)
user = models.ForeignKey(User)
name = models.CharField(max_length=255)
public = models.BooleanField(default=False)
2011-01-06 07:07:25 +00:00
featured = models.BooleanField(default=False)
query = DictField(default={})
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
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):
2010-12-28 10:16:16 +00:00
return u'%s (%s)' % (self.title, self.user)
2010-11-26 15:42:54 +00:00
def editable(self, user):
#FIXME: make permissions work
if self.user == user or user.has_perm('Ox.admin'):
return True
return False
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)