mirror of
https://github.com/nanoy42/coope
synced 2024-11-26 06:32:27 +00:00
Proprification de l'export
This commit is contained in:
parent
020bd21bd1
commit
dd49d147ee
3 changed files with 19 additions and 15 deletions
|
@ -122,10 +122,10 @@ class ExportForm(forms.Form):
|
||||||
('last_name', 'Nom'),
|
('last_name', 'Nom'),
|
||||||
('first_name', 'Prénom'),
|
('first_name', 'Prénom'),
|
||||||
('email', 'Adresse mail'),
|
('email', 'Adresse mail'),
|
||||||
('profile.school', 'École'),
|
('school', 'École'),
|
||||||
('profile.balance', 'Solde'),
|
('balance', 'Solde'),
|
||||||
('profile.credit', 'Crédit'),
|
('credit', 'Crédit'),
|
||||||
('profile.debit', 'Débit')
|
('debit', 'Débit')
|
||||||
)
|
)
|
||||||
query_type = forms.ChoiceField(choices=QUERY_TYPE_CHOICES, label="Ensemble de la demande")
|
query_type = forms.ChoiceField(choices=QUERY_TYPE_CHOICES, label="Ensemble de la demande")
|
||||||
fields = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=FIELDS_CHOICES, label="Champs")
|
fields = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=FIELDS_CHOICES, label="Champs")
|
|
@ -122,6 +122,18 @@ class Profile(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.user)
|
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)
|
@receiver(post_save, sender=User)
|
||||||
def create_user_profile(sender, instance, created, **kwargs):
|
def create_user_profile(sender, instance, created, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -105,19 +105,11 @@ def export_csv(request):
|
||||||
top.append(dict(ExportForm.FIELDS_CHOICES)[field])
|
top.append(dict(ExportForm.FIELDS_CHOICES)[field])
|
||||||
writer.writerow(top)
|
writer.writerow(top)
|
||||||
for user in users:
|
for user in users:
|
||||||
userD = model_to_dict(user)
|
|
||||||
profileD = model_to_dict(user.profile)
|
|
||||||
row = [user.pk]
|
row = [user.pk]
|
||||||
for field in fields:
|
for field in fields:
|
||||||
if "profile" in field:
|
r = getattr(user.profile, field, None)
|
||||||
if "balance" in field:
|
if r is not None:
|
||||||
row.append(user.profile.balance)
|
row.append(str(r))
|
||||||
elif "school" in field:
|
|
||||||
row.append(str(user.profile.school))
|
|
||||||
else:
|
|
||||||
row.append(profileD[field[8:]])
|
|
||||||
else:
|
|
||||||
row.append(userD[field])
|
|
||||||
writer.writerow(row)
|
writer.writerow(row)
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue