Skip to content
GitLab
Explorer
Connexion
Navigation principale
Rechercher ou aller à…
Projet
D
django-representatives
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Compilation
Pipelines
Jobs
Planifications de pipeline
Artéfacts
Déploiement
Releases
Registre de conteneurs
Registre de modèles
Opération
Environnements
Surveillance
Incidents
Analyse
Données d'analyse des chaînes de valeur
Analyse des contributeurs
Données d'analyse CI/CD
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté
Contribuer à GitLab
Donner votre avis
Conditions générales et politique de confidentialité
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Ce projet est archivé. Le dépôt et les autres ressources du projet sont en lecture seule.
Afficher davantage de fils d'Ariane
La Quadrature du Net
Political Memory
django-representatives
Validations
5a3d609d
Valider
5a3d609d
rédigé
8 years ago
par
Nicolas Joyard
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
Add data migration for multi-chamber support
parent
86348a46
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
1
Masquer les modifications d'espaces
En ligne
Côte à côte
Affichage de
1 fichier modifié
representatives/migrations/0016_chamber_migrate_data.py
+120
-0
120 ajouts, 0 suppression
representatives/migrations/0016_chamber_migrate_data.py
avec
120 ajouts
et
0 suppression
representatives/migrations/0016_chamber_migrate_data.py
0 → 100644
+
120
−
0
Voir le fichier @
5a3d609d
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
import
hashlib
from
django.db
import
migrations
,
models
from
django.utils.encoding
import
smart_str
def
calculate_hash
(
obj
):
"""
Computes fingerprint for an object, this code is duplicated from
representatives.models.HashableModel because we don
'
t have access to model
methods in a migration scenario.
"""
hashable_fields
=
{
'
Chamber
'
:
[
'
name
'
,
'
country
'
,
'
abbreviation
'
],
'
Constituency
'
:
[
'
name
'
],
'
Group
'
:
[
'
name
'
,
'
abbreviation
'
,
'
kind
'
,
'
chamber
'
],
'
Mandate
'
:
[
'
group
'
,
'
constituency
'
,
'
role
'
,
'
begin_date
'
,
'
end_date
'
,
'
representative
'
]
}
fingerprint
=
hashlib
.
sha1
()
for
field_name
in
hashable_fields
[
obj
.
__class__
.
__name__
]:
field
=
obj
.
_meta
.
get_field
(
field_name
)
if
field
.
is_relation
:
related
=
getattr
(
obj
,
field_name
)
if
related
is
None
:
fingerprint
.
update
(
smart_str
(
related
))
else
:
fingerprint
.
update
(
related
.
fingerprint
)
else
:
fingerprint
.
update
(
smart_str
(
getattr
(
obj
,
field_name
)))
obj
.
fingerprint
=
fingerprint
.
hexdigest
()
return
obj
.
fingerprint
def
get_or_create
(
cls
,
**
kwargs
):
"""
Implements get_or_create logic for models that inherit from
representatives.models.HashableModel because we don
'
t have access to model
methods in a migration scenario.
"""
try
:
obj
=
cls
.
objects
.
get
(
**
kwargs
)
created
=
False
except
cls
.
DoesNotExist
:
obj
=
cls
(
**
kwargs
)
created
=
True
calculate_hash
(
obj
)
obj
.
save
()
return
(
obj
,
created
)
def
migrate_ep_chamber
(
apps
,
schema_editor
):
# Get model managers
Chamber
=
apps
.
get_model
(
"
representatives
"
,
"
Chamber
"
)
Constituency
=
apps
.
get_model
(
"
representatives
"
,
"
Constituency
"
)
Group
=
apps
.
get_model
(
"
representatives
"
,
"
Group
"
)
Mandate
=
apps
.
get_model
(
"
representatives
"
,
"
Mandate
"
)
# Create EP chamber, constituency and group
name
=
'
European Parliament
'
abbr
=
'
EP
'
ep_chamber
,
_
=
get_or_create
(
Chamber
,
name
=
name
,
abbreviation
=
abbr
)
ep_constituency
,
_
=
get_or_create
(
Constituency
,
name
=
name
)
ep_group
,
_
=
get_or_create
(
Group
,
name
=
name
,
kind
=
'
chamber
'
,
abbreviation
=
abbr
,
chamber
=
ep_chamber
)
# Set chamber on groups
ep_group_kinds
=
[
'
committee
'
,
'
delegation
'
,
'
group
'
]
Group
.
objects
.
filter
(
kind__in
=
ep_group_kinds
).
update
(
chamber
=
ep_chamber
)
# Set constituency on EP mandates
ep_mandate_kinds
=
[
'
committee
'
,
'
delegation
'
,
'
group
'
,
'
organization
'
]
Mandate
.
objects
.
filter
(
group__kind__in
=
ep_mandate_kinds
).
update
(
constituency
=
ep_constituency
)
# Create EP mandates with EP constituency & group with same timespan as
# the country mandate
for
m
in
Mandate
.
objects
.
filter
(
group__kind__exact
=
'
country
'
):
mandate
,
_
=
get_or_create
(
Mandate
,
representative
=
m
.
representative
,
group
=
ep_group
,
constituency
=
ep_constituency
,
role
=
m
.
role
,
begin_date
=
m
.
begin_date
,
end_date
=
m
.
end_date
)
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'
representatives
'
,
'
0015_chamber_abbreviation
'
),
]
operations
=
[
migrations
.
RunPython
(
migrate_ep_chamber
)
]
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter