Feature change ordering in employees' list#557
Conversation
8d8be88 to
9ddf4c8
Compare
kbeker
left a comment
There was a problem hiding this comment.
There is no test at all. Please add
users/views.py
Outdated
| admins = self.queryset.filter(user_type=CustomUser.UserType.ADMIN.name) | ||
| managers = self.queryset.filter(user_type=CustomUser.UserType.MANAGER.name) | ||
| employees = self.queryset.filter(user_type=CustomUser.UserType.EMPLOYEE.name) | ||
| context_data["ordered_object_list"] = list(admins) + list(managers) + list(employees) |
There was a problem hiding this comment.
Please extract this to private methods for example something like this:
def _get_users_by_user_type(self, user_type):
return self.queryset.filter(user_type=user_type)
def _get_ordered_users_by_user_type(self):
admins = self._get_users_by_user_type(user_type=CustomUser.UserType.ADMIN.name)
managers = self._get_users_by_user_type(user_type=CustomUser.UserType.MANAGER.name)
employees = self._get_users_by_user_type(user_type=CustomUser.UserType.EMPLOYEE.name)
return list(admins) + list(managers) + list(employees)
context_data["ordered_object_list"] = self._get_ordered_users_by_user_type()Also there is no test for this - please add some.
I can't find different way to order all users in one query, but I can't believe that there is not other way to query that queryset should by ordered by uset_type where user_type first is ADMIN then MANAGER etc. But don't waste time for this right now. I just telling you this to keep it in mind :)
There was a problem hiding this comment.
Actually, I could try to either use Python's sorted (but this one returns a list) or write a function in the model manager. I was hesitant to use the latter, because I found it too complex for such simple task, but now that I look at it again, I think I could try it.
There was a problem hiding this comment.
Alright, I checked that model manager solution and it involves calling annotate to create a new integer field. It's values would be set based on the value of the field we want to sort by (so for "ADMIN" we set 0, for "MANAGER" we set 1, etc.). Then we call .order_by and pass that new field we created.
Seems like it's impossible in Django to fiddle with database ordering on the backend side.
There was a problem hiding this comment.
I think that we could accomplished that with some changes in Employee model. I just though about changing user type into IntEnums and by this we could get that really simple sorting mechanism but I think that we for this moment we can leave as it is:)
2412f32 to
e632d2f
Compare
e632d2f to
c09ddf7
Compare
Changes ordering in employees list in following hierarchy:
user type -> last name -> first name -> email