switch from ArrayField to M2M
gitea/barbacompta/pipeline/head This commit looks good Details

This commit is contained in:
Pierre Ducroquet 2023-11-08 16:28:05 +01:00
parent 4b461758ce
commit f07bc69eba
5 changed files with 45 additions and 7 deletions

View File

@ -139,7 +139,7 @@ class MyClientsFilter(admin.SimpleListFilter):
value = super().value()
default_value = 'all'
user = self.request.user
if user and Project.objects.filter(cpfs__contains=[user.username]).exists():
if user and user.project_set.exists():
default_value = 'True'
return value if value in ('True', 'all') else default_value
@ -153,7 +153,7 @@ class MyClientsFilter(admin.SimpleListFilter):
def queryset(self, request, queryset):
if self.value() == 'True':
return queryset.filter(project__cpfs__contains=[request.user.username])
return queryset.filter(project__cpfs=request.user)
else:
return queryset

View File

@ -7,7 +7,7 @@ from . import models
class ProjectAdmin(admin.ModelAdmin):
readonly_fields = ['name', 'cpfs']
list_display = ['name', 'cpfs', 'client']
list_display = ['name', 'client']
admin.site.register(models.Project, ProjectAdmin)

View File

@ -1,5 +1,6 @@
import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
@ -58,7 +59,11 @@ class Command(BaseCommand):
cpf_ids.add(membership['user']['id'])
cpfs = []
for cpf_id in cpf_ids:
cpfs.append(redmine_get_login(cpf_id))
try:
cpfs.append(User.objects.get(username=redmine_get_login(cpf_id)))
except User.DoesNotExist:
# missing user in barba, ignoring, will be fixed later once he is created
pass
# now match in our DB, maybe update
insert = False
@ -70,7 +75,7 @@ class Command(BaseCommand):
insert = True
project_object.name = project_name
project_object.cpfs = cpfs
project_object.cpfs.set(cpfs)
project_object.save(force_insert=insert)
# Purge dead projects

View File

@ -0,0 +1,33 @@
# Generated by Django 3.2.21 on 2023-11-08 15:17
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('eo_facture', '0019_alter_contrat_client'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('eo_redmine', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='project',
name='client',
field=models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='eo_facture.client'
),
),
migrations.RemoveField(
model_name='project',
name='cpfs',
),
migrations.AddField(
model_name='project',
name='cpfs',
field=models.ManyToManyField(to=settings.AUTH_USER_MODEL),
),
]

View File

@ -1,4 +1,4 @@
from django.contrib.postgres.fields import ArrayField
from django.contrib.auth.models import User
from django.db import models
from eo_gestion.eo_facture.models import Client
@ -6,7 +6,7 @@ from eo_gestion.eo_facture.models import Client
class Project(models.Model):
name = models.CharField(max_length=255)
cpfs = ArrayField(models.CharField(max_length=255))
cpfs = models.ManyToManyField(User)
client = models.ForeignKey(Client, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):