Changeset 1404:3f20ee4a3423
- Timestamp:
- 03/11/10 16:26:40 (7 hours ago)
- Author:
- Diego Búrigo Zacarão <diegobz@…>
- Branch:
- default
- Tags:
- tip
- Transplant:
- abba9baac5d6aeaff1a5c0535e08efbb28198321
- Message:
-
Fixed multiple copies of same event on timeline lists (#528)
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r1263
|
r1404
|
|
| 52 | 52 | return result |
| 53 | 53 | |
| | 54 | def _distinct_action_time(query): |
| | 55 | """ |
| | 56 | Distinct rows by the 'action_time' field, keeping in the query only the |
| | 57 | entry with the highest 'id' for the related set of entries with equal |
| | 58 | 'action_time'. |
| | 59 | |
| | 60 | Example: |
| | 61 | |
| | 62 | For the following query set: |
| | 63 | |
| | 64 | id | action_time |
| | 65 | ----+---------------------------- |
| | 66 | 1 | 2010-03-11 10:55:26.32941-03 |
| | 67 | 2 | 2010-03-11 10:55:26.32941-03 |
| | 68 | 3 | 2010-03-11 13:48:22.202596-09 |
| | 69 | 4 | 2010-03-11 13:48:53.505697-03 |
| | 70 | 5 | 2010-03-11 13:48:53.505697-03 |
| | 71 | 6 | 2010-03-11 13:51:09.013079-05 |
| | 72 | 7 | 2010-03-11 13:51:09.013079-05 |
| | 73 | 8 | 2010-03-11 13:51:09.013079-05 |
| | 74 | |
| | 75 | After passing through this function the query will be: |
| | 76 | |
| | 77 | id | action_time |
| | 78 | ----+---------------------------- |
| | 79 | 2 | 2010-03-11 10:55:26.32941-03 |
| | 80 | 3 | 2010-03-11 13:48:22.202596-09 |
| | 81 | 5 | 2010-03-11 13:48:53.505697-03 |
| | 82 | 8 | 2010-03-11 13:51:09.013079-05 |
| | 83 | |
| | 84 | Rows with the same 'action_time' are eliminated, keeping the one with |
| | 85 | highest 'id'. |
| | 86 | """ |
| | 87 | pks = query.values('action_time').annotate( |
| | 88 | id=models.Max('id')).order_by().values_list('id', flat=True) |
| | 89 | return LogEntry.objects.filter(pk__in=pks) |
| | 90 | |
| | 91 | |
| 54 | 92 | class LogEntryManager(models.Manager): |
| 55 | 93 | def by_object(self, obj): |
| 56 | 94 | """Return LogEntries for a related object.""" |
| 57 | 95 | ctype = ContentType.objects.get_for_model(obj) |
| 58 | | return self.filter(content_type__pk=ctype.pk, object_id=obj.pk) |
| | 96 | q = self.filter(content_type__pk=ctype.pk, object_id=obj.pk) |
| | 97 | return _distinct_action_time(q) |
| 59 | 98 | |
| 60 | 99 | def by_user(self, user): |
| 61 | 100 | """Return LogEntries for a specific user.""" |
| 62 | | return self.filter(user__pk__exact=user.pk) |
| | 101 | q = self.filter(user__pk__exact=user.pk) |
| | 102 | return _distinct_action_time(q) |
| 63 | 103 | |
| 64 | 104 | def by_object_last_week(self, obj): |