This commit is contained in:
j 2011-01-21 15:01:49 +05:30
parent a109f67db5
commit cad35ebc46
6 changed files with 37 additions and 23 deletions

View file

@ -65,9 +65,9 @@ class Annotation(models.Model):
def editable(self, user): def editable(self, user):
if user.is_authenticated(): if user.is_authenticated():
if self.user == user or user.has_perm('0x.admin'): if user.is_staff or \
return True self.user == user or \
if user.groups.filter(id__in=self.groups.all()).count() > 0: user.groups.filter(id__in=self.groups.all()).count() > 0:
return True return True
return False return False

View file

@ -117,12 +117,6 @@ class File(models.Model):
super(File, self).save(*args, **kwargs) super(File, self).save(*args, **kwargs)
def json(self):
r = {}
for k in self:
r[k] = unicode(self[k])
return r
#upload and data handling #upload and data handling
video = models.FileField(null=True, blank=True, upload_to=lambda f, x: f.path('%s.webm'%settings.VIDEO_PROFILE)) video = models.FileField(null=True, blank=True, upload_to=lambda f, x: f.path('%s.webm'%settings.VIDEO_PROFILE))
data = models.FileField(null=True, blank=True, upload_to=lambda f, x: f.path('data.bin')) data = models.FileField(null=True, blank=True, upload_to=lambda f, x: f.path('data.bin'))
@ -227,7 +221,8 @@ class File(models.Model):
'audio_codec': self.audio_codec, 'audio_codec': self.audio_codec,
'name': self.name, 'name': self.name,
'size': self.size, 'size': self.size,
'info': self.info 'info': self.info,
'instances': self.instances.count()
} }
if keys: if keys:
for k in data.keys(): for k in data.keys():
@ -235,6 +230,7 @@ class File(models.Model):
del data[k] del data[k]
return data return data
class Volume(models.Model): class Volume(models.Model):
class Meta: class Meta:

View file

@ -241,8 +241,11 @@ class ItemManager(Manager):
data['query'].get('operator', '&')) data['query'].get('operator', '&'))
qs = qs.filter(conditions) qs = qs.filter(conditions)
#FIXME: lists are part of query now #anonymous can only see public items
# filter list, works for own or public lists if user.is_anonymous():
l = data.get('list', 'all') qs = qs.filter(public=True)
qs = self.filter_list(qs, l, user) #users can see public items, there own items and items of there groups
elif not user.is_staff:
qs = qs.filter(Q(public=True)|Q(user=user)|Q(groups__in=user.groups.all()))
#admins can see all available items
return qs return qs

View file

@ -12,6 +12,7 @@ from django.db import models
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.utils import simplejson as json from django.utils import simplejson as json
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User, Group
import ox import ox
from ox.django import fields from ox.django import fields
@ -89,9 +90,14 @@ class Item(models.Model):
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
published = models.DateTimeField(default=datetime.now, editable=False) published = models.DateTimeField(default=datetime.now, editable=False)
user = models.ForeignKey(User, related_name='items')
groups = models.ManyToManyField(Group, related_name='items')
#only items that have data from files are available, #only items that have data from files are available,
#this is indicated by setting available to True #this is indicated by setting available to True
available = models.BooleanField(default=False, db_index=True) available = models.BooleanField(default=False, db_index=True)
public = models.BooleanField(default=False, db_index=True)
itemId = models.CharField(max_length=128, unique=True, blank=True) itemId = models.CharField(max_length=128, unique=True, blank=True)
oxdbId = models.CharField(max_length=42, unique=True, blank=True) oxdbId = models.CharField(max_length=42, unique=True, blank=True)
external_data = fields.DictField(default={}, editable=False) external_data = fields.DictField(default={}, editable=False)
@ -118,15 +124,27 @@ class Item(models.Model):
return default return default
def editable(self, user): def editable(self, user):
#FIXME: make permissions work if user.is_staff or \
self.user == user or \
self.groups.filter(id__in=user.groups.all()).count() > 0:
return True
return False return False
def edit(self, data): def edit(self, data):
#FIXME: how to map the keys to the right place to write them to? #FIXME: how to map the keys to the right place to write them to?
if 'id' in data:
#FIXME: check if id is valid and exists and move/merge items accordingly
del data['id']
if 'id' in data:
groups = data.pop('groups')
self.groups.exclude(name__in=groups).delete()
for g in groups:
group, created = Group.objects.get_or_create(name=g)
self.groups.add(group)
for key in data: for key in data:
if key != 'id': if key != 'id':
setattr(self.data, key, data[key]) setattr(self.data, key, data[key])
self.oxdb.save() self.data.save()
self.save() self.save()
def reviews(self): def reviews(self):

View file

@ -62,7 +62,7 @@ def _parse_query(data, user):
query = {} query = {}
query['range'] = [0, 100] query['range'] = [0, 100]
query['sort'] = [{'key':'title', 'operator':'+'}] query['sort'] = [{'key':'title', 'operator':'+'}]
for key in ('sort', 'keys', 'group', 'list', 'range', 'ids'): for key in ('sort', 'keys', 'group', 'range', 'ids'):
if key in data: if key in data:
query[key] = data[key] query[key] = data[key]
query['qs'] = models.Item.objects.find(data, user) query['qs'] = models.Item.objects.find(data, user)

View file

@ -89,11 +89,8 @@ def get_user_json(user):
result = {} result = {}
for key in ('username', ): for key in ('username', ):
result[key] = getattr(user, key) result[key] = getattr(user, key)
result['group'] = 'user' result['admin'] = user.is_staff
if user.is_staff: result['groups'] = [g.name for g in user.groups.all()]
result['group'] = 'admin'
elif user.has_perm('0x.vip'): #FIXME: permissions
result['group'] = 'vip'
result['preferences'] = profile.get_preferences() result['preferences'] = profile.get_preferences()
result['ui'] = profile.get_ui() result['ui'] = profile.get_ui()
return result return result