-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathviews.py
More file actions
861 lines (785 loc) · 33.3 KB
/
views.py
File metadata and controls
861 lines (785 loc) · 33.3 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
"""Views for websites"""
import json
import logging
from pathlib import Path
from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.postgres.search import SearchQuery, SearchVector
from django.db.models import Case, CharField, F, OuterRef, Prefetch, Q, Value, When
from django.utils.functional import cached_property
from django.utils.text import slugify
from github import GithubException
from guardian.shortcuts import get_groups_with_perms, get_objects_for_user
from mitol.common.utils.datetime import now_in_utc
from requests import HTTPError
from rest_framework import mixins, status, viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework_extensions.mixins import NestedViewSetMixin
from content_sync.api import (
sync_github_website_starters,
trigger_publish,
trigger_unpublished_removal,
update_website_backend,
)
from content_sync.constants import VERSION_DRAFT, VERSION_LIVE
from content_sync.tasks import update_mass_build_pipelines_on_publish
from gdrive_sync.constants import WebsiteSyncStatus
from gdrive_sync.tasks import import_website_files
from main import features
from main.permissions import ReadonlyPermission
from main.utils import uuid_string, valid_key
from main.views import DefaultPagination
from users.models import User
from websites import constants
from websites.api import get_valid_new_filename, update_website_status
from websites.constants import (
CONTENT_TYPE_COURSE_LIST,
CONTENT_TYPE_METADATA,
CONTENT_TYPE_PAGE,
CONTENT_TYPE_RESOURCE,
CONTENT_TYPE_RESOURCE_COLLECTION,
PUBLISH_STATUS_NOT_STARTED,
PUBLISH_STATUS_SUCCEEDED,
RESOURCE_TYPE_DOCUMENT,
RESOURCE_TYPE_IMAGE,
RESOURCE_TYPE_OTHER,
RESOURCE_TYPE_VIDEO,
WebsiteStarterStatus,
)
from websites.deletion_utils import (
delete_related_captions_and_transcript,
delete_resource,
)
from websites.models import Website, WebsiteContent, WebsiteStarter
from websites.permissions import (
BearerTokenPermission,
HasWebsiteCollaborationPermission,
HasWebsiteContentPermission,
HasWebsitePermission,
HasWebsitePreviewPermission,
HasWebsitePublishPermission,
is_global_admin,
)
from websites.serializers import (
WebsiteBasicSerializer,
WebsiteCollaboratorSerializer,
WebsiteContentCreateSerializer,
WebsiteContentDetailSerializer,
WebsiteContentSerializer,
WebsiteDetailSerializer,
WebsiteMassBuildSerializer,
WebsiteSerializer,
WebsiteStarterDetailSerializer,
WebsiteStarterSerializer,
WebsiteStatusSerializer,
WebsiteUnpublishSerializer,
WebsiteUrlSerializer,
WebsiteWriteSerializer,
)
from websites.site_config_api import SiteConfig
from websites.utils import (
compile_referencing_content,
get_valid_base_filename,
permissions_group_name_for_role,
)
log = logging.getLogger(__name__)
class WebsiteViewSet(
NestedViewSetMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet,
):
"""
Viewset for Websites
"""
pagination_class = DefaultPagination
permission_classes = (HasWebsitePermission,)
lookup_field = "name"
def get_queryset(self):
"""
Generate a QuerySet for fetching websites.
"""
ordering = self.request.query_params.get("sort", "-updated_on")
website_type = self.request.query_params.get("type", None)
search = self.request.query_params.get("search", None)
resourcetype = self.request.query_params.get("resourcetype", None)
published = self.request.query_params.get("published", None)
user = self.request.user
published_filter = Q(
first_published_to_production__isnull=False,
first_published_to_production__lte=now_in_utc(),
publish_date__isnull=False,
unpublish_status__isnull=True,
websitecontent__type=CONTENT_TYPE_METADATA,
websitecontent__metadata__isnull=False,
)
if self.request.user.is_anonymous:
# Anonymous users should get a list of all published websites (used for ocw-www carousel) # noqa: E501
ordering = "-first_published_to_production"
queryset = Website.objects.filter(published_filter).distinct()
elif is_global_admin(user):
# Global admins should get a list of all websites, published or not.
queryset = Website.objects.all()
else:
# Other authenticated users should get a list of websites they are editors/admins/owners for. # noqa: E501
queryset = get_objects_for_user(user, constants.PERMISSION_VIEW)
# Only superusers should be able to edit the test sites
if not self.request.user.is_superuser:
queryset = queryset.exclude(name__in=settings.OCW_TEST_SITE_SLUGS)
if search is not None and search != "":
# search query param is used in react-select typeahead, and should
# match on the title, name, and short_id
search_filter = Q(search=SearchQuery(search)) | Q(search__icontains=search)
if "." in search:
# postgres text search behaves oddly with periods but not dashes
search_filter = search_filter | Q(
search=SearchQuery(search.replace(".", "-"))
)
queryset = queryset.annotate(
search=SearchVector(
"name",
"title",
"short_id",
)
).filter(search_filter)
if resourcetype is not None:
queryset = queryset.filter(metadata__resourcetype=resourcetype)
if website_type is not None:
queryset = queryset.filter(starter__slug=website_type)
if published is not None:
published = _parse_bool(published)
if published:
queryset = queryset.filter(published_filter)
else:
queryset = queryset.exclude(published_filter)
return queryset.select_related("starter").order_by(ordering)
def get_serializer_class(self):
if self.action == "list":
return WebsiteSerializer
elif self.action == "create":
return WebsiteWriteSerializer
elif self.action == "retrieve" and _parse_bool(
self.request.query_params.get("only_status")
):
return WebsiteStatusSerializer
elif self.action in ("preview", "publish"):
return WebsiteUrlSerializer
else:
return WebsiteDetailSerializer
def get_serializer(self, *args, **kwargs):
serializer_class = self.get_serializer_class()
kwargs["context"] = self.get_serializer_context()
# If a new website is being created, and the request includes a title but not a name, auto-generate the # noqa: E501
# name by slugify-ing the title before passing the data off to the serializer.
if (
self.request.method == "POST"
and "name" not in self.request.data
and self.request.data.get("title")
):
request_data = self.request.data.copy()
request_data["name"] = slugify(request_data["title"])
kwargs["data"] = request_data
return serializer_class(*args, **kwargs)
def publish_version(self, name, version, request):
"""Process a publish request for the specified version"""
try:
website = self.get_object()
if website.publish_date is None:
if not request.data.get("url_path"):
request.data["url_path"] = None
serializer = WebsiteUrlSerializer(data=request.data, instance=website)
if serializer.is_valid(raise_exception=True):
serializer.update(website, serializer.validated_data)
if version == VERSION_DRAFT:
Website.objects.filter(pk=website.pk).update(
has_unpublished_draft=False,
draft_publish_status=constants.PUBLISH_STATUS_NOT_STARTED,
draft_publish_status_updated_on=now_in_utc(),
latest_build_id_draft=None,
draft_last_published_by=request.user,
)
else:
Website.objects.filter(pk=website.pk).update(
has_unpublished_live=False,
live_publish_status=constants.PUBLISH_STATUS_NOT_STARTED,
live_publish_status_updated_on=now_in_utc(),
latest_build_id_live=None,
live_last_published_by=request.user,
unpublish_status=None,
last_unpublished_by=None,
)
trigger_publish(website.name, version)
return Response(status=200)
except (ValidationError, PermissionDenied):
# Let DRF handle these exceptions
raise
except GithubException:
log.exception(
"GitHub error publishing %s version for %s (user: %s)",
version,
name,
request.user.username if request.user else "anonymous",
)
return Response(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
data={
"error": "Failed to publish website",
"error_type": "github_error",
},
)
except Exception: # pylint: disable=broad-except
log.exception(
"Error publishing %s version for %s (user: %s)",
version,
name,
request.user.username if request.user else "anonymous",
)
return Response(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
data={"error": "Failed to publish website", "error_type": "unknown"},
)
@action(
detail=True, methods=["post"], permission_classes=[HasWebsitePreviewPermission]
)
def preview(self, request, name=None):
"""Trigger a preview task for the website"""
return self.publish_version(name, VERSION_DRAFT, request)
@action(
detail=True, methods=["post"], permission_classes=[HasWebsitePublishPermission]
)
def publish(self, request, name=None):
"""Trigger a publish task for the website"""
return self.publish_version(name, VERSION_LIVE, request)
@action(
detail=True,
methods=["post", "get"],
permission_classes=[HasWebsitePublishPermission],
)
def unpublish(self, request, name=None):
"""Unpublish the site and trigger the remove-unpublished-sites pipeline"""
try:
website = self.get_object()
if request.method == "GET":
ocw_www_dependencies = WebsiteContent.objects.filter(
(
Q(type=CONTENT_TYPE_COURSE_LIST)
| Q(type=CONTENT_TYPE_RESOURCE_COLLECTION)
),
(
Q(metadata__courses__icontains=website.name)
| Q(metadata__resources__content__icontains=website.name)
),
website__name=settings.ROOT_WEBSITE_NAME,
)
course_content_dependencies = WebsiteContent.objects.filter(
~Q(website__name=website.name),
type=CONTENT_TYPE_PAGE,
markdown__icontains=website.name,
)
course_dependencies = Website.objects.filter(
~Q(name=website.name), metadata__icontains=website.name
)
return Response(
status=200,
data={
"site_dependencies": {
"ocw_www": WebsiteContentSerializer(
instance=ocw_www_dependencies, many=True
).data,
"course": WebsiteBasicSerializer(
instance=course_dependencies, many=True
).data,
"course_content": WebsiteContentSerializer(
instance=course_content_dependencies, many=True
).data,
}
},
)
else:
website.unpublish_status = PUBLISH_STATUS_NOT_STARTED
website.unpublish_status_updated_on = now_in_utc()
website.last_unpublished_by = request.user
website.save()
trigger_unpublished_removal(website)
update_mass_build_pipelines_on_publish.delay(
version=VERSION_LIVE, website_pk=website.pk
)
return Response(
status=200,
data="The site has been submitted for unpublishing.",
)
except PermissionDenied:
# Let DRF handle permission exceptions
raise
except HTTPError:
log.exception(
"HTTP error unpublishing %s (user: %s)",
name,
request.user.username if request.user else "anonymous",
)
return Response(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
data={
"error": "Failed to unpublish website",
"error_type": "http_error",
},
)
except Exception: # pylint: disable=broad-except
log.exception(
"Error unpublishing %s (user: %s)",
name,
request.user.username if request.user else "anonymous",
)
return Response(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
data={"error": "Failed to unpublish website", "error_type": "unknown"},
)
@action(
detail=True,
methods=["post"],
permission_classes=[BearerTokenPermission],
)
def pipeline_status(self, request, name=None):
"""Process webhook requests from concourse pipeline runs"""
website = get_object_or_404(Website, name=name)
data = request.data
version = data["version"]
publish_status = data.get("status")
unpublished = data.get("unpublished", False) and version == VERSION_LIVE
update_website_status(
website, version, publish_status, now_in_utc(), unpublished=unpublished
)
return Response(status=200)
@action(detail=True, methods=["get"], permission_classes=[BearerTokenPermission])
def hide_download(self, request, name=None): # noqa: ARG002
"""Process webhook requests from concourse pipeline runs"""
website = get_object_or_404(Website, name=name)
hide_download = False
if website.name != settings.ROOT_WEBSITE_NAME:
content = WebsiteContent.objects.get(
website=website, type=CONTENT_TYPE_METADATA
)
hide_download = content and content.metadata.get("hide_download")
return Response(
status=200,
data={} if hide_download else {"version": str(now_in_utc().timestamp())},
)
class WebsiteMassBuildViewSet(viewsets.ViewSet):
"""Return a list of previously published sites, with the info required by the mass-build-sites pipeline""" # noqa: E501
serializer_class = WebsiteMassBuildSerializer
permission_classes = (BearerTokenPermission,)
def list(self, request): # noqa: ARG002
"""Return a list of websites that have been previously published, per version"""
version = self.request.query_params.get("version")
starter = self.request.query_params.get("starter")
if version not in (VERSION_LIVE, VERSION_DRAFT):
msg = "Invalid version"
raise ValidationError(msg)
publish_date_field = (
"publish_date" if version == VERSION_LIVE else "draft_publish_date"
)
# Get all sites, minus any sites that have never been successfully published
sites = Website.objects.exclude(
Q(**{f"{publish_date_field}__isnull": True}) | Q(url_path__isnull=True)
)
# For live builds, exclude previously published sites that have been unpublished
if version == VERSION_LIVE:
sites = sites.exclude(unpublish_status__isnull=False)
# If a starter has been specified by the query, only return sites made with that starter # noqa: E501
if starter:
sites = sites.filter(starter=WebsiteStarter.objects.get(slug=starter))
# Exclude the test sites from the mass build
sites = sites.exclude(name__in=settings.OCW_TEST_SITE_SLUGS)
sites = sites.prefetch_related("starter").order_by("name")
serializer = WebsiteMassBuildSerializer(instance=sites, many=True)
return Response({"sites": serializer.data})
class WebsiteUnpublishViewSet(viewsets.ViewSet):
"""
Return a list of sites that need to be unpublished, with the info required by the remove-unpublished-sites pipeline
""" # noqa: E501
permission_classes = (BearerTokenPermission,)
def list(self, request): # noqa: ARG002
"""Return a list of websites that need to be processed by the remove-unpublished-sites pipeline""" # noqa: E501
sites = (
Website.objects.exclude(
Q(unpublish_status=PUBLISH_STATUS_SUCCEEDED)
| Q(unpublish_status__isnull=True)
)
.prefetch_related("starter")
.order_by("name")
)
serializer = WebsiteUnpublishSerializer(instance=sites, many=True)
return Response({"sites": serializer.data})
class WebsiteStarterViewSet(
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet,
):
"""
Viewset for WebsiteStarters
"""
permission_classes = (ReadonlyPermission,)
def get_queryset(self):
queryset = WebsiteStarter.objects.filter(
status__in=WebsiteStarterStatus.ALLOWED_STATUSES
).all()
if features.is_enabled(features.USE_LOCAL_STARTERS):
return queryset
else:
return queryset.filter(source=constants.STARTER_SOURCE_GITHUB)
def get_serializer_class(self):
if self.action == "list":
return WebsiteStarterSerializer
else:
return WebsiteStarterDetailSerializer
@action(detail=False, methods=["post"], permission_classes=[])
def site_configs(self, request):
"""Process webhook requests for WebsiteStarter site configs"""
data = json.loads(request.body)
if data.get("repository"):
try:
if not valid_key(settings.GITHUB_WEBHOOK_KEY, request):
return Response(status=status.HTTP_403_FORBIDDEN)
files = [
file
for sublist in [
commit["modified"] + commit["added"]
for commit in data["commits"]
]
for file in sublist
if Path(file).name == settings.OCW_STUDIO_SITE_CONFIG_FILE
]
sync_github_website_starters(
data["repository"]["html_url"], files, commit=data.get("after")
)
except KeyError:
log.exception(
"Key error syncing config files from repo %s, commit: %s",
data.get("repository", {}).get("html_url"),
data.get("after"),
)
return Response(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
data={
"error": "Failed to sync site configuration files",
"error_type": "key_error",
},
)
except Exception: # pylint: disable=broad-except
log.exception(
"Error syncing config files from repo %s, files: %s, commit: %s",
data.get("repository", {}).get("html_url"),
files if "files" in locals() else [],
data.get("after"),
)
return Response(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
data={
"error": "Failed to sync site configuration files",
"error_type": "unknown",
},
)
return Response(status=status.HTTP_202_ACCEPTED)
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
class WebsiteCollaboratorViewSet(
NestedViewSetMixin,
viewsets.ModelViewSet,
):
"""Viewset for Website collaborators along with their group/role"""
serializer_class = WebsiteCollaboratorSerializer
permission_classes = (HasWebsiteCollaborationPermission,)
pagination_class = DefaultPagination
lookup_url_kwarg = "user_id"
@cached_property
def website(self):
"""Fetches the Website for this request""" # noqa: D401
return get_object_or_404(Website, name=self.kwargs.get("parent_lookup_website"))
def get_queryset(self):
"""
Builds a queryset of relevant users with permissions for this website, and annotates them by group name/role
(owner, administrator, editor, or global administrator)
""" # noqa: D401, E501
website = self.website
website_group_names = [
*list(get_groups_with_perms(website).values_list("name", flat=True)),
constants.GLOBAL_ADMIN,
]
owner_user_id = website.owner.id if website.owner else None
return (
User.objects.filter(
Q(id=owner_user_id) | Q(groups__name__in=website_group_names)
)
.annotate(
role=Case(
When(id=owner_user_id, then=Value(constants.ROLE_OWNER)),
default=Group.objects.filter(
user__id=OuterRef("id"), name__in=website_group_names
)
.annotate(
role_name=Case(
*(
[
When(
name=permissions_group_name_for_role(
role, website
),
then=Value(role),
)
for role in constants.ROLE_GROUP_MAPPING
]
),
output_field=CharField(),
)
)
.values("role_name")[:1],
output_field=CharField(),
)
)
.order_by("name", "id")
.distinct()
)
def get_serializer_context(self):
"""Get the serializer context"""
return {
"website": self.website,
}
def destroy(self, request, *args, **kwargs): # noqa: ARG002
"""Remove the user from all groups for this website"""
user = self.get_object()
user.groups.remove(*get_groups_with_perms(self.website))
return Response(status=status.HTTP_204_NO_CONTENT)
def _get_derived_website_content_data(
request_data: dict, site_config: SiteConfig, website_pk: str
) -> dict:
"""Derives values that should be added to the request data if a WebsiteContent object is being created""" # noqa: D401, E501
added_data = {}
if "text_id" not in request_data:
added_data["text_id"] = uuid_string()
content_type = request_data.get("type")
config_item = (
site_config.find_item_by_name(name=content_type)
if content_type is not None
else None
)
is_page_content = False
if site_config and config_item is not None:
is_page_content = site_config.is_page_content(config_item)
added_data["is_page_content"] = is_page_content
dirpath = request_data.get("dirpath")
if dirpath is None and config_item is not None and is_page_content:
dirpath = config_item.file_target
added_data["dirpath"] = dirpath
slug_key = config_item.item.get("slug") if config_item is not None else None
if not slug_key:
slug_key = "title"
slug = (
added_data.get(slug_key)
or request_data.get(slug_key)
or request_data.get("metadata", {}).get(slug_key)
)
if slug is not None:
added_data["filename"] = get_valid_new_filename(
website_pk=website_pk,
dirpath=dirpath,
filename_base=slugify(
get_valid_base_filename(slug, content_type), allow_unicode=True
),
)
return added_data
def _get_value_list_from_query_params(query_params, key):
"""
Get a list of values which have keys that start with key[ or key
"""
filter_type_keys = [
qs_key
for qs_key in query_params
# View should accept "type" as a single query param, or multiple types,
# e.g.: "?type[0]=sometype&type[1]=othertype"
if qs_key == key or qs_key.startswith(f"{key}[")
]
return [query_params[_key] for _key in filter_type_keys]
def _parse_bool(value):
"""
Parse a query string value into a bool
"""
return value and value.lower() != "false"
class WebsiteContentViewSet(
NestedViewSetMixin,
viewsets.ModelViewSet,
):
"""Viewset for WebsiteContent"""
permission_classes = (HasWebsiteContentPermission,)
pagination_class = DefaultPagination
lookup_field = "text_id"
def get_queryset(self):
parent_lookup_website = self.kwargs.get("parent_lookup_website")
search = self.request.query_params.get("search")
resourcetype = self.request.query_params.get("resourcetype")
types = _get_value_list_from_query_params(self.request.query_params, "type")
published = self.request.query_params.get("published", None)
base = WebsiteContent.objects.filter(
website__name=parent_lookup_website
).select_related("website", "website__starter")
queryset = base.prefetch_related(
Prefetch(
"referencing_content",
queryset=WebsiteContent.objects.only("pk"),
to_attr="prefetched_referencing_content",
)
)
if types:
queryset = queryset.filter(type__in=types)
if search:
queryset = queryset.filter(
Q(title__icontains=search) | Q(file__iregex=f"{search}([^/]+$|$)")
)
if resourcetype:
if resourcetype == RESOURCE_TYPE_OTHER:
queryset = queryset.exclude(
metadata__resourcetype__in=[
RESOURCE_TYPE_IMAGE,
RESOURCE_TYPE_DOCUMENT,
RESOURCE_TYPE_VIDEO,
]
)
else:
queryset = queryset.filter(metadata__resourcetype=resourcetype)
if published:
published = _parse_bool(published)
if published is True:
queryset = queryset.filter(
website__publish_date__isnull=False,
# if the associated website has been published after
# the content record was created, then it has been published
# at least once
website__publish_date__gt=F("created_on"),
)
else:
# unpublished content is any content where the associated
# website has never been published or where the website was
# most recently published before it was created
queryset = queryset.filter(
Q(website__publish_date__isnull=True)
| Q(website__publish_date__lt=F("created_on"))
)
if "page_content" in self.request.query_params:
queryset = queryset.filter(
is_page_content=(_parse_bool(self.request.query_params["page_content"]))
)
return queryset.order_by("-updated_on")
def get_serializer_class(self):
detailed_list = self.request.query_params.get("detailed_list", False)
if self.action == "list" and detailed_list:
return WebsiteContentDetailSerializer
elif self.action == "list":
return WebsiteContentSerializer
elif self.action == "create":
return WebsiteContentCreateSerializer
else:
return WebsiteContentDetailSerializer
def get_serializer_context(self):
if self.action != "create":
return {
**super().get_serializer_context(),
"content_context": _parse_bool(
self.request.query_params.get("content_context")
),
}
parent_lookup_website = self.kwargs.get("parent_lookup_website")
website_qset = Website.objects.values("pk", "starter__config").get(
name=parent_lookup_website
)
website_pk = website_qset["pk"]
added_context = {"website_id": website_pk}
raw_site_config = website_qset["starter__config"] or {}
site_config = SiteConfig(raw_site_config)
added_context.update(
_get_derived_website_content_data(
request_data=self.request.data,
site_config=site_config,
website_pk=website_pk,
)
)
return {**super().get_serializer_context(), **added_context}
def destroy(self, request, *args, **kwargs):
content: WebsiteContent = self.get_object()
is_video = (
content.type == CONTENT_TYPE_RESOURCE
and (content.metadata or {}).get("resourcetype") == RESOURCE_TYPE_VIDEO
)
if is_video:
delete_related_captions_and_transcript(content)
delete_resource(content)
self.perform_destroy(content)
return Response(status=status.HTTP_204_NO_CONTENT)
return super().destroy(request, *args, **kwargs)
def perform_destroy(self, instance: WebsiteContent):
"""(soft) deletes a WebsiteContent record"""
instance.updated_by = self.request.user
super().perform_destroy(
instance
) # this actually performs a save() because it's a soft delete
update_website_backend(instance.website)
return instance
def create(self, request, *args, **kwargs): # noqa: ARG002
"""
Override the create method to implement 'create_or_get' functionality.
"""
parent_lookup_website = self.kwargs.get("parent_lookup_website")
# Extract the data from the request
types = _get_value_list_from_query_params(request.data, "type")
metadata = request.data.get("metadata")
# Criteria for checking duplicates in external resources only
if constants.CONTENT_TYPE_EXTERNAL_RESOURCE in types:
queryset = WebsiteContent.objects.filter(
website__name=parent_lookup_website
)
queryset = queryset.filter(metadata__external_url=metadata["external_url"])
content = queryset.first()
if content:
# If the content already exists, return it instead of creating a new one
serializer = self.get_serializer(content)
self._referencing_content(content)
return Response(serializer.data, status=status.HTTP_200_OK)
# Proceed with creation if no existing content is found
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = serializer.save(updated_by=self.request.user)
self._referencing_content(instance)
return Response(serializer.data, status=status.HTTP_201_CREATED)
def _referencing_content(self, instance: WebsiteContent):
"""
Add referencing content to the instance if `referencing_content` is provided.
"""
if references := compile_referencing_content(instance):
references = WebsiteContent.objects.filter(text_id__in=references).all()
instance.referenced_by.set(references)
instance.save()
def perform_update(self, serializer):
"""
Override the update request for content
"""
instance = serializer.save(updated_by=self.request.user)
self._referencing_content(instance)
@action(
detail=False,
methods=["post"],
permission_classes=(HasWebsiteContentPermission,),
)
def gdrive_sync(
self,
request, # noqa: ARG002
**kwargs, # noqa: ARG002
): # pylint:disable=unused-argument
"""Trigger a task to sync all non-video Google Drive files"""
website = Website.objects.get(name=self.kwargs.get("parent_lookup_website"))
website.sync_status = WebsiteSyncStatus.PENDING
website.save()
import_website_files.delay(website.name)
return Response(status=200)