-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmodels.py
More file actions
516 lines (436 loc) · 18 KB
/
models.py
File metadata and controls
516 lines (436 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
from django.db.models.query import QuerySet
from .sectionable.models import SectionablePage
from article.models import ArticlePage
from home import blocks as homeblocks
from article import blocks_outer_article
from article import blocks_inner_article
from ubyssey import blocks as general_blocks
from django.core.cache import cache
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db import models
from django.db.models.fields import CharField, BooleanField, TextField, SlugField
from django.db.models.fields.related import ForeignKey
from django.shortcuts import render
from modelcluster.models import ClusterableModel
from modelcluster.fields import ParentalKey
from wagtail.admin.panels import TitleFieldPanel, FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.fields import StreamField, RichTextField
from wagtail import models as wagtail_core_models
from wagtail.models import Page
from wagtail.contrib.routable_page.models import route, RoutablePageMixin
from wagtail.search import index
from wagtail.snippets.models import register_snippet
from wagtail_color_panel.fields import ColorField
from wagtail_color_panel.edit_handlers import NativeColorPanel
from wagtail.documents.models import Document
from home import blocks as homeblocks
from infinitefeed import blocks as infinitefeedblocks
import datetime
from django.utils import timezone
from topics.views import cluster_articles_by_topic
#-----Snippet models-----
class CategorySnippet(index.Indexed, ClusterableModel):
"""
Formerly known as a 'Subsection'
"""
title = CharField(
blank=False,
null=False,
max_length=100
)
slug = SlugField(
unique=True,
blank=False,
null=False,
max_length=100
)
description = TextField(
null=False,
blank=True,
default='',
)
banner = models.ForeignKey(
"images.UbysseyImage",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='categoryBanner',
)
# authors = ManyToManyField('Author', related_name='subsection_authors')
is_active = BooleanField( # legacy field
default=False
)
section_page = ParentalKey(
"section.SectionPage",
related_name="categories",
)
search_fields = [
index.AutocompleteField('title'),
]
panels = [
MultiFieldPanel(
[
TitleFieldPanel("title"),
FieldPanel("slug"),
FieldPanel("section_page"),
FieldPanel("description"),
],
heading="Essentials"
),
MultiFieldPanel(
[
FieldPanel("banner"),
],
heading="Banner",
),
MultiFieldPanel(
[
InlinePanel("category_authors"),
],
heading="Category Author(s)"
),
]
def __str__(self):
return "%s - %s" % (self.section_page, self.title)
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
#-----Orderable models-----
class CategoryAuthor(wagtail_core_models.Orderable):
author = ForeignKey(
"authors.AuthorPage",
blank=False,
null=False,
on_delete=models.CASCADE,
)
category = ParentalKey(
CategorySnippet,
blank=True,
null=True,
related_name="category_authors",
)
panels = [
FieldPanel("author"),
]
class CategoryMenuItem(wagtail_core_models.Orderable):
category_page = ForeignKey(
"section.CategoryPage",
blank=True,
null=True,
on_delete=models.SET_NULL,
)
section = ParentalKey(
"section.SectionPage",
blank=True,
null=True,
related_name="category_menu",
)
panels = [
FieldPanel("category_page"),
]
class SectionPage(RoutablePageMixin, SectionablePage):
template = 'section/section_page.html'
subpage_types = [
'article.TipTapArticlePage',
'article.StandardArticlePage',
'article.StandardArticlePageWithRightColumn',
'article.SpecialArticleLikePage',
'liveblog.LiveBlogArticlePage',
'specialfeaturelanding.SpecialLandingPage',
'section.CategoryPage',
]
parent_page_types = [
'home.HomePage',
]
show_in_menus_default = True
banner = models.ForeignKey(
"images.UbysseyImage",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='banner',
)
description = RichTextField(
# Was called "snippet" in Dispatch - do not want to reuse this work, so we call it 'lede' instead
null=False,
blank=True,
default='',
)
label_svg = models.ForeignKey(
'wagtaildocs.Document',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
top_stream = StreamField(
[
('article_gatherer', blocks_outer_article.ArticleGathererBlock()),
('landing', blocks_outer_article.SpecialLandingPageBlock()),
('article_manual', blocks_outer_article.ManualArticles()),
('article_gatherer_with_pinned', blocks_outer_article.ArticleGathererWithPinnedBlock()),
('grouped_articles_manual', blocks_outer_article.ManualArticleLinkGroup()),
('header_menu', blocks_inner_article.HeaderMenuBlock()),
('info', general_blocks.LandingStreamInfo()),
],
null=True,
blank=True,
use_json_field=True,
)
sidebar_stream = StreamField(
[
("sidebar_advertisement_block", infinitefeedblocks.SidebarAdvertisementBlock()),
("sidebar_issues_block", infinitefeedblocks.SidebarIssuesBlock()),
("sidebar_flex_stream_block", infinitefeedblocks.SidebarFlexStreamBlock()),
("sidebar_gatherer_block", infinitefeedblocks.SidebarArticleGatherer()),
("sidebar_manual", infinitefeedblocks.SidebarManualArticles()),
('siderbar_info', infinitefeedblocks.SidebarInfo()),
],
null=True,
blank=True,
use_json_field=True,
)
content_panels = wagtail_core_models.Page.content_panels + [
MultiFieldPanel(
[
FieldPanel("banner"),
],
heading="Banner",
),
MultiFieldPanel(
[
FieldPanel("description"),
],
heading="Description",
),
MultiFieldPanel(
[
FieldPanel("top_stream"),
],
heading="Top stream"
),
MultiFieldPanel(
[
FieldPanel('label_svg'),
],
heading="Label svg"
),
MultiFieldPanel(
[
InlinePanel("category_menu"),
],
heading="Category Menu",
),
MultiFieldPanel(
[
FieldPanel("sidebar_stream"),
],
heading="Sidebar"
)
]
def get_filter(self):
filters = {"section": self.current_section}
return filters
filter = property(fget=get_filter)
def get_all_categories(self):
def get_academic_year(date):
academic_year = "Unknown"
if date != None:
if date.month > 4:
academic_year = str(date.year) + "/" + str(date.year+1)[-2:]
else:
academic_year = str(date.year-1) + "/" + str(date.year)[-2:]
return academic_year
categories_filter_value = lambda category: ArticlePage.objects.live().filter(category_page=category).exists()
categories_order_value = lambda category: datetime.datetime.min if ArticlePage.objects.live().filter(category_page=category).order_by("-first_published_at")[0].published_at == None else ArticlePage.objects.live().filter(category_page=category).order_by("-first_published_at")[0].published_at.replace(tzinfo=None)
categories = list(CategoryPage.objects.live().child_of(self))
categories = list(filter(categories_filter_value, categories))
categories = list(map(lambda c: [categories_order_value(c), c], categories))
categories.sort(key=lambda c: c[0], reverse=True)
category_groups = {}
current = get_academic_year(datetime.datetime.now())
for category in categories:
group = "Unknown"
if category[0] != datetime.datetime.min:
group = get_academic_year(category[0])
if group == current:
group = "Current"
if group in category_groups:
category_groups[group].append(category[1])
else:
category_groups[group] = [category[1]]
category_groups = list(map(lambda k: {"group": k, "categories": category_groups[k]}, category_groups.keys()))
return category_groups
all_categories = property(fget=get_all_categories)
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
search_query = request.GET.get("q")
filters = self.filter
if search_query:
filters["search_query"] = search_query
context["filters"] = filters
context["section_slug"] = self.slug
# context["featured_articles"] = self.get_featured_articles()
if search_query:
context["search_query"] = search_query
return context
def get_section_articles(self, order='-first_published_at') -> QuerySet:
# order should be explicit_published_at but that is in the ArticlePage table and accessing slows down the query
section_articles = ArticlePage.objects \
.child_of(self) \
.order_by(order) \
.live()
return section_articles
def get_featured_articles(self, queryset=None, number_featured=4) -> QuerySet:
"""
Returns a truncated queryset of articles
queryset: if not included, will default to all live, public, ArticlePage descendents of this SectionPage
number_featured: defaults to 4 as brute fact about our template's design
"""
if queryset == None:
# queryset = ArticlePage.objects.from_section(section_root=self)
queryset = self.get_section_articles()
return queryset[:number_featured]
featured_articles = property(fget=get_featured_articles)
def get_recent_articles(self, max_items=10):
return ArticlePage.objects.live().child_of(self).order_by("-first_published_at")[:max_items]
def get_recent_topic_cluster(self, items=12, max_in_cluster=3):
'''
Cluster articles by their topic. The clustering works by iterating
from the most recent article and selecting a unique primary or listed topic. These topics are
then iterated through and recent articles with these topics are joined in a cluster
'''
#considered_articles = ArticlePage.objects.live().child_of(self).order_by('-first_published_at')[:2*items]
#return cluster_articles_by_topic(considered_articles, items=items, max_in_cluster=max_in_cluster)
# Get recent articles to cluster
considered_articles = ArticlePage.objects.live().child_of(self).order_by('-first_published_at')[:2*items]
# Get the topics of all these articles
article_topics = [
{
"article": article,
"topics": article.topics.all()
}
for article in considered_articles
]
# Iterate through articles and select a topic to represent it
used_topics = []
for article_topic in article_topics:
article = article_topic["article"]
# Use the primary topic if multiple recent articles are tagged with it
primary_topic = article.get_primary_topic()
if primary_topic and not primary_topic in used_topics:
possible_articles = list(filter(lambda article: primary_topic in article["topics"],
article_topics))
if len(possible_articles) > 1:
used_topics.append(primary_topic)
continue
# Get the number of recent articles tagged by topic tagged by this article
possible_topics = [
{
"topic": topic,
"count": len(list(filter(lambda considered_article: topic in considered_article["topics"], article_topics)))
}
for topic in article_topic["topics"]]
# Remove topics that aren't listed or tagged with multiple articles
possible_topics = list(filter(lambda topic: topic["count"] > 1 and topic["topic"].listed, possible_topics))
# If the there are no listed topics that are tagged with multiple aritcles, then use the primary topic
if primary_topic:
if len(possible_topics) == 0 and not primary_topic in used_topics:
used_topics.append(primary_topic)
continue
# Use the first unique listed topic with the lowest number of tagged articles greater than 1
possible_topics = sorted(possible_topics, key=lambda topic: topic["count"])
for topic in possible_topics:
if not topic["topic"] in used_topics:
used_topics.append(topic["topic"])
break
# Iterate through the collected topics in the order they were added at.
# We gather the articles under these topics, avoiding articles we have already gathered.
seen_articles = []
cluster = []
articles_by_topic = {}
for topic in used_topics:
articles_by_topic[topic.name] = list(filter(lambda considered_article: topic in considered_article["topics"], article_topics))
articles_by_topic[topic.name] = list(map(lambda a: a["article"], articles_by_topic[topic.name]))
while len(used_topics) > 0:
topic = used_topics[0]
articles_in_topic = articles_by_topic[topic.name]
cluster_articles = []
for article in articles_in_topic:
cluster_articles.append(article)
seen_articles.append(article)
if len(seen_articles) >= items or len(cluster_articles) >= max_in_cluster:
break
if len(cluster_articles) > 0:
cluster.append({"topic": topic, "articles": cluster_articles})
if len(seen_articles) >= items:
break
used_topics.pop(0)
for topic in used_topics:
articles_by_topic[topic.name] = list(filter(lambda article: not article in seen_articles, articles_by_topic[topic.name]))
used_topics = list(filter(lambda t: len(articles_by_topic[t.name]) > 0, used_topics))
used_topics = sorted(used_topics, key=lambda t: articles_by_topic[t.name][0].published_at, reverse=True)
#for clust in cluster:
# print(clust["topic"].name)
# for article in clust["articles"]:
# print(" - " + article.title)
return cluster
def get_recent_topic_cluster_grouped(self, items=12, columns=4, group_max=3):
'''
Divide the topic clusers into groups where each group has 'group_max' number of articles.
'''
# Get clusters
clusters = self.get_recent_topic_cluster(items=items+columns, max_in_cluster=group_max)
groups = []
# Iterate through clusters, first adding 'columns' number to 'groups',
# then iterating back over 'groups' to fill in each group so that they include 'group_max' number of articles.
for cluster in clusters:
if len(groups) < columns:
if len(groups) > 0:
length = sum(map(lambda cluster: len(cluster["articles"]), groups[-1]))
if length + len(cluster["articles"]) <= group_max:
groups[-1].append(cluster)
continue
groups.append([cluster])
else:
is_perfect = True
for group in groups:
length = sum(map(lambda cluster: len(cluster["articles"]), group))
if length < group_max:
is_perfect = False
cluster["articles"] = cluster["articles"][:group_max - length]
group.append(cluster)
break
if is_perfect:
break
return groups
@route(r'^rss/$', name='rss_view')
def rss_view(self, request):
from ubyssey.views.feed import SectionFeed
return SectionFeed().__call__(request, section=self)
def save(self, *args, **kwargs):
self.current_section = self.slug
return Page.save(self,*args, **kwargs)
class Meta:
verbose_name = "Section"
verbose_name_plural = "Sections"
class CategoryPage(SectionPage):
template = 'section/section_page.html'
parent_page_types = [
'section.SectionPage',
]
subpage_types = []
def get_filter(self):
filters = {"section": self.get_parent().slug, "category": self.slug}
return filters
filter = property(fget=get_filter)
def get_all_categories(self):
return self.get_parent().specific.get_all_categories()
all_categories = property(fget=get_all_categories)
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context["parent"] = self.get_parent()
context["section_slug"] = context["parent"].slug
return context
def get_recent_articles(self, max_items=10):
return ArticlePage.objects.live().filter(category_page = self).order_by("-first_published_at")[:max_items]