New models

This commit is contained in:
Ludovic Stephan 2020-08-03 19:04:36 +02:00
parent 34be9e2393
commit e92d50593c
3 changed files with 105 additions and 0 deletions

View file

@ -0,0 +1,55 @@
# Generated by Django 2.2.8 on 2020-01-08 21:03
import django.contrib.auth.models
import django.db.models.deletion
import django.db.models.manager
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("auth", "0011_update_proxy_permissions"),
("kfetauth", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="KFetGroup",
fields=[
(
"group_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="auth.Group",
),
),
],
options={
"verbose_name": "Groupe K-Fêt",
"verbose_name_plural": "Groupes K-Fêt",
},
bases=("auth.group",),
managers=[("objects", django.contrib.auth.models.GroupManager())],
),
migrations.CreateModel(
name="KFetPermission",
fields=[],
options={
"verbose_name": "Permission K-Fêt",
"verbose_name_plural": "Permissions K-Fêt",
"proxy": True,
"indexes": [],
"constraints": [],
},
bases=("auth.permission",),
managers=[
("kfet", django.db.models.manager.Manager()),
("objects", django.contrib.auth.models.PermissionManager()),
],
),
]

View file

@ -0,0 +1,21 @@
# Generated by Django 2.2.8 on 2020-01-08 21:04
from django.db import migrations
def existing_groups(apps, schema_editor):
Group = apps.get_model("auth", "Group")
KFetGroup = apps.get_model("kfetauth", "KFetGroup")
for group in Group.objects.filter(name__icontains="K-Fêt"):
kf_group = KFetGroup(group_ptr=group, pk=group.pk, name=group.name)
kf_group.save()
class Migration(migrations.Migration):
dependencies = [
("kfetauth", "0002_kfetgroup_kfetpermission"),
]
operations = [migrations.RunPython(existing_groups)]

View file

@ -1,5 +1,7 @@
from django.contrib.auth.models import Group, Permission
from django.db import models from django.db import models
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from django.utils.translation import ugettext_lazy as _
class GenericTeamTokenManager(models.Manager): class GenericTeamTokenManager(models.Manager):
@ -14,3 +16,30 @@ class GenericTeamToken(models.Model):
token = models.CharField(max_length=50, unique=True) token = models.CharField(max_length=50, unique=True)
objects = GenericTeamTokenManager() objects = GenericTeamTokenManager()
class KFetGroup(Group):
# Même si on n'ajoute que des méthodes, on fait un héritage complet pour
# mieux distinguer les groupes K-Fêt via l'ORM (i.e. faire `KFetGroup.objects.all`)
class Meta:
verbose_name = _("Groupe K-Fêt")
verbose_name_plural = _("Groupes K-Fêt")
class KFetPermissionManager(models.Manager):
def get_queryset(self):
return (
super()
.get_queryset()
.filter(content_type__app_label__in=["kfet", "kfetauth"])
)
class KFetPermission(Permission):
kfet = KFetPermissionManager()
class Meta:
proxy = True
verbose_name = _("Permission K-Fêt")
verbose_name_plural = _("Permissions K-Fêt")