3
0
Fork 0
mirror of https://github.com/nanoy42/coope synced 2024-11-22 11:23:11 +00:00

Proprification de l'export

This commit is contained in:
Yoann Pétri 2018-12-18 19:01:09 +01:00
parent 020bd21bd1
commit dd49d147ee
3 changed files with 19 additions and 15 deletions

View file

@ -122,10 +122,10 @@ class ExportForm(forms.Form):
('last_name', 'Nom'),
('first_name', 'Prénom'),
('email', 'Adresse mail'),
('profile.school', 'École'),
('profile.balance', 'Solde'),
('profile.credit', 'Crédit'),
('profile.debit', 'Débit')
('school', 'École'),
('balance', 'Solde'),
('credit', 'Crédit'),
('debit', 'Débit')
)
query_type = forms.ChoiceField(choices=QUERY_TYPE_CHOICES, label="Ensemble de la demande")
fields = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=FIELDS_CHOICES, label="Champs")

View file

@ -122,6 +122,18 @@ class Profile(models.Model):
def __str__(self):
return str(self.user)
def __getattr__(self, name):
"""
Tente de retourner l'attribut de l'instance et si l'attribut n'existe pas,
tente de retourner l'attribut de l'user associé à l'instance
"""
try:
r = super().__getattr__(name)
except AttributeError:
r = getattr(self.user, name)
return r
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
"""

View file

@ -105,19 +105,11 @@ def export_csv(request):
top.append(dict(ExportForm.FIELDS_CHOICES)[field])
writer.writerow(top)
for user in users:
userD = model_to_dict(user)
profileD = model_to_dict(user.profile)
row = [user.pk]
for field in fields:
if "profile" in field:
if "balance" in field:
row.append(user.profile.balance)
elif "school" in field:
row.append(str(user.profile.school))
else:
row.append(profileD[field[8:]])
else:
row.append(userD[field])
r = getattr(user.profile, field, None)
if r is not None:
row.append(str(r))
writer.writerow(row)
return response
else: