add groups to collections, edits and lists, towards #3063

This commit is contained in:
j 2017-12-31 14:06:27 +01:00
parent 15c5ad9d54
commit 85c5fafe0d
8 changed files with 143 additions and 19 deletions

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2017-12-31 12:33
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auth', '0008_auto_20171231_1233'),
('documentcollection', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='collection',
name='groups',
field=models.ManyToManyField(blank=True, related_name='collections', to='auth.Group'),
),
]

View file

@ -9,7 +9,7 @@ from glob import glob
from django.db import models from django.db import models
from django.db.models import Max from django.db.models import Max
from django.contrib.auth.models import User from django.contrib.auth.models import User, Group
from django.conf import settings from django.conf import settings
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
@ -18,6 +18,7 @@ import ox
from oxdjango.fields import DictField, TupleField from oxdjango.fields import DictField, TupleField
from archive import extract from archive import extract
from user.utils import update_groups
from . import managers from . import managers
@ -43,6 +44,7 @@ class Collection(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, related_name='collections') user = models.ForeignKey(User, related_name='collections')
groups = models.ManyToManyField(Group, blank=True, related_name='collections')
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
status = models.CharField(max_length=20, default='private') status = models.CharField(max_length=20, default='private')
_status = ['private', 'public', 'featured'] _status = ['private', 'public', 'featured']
@ -123,12 +125,16 @@ class Collection(models.Model):
if user.is_anonymous(): if user.is_anonymous():
return False return False
if self.user == user or \ if self.user == user or \
self.groups.filter(id__in=user.groups.all()).count() > 0 or \
user.is_staff or \ user.is_staff or \
user.profile.capability('canEditFeaturedCollections') is True: user.profile.capability('canEditFeaturedCollections'):
return True return True
return False return False
def edit(self, data, user): def edit(self, data, user):
if 'groups' in data:
groups = data.pop('groups')
update_groups(self, groups)
for key in data: for key in data:
if key == 'query' and not data['query']: if key == 'query' and not data['query']:
setattr(self, key, {"static": True}) setattr(self, key, {"static": True})
@ -212,7 +218,20 @@ class Collection(models.Model):
def json(self, keys=None, user=None): def json(self, keys=None, user=None):
if not keys: if not keys:
keys = ['id', 'name', 'user', 'type', 'query', 'status', 'subscribed', 'posterFrames', 'description', 'view'] keys = [
'description',
'editable',
'groups',
'id',
'name',
'posterFrames',
'query',
'status',
'subscribed',
'type',
'user',
'view',
]
response = {} response = {}
for key in keys: for key in keys:
if key in ('items', 'documents'): if key in ('items', 'documents'):
@ -221,6 +240,10 @@ class Collection(models.Model):
response[key] = self.get_id() response[key] = self.get_id()
elif key == 'user': elif key == 'user':
response[key] = self.user.username response[key] = self.user.username
elif key == 'groups':
response[key] = [g.name for g in self.groups.all()]
elif key == 'editable':
response[key] = self.editable(user)
elif key == 'query': elif key == 'query':
if not self.query.get('static', False): if not self.query.get('static', False):
response[key] = self.query response[key] = self.query

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2017-12-31 12:33
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auth', '0008_auto_20171231_1233'),
('edit', '0003_auto_20170415_1029'),
]
operations = [
migrations.AddField(
model_name='edit',
name='groups',
field=models.ManyToManyField(blank=True, related_name='edits', to='auth.Group'),
),
]

View file

@ -15,7 +15,7 @@ from oxdjango.fields import DictField, TupleField
from django.conf import settings from django.conf import settings
from django.db import models, transaction from django.db import models, transaction
from django.db.models import Max from django.db.models import Max
from django.contrib.auth.models import User from django.contrib.auth.models import User, Group
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from annotation.models import Annotation from annotation.models import Annotation
@ -24,6 +24,7 @@ from item.utils import get_by_id
import clip.models import clip.models
from archive import extract from archive import extract
from user.utils import update_groups
from . import managers from . import managers
@ -41,6 +42,7 @@ class Edit(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, related_name='edits') user = models.ForeignKey(User, related_name='edits')
groups = models.ManyToManyField(Group, blank=True, related_name='edits')
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
status = models.CharField(max_length=20, default='private') status = models.CharField(max_length=20, default='private')
@ -134,12 +136,16 @@ class Edit(models.Model):
if not user or user.is_anonymous(): if not user or user.is_anonymous():
return False return False
if self.user == user or \ if self.user == user or \
self.groups.filter(id__in=user.groups.all()).count() > 0 or \
user.is_staff or \ user.is_staff or \
user.profile.capability('canEditFeaturedEdits'): user.profile.capability('canEditFeaturedEdits'):
return True return True
return False return False
def edit(self, data, user): def edit(self, data, user):
if 'groups' in data:
groups = data.pop('groups')
update_groups(self, groups)
for key in data: for key in data:
if key == 'status': if key == 'status':
value = data[key] value = data[key]
@ -344,6 +350,8 @@ class Edit(models.Model):
'description', 'description',
'duration', 'duration',
'editable', 'editable',
'editable',
'groups',
'id', 'id',
'items', 'items',
'name', 'name',
@ -387,6 +395,8 @@ class Edit(models.Model):
response[key] = self.editable(user) response[key] = self.editable(user)
elif key == 'user': elif key == 'user':
response[key] = self.user.username response[key] = self.user.username
elif key == 'groups':
response[key] = [g.name for g in self.groups.all()]
elif key == 'subscribers': elif key == 'subscribers':
response[key] = self.subscribed_users.all().count() response[key] = self.subscribed_users.all().count()
elif key == 'subscribed': elif key == 'subscribed':

View file

@ -41,6 +41,7 @@ from clip.models import Clip, get_layers
from person.models import get_name_sort from person.models import get_name_sort
from sequence.tasks import get_sequences from sequence.tasks import get_sequences
from title.models import get_title_sort from title.models import get_title_sort
from user.utils import update_groups
import archive.models import archive.models
@ -247,15 +248,7 @@ class Item(models.Model):
del data['id'] del data['id']
if 'groups' in data: if 'groups' in data:
groups = data.pop('groups') groups = data.pop('groups')
if isinstance(groups, list): update_groups(self, groups)
groups = list(filter(lambda g: g.strip(), groups))
groups = [ox.escape_html(g) for g in groups]
for g in self.groups.exclude(name__in=groups):
self.groups.remove(g)
current_groups = [g.name for g in self.groups.all()]
for g in list(filter(lambda g: g not in current_groups, groups)):
group, created = Group.objects.get_or_create(name=g)
self.groups.add(group)
keys = [k['id'] for k in keys = [k['id'] for k in
list(filter(lambda i: i.get('description'), settings.CONFIG['itemKeys']))] list(filter(lambda i: i.get('description'), settings.CONFIG['itemKeys']))]
for k in keys: for k in keys:

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2017-12-31 12:33
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auth', '0008_auto_20171231_1233'),
('itemlist', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='list',
name='groups',
field=models.ManyToManyField(blank=True, related_name='lists', to='auth.Group'),
),
]

View file

@ -9,7 +9,7 @@ from glob import glob
from django.db import models from django.db import models
from django.db.models import Max from django.db.models import Max
from django.contrib.auth.models import User from django.contrib.auth.models import User, Group
from django.conf import settings from django.conf import settings
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
import ox import ox
@ -17,6 +17,7 @@ import ox
from oxdjango.fields import DictField, TupleField from oxdjango.fields import DictField, TupleField
from archive import extract from archive import extract
from user.utils import update_groups
from . import managers from . import managers
@ -35,6 +36,7 @@ class List(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, related_name='lists') user = models.ForeignKey(User, related_name='lists')
groups = models.ManyToManyField(Group, blank=True, related_name='lists')
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
status = models.CharField(max_length=20, default='private') status = models.CharField(max_length=20, default='private')
_status = ['private', 'public', 'featured'] _status = ['private', 'public', 'featured']
@ -116,20 +118,24 @@ class List(models.Model):
if user.is_anonymous(): if user.is_anonymous():
return False return False
if self.user == user or \ if self.user == user or \
self.groups.filter(id__in=user.groups.all()).count() > 0 or \
user.is_staff or \ user.is_staff or \
user.profile.capability('canEditFeaturedLists') == True: user.profile.capability('canEditFeaturedLists'):
return True return True
return False return False
def edit(self, data, user): def edit(self, data, user):
if 'groups' in data:
groups = data.pop('groups')
update_groups(self, groups)
for key in data: for key in data:
if key == 'query' and not data['query']: if key == 'query' and not data['query']:
setattr(self, key, {"static":True}) setattr(self, key, {"static": True})
elif key == 'query' and isinstance(data[key], dict): elif key == 'query' and isinstance(data[key], dict):
setattr(self, key, data[key]) setattr(self, key, data[key])
elif key == 'type': elif key == 'type':
if data[key] == 'static': if data[key] == 'static':
self.query = {"static":True} self.query = {"static": True}
self.type = 'static' self.type = 'static'
else: else:
self.type = 'smart' self.type = 'smart'
@ -198,14 +204,27 @@ class List(models.Model):
if 'view' in data: if 'view' in data:
self.view = data['view'] self.view = data['view']
if 'sort' in data: if 'sort' in data:
self.sort= tuple(data['sort']) self.sort = tuple(data['sort'])
self.save() self.save()
if 'posterFrames' in data: if 'posterFrames' in data:
self.update_icon() self.update_icon()
def json(self, keys=None, user=None): def json(self, keys=None, user=None):
if not keys: if not keys:
keys=['id', 'name', 'user', 'type', 'query', 'status', 'subscribed', 'posterFrames', 'description', 'view'] keys = [
'description',
'editable',
'groups',
'id',
'name',
'posterFrames',
'query',
'status',
'subscribed',
'type',
'user',
'view',
]
response = {} response = {}
for key in keys: for key in keys:
if key == 'items': if key == 'items':
@ -214,6 +233,10 @@ class List(models.Model):
response[key] = self.get_id() response[key] = self.get_id()
elif key == 'user': elif key == 'user':
response[key] = self.user.username response[key] = self.user.username
elif key == 'groups':
response[key] = [g.name for g in self.groups.all()]
elif key == 'editable':
response[key] = self.editable(user)
elif key == 'query': elif key == 'query':
if not self.query.get('static', False): if not self.query.get('static', False):
response[key] = self.query response[key] = self.query

View file

@ -1,4 +1,5 @@
from django.contrib.gis.geoip2 import GeoIP2 from django.contrib.gis.geoip2 import GeoIP2
from django.contrib.auth.models import Group
import ox import ox
@ -69,3 +70,14 @@ def rename_user(user, new):
# update user item find # update user item find
item.models.ItemFind.objects.filter(key='user', value=old).update(value=new) item.models.ItemFind.objects.filter(key='user', value=old).update(value=new)
user.username = new user.username = new
def update_groups(model, groups):
if isinstance(groups, list):
groups = list(filter(lambda g: g.strip(), groups))
groups = [ox.escape_html(g) for g in groups]
for g in model.groups.exclude(name__in=groups):
model.groups.remove(g)
current_groups = [g.name for g in model.groups.all()]
for g in list(filter(lambda g: g not in current_groups, groups)):
group, created = Group.objects.get_or_create(name=g)
model.groups.add(group)