Revision 4845dfaa

b/snf-django-lib/snf_django/management/commands/__init__.py
41 41

  
42 42
import distutils
43 43

  
44
USER_EMAIL_FIELD = "user.email"
45

  
44 46

  
45 47
class SynnefoCommand(BaseCommand):
46 48
    option_list = BaseCommand.option_list + (
......
189 191

  
190 192
        assert(self.object_class), "object_class variable must be declared"
191 193

  
194
        # If an user field is declared, include the USER_EMAIL_FIELD in the
195
        # available fields
196
        if self.user_uuid_field is not None:
197
            self.FIELDS[USER_EMAIL_FIELD] =\
198
                ("_user_email", "The email of the owner")
199

  
192 200
        if options["list_fields"]:
193 201
            self.display_fields()
194 202
            return
......
204 212
            self.validate_fields(fields)
205 213
            self.fields = options["fields"].split(",")
206 214

  
215
        # --display-mails option
216
        if options.get("display_mails"):
217
            self.fields.append(USER_EMAIL_FIELD)
218

  
207 219
        # --filter-by option
208 220
        if options["filter_by"]:
209 221
            filters, excludes = \
......
252 264
        order_key = self.order_by if self.order_by is not None else 'pk'
253 265
        objects = objects.order_by(order_key)
254 266

  
255
        # --display-mails option
256
        display_mails = options.get("display_mails")
257
        if display_mails:
258
            if 'user_mail' in self.object_class._meta.get_all_field_names():
267
        if USER_EMAIL_FIELD in self.fields:
268
            if '_user_email' in self.object_class._meta.get_all_field_names():
259 269
                raise RuntimeError("%s has already a 'user_mail' attribute")
260

  
261
            self.fields.append("user.email")
262
            self.FIELDS["user.email"] =\
263
                ("user_email", "The email of the owner.")
264 270
            uuids = [getattr(obj, self.user_uuid_field) for obj in objects]
265 271
            ucache = UserCache(self.astakos_auth_url, self.astakos_token)
266 272
            ucache.fetch_names(list(set(uuids)))
267 273
            for obj in objects:
268 274
                uuid = getattr(obj, self.user_uuid_field)
269
                obj.user_email = ucache.get_name(uuid)
275
                obj._user_email = ucache.get_name(uuid)
270 276

  
271 277
        # Special handling of DB results
272 278
        objects = list(objects)

Also available in: Unified diff