1
1
from django .conf import settings
2
- from django .contrib .contenttypes .models import ContentType
3
2
from django .contrib .sites .models import Site
4
3
from django .db import models
5
4
from django .utils .translation import ugettext_lazy as _
5
+ from fluent_faq import appsettings
6
6
from parler .models import TranslatableModel , TranslatedFields
7
+ from parler .utils .context import switch_language
7
8
from fluent_contents .models import PlaceholderField , ContentItemRelation
8
9
from fluent_faq .urlresolvers import faq_reverse
9
10
from fluent_faq .managers import FaqQuestionManager , FaqCategoryManager
10
-
11
-
12
-
13
- # Optional tagging support
14
- from parler .utils .context import switch_language
15
-
16
- TaggableManager = None
17
- if 'taggit_autosuggest' in settings .INSTALLED_APPS :
18
- from taggit_autosuggest .managers import TaggableManager
19
- elif 'taggit_autocomplete_modified' in settings .INSTALLED_APPS :
20
- from taggit_autocomplete_modified .managers import TaggableManagerAutocomplete as TaggableManager
21
- elif 'taggit' in settings .INSTALLED_APPS :
22
- from taggit .managers import TaggableManager
11
+ from fluent_utils .softdeps .taggit import TagsMixin
23
12
24
13
25
14
class FaqBaseModel (TranslatableModel ):
@@ -97,7 +86,7 @@ def get_relative_url(self):
97
86
98
87
99
88
100
- class FaqQuestion (FaqBaseModel ):
89
+ class FaqQuestion (TagsMixin , FaqBaseModel ):
101
90
"""
102
91
Category in the FAQ.
103
92
"""
@@ -119,12 +108,6 @@ class FaqQuestion(FaqBaseModel):
119
108
# Organisation
120
109
category = models .ForeignKey (FaqCategory , verbose_name = _ ("Category" ), related_name = 'questions' )
121
110
122
- # Make association with tags optional.
123
- if TaggableManager is not None :
124
- tags = TaggableManager (blank = True , help_text = _ ("Tags are used to find related questions" ))
125
- else :
126
- tags = None
127
-
128
111
objects = FaqQuestionManager ()
129
112
130
113
class Meta :
@@ -143,70 +126,15 @@ def get_relative_url(self):
143
126
# Return the link style, using the permalink style setting.
144
127
return u'{0}{1}/' .format (self .category .get_relative_url (), self .slug )
145
128
146
-
147
129
def similar_objects (self , num = None , ** filters ):
148
- tags = self .tags
149
- if not tags :
150
- return []
151
-
152
- content_type = ContentType .objects .get_for_model (self .__class__ )
153
- filters ['content_type' ] = content_type
154
-
155
- # can't filter, see
156
- # - https://github.com/alex/django-taggit/issues/32
157
- # - http://django-taggit.readthedocs.org/en/latest/api.html#TaggableManager.similar_objects
158
- #
159
- # Otherwise this would be possible:
160
- # return tags.similar_objects(**filters)
161
-
162
- lookup_kwargs = tags ._lookup_kwargs ()
163
- lookup_keys = sorted (lookup_kwargs )
164
- qs = tags .through .objects .values (* lookup_kwargs .keys ())
165
- qs = qs .annotate (n = models .Count ('pk' ))
166
- qs = qs .exclude (** lookup_kwargs )
167
- subq = tags .all ()
168
- qs = qs .filter (tag__in = list (subq ))
169
- qs = qs .order_by ('-n' )
170
-
171
- # from https://github.com/alex/django-taggit/issues/32#issuecomment-1002491
172
- if filters is not None :
173
- qs = qs .filter (** filters )
174
-
175
- if num is not None :
176
- qs = qs [:num ]
177
-
178
- # Normal taggit code continues
179
-
180
- # TODO: This all feels like a bit of a hack.
181
- items = {}
182
- if len (lookup_keys ) == 1 :
183
- # Can we do this without a second query by using a select_related()
184
- # somehow?
185
- f = tags .through ._meta .get_field_by_name (lookup_keys [0 ])[0 ]
186
- objs = f .rel .to ._default_manager .filter (** {
187
- "%s__in" % f .rel .field_name : [r ["content_object" ] for r in qs ]
188
- })
189
- for obj in objs :
190
- items [(getattr (obj , f .rel .field_name ),)] = obj
191
- else :
192
- preload = {}
193
- for result in qs :
194
- preload .setdefault (result ['content_type' ], set ())
195
- preload [result ["content_type" ]].add (result ["object_id" ])
196
-
197
- for ct , obj_ids in preload .items ():
198
- ct = ContentType .objects .get_for_id (ct )
199
- for obj in ct .model_class ()._default_manager .filter (pk__in = obj_ids ):
200
- items [(ct .pk , obj .pk )] = obj
201
-
202
- results = []
203
- for result in qs :
204
- obj = items [
205
- tuple (result [k ] for k in lookup_keys )
206
- ]
207
- obj .similar_tags = result ["n" ]
208
- results .append (obj )
209
- return results
130
+ """
131
+ Find similar objects using related tags.
132
+ """
133
+ #TODO: filter appsettings.FLUENT_FAQ_FILTER_SITE_ID:
134
+ # filters.setdefault('parent_site', self.parent_site_id)
135
+
136
+ # FIXME: Using super() doesn't work, calling directly.
137
+ return TagsMixin .similar_objects (self , num = num , ** filters )
210
138
211
139
212
140
def _register_anyurlfield_type ():
0 commit comments