Add command to set default permission (./manage.py assignpermissions) + corrected migration order
--- a/src/ldt/ldt/ldt_utils/migrations/0005_add_permissions.py Wed Jan 04 17:28:54 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/migrations/0005_add_permissions.py Thu Jan 05 14:19:06 2012 +0100
@@ -1,50 +1,29 @@
+#@PydevCodeAnalysisIgnore
# encoding: utf-8
-#@PydevCodeAnalysisIgnore
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
from django.conf import settings
-from django.contrib.auth.models import Group, User
-from ldt.ldt_utils.models import Project, Content
-from guardian.shortcuts import assign
-from guardian.core import ObjectPermissionChecker
+from ldt.security.command import set_default_permissions
+
class Migration(DataMigration):
depends_on = (
("guardian", "0005_auto__chg_field_groupobjectpermission_object_pk__chg_field_userobjectp"),
("user", "0004_fill_language"),
+ ("user", "0007_auto__add_groupprofile")
)
def forwards(self, orm):
for model in ["project", "content", "segment", "author", "media"]:
- self.add_perm(orm, model)
-
- everyone = Group.objects.create(name=settings.PUBLIC_GROUP_NAME)
+ self.add_perm(orm, model)
- for proj in Project.objects.all():
- assign('ldt_utils.change_project', proj.owner, proj)
- assign('ldt_utils.view_project', proj.owner, proj)
-
- for published_proj in Project.objects.filter(state=2):
- assign('ldt_utils.view_project', everyone, published_proj)
- for user in User.objects.all():
- user.groups.add(everyone)
-
- for group in user.groups.exclude(name=settings.PUBLIC_GROUP_NAME):
- for proj in Project.objects.filter(owner=user):
- assign('ldt_utils.view_project', group, proj)
-
- checker = ObjectPermissionChecker(user)
- for content in Content.objects.all():
- if checker.has_perm('view_content', content):
- assign('view_media', user, content.media_obj)
-
- for c in Content.objects.all():
- c.is_public = True
+ set_default_permissions(is_migration=True, orm=orm)
+
def add_perm(self, orm, model_name):
@@ -64,6 +43,12 @@
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
+ 'auth.message': {
+ 'Meta': {'object_name': 'Message'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'message': ('django.db.models.fields.TextField', [], {}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'_message_set'", 'to': "orm['auth.User']"})
+ },
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
--- a/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/groupslist.html Wed Jan 04 17:28:54 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/groupslist.html Thu Jan 05 14:19:06 2012 +0100
@@ -36,7 +36,7 @@
</td>
<td><b>{{ group.name }}</b></td>
{% else %}
- <td>
+ <td class="groupimg">
{% thumbnail group.profile.image "50x50" crop="center" format="PNG" as im %}<img class="grouplink" src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" title="{% trans "You are not allowed to edit this group" %}"/>{% empty %} {% endthumbnail %}
</td>
<td>{{ group.name }}</td>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/management/commands/assignpermissions.py Thu Jan 05 14:19:06 2012 +0100
@@ -0,0 +1,10 @@
+from django.core.management.base import BaseCommand
+from ldt.security.command import set_default_permissions
+
+
+class Command(BaseCommand):
+ help = 'Set default permissions'
+
+ def handle(self, *args, **options):
+
+ set_default_permissions(verbose=True)
\ No newline at end of file
--- a/src/ldt/ldt/management/commands/setprojecticon.py Wed Jan 04 17:28:54 2012 +0100
+++ b/src/ldt/ldt/management/commands/setprojecticon.py Thu Jan 05 14:19:06 2012 +0100
@@ -6,8 +6,12 @@
def handle(self, *args, **options):
+ new_icons = 0
for project in Project.objects.all():
- project.set_icon()
- project.save()
+ if project.set_icon():
+ new_icons += 1
+ project.save()
+
+ print "%s icons set" % new_icons
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/security/command.py Thu Jan 05 14:19:06 2012 +0100
@@ -0,0 +1,55 @@
+from django.conf import settings
+from django.contrib.auth.models import Group, User
+from ldt.ldt_utils.models import Project, Content
+from guardian.shortcuts import assign
+import pdb
+
+def set_default_permissions(verbose=False, is_migration=False, orm=None):
+
+ list_model = {'Project': Project,
+ 'Content': Content,
+ 'User': User,
+ 'Group': Group
+ }
+
+ if is_migration:
+ list_model = {'User': orm['auth.user'],
+ 'Group': orm['auth.group'],
+ 'Project': orm.Project,
+ 'Content': orm.Content
+ }
+
+ everyone, created = Group.objects.get_or_create(name=settings.PUBLIC_GROUP_NAME)
+
+ if verbose:
+ print "Set project permissions..."
+
+ for proj in list_model['Project'].objects.all():
+ assign('ldt_utils.change_project', proj.owner, proj)
+ assign('ldt_utils.view_project', proj.owner, proj)
+
+ for published_proj in list_model['Project'].objects.filter(state=2):
+ assign('ldt_utils.view_project', everyone, published_proj)
+
+ if verbose:
+ print "Set group permissions...[This may take a while]"
+
+ for user in list_model['User'].objects.all():
+ everyone.user_set.add(user)
+
+ for group in user.groups.exclude(name=settings.PUBLIC_GROUP_NAME):
+ for proj in list_model['Project'].objects.filter(owner=user):
+ assign('ldt_utils.view_project', group, proj)
+
+ for content in list_model['Content'].objects.all():
+ if user.has_perm('view_content', content):
+ assign('view_media', user, content.media_obj)
+
+ if verbose:
+ print "Set content permissions..."
+ for c in list_model['Content'].objects.all():
+ c.is_public = True
+
+ for admin in list_model['User'].objects.filter(is_superuser=True):
+ for g in list_model['Group'].objects.all():
+ g.user_set.add(admin)
\ No newline at end of file
--- a/src/ldt/ldt/static/ldt/js/projectscontents.js Wed Jan 04 17:28:54 2012 +0100
+++ b/src/ldt/ldt/static/ldt/js/projectscontents.js Thu Jan 05 14:19:06 2012 +0100
@@ -148,8 +148,7 @@
setTimeout(function() {
target.next(".searchajaxloader").show();
target.nextAll(".searchclear").hide();
- //var filterVal = "_" + escape(target.realVal());
- var filterVal = "_" + target.realVal();
+ var filterVal = "_" + encodeURIComponent(target.realVal());
url = url.replace('__FILTER__',filterVal);
url = url.replace('__ID_GROUP__',$('#id_group').val());
$.ajax({
@@ -163,9 +162,6 @@
}
init_events($(container_selector));
target.removeAttr('timer');
- },
- error: function(jqXHR, textStatus, errorThrown) {
- alert(jqXHR.responseText);
}
});
},
--- a/src/ldt/ldt/user/migrations/0004_fill_language.py Wed Jan 04 17:28:54 2012 +0100
+++ b/src/ldt/ldt/user/migrations/0004_fill_language.py Thu Jan 05 14:19:06 2012 +0100
@@ -4,16 +4,14 @@
from south.db import db
from south.v2 import DataMigration
from django.db import models
-from django.contrib.auth.models import User
-from ldt.user.models import UserProfile
class Migration(DataMigration):
def forwards(self, orm):
"Write your forwards methods here."
- for i in User.objects.all():
- UserProfile.objects.create(user=i)
+ for i in orm['auth.user'].objects.all():
+ orm.UserProfile.objects.create(user=i)