Commit fa6c79ff086b931f900613fa7dcf1fff712eb18f
1 parent
42b46a41
Exists in
master
and in
39 other branches
Refactoring modified by and url
Showing
3 changed files
with
56 additions
and
42 deletions
Show diff stats
src/search/base_indexes.py
... | ... | @@ -32,20 +32,23 @@ class BaseIndex(indexes.SearchIndex): |
32 | 32 | return math.log(obj.hits) |
33 | 33 | |
34 | 34 | def prepare(self, obj): |
35 | + self.author_obj = None | |
36 | + if hasattr(obj, 'get_author'): | |
37 | + self.author_obj = obj.get_author() | |
38 | + | |
35 | 39 | data = super(BaseIndex, self).prepare(obj) |
36 | 40 | data['boost'] = self.get_boost(obj) |
41 | + | |
37 | 42 | return data |
38 | 43 | |
39 | 44 | def prepare_author(self, obj): |
40 | - author = obj.get_author() | |
41 | - if author: | |
42 | - return author.username | |
45 | + if self.author_obj: | |
46 | + return self.author_obj.username | |
43 | 47 | return obj.author |
44 | 48 | |
45 | 49 | def prepare_author_url(self, obj): |
46 | - author = obj.get_author() | |
47 | - if author: | |
48 | - return author.get_absolute_url() | |
50 | + if self.author_obj: | |
51 | + return self.author_obj.get_absolute_url() | |
49 | 52 | return None |
50 | 53 | |
51 | 54 | def prepare_fullname(self, obj): |
... | ... | @@ -53,30 +56,37 @@ class BaseIndex(indexes.SearchIndex): |
53 | 56 | modified_by = obj.get_modified_by() |
54 | 57 | if modified_by: |
55 | 58 | return modified_by.get_full_name() |
56 | - return None | |
57 | - else: | |
58 | - author = obj.get_author() | |
59 | - if author: | |
60 | - return author.get_full_name() | |
61 | - return obj.author | |
59 | + if self.author_obj: | |
60 | + return self.author_obj.get_full_name() | |
61 | + return obj.author | |
62 | 62 | |
63 | 63 | def prepare_fullname_and_username(self, obj): |
64 | - author = obj.get_author() | |
65 | - if not author: | |
64 | + if hasattr(obj, 'modified_by'): | |
65 | + modified_by = obj.get_modified_by() | |
66 | + if modified_by: | |
67 | + return u'{}\n{}'.format( | |
68 | + modified_by.get_full_name(), | |
69 | + modified_by.username, | |
70 | + ) | |
71 | + if not self.author_obj: | |
66 | 72 | return obj.author |
67 | 73 | return u'{}\n{}'.format( |
68 | - author.get_full_name(), | |
69 | - author.username, | |
74 | + self.author_obj.get_full_name(), | |
75 | + self.author_obj.username, | |
70 | 76 | ) |
71 | 77 | |
72 | 78 | def prepare_modified_by(self, obj): |
73 | 79 | if hasattr(obj, 'modified_by'): |
74 | 80 | return obj.modified_by |
75 | - return None | |
81 | + if self.author_obj: | |
82 | + return self.author_obj.get_full_name() | |
83 | + return obj.author | |
76 | 84 | |
77 | 85 | def prepare_modified_by_url(self, obj): |
78 | 86 | if hasattr(obj, 'modified_by'): |
79 | 87 | modified_by = obj.get_modified_by() |
80 | 88 | if modified_by: |
81 | 89 | return modified_by.get_absolute_url() |
90 | + if self.author_obj: | |
91 | + return self.author_obj.get_absolute_url() | |
82 | 92 | return None | ... | ... |
src/super_archives/search_indexes.py
... | ... | @@ -50,25 +50,33 @@ class ThreadIndex(BaseIndex, indexes.Indexable): |
50 | 50 | ) |
51 | 51 | |
52 | 52 | def prepare_author(self, obj): |
53 | - return obj.latest_message.author | |
53 | + first_message = obj.message_set.first() | |
54 | + return first_message.from_address.get_full_name() | |
54 | 55 | |
55 | 56 | def prepare_author_url(self, obj): |
56 | 57 | first_message = obj.message_set.first() |
57 | - if first_message.from_address.user: | |
58 | - return first_message.from_address.user.get_absolute_url() | |
59 | - return None | |
58 | + return first_message.author_url | |
60 | 59 | |
61 | 60 | def prepare_modified_by(self, obj): |
62 | - return obj.latest_message.author | |
61 | + modified_by = obj.latest_message.modified_by | |
62 | + if modified_by: | |
63 | + return modified_by | |
64 | + return obj.message_set.first().author | |
63 | 65 | |
64 | 66 | def prepare_modified_by_url(self, obj): |
65 | - return obj.latest_message.author_url | |
67 | + modified_by_url = obj.latest_message.modified_by_url | |
68 | + if modified_by_url: | |
69 | + return modified_by_url | |
70 | + return None | |
66 | 71 | |
67 | 72 | def prepare_created(self, obj): |
68 | 73 | return obj.message_set.first().received_time |
69 | 74 | |
70 | 75 | def prepare_fullname(self, obj): |
71 | - return obj.latest_message.from_address.get_full_name() | |
76 | + fullname = obj.latest_message.from_address.get_full_name() | |
77 | + if not fullname: | |
78 | + fullname = obj.message_set.first().from_address.get_full_name() | |
79 | + return fullname | |
72 | 80 | |
73 | 81 | def prepare_icon_name(self, obj): |
74 | 82 | return u'envelope' | ... | ... |
src/super_archives/templates/message-preview.html
... | ... | @@ -38,28 +38,24 @@ |
38 | 38 | |
39 | 39 | {% if result.fullname or result.modified or result.modified_by %} |
40 | 40 | <div class="quiet"> |
41 | - {% if result.fullname or result.modified_by %} | |
41 | + {% if result.modified_by %} | |
42 | 42 | <span class="pull-left">{% trans "by" %} |
43 | - {% if result.modified_by and result.modified_by_url %} | |
43 | + {% if result.modified_by_url %} | |
44 | 44 | <a href="{{ result.modified_by_url }}"> |
45 | - {% if query %} | |
46 | - {% highlight result.modified_by with query %} | |
47 | - {% else %} | |
48 | - {{ result.modified_by }} | |
49 | - {% endif %} | |
50 | - </a> | |
51 | - {% elif result.modified_by and not result.modified_by_url %} | |
52 | - <span>{{ result.modified_by }}</span> | |
53 | - {% elif result.fullname and result.author_url %} | |
54 | - <a href="{{ result.author_url }}"> | |
55 | - {% if query %} | |
56 | - {% highlight result.fullname with query %} | |
57 | - {% else %} | |
58 | - {{ result.fullname }} | |
59 | - {% endif %} | |
45 | + {% else %} | |
46 | + <span> | |
47 | + {% endif %} | |
48 | + | |
49 | + {% if query %} | |
50 | + {% highlight result.modified_by with query %} | |
51 | + {% else %} | |
52 | + {{ result.modified_by }} | |
53 | + {% endif %} | |
54 | + | |
55 | + {% if result.modified_by_url %} | |
60 | 56 | </a> |
61 | 57 | {% else %} |
62 | - <span>{{ result.fullname }}</span> | |
58 | + </span> | |
63 | 59 | {% endif %} |
64 | 60 | </span> |
65 | 61 | {% else %} | ... | ... |