1
1
from pathlib import Path
2
2
3
+ from structlog import get_logger
3
4
from django .contrib .auth import get_user_model
4
5
from django .db import models
5
6
from django .db .models import Case , Value , When
17
18
)
18
19
19
20
from . import acl
21
+ from .constants import CONTENT_SUMMARIZATION_THRESHOLD
22
+ from .tasks import summary_dispatcher
20
23
21
24
User = get_user_model ()
25
+ logger = get_logger (__name__ )
22
26
23
27
24
28
class EntryManager (models .Manager ):
@@ -86,6 +90,9 @@ class AlreadyApprovedError(Exception):
86
90
approved_at = models .DateTimeField (null = True , blank = True )
87
91
modified_at = models .DateTimeField (auto_now = True )
88
92
publish_at = models .DateTimeField (default = now )
93
+ summary = models .TextField (
94
+ null = True , blank = True , help_text = "AI generated summary. Delete to regenerate."
95
+ )
89
96
90
97
objects = EntryManager ()
91
98
@@ -154,6 +161,21 @@ def is_video(self):
154
161
result = False
155
162
return result
156
163
164
+ @cached_property
165
+ def determined_news_type (self ):
166
+ if self .is_blogpost :
167
+ return "blogpost"
168
+ elif self .is_link :
169
+ return "link"
170
+ elif self .is_news :
171
+ return "news"
172
+ elif self .is_poll :
173
+ return "poll"
174
+ elif self .is_video :
175
+ return "video"
176
+ else :
177
+ return None
178
+
157
179
def approve (self , user , commit = True ):
158
180
"""Mark this entry as approved by the given `user`."""
159
181
if self .is_approved :
@@ -163,10 +185,28 @@ def approve(self, user, commit=True):
163
185
if commit :
164
186
self .save (update_fields = ["moderator" , "approved_at" , "modified_at" ])
165
187
188
+ @cached_property
189
+ def use_summary (self ):
190
+ return self .summary and (
191
+ not self .content or len (self .content ) > CONTENT_SUMMARIZATION_THRESHOLD
192
+ )
193
+
194
+ @cached_property
195
+ def visible_content (self ):
196
+ if self .use_summary :
197
+ return self .summary
198
+ return self .content
199
+
166
200
def save (self , * args , ** kwargs ):
167
201
if not self .slug :
168
202
self .slug = slugify (self .title )
169
- return super ().save (* args , ** kwargs )
203
+ result = super ().save (* args , ** kwargs )
204
+
205
+ if not self .summary :
206
+ logger .info (f"Passing { self .pk = } to dispatcher" )
207
+ summary_dispatcher .delay (self .pk )
208
+
209
+ return result
170
210
171
211
def get_absolute_url (self ):
172
212
return reverse ("news-detail" , args = [self .slug ])
0 commit comments