Commandes de gestion pour nettoyer les doublons
This commit is contained in:
parent
f6b43bc8eb
commit
71bfa6435f
5 changed files with 144 additions and 0 deletions
0
avisstage/management/__init__.py
Normal file
0
avisstage/management/__init__.py
Normal file
0
avisstage/management/commands/__init__.py
Normal file
0
avisstage/management/commands/__init__.py
Normal file
43
avisstage/management/commands/nettoie_lieux.py
Normal file
43
avisstage/management/commands/nettoie_lieux.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
#coding: utf-8
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db.models import Count
|
||||
from avisstage.models import Stage, Lieu
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Nettoie les stages à plusieurs lieux identiques'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('min_lieu', nargs='?', default=0, type=int)
|
||||
parser.add_argument(
|
||||
'--apply',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Applies the modifications',
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
rundb = False
|
||||
if options.get('apply', False):
|
||||
rundb = True
|
||||
else:
|
||||
print u"Les modifications ne seront pas appliquées"
|
||||
|
||||
min_lieu = options.get('min_lieu', 0)
|
||||
|
||||
for lieu in Lieu.objects.filter(id__gte=min_lieu).order_by('-id'):
|
||||
lproches = Lieu.objects.filter(id__lt=lieu.id, coord__distance_lte=(lieu.coord, 5))
|
||||
if len(lproches) == 0:
|
||||
continue
|
||||
print u"Doublons possibles pour %s (id=%d, %d avis) :" % (lieu, lieu.id, lieu.avislieu_set.count())
|
||||
for plieu in lproches:
|
||||
pprint = u" > %s (id=%d, %d avis)" % (plieu, plieu.id, plieu.avislieu_set.count())
|
||||
if plieu.nom == lieu.nom and plieu.ville == lieu.ville and plieu.type_lieu == lieu.type_lieu:
|
||||
print u"%s %s" % (pprint, self.style.SUCCESS(u'-> Suppression'))
|
||||
if rundb:
|
||||
for avis in plieu.avislieu_set.all():
|
||||
avis.lieu = lieu
|
||||
avis.save()
|
||||
plieu.delete()
|
||||
else:
|
||||
print u"%s %s" % (pprint, self.style.WARNING(u'-> À supprimer manuellement'))
|
||||
self.stdout.write(self.style.SUCCESS(u'Nettoyage des lieux effectué'))
|
66
avisstage/management/commands/nettoie_stages.py
Normal file
66
avisstage/management/commands/nettoie_stages.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
#coding: utf-8
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db.models import Count
|
||||
from avisstage.models import Stage, Lieu
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Nettoie les stages à plusieurs lieux identiques'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('min_stage', nargs='?', default=0, type=int)
|
||||
parser.add_argument(
|
||||
'--apply',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Applies the modifications',
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
def get_len(obj):
|
||||
length = 0
|
||||
avis = obj.avis_all
|
||||
for k, av in avis:
|
||||
length += len(av.split())
|
||||
length += len(obj.chapo.split())
|
||||
length += len(obj.les_plus.split())
|
||||
length += len(obj.les_moins.split())
|
||||
return length
|
||||
|
||||
rundb = False
|
||||
if options.get('apply', False):
|
||||
rundb = True
|
||||
else:
|
||||
print u"Les modifications ne seront pas appliquées"
|
||||
|
||||
min_stage = options.get('min_stage', 0)
|
||||
|
||||
for stage in Stage.objects.annotate(c=Count("lieux"))\
|
||||
.filter(c__gte=2, id__gte=min_stage):
|
||||
lieuset = {}
|
||||
todel = []
|
||||
problems = []
|
||||
for avis in stage.avislieu_set.all():
|
||||
alen = get_len(avis)
|
||||
aid = avis.lieu.id
|
||||
if aid in lieuset:
|
||||
if alen == 0:
|
||||
todel.append((avis, alen))
|
||||
continue
|
||||
elif lieuset[aid][1] == 0:
|
||||
todel.append(lieuset[aid])
|
||||
else:
|
||||
problems += [(avis, alen), lieuset[aid]]
|
||||
lieuset[aid] = (avis, alen)
|
||||
if len(todel) > 0:
|
||||
print u"Doublons détectés dans %s" % (stage,)
|
||||
for avis, alen in todel:
|
||||
print u" > Suppression de l'avis sur %s de %d mots" % \
|
||||
(avis.lieu, alen)
|
||||
if rundb:
|
||||
avis.delete()
|
||||
if len(problems) > 0:
|
||||
self.stdout.write(self.style.WARNING(u"Réparation impossible de %s (id=%d)" % (stage, stage.id)))
|
||||
for avis, alen in problems:
|
||||
print u" > Avis sur %s de %d mots" % \
|
||||
(avis.lieu, alen)
|
||||
self.stdout.write(self.style.SUCCESS(u'Nettoyage des stages effectué'))
|
35
avisstage/management/commands/supprime_lieu.py
Normal file
35
avisstage/management/commands/supprime_lieu.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
#coding: utf-8
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db.models import Count
|
||||
from avisstage.models import Stage, Lieu
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Nettoie les stages à plusieurs lieux identiques'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('del_lieu', type=int, help='Lieu à supprimer')
|
||||
parser.add_argument('repl_lieu', type=int, help='Lieu le remplaçant')
|
||||
parser.add_argument(
|
||||
'--apply',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Applies the modifications',
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
rundb = False
|
||||
if options.get('apply', False):
|
||||
rundb = True
|
||||
else:
|
||||
print u"Les modifications ne seront pas appliquées"
|
||||
|
||||
plieu = Lieu.objects.get(id=options['del_lieu'])
|
||||
lieu = Lieu.objects.get(id=options['repl_lieu'])
|
||||
print u"Suppression de %s (id=%d, %d avis)" % (plieu, plieu.id, plieu.avislieu_set.count())
|
||||
print u"Remplacement par %s (id=%d, %d avis)" % (lieu, lieu.id, lieu.avislieu_set.count())
|
||||
if rundb:
|
||||
for avis in plieu.avislieu_set.all():
|
||||
avis.lieu = lieu
|
||||
avis.save()
|
||||
plieu.delete()
|
||||
self.stdout.write(self.style.SUCCESS(u'Terminé'))
|
Loading…
Reference in a new issue