Commit 3c9e636d authored by alexandre's avatar alexandre

Created perm fields + migrations from dj guardian

parent c5a0c6b6
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2019-01-04 14:25
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('playground', '0022_auto_20181217_1010'),
]
operations = [
migrations.AddField(
model_name='score',
name='created_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='creator', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='score',
name='is_public',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='score',
name='shared_with',
field=models.ManyToManyField(to=settings.AUTH_USER_MODEL),
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2019-01-04 14:26
from __future__ import unicode_literals
from django.db import migrations
def populate_permissions(apps, schema_editor):
from guardian.shortcuts import get_users_with_perms
from guardian.utils import get_anonymous_user
# We can't import the models directly as it may be a newer
# version than this migration expects. We use the historical version.
Score = apps.get_model('playground', 'Score')
User = apps.get_model('auth', 'User')
creator = User.objects.get(username="jeanne")
anonymous_user = get_anonymous_user()
for score in Score.objects.all():
score.created_by = creator
users = get_users_with_perms(score)
score.is_public = users.filter(id=anonymous_user.id).exists()
users = users.exclude(id__in=[creator.id, anonymous_user.id])
for user in users:
score.shared_with.add(User.objects.get(id=user.id))
score.save()
class Migration(migrations.Migration):
dependencies = [
('playground', '0023_auto_20190104_1425'),
]
operations = [
migrations.RunPython(populate_permissions),
]
from django.db import models
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.postgres.fields import JSONField
from taggit.managers import TaggableManager
class Attachment(models.Model):
title = models.CharField(max_length=255)
attachment = models.FileField(blank=True, null=True)
......@@ -34,6 +37,10 @@ class Score(models.Model):
mainline = JSONField(blank=True)
language = models.TextField(blank=True)
is_public = models.BooleanField(default=False)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, blank=True, null=True, related_name="creator")
shared_with = models.ManyToManyField(settings.AUTH_USER_MODEL)
def __str__(self):
return self.title
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment